--- smsdaemon/plugins/TrainInfo.cpp 2009/09/14 07:29:59 322 +++ smsdaemon/plugins/TrainInfo.cpp 2009/09/16 09:08:40 323 @@ -12,9 +12,16 @@ #include #include +#include + + using namespace std; +vector traininfoList; +TrainInfo tmpTrainInfo; +ostringstream buffer; + string ConvertUpdateInfo(char ch) { string result; @@ -38,26 +45,88 @@ return result; } -inline string clean(string& str) + + + +void startElement(void *userData, const char *el, const char **attr) { - using namespace Util; + buffer.str(""); //clear buffer +} + +void endElement(void *userData, const char *el) +{ + string name(el); + if (name == "time") { + tmpTrainInfo.time = buffer.str(); + } else if (name == "updated") { + tmpTrainInfo.update = ConvertUpdateInfo( buffer.str().at(0) ); + } else if (name == "trainnumber") { + tmpTrainInfo.type = buffer.str(); + } else if (name == "destination") { + tmpTrainInfo.destination = buffer.str(); + } else if (name == "origin") { + tmpTrainInfo.origin = buffer.str(); + } else if (name == "location") { + tmpTrainInfo.current = buffer.str(); + } else if (name == "status") { + tmpTrainInfo.status = buffer.str(); + } else if (name == "note") { + tmpTrainInfo.note = buffer.str(); + } else if (name == "train") { + traininfoList.push_back(tmpTrainInfo); + } +} - str = str_replace(str, " ", " "); - return str_trim(str_characters(str_striptags(str))); +void charData(void *userData, const XML_Char *s,int len) +{ + for (int i=0; i GetTrainInfo(int stationID) { - info.time = clean( info.time ); - info.type = clean( info.type ); - info.destination = clean( info.destination ); - info.origin = clean( info.origin ); - info.current = clean( info.current ); - info.status = clean( info.status ); - info.note = clean( info.note ); + traininfoList.clear(); + + ostringstream url; + url << "http://app.t-hoerup.dk/TrainInfoService/DepartureServlet?format=xml&station=" << stationID; + + + XML_Parser parser = NULL; + + try + { + string trainXml = HttpClient::GetString( url.str() ); + + parser = XML_ParserCreate( NULL ); + XML_SetElementHandler(parser, startElement, endElement); + XML_SetCharacterDataHandler(parser, charData); + + + int done; + XML_Parse(parser, trainXml.c_str(), trainXml.size(), done); + } + catch (exception &e) + { + Logger::logMessage( e.what() ); + } + catch (... ) + { + Logger::logMessage( "unknown error in GetTrainInfo(int stationID)" ); + } + + if (parser != NULL) + XML_ParserFree(parser); + + + return traininfoList; } +/* +//old html screen scaper bit vector GetTrainInfo(string stationcode, string stationname) { vector result; @@ -161,5 +230,5 @@ return result; } - +*/