/* * SpaceReader.cc * * Created on: 5 août 2013 * Author: CS */ #include "Catalog.hh" #include "SpaceData.hh" #include "SpaceReader.hh" #include #include #include #include #include "TimeTableCatalogUtil.hh" #include #include namespace TimeTableCatalog { const std::string SpaceReader::FORMAT = "SPACE"; SpaceReader::SpaceReader(const std::string& pPath) : AbstractReader(pPath), timeSize(0) { } SpaceReader::~SpaceReader() { } // ----------------- PUBLIC -------------------------- /** * Checks the TT format to know if the reader is the good one. */ bool SpaceReader::canRead(const std::string& pPath){ std::string tmpPath(pPath); std::transform(tmpPath.begin(), tmpPath.end(), tmpPath.begin(), ::tolower); return boost::algorithm::ends_with(tmpPath, ".txt"); } void SpaceReader::read(TimeTable& ptt) { std::ifstream ttfile(getLocalPath(), std::ios::in); // -- open file if (ttfile) { std::string line; // -- read line int crtIndex = 0; while (getline(ttfile, line)) { boost::algorithm::trim(line); if (boost::starts_with(line, "#")) { // -- it is a metadata readMetadata(line, ptt); } else { // -- add it to TimeTable if (line.empty()) { continue; } ptt.addInterval(*readInterval(line, ptt,crtIndex)); ++crtIndex; } } // -- close file ttfile.close(); } else { LOG4CXX_INFO(_logger, "TimeTable file Not Found or unreadable (" + getPath() + ")"); } } std::unique_ptr SpaceReader::createInstance( const std::string& pPath) { return std::unique_ptr < AbstractReader > (new SpaceReader(pPath)); } // ----------------- PRIVATE -------------------------- std::unique_ptr SpaceReader::readInterval( const std::string& pline, TimeTable& pTT, int pcrtIndex) { // set date time format if not set (on first interval) if (pTT._timeFormat == TimeTable::TIME_FORMAT::UNKNOWN) { pTT._timeFormat = getTimeFormat(pline, this->timeSize); if (pTT._timeFormat == TimeTable::TIME_FORMAT::UNKNOWN) { std::unique_ptr ti (new TimeInterval(0, 0, pcrtIndex)); } } std::string line = pline; boost::algorithm::trim(line); // read start time double startTime = readISOTime(line, pTT._timeFormat); line.erase(0, this->timeSize); boost::algorithm::trim(line); // read stop time double stopTime = readISOTime(line, pTT._timeFormat); line.erase(0, this->timeSize); boost::algorithm::trim(line); // -- create time interval std::unique_ptr ti (new TimeInterval(startTime, stopTime, pcrtIndex)); std::vector params; split(line, SpaceData::SEPARATOR.c_str()[0], params); // If parameters value found, set them for the interval if (!params.empty()) { // Starts setting parameter values with the third field of the // intervalFields vector (The first and second one are strat and stop dates) ParameterDescriptionList pdl = pTT.getParameterDescritptions(); std::vector paremeterValues; int paramPos = 0; for (auto parameterDesc : pdl) { paremeterValues.clear(); for (int paramIdx=0; paramIdx< parameterDesc.getSizeAsInt(); paramIdx++) { if (paramPos < (int)params.size()) { paremeterValues.push_back(params[paramPos]); ++paramPos; } } ti->addParameterData(parameterDesc.getId(), paremeterValues); } } return ti; } void SpaceReader::readMetadata(const std::string& pline, TimeTable& ptimeTable) { // try to find string like "Prop:xxxx" and set _prop attribute to "xxxx" // -- here try to find "Name:" if (contains(pline, SpaceData::LIST_TITLE_KEYWORD, "#")) { extractvalue(pline, ptimeTable._name); } // -- here try to find "CreationDate:" // and try to set it as unix time // if impossible, add the line to the description else if (contains(pline, SpaceData::CREATION_DATE_KEYWORD, "#")) { // convert creation date string to char* std::string creationDate; extractvalue(pline, creationDate); // try to read date try { ptimeTable._creationDate = TimeTableCatalog::readISOTime(creationDate); } catch (...) { addDescription(pline, ptimeTable); } } // start and stop dates else if (contains(pline, SpaceData::LIST_START_DATE_KEYWORD, "#")) { // convert creation date string to char* std::string startDate; extractvalue(pline, startDate); // try to read date try { ptimeTable._creationDate = TimeTableCatalog::readISOTime(startDate); } catch (...) { addDescription(pline, ptimeTable); } } else if (contains(pline, SpaceData::LIST_STOP_DATE_KEYWORD, "#")) { // convert creation date string to char* std::string stopDate; extractvalue(pline, stopDate); // try to read date try { ptimeTable._creationDate = TimeTableCatalog::readISOTime(stopDate); } catch (...) { addDescription(pline, ptimeTable); } } else { addDescription(pline, ptimeTable); } } void SpaceReader::addDescription(const std::string & pline, TimeTable& ptt) { // get value after "#" std::string value = pline.substr(1); // remove ; character at the end of the line boost::algorithm::replace_last(value, ";", ""); ptt._description.push_back(value); } ParameterDescription::ParameterType SpaceReader::getTypeFromString(std::string type) { boost::algorithm::trim(type); boost::algorithm::to_lower(type); if ((type.compare("string") == 0) || (type.compare("char") == 0)) { return ParameterDescription::ParameterType::String; } else if ((type.compare("integer") == 0) || (type.compare("int") == 0)) { return ParameterDescription::ParameterType::Integer; } else if ((type.compare("float") == 0) || (type.compare("double") == 0)) { return ParameterDescription::ParameterType::Double; } else if (type.compare("date") == 0) { return ParameterDescription::ParameterType::Date; } return ParameterDescription::ParameterType::String; } } /* namespace TimeTableCatalog */