Generic hardware access library
|
00001 #include "hal/VMEAddressTableASCIIReader.hh" 00002 #include <fstream> 00003 #include <sstream> 00004 #include <iomanip> 00005 #include <strings.h> 00006 00007 HAL::VMEAddressTableASCIIReader::VMEAddressTableASCIIReader( std::string fileName ) 00008 throw (HAL::NoSuchFileException) 00009 : HAL::AddressTableReader() { 00010 // some preparation to read the file: 00011 const int MaxLength=1024; 00012 char buffer[MaxLength]; 00013 std::ifstream FileStream( fileName.c_str() ); 00014 if ( ! FileStream ) { 00015 std::string text = "Cannot open file : " + fileName + " (HAL::VMEAddressTableASCIIReader::VMEAddressTableASCIIReader)"; 00016 throw (HAL::NoSuchFileException( text, __FILE__, __LINE__, __FUNCTION__ )); 00017 } 00018 // prepare the loop over all lines of the ascii file: 00019 std::string key, description, addressSpaceStr; 00020 uint32_t address, addressModifier, dataWidth, mask, isReadable, isWritable, windowId; 00021 HAL::AddressSpace addressSpace = MEMORY; 00022 char charbuf[MaxLength]; 00023 bool first = true; 00024 bool isVme64x = false; 00025 // loop over all lines of the file: 00026 while ( ! FileStream.eof() ) { 00027 FileStream.getline(buffer, MaxLength); 00028 if ( FileStream.eof() ) break; 00029 //skip all white space at beginning of buffer: 00030 int ic = 0; 00031 while ( buffer[ic] == ' ' || 00032 buffer[ic] == '\t' ) { 00033 ic++; 00034 } 00035 if (buffer[ic] == '*') continue; 00036 if (buffer[ic] == '\n') continue; // should never happen since \n is not extracted with getline 00037 if ((uint32_t)buffer[ic] == 0xd) continue; // in case of some windows edited ascii files.... 00038 if (buffer[ic] == 0x00) continue; 00039 00040 std::istringstream Line(buffer); 00041 Line >> key ; 00042 00043 // first item: find out if we deal with vme64x or standard vme 00044 if ( first ) { 00045 first = false; 00046 std::string nextWord; 00047 Line >> nextWord; 00048 if ( nextWord == "memory" || 00049 nextWord == "configuration" || 00050 nextWord == "io" ) { 00051 isVme64x = true; 00052 addressSpaceStr = nextWord; 00053 addressSpace = convertAddressSpaceStr( addressSpaceStr ); 00054 } else { 00055 isVme64x = false; 00056 std::istringstream tmp( nextWord ); 00057 tmp >> std::hex >> addressModifier; 00058 } 00059 } else { 00060 if ( isVme64x ) { 00061 Line >> addressSpaceStr; 00062 addressSpace = convertAddressSpaceStr( addressSpaceStr ); 00063 } else { 00064 Line >> std::hex >> addressModifier; 00065 } 00066 } 00067 00068 if ( isVme64x ) { 00069 if ( addressSpace == MEMORY ) { 00070 Line >> std::dec >> windowId; 00071 dataWidth = 0; 00072 } else if ( addressSpace == CONFIGURATION ) { 00073 Line >> std::dec >> dataWidth; 00074 } 00075 } else { 00076 Line >> std::dec >> dataWidth; 00077 } 00078 00079 Line >> std::hex >> address; 00080 Line >> std::hex >> mask; 00081 Line >> isReadable; 00082 Line >> isWritable; 00083 Line >> std::ws; 00084 Line.getline(charbuf, MaxLength); 00085 description = std::string(charbuf); 00086 // it will be destroyed in the desructor of the AddresstableItem 00087 00088 // load the list : 00089 HAL::GeneralHardwareAddress* addressPointer; 00090 if ( isVme64x ) { 00091 addressPointer = new HAL::VME64xHardwareAddress(address, addressSpace, windowId, dataWidth); 00092 } else { 00093 addressPointer = new HAL::VMEHardwareAddress(address, 00094 addressModifier, 00095 dataWidth); 00096 } 00097 00098 HAL::AddressTableItem* itemPointer = 00099 new HAL::AddressTableItem(key, description, *addressPointer, 00100 mask, isWritable, isReadable); 00101 itemPointerList.push_back(itemPointer); 00102 } 00103 FileStream.close(); 00104 } 00105 00106 HAL::AddressSpace 00107 HAL::VMEAddressTableASCIIReader::convertAddressSpaceStr( std::string addressSpaceStr ) 00108 throw( HAL::IllegalValueException ) { 00109 00110 AddressSpace addressSpace; 00111 00112 if ( strcasecmp( addressSpaceStr.c_str(), "memory" ) == 0 ) { 00113 addressSpace = MEMORY; 00114 } else if ( strcasecmp( addressSpaceStr.c_str(), "configuration") == 0 ) { 00115 addressSpace = CONFIGURATION; 00116 } else { 00117 std::string text = "No such address-space in VME64x : " + addressSpaceStr + 00118 "\npossible spaces are 'memory' and 'configuration'." + 00119 "\n (HAL::VME64xAddressTableASCIIReader::HAL::VME64xAddressTableASCIIReader)"; 00120 throw (HAL::IllegalValueException( text, __FILE__, __LINE__, __FUNCTION__ )); 00121 } 00122 return addressSpace; 00123 }