Generic hardware access library
/home/cschwick/hal/generic/src/common/ASCIIFileAddressTableContainer.cc
Go to the documentation of this file.
00001 #include <sstream>
00002 #include <fstream>
00003 #include <iomanip>
00004 
00005 #include "hal/ASCIIFileAddressTableContainer.hh"
00006 
00007 HAL::ASCIIFileAddressTableContainer::ASCIIFileAddressTableContainer(HAL::ASCIIFileModuleMapper& moduleMapper,
00008                                                                     std::string addressTableMapFile,
00009                                                                     std::string addressTablePrefix )
00010   throw( HAL::NoSuchFileException,
00011          HAL::IllegalValueException,
00012          HAL::XMLProcessingException) 
00013   : moduleMapper_(moduleMapper),
00014     mappingTable_(addressTableMapFile) {
00015 
00016   // some preparation to read the file:
00017   const int MaxLength=1024;
00018   char buffer[MaxLength];
00019   std::ifstream FileStream( addressTableMapFile.c_str() );
00020   if ( ! FileStream ) {
00021     std::string text = "Cannot open file : " + addressTableMapFile + "    (HAL::VMEAddressTableASCIIReader::HAL::VMEAddressTableASCIIReader)";
00022     throw (HAL::NoSuchFileException( text, __FILE__, __LINE__, __FUNCTION__ ));
00023   }
00024 
00025 
00026   // prepare the loop over all lines of the ascii file:
00027   std::string typeId, technology, addressTableFileName;
00028 
00029   // loop over all lines of the file:
00030   while ( ! FileStream.eof() ) {
00031     FileStream.getline(buffer, MaxLength);
00032     if ( FileStream.eof() ) break;
00033     //skip all white space at beginning of buffer:
00034     int ic = 0;
00035     while ( buffer[ic] == ' ' ||
00036             buffer[ic] == '\t' ) {
00037       ic++;
00038     }
00039     if (buffer[ic] == '*') continue;
00040     if (buffer[ic] == '\n') continue; // should never happen since \n is not extracted with getline
00041     if (buffer[ic] == 0x00) continue;
00042     
00043     std::istringstream Line(buffer);    
00044     Line >> typeId;
00045     Line >> std::ws >> technology;
00046     Line >> std::ws >> addressTableFileName;
00047 
00048     // check if the termination is ".xml" or ".XML"
00049     // set a flag to indicate if we deal with xml or ascii 
00050     // AddressTables. The decision is made looking at the
00051     // file-extension: .xml and .XML are treated as xml files.
00052     bool xmlFile = false;
00053     if ( ( addressTableFileName.rfind( ".xml" ) != std::string::npos ) || 
00054          ( addressTableFileName.rfind( ".XML" ) != std::string::npos ) ) {
00055       xmlFile = true;
00056     }
00057 
00058     if ( addressTablePrefix != "" ) {
00059       addressTableFileName = addressTablePrefix + addressTableFileName;
00060     }
00061 
00062     HAL::AddressTableReader* readerPtr;
00063 
00064         if ( technology == "vme" ) {
00065 
00066       if ( xmlFile ) {
00067         readerPtr = new HAL::VMEAddressTableXMLFileReader( addressTableFileName );
00068       } else {
00069         readerPtr = new HAL::VMEAddressTableASCIIReader( addressTableFileName );
00070       }
00071 
00072       try {
00073         HAL::VMEAddressTable* adrTablePtr = new HAL::VMEAddressTable ( typeId, *readerPtr );
00074         vmeAddressTableMap_[typeId] = adrTablePtr;
00075       } catch( HAL::HardwareAccessException& e ) {
00076         delete (readerPtr);
00077         throw;
00078       }
00079 
00080       delete readerPtr;
00081 
00082     } else if ( technology == "pci" ) {
00083       
00084       if ( xmlFile ) {
00085         readerPtr = new HAL::PCIAddressTableXMLFileReader( addressTableFileName );
00086       } else {
00087         readerPtr = new HAL::PCIAddressTableASCIIReader( addressTableFileName );
00088       }
00089 
00090       try {
00091         HAL::PCIAddressTable* adrTablePtr = new HAL::PCIAddressTable ( typeId, *readerPtr );
00092         pciAddressTableMap_[typeId] = adrTablePtr;
00093       } catch ( HAL::HardwareAccessException& e ) {
00094         delete (readerPtr);
00095         throw;
00096       }
00097       delete readerPtr;
00098 
00099     } else {
00100       std::stringstream text;
00101       text << "Unknown technology : " << technology
00102            << "\n    (HAL::ASCIIFileAddressTableContainer::ASCIIFileAddressTableContainer)" << std::ends;
00103       throw (HAL::IllegalValueException( text.str(), __FILE__, __LINE__, __FUNCTION__ ) );
00104     }
00105   }
00106   FileStream.close();
00107 }
00108 
00109 HAL::ASCIIFileAddressTableContainer::~ASCIIFileAddressTableContainer() {
00110   std::map< std::string, HAL::VMEAddressTable* >::iterator vmeIt;
00111   for ( vmeIt = vmeAddressTableMap_.begin(); vmeIt != vmeAddressTableMap_.end(); vmeIt++ ) {
00112     delete ((*vmeIt).second);
00113   }
00114   
00115   std::map< std::string, HAL::PCIAddressTable* >::iterator pciIt;
00116   for ( pciIt = pciAddressTableMap_.begin(); pciIt != pciAddressTableMap_.end(); pciIt++ ) {
00117     delete ((*pciIt).second);
00118   }
00119 
00120 }
00121 
00122 HAL::VMEAddressTable& 
00123 HAL::ASCIIFileAddressTableContainer::getVMETableFromSerialNumber( std::string serialNumber )
00124   throw( HAL::IllegalValueException ) {
00125   std::string typeId = moduleMapper_.getTypeId( serialNumber );
00126   std::map< std::string, HAL::VMEAddressTable* >::iterator itTables;
00127   if ( (itTables = vmeAddressTableMap_.find( typeId )) == vmeAddressTableMap_.end() ) {
00128       std::stringstream text;
00129       text << "No vme address table for serial number " << serialNumber
00130            << " mapped to typeId " << typeId 
00131            << "\n    (HAL::ASCIIFileAddressTableContainer::ASCIIFileAddressTableContainer)" << std::ends;
00132       throw (HAL::IllegalValueException( text.str(), __FILE__, __LINE__, __FUNCTION__ ));
00133   }
00134   return *((*itTables).second);
00135 }