Library of Bus-Adapters
/home/cschwick/hal/busAdapter/dummy/src/common/ConfigurationSpaceFileReader.cc
Go to the documentation of this file.
00001 #include "hal/ConfigurationSpaceFileReader.hh"
00002 #include <fstream>
00003 #include <iostream>
00004 #include <iomanip>
00005 #include <sstream>
00006 
00007 HAL::ConfigurationSpaceFileReader::~ConfigurationSpaceFileReader() {
00008   if ( ! gaveDataAway_ ) {
00009     delete dataMapPtr_;
00010   }
00011 }
00012 
00013 HAL::ConfigurationSpaceFileReader::ConfigurationSpaceFileReader( std::string fileName ) 
00014   throw (NoSuchFileException,
00015          IllegalValueException)  {
00016   //  cout << "about to read " << fileName << std::endl;
00017   // some preparation to read the file:
00018   gaveDataAway_ = false;
00019   dataMapPtr_ = new std::map<uint32_t, uint32_t>;
00020   const int MaxLength=1024;
00021   char buffer[MaxLength];
00022   std::ifstream FileStream( fileName.c_str() );
00023   if ( ! FileStream ) {
00024     std::string text = "Cannot open file : " + fileName + "    (HAL::ConfigurationSpaceFileReader::ConfigurationSpaceFileReader)";
00025     throw (NoSuchFileException( text, __FILE__, __LINE__, __FUNCTION__ ));
00026   }
00027 
00028   // prepare the loop over all lines of the ascii file:
00029   std::string dummy;
00030   uint32_t address, data;
00031   // loop over all lines of the file:
00032   while ( ! FileStream.eof() ) {
00033     FileStream.getline(buffer, MaxLength);
00034     if ( FileStream.eof() ) break;
00035     // skip all white space at beginning of buffer:
00036     int ic = 0;
00037     while ( buffer[ic] == ' ' ||
00038             buffer[ic] == '\t' ) {
00039       ic++;
00040     }
00041     if (buffer[ic] == '#') continue; // a comment
00042     if (buffer[ic] == '\n') continue; // should never happen since \n is not extracted with getline
00043     if (buffer[ic] == 0x00) continue;
00044 
00045     std::istringstream Line(buffer);    
00046     Line >> std::hex >> address ;
00047     Line >> dummy;
00048     
00049     if ( address%4 != 0 ) {
00050       std::stringstream text;
00051       text << "Addresses must be 32 bit aligned! (found " << std::hex << address 
00052            << ")\n    (HAL::ConfigurationSpaceFileReader::ConfigurationSpaceFileReader)" << std::endl;
00053       throw( IllegalValueException( text.str(), __FILE__, __LINE__, __FUNCTION__ ) );
00054     }
00055     
00056     while ( Line >> std::hex >> data ) {
00057       (*dataMapPtr_)[address] = data;
00058       address += 4;
00059     }
00060   }
00061   FileStream.close();
00062 }
00063 
00064 std::map< uint32_t, uint32_t>* HAL::ConfigurationSpaceFileReader::getDataMapPtr() {
00065   gaveDataAway_ = true;
00066   return dataMapPtr_; 
00067 }