Generic hardware access library
/home/cschwick/hal/generic/src/common/CommandSequenceASCIIReader.cc
Go to the documentation of this file.
00001 #include "hal/CommandSequenceASCIIReader.hh"
00002 #include <stdint.h>
00003 #include <fstream>
00004 #include <list>
00005 #include <sstream>
00006 
00007 HAL::CommandSequenceASCIIReader::CommandSequenceASCIIReader( std::string fileName )
00008   throw (HAL::NoSuchFileException)
00009   : fileName(fileName) {
00010  rescan();
00011 }
00012 
00013 HAL::CommandSequenceASCIIReader::~CommandSequenceASCIIReader( ) {
00014 }
00015 
00016 void HAL::CommandSequenceASCIIReader::rescan() 
00017   throw (HAL::NoSuchFileException) {
00018   // some preparation to read the file:
00019   const int MaxLength=1024;
00020   char buffer[MaxLength];
00021   std::ifstream FileStream( fileName.c_str() );
00022   std::list<std::string> commandStringList;
00023 
00024   commandList.clear();
00025 
00026   if ( ! FileStream ) {
00027     std::string text = "Cannot open file : " + fileName + 
00028       "\n    (HAL::CommandSequenceASCIIReader::CommandSequenceASCIIReader)";
00029     throw (HAL::NoSuchFileException( text, __FILE__, __LINE__, __FUNCTION__ ));
00030   }
00031 
00032   // loop over all lines of the file:
00033   
00034   while ( ! FileStream.eof() ) {
00035     FileStream.getline(buffer, MaxLength);
00036     if (buffer[0] == '#') continue;
00037     if ((uint32_t)buffer[0] == 0xd) continue; // in case of some windows edited ascii files....
00038     
00039     std::istringstream Line(buffer);
00040     std::string word;
00041     commandStringList.clear();
00042     // this construction should allow empty lines...
00043     word = "";
00044     Line >> word;
00045     if ( word != "" ) {
00046       commandStringList.push_back( word );
00047       while ( ! Line.eof() ) {
00048                 word = "";
00049                 Line >> word ;
00050                 if ( word != "" ) {
00051                   commandStringList.push_back( word );
00052                 }
00053       }
00054       commandList.push_back( commandStringList );
00055     }
00056   }
00057   FileStream.close();
00058   firstAccess = true;
00059 }
00060