AirSched Logo  0.1.4
C++ Simulated Airline Schedule Manager Library
TravelSolutionParser.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <sstream>
00006 #include <fstream>
00007 #include <cassert>
00008 // StdAir
00009 #include <stdair/stdair_exceptions.hpp>
00010 #include <stdair/basic/BasConst_TravelSolution.hpp>
00011 #include <stdair/basic/BasFileMgr.hpp>
00012 #include <stdair/bom/BomRoot.hpp>
00013 #include <stdair/service/Logger.hpp>
00014 // AirSched
00015 #include <airsched/command/TravelSolutionParser.hpp>
00016 
00017 namespace AIRSCHED {
00018 
00019   // ////////////////////////////////////////////////////////////////////
00020   bool TravelSolutionParser::
00021   parseInputFileAndBuildBom (const std::string& iInputFileName) {
00022     bool hasReadBeenSuccessful = false;
00023 
00024     // Check that the file path given as input corresponds to an actual file
00025     const bool doesExistAndIsReadable =
00026       stdair::BasFileMgr::doesExistAndIsReadable (iInputFileName);
00027     if (doesExistAndIsReadable == false) {
00028       std::ostringstream oMessage;
00029       oMessage << "The input file, '" << iInputFileName
00030                << "', can not be retrieved on the file-system";
00031       throw stdair::FileNotFoundException (oMessage.str());
00032     }
00033 
00034     // Open the input file
00035     std::ifstream inputFile (iInputFileName.c_str());
00036     if (! inputFile) {
00037       STDAIR_LOG_ERROR ("Can not open input file '" << iInputFileName << "'");
00038       throw new stdair::FileNotFoundException ("Can not open input file '"
00039                                                + iInputFileName + "'");
00040     }
00041     
00042     char buffer[80];
00043     double dval = 0.0;
00044     std::string dvalStr;
00045     short i = 1;
00046     bool hasAllPArams = true;
00047 
00048     stdair::AirportCode_T dAirport;
00049     stdair::AirportCode_T aAirport;
00050     stdair::Date_T depDate;
00051     stdair::Duration_T depTime;
00052     stdair::Duration_T arTime;
00053     stdair::Duration_T dur;
00054     //bool Ref;
00055     stdair::AirlineCode_T airline;
00056     stdair::CabinCode_T cabin;
00057     //stdair::FlightNumber_T flightNum;
00058     //stdair::Fare_T fare;
00059     //int lagsNum;
00060     //bool SNS;
00061     //bool change;
00062     
00063     while (inputFile.getline (buffer, sizeof (buffer), ';')) {
00064       std::istringstream iStringStr (buffer);
00065 
00066       bool hasRead = false;
00067 
00068       if (i == 1) {
00069         hasAllPArams = true;
00070       }
00071 
00072       if (i>=1 && i<=14) {
00073         hasRead = (iStringStr >> dvalStr);
00074       }
00075 
00076       if (i == 15) {
00077         hasRead = (iStringStr >> dval);
00078       }
00079 
00080       if (hasRead) {
00081         if (i == 1) {
00082           dAirport = dvalStr;
00083 
00084         } else if (i == 2) {
00085           aAirport = dvalStr;
00086           // std::cout << "City Pair = '" << dAiport
00087           // << "-" << aAirport << "'" << std::endl;
00088 
00089         } else if (i == 3) {
00090           depDate = boost::gregorian::from_simple_string (dvalStr);
00091           // std::cout << "Date = '" << depDate << "'" << std::endl;
00092 
00093         } else if (i == 4) {
00094           depTime = boost::posix_time::duration_from_string (dvalStr);
00095 
00096         } else if (i == 5) {
00097           arTime = boost::posix_time::duration_from_string (dvalStr);
00098 
00099         } else if (i == 6) {
00100           dur = boost::posix_time::duration_from_string (dvalStr);
00101 
00102         } else if (i == 7) {
00103           //if (dvalStr == "refundable fare")
00104           //  Ref = true;
00105           //else Ref  = false;
00106 
00107         } else if (i == 8) {
00108           airline = dvalStr;
00109 
00110         } else if (i == 9) {
00111           cabin = dvalStr;
00112 
00113         } else if (i == 10) {
00114           //flightNum = dval;
00115 
00116         } else if (i == 11) {
00117           //fare = dval;
00118 
00119         } else if (i == 12) {
00120           //lagsNum = dval;
00121 
00122         } else if (i == 13) {
00123           //if (dvalStr == "Saturday Nigth Stay mandatory")
00124           //  SNS = true;
00125           //else SNS = false;
00126 
00127         } else if (i == 14) {
00128           //if (dvalStr == "changeable fare")
00129           //  change = true;
00130           //else change = false;
00131           i = 0;
00132         }
00133 
00134         //
00135         ++i;
00136 
00137       } else {
00138         hasAllPArams = false;
00139       }
00140     }
00141 
00142     if (hasAllPArams && i == 1) {
00143       STDAIR_LOG_DEBUG ("Successfully read");
00144     }
00145     
00146     //
00147     if (!inputFile.eof()) {
00148       STDAIR_LOG_ERROR ("Problem when reading input file '" << iInputFileName
00149                         << "'");
00150       return hasReadBeenSuccessful;
00151     }
00152 
00153     //
00154     hasReadBeenSuccessful = true;
00155     return hasReadBeenSuccessful;
00156   }
00157   
00158 }