Generic hardware access library
/home/cschwick/hal/generic/src/common/PCIAddressTableASCIIReader.cc
Go to the documentation of this file.
00001 #include "hal/PCIAddressTableASCIIReader.hh"
00002 
00003 #include <sstream>
00004 #include <fstream>
00005 #include <iomanip>
00006 
00007 HAL::PCIAddressTableASCIIReader::PCIAddressTableASCIIReader( std::string fileName )
00008   throw (HAL::NoSuchFileException, 
00009          HAL::IllegalValueException) 
00010   : HAL::AddressTableReader() {
00011 
00012   // some preparation to read the file:
00013   const int MaxLength=1024;
00014   char buffer[MaxLength];
00015   std::ifstream FileStream( fileName.c_str() );
00016   if ( ! FileStream ) {
00017     std::string text = "Cannot open file : " + fileName + 
00018       " (HAL::PCIAddressTableASCIIReader::PCIAddressTableASCIIReader)";
00019     throw (HAL::NoSuchFileException( text, __FILE__, __LINE__, __FUNCTION__ ));
00020   }
00021 
00022   // prepare the loop over all lines of the ascii file:
00023   std::string key, pciSpaceString, description;
00024   uint32_t address, barId, mask, isReadable, isWritable;
00025   enum AddressSpace pciSpace;
00026   char charbuf[MaxLength];
00027   // loop over all lines of the file:
00028   while ( ! FileStream.eof() ) {
00029     FileStream.getline(buffer, MaxLength);
00030     if ( FileStream.eof() ) break;
00031     //skip all white space at beginning of buffer:
00032     int ic = 0;
00033     while ( buffer[ic] == ' ' ||
00034             buffer[ic] == '\t' ) {
00035       ic++;
00036     }
00037     if (buffer[ic] == '*') continue;
00038     if (buffer[ic] == '\n') continue; // should never happen since \n is not extracted with getline
00039     if ((uint32_t)buffer[ic] == 0xd) continue; // in case of some windows edited ascii files....
00040     if (buffer[ic] == 0x00) continue;
00041 
00042     std::istringstream Line(buffer);    
00043     Line >> key ;
00044     Line >> pciSpaceString;
00045     if (pciSpaceString == "memory") {
00046       pciSpace = (AddressSpace)MEMORY;
00047       Line >> std::hex >> barId;
00048     } else if ( pciSpaceString == "configuration" ) {
00049       pciSpace = (AddressSpace)CONFIGURATION;
00050       barId = 999;
00051     } else {
00052       std::string text =  "Illegal Access Mode : " + pciSpaceString
00053         + "\n    Must be \"configure\" or \"memory\"\n    (HAL::PCIAddressTableASCIIReader::PCIAddressTableASCIIReader)";
00054       throw( HAL::IllegalValueException( text, __FILE__, __LINE__, __FUNCTION__ ) );
00055     }
00056     Line >> std::hex >> address;
00057     Line >> std::hex >> mask;
00058     Line >> isReadable;
00059     Line >> isWritable;
00060     Line >> std::ws;
00061     Line.getline(charbuf, MaxLength);
00062     description = std::string(charbuf);
00063     
00064     HAL::PCIHardwareAddress* addressPointer = new HAL::PCIHardwareAddress(address, pciSpace, barId);
00065     HAL::AddressTableItem* itemPointer = 
00066           new HAL::AddressTableItem(key, description, *addressPointer,
00067                                     mask, isWritable, isReadable);
00068     itemPointerList.push_back(itemPointer);
00069   }
00070   FileStream.close();
00071 }