Generic hardware access library
|
00001 #include <sstream> 00002 #include <fstream> 00003 #include <iomanip> 00004 00005 #include "hal/ASCIIFileModuleMapper.hh" 00006 00007 HAL::ASCIIFileModuleMapper::ASCIIFileModuleMapper( std::string mapFile ) 00008 throw( HAL::NoSuchFileException ) { 00009 // some preparation to read the file: 00010 const int MaxLength=1024; 00011 char buffer[MaxLength]; 00012 std::ifstream FileStream( mapFile.c_str() ); 00013 if ( ! FileStream ) { 00014 std::string text = "Cannot open file : " + mapFile + " (HAL::ASCIIFileModuleMapper::ASCIIFileModuleMapper)"; 00015 throw (HAL::NoSuchFileException( text, __FILE__, __LINE__, __FUNCTION__ )); 00016 } 00017 // prepare the loop over all lines of the ascii file: 00018 std::string serialNumber, typeId; 00019 uint32_t baseaddress; 00020 // loop over all lines of the file: 00021 while ( ! FileStream.eof() ) { 00022 FileStream.getline(buffer, MaxLength); 00023 if ( FileStream.eof() ) break; 00024 //skip all white space at beginning of buffer: 00025 int ic = 0; 00026 while ( buffer[ic] == ' ' || 00027 buffer[ic] == '\t' ) { 00028 ic++; 00029 } 00030 if (buffer[ic] == '*') continue; 00031 if (buffer[ic] == '\n') continue; // should never happen since \n is not extracted with getline 00032 if (buffer[ic] == 0x00) continue; 00033 00034 std::istringstream Line(buffer); 00035 Line >> serialNumber ; 00036 Line >> std::ws >> typeId; 00037 Line >> std::ws >> std::hex >> baseaddress; 00038 typeIdMap_[serialNumber] = typeId; 00039 baseaddressMap_[serialNumber] = baseaddress; 00040 } 00041 FileStream.close(); 00042 } 00043 00044 std::string HAL::ASCIIFileModuleMapper::getTypeId( std::string serialNumber ) 00045 throw( HAL::IllegalValueException ) { 00046 if ( typeIdMap_.find( serialNumber ) == typeIdMap_.end() ) { 00047 std::stringstream text; 00048 text << "There is no typeId mapped for the serialNumber " 00049 << serialNumber 00050 << "\n (HAL::ASCIIFileModuleMapper::getTypeId)" << std::ends; 00051 throw( HAL::IllegalValueException( text.str(), __FILE__, __LINE__, __FUNCTION__ ) ); 00052 } 00053 return typeIdMap_[serialNumber]; 00054 } 00055 00056 00057 uint32_t HAL::ASCIIFileModuleMapper::getBaseaddress( std::string serialNumber ) 00058 throw( HAL::IllegalValueException ) { 00059 if ( baseaddressMap_.find( serialNumber ) == baseaddressMap_.end()) { 00060 std::stringstream text; 00061 text << "There is no baseaddress mapped for the serialNumber " 00062 << serialNumber 00063 << "\n (HAL::ASCIIFileModuleMapper::getBaseaddress)" << std::ends; 00064 throw( HAL::IllegalValueException( text.str(), __FILE__, __LINE__, __FUNCTION__ ) ); 00065 } 00066 return baseaddressMap_[serialNumber]; 00067 00068 }