Transit Planner  1.0
An experiment on transfer patterns robustness
src/CsvParser.h
00001 // Copyright 2011, University of Freiburg,
00002 // Chair of Algorithms and Data Structures.
00003 // Author: Hannah Bast <bast>.
00004 
00005 #ifndef SRC_CSVPARSER_H_
00006 #define SRC_CSVPARSER_H_
00007 
00008 #include <gtest/gtest.h>
00009 #include <fstream>
00010 #include <string>
00011 #include <vector>
00012 
00013 using std::string;
00014 using std::vector;
00015 
00016 // Class for processing the CSV files from GTFS.
00017 class CsvParser {
00018  public:
00019   // Opens file and reads first line with table headers.
00020   void openFile(string fileName);
00021 
00022   // Returns true iff same function of underlying stream returns true.
00023   bool eof() const { return _fileStream.eof(); }
00024 
00025   // Read next line.
00026   void readNextLine();
00027   FRIEND_TEST(CsvParserTest, readNextLine);
00028 
00029   // Get i-th column from current line. Prerequisite: i < _numColumns.
00030   const char* getItem(size_t i);
00031 
00032   // Close file. Prerequisite: file must have been opened with openFile before,
00033   // assert failure otherwise.
00034   void closeFile();
00035 
00036   // Get the number of columns. Will be zero before openFile has been called.
00037   size_t getNumColumns() const { return _currentItems.size(); }
00038 
00039  private:
00040   // The handle to the file.
00041   std::ifstream _fileStream;
00042 
00043   // Current line (pointers returned by readNextLine will refer to parts of it.
00044   string _currentLine;
00045 
00046   // Pointers to the items in the current line.
00047   vector<const char*> _currentItems;
00048 };
00049 
00050 #endif  // SRC_CSVPARSER_H_
 All Classes