AirTSP Logo  1.01.0
C++ Simulated Airline Travel Solution Provider Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TravelSolutionParser.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <sstream>
6 #include <fstream>
7 #include <cassert>
8 // StdAir
9 #include <stdair/stdair_exceptions.hpp>
10 #include <stdair/basic/BasConst_TravelSolution.hpp>
11 #include <stdair/basic/BasFileMgr.hpp>
12 #include <stdair/bom/BomRoot.hpp>
13 #include <stdair/service/Logger.hpp>
14 // AirTSP
16 
17 namespace AIRTSP {
18 
19  // ////////////////////////////////////////////////////////////////////
21  parseInputFileAndBuildBom (const std::string& iInputFileName) {
22  bool hasReadBeenSuccessful = false;
23 
24  // Check that the file path given as input corresponds to an actual file
25  const bool doesExistAndIsReadable =
26  stdair::BasFileMgr::doesExistAndIsReadable (iInputFileName);
27  if (doesExistAndIsReadable == false) {
28  std::ostringstream oMessage;
29  oMessage << "The input file, '" << iInputFileName
30  << "', can not be retrieved on the file-system";
31  throw stdair::FileNotFoundException (oMessage.str());
32  }
33 
34  // Open the input file
35  std::ifstream inputFile (iInputFileName.c_str());
36  if (! inputFile) {
37  STDAIR_LOG_ERROR ("Can not open input file '" << iInputFileName << "'");
38  throw new stdair::FileNotFoundException ("Can not open input file '"
39  + iInputFileName + "'");
40  }
41 
42  char buffer[80];
43  double dval = 0.0;
44  std::string dvalStr;
45  short i = 1;
46  bool hasAllPArams = true;
47 
48  stdair::AirportCode_T dAirport;
49  stdair::AirportCode_T aAirport;
50  stdair::Date_T depDate;
51  stdair::Duration_T depTime;
52  stdair::Duration_T arTime;
53  stdair::Duration_T dur;
54  //bool Ref;
55  stdair::AirlineCode_T airline;
56  stdair::CabinCode_T cabin;
57  //stdair::FlightNumber_T flightNum;
58  //stdair::Fare_T fare;
59  //int lagsNum;
60  //bool SNS;
61  //bool change;
62 
63  while (inputFile.getline (buffer, sizeof (buffer), ';')) {
64  std::istringstream iStringStr (buffer);
65 
66  bool hasRead = false;
67 
68  if (i == 1) {
69  hasAllPArams = true;
70  }
71 
72  if (i>=1 && i<=14) {
73  hasRead = (iStringStr >> dvalStr);
74  }
75 
76  if (i == 15) {
77  hasRead = (iStringStr >> dval);
78  }
79 
80  if (hasRead) {
81  if (i == 1) {
82  dAirport = dvalStr;
83 
84  } else if (i == 2) {
85  aAirport = dvalStr;
86  // std::cout << "City Pair = '" << dAiport
87  // << "-" << aAirport << "'" << std::endl;
88 
89  } else if (i == 3) {
90  depDate = boost::gregorian::from_simple_string (dvalStr);
91  // std::cout << "Date = '" << depDate << "'" << std::endl;
92 
93  } else if (i == 4) {
94  depTime = boost::posix_time::duration_from_string (dvalStr);
95 
96  } else if (i == 5) {
97  arTime = boost::posix_time::duration_from_string (dvalStr);
98 
99  } else if (i == 6) {
100  dur = boost::posix_time::duration_from_string (dvalStr);
101 
102  } else if (i == 7) {
103  //if (dvalStr == "refundable fare")
104  // Ref = true;
105  //else Ref = false;
106 
107  } else if (i == 8) {
108  airline = dvalStr;
109 
110  } else if (i == 9) {
111  cabin = dvalStr;
112 
113  } else if (i == 10) {
114  //flightNum = dval;
115 
116  } else if (i == 11) {
117  //fare = dval;
118 
119  } else if (i == 12) {
120  //lagsNum = dval;
121 
122  } else if (i == 13) {
123  //if (dvalStr == "Saturday Nigth Stay mandatory")
124  // SNS = true;
125  //else SNS = false;
126 
127  } else if (i == 14) {
128  //if (dvalStr == "changeable fare")
129  // change = true;
130  //else change = false;
131  i = 0;
132  }
133 
134  //
135  ++i;
136 
137  } else {
138  hasAllPArams = false;
139  }
140  }
141 
142  if (hasAllPArams && i == 1) {
143  STDAIR_LOG_DEBUG ("Successfully read");
144  }
145 
146  //
147  if (!inputFile.eof()) {
148  STDAIR_LOG_ERROR ("Problem when reading input file '" << iInputFileName
149  << "'");
150  return hasReadBeenSuccessful;
151  }
152 
153  //
154  hasReadBeenSuccessful = true;
155  return hasReadBeenSuccessful;
156  }
157 
158 }