EventManager.cpp 3.58 KB
Newer Older
1 2 3 4
#include <cstdio>
#include <cstdlib> 
#include <iostream>
#include <string>
5
#include "EventManager.h"
6 7 8 9 10
#include "../tinyxml/tinyxml.h"
#include "../IO/OutputHandler.h"

using namespace std;

11 12 13 14 15
/*******************
 Konstruktoren
 ******************/

EventManager::EventManager(){
16
    _event_times=vector<double>();
17
    _event_values=vector<string>();
18
    _projectFilename = "";
19
}
20

21 22 23 24 25 26
/*******************
 Dateien einlesen
 ******************/
void EventManager::SetProjectFilename(const std::string &filename){
    _projectFilename=filename;
}
27

28 29 30 31 32 33 34 35 36 37 38 39 40
void EventManager::SetProjectRootDir(const std::string &filename){
    _projectRootDir= filename;
}

void EventManager::readEventsXml(){
    printf("INFO: \tReading events\n ");
    //get the geometry filename from the project file
    TiXmlDocument doc(_projectFilename);
    if (!doc.LoadFile()){
        //Log->Write("ERROR: \t%s", doc.ErrorDesc());
        //Log->Write("ERROR: \t could not parse the project file");
        printf("ERROR: \t%s\nRROR: \t could not parse the project file\n",doc.ErrorDesc());
        exit(EXIT_FAILURE);
41 42
    }

43 44 45 46 47 48 49 50
    //Log->Write("INFO: \tParsing the event file");
    printf("INFO: \tParsing the event file\n");
    TiXmlElement* xMainNode = doc.RootElement();
    string eventfile="";
    if(xMainNode->FirstChild("events")){
        eventfile=_projectRootDir+xMainNode->FirstChild("events")->FirstChild()->Value();
        //Log->Write("INFO: \tevents <"+eventfile+">");
        //printf("INFO: \t events <%s>\n", eventfile);
51
    }
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

    TiXmlDocument docEvent(eventfile);
    if(!docEvent.LoadFile()){
        //Log->Write("EROOR: \t%s",docEvent.ErrorDesc());
        //Log->Write("ERROR: \t could not parse the event file");
        printf("EROOR: \t%s\nERROR: \t could not parse the event file\n",docEvent.ErrorDesc());
        exit(EXIT_FAILURE);
    }

    TiXmlElement* xRootNode = docEvent.RootElement();
    if(!xRootNode){
        //Log->Write("ERROR:\tRoot element does not exist.");
        printf("ERROR:\tRoot element does not exist.\n");
        exit(EXIT_FAILURE);
    }

    if( xRootNode->ValueStr () != "JPScore" ) {
        //Log->Write("ERROR:\tRoot element value is not 'geometry'.");
        printf("ERROR:\tRoot element value is not 'JPScore'.");
        exit(EXIT_FAILURE);
    }

    TiXmlNode* xEvents = xRootNode->FirstChild("events");
75
    if(!xEvents){
76 77 78
        //Log->Write("ERROR:\tNo events found.");
        printf("ERROR:\tNo events found.\n");
        exit(EXIT_FAILURE);
79 80 81
    }
    
    for(TiXmlElement* e = xEvents->FirstChildElement("event"); e; e= e->NextSiblingElement("event")){
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        _event_times.push_back(atoi(e->Attribute("time")));
        string tmp=e->Attribute("type");
        tmp=tmp+"-";
        tmp=tmp+e->Attribute("id")+"-";
        tmp=tmp+=e->Attribute("state");
        _event_values.push_back(tmp);
    }
    printf("INFO: \tEvents were read\n");
}

void EventManager::listEvents(){
    if(_event_times.size()==0&&_event_values.size()==0){
        //Log->Write("INFO: \tNo events in the events.xml");
        printf("INFO: \tNo events in the events.xml\n");
    }
    else if(_event_times.size()!=_event_values.size()){
        //Log->Write("ERROR: \tThe event.xml is irregular.");
        printf("ERROR: \tThe event.xml is irregular.\n");
100
    }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    else{
        int i;
        for(i=0;i<_event_times.size();i++){
           // Log->Write("INFO: \tEvent "+(i+1)+" after "+_event_times[i]+" sec.: "+_event_values[i]);

            cout <<"INFO: \tEvent "<<i+1<<" after "<< _event_times[i] << "sec.: "<<_event_values[i] <<endl;
        }
    }

}

/***********
 Update
 **********/

void EventManager::Update_Events(double time){
  
118
}