// Function now converts the passed key to lower case to search the header std::string BinaryReader::FromHeader(std::string key,int itemnum,std::string THROW) { //DEBUG statement DEBUGPRINT("Retrieving item "<<itemnum<<" from value relating to key "<<key); //Function to return the value string mapped by the key string //itemnum starts from 0 and items are separated by ; std::map<std::string,std::string>::iterator it; std::string str=""; //Find the value from the map it=this->Header.find(ToLowerCase(key)); if(it==this->Header.end()) //if the key doesnt exist return an empty string { if(THROW.compare("true")==0) { throw missingheaderitemerror+key; } return ""; } else //else continue and get the related value to the key str=it->second; std::string retstr=""; if(!str.compare(""))//This should never get called now that above check is in place, unless a key has an empty string as value (shouldnt happen) { //Empty string - probably means key is unknown return retstr; } else { if(str.find(';')!=std::string::npos) { //This occurs when multiple objects where on the multiple lines //eg {;1 // ;56 // ;76} //Not an empty string - search for itemnums ; delimiter unsigned int pos=0; //set position to start of string for(int i=0;i<=itemnum;i++) { pos=str.find(';',pos+1); // if(pos == std::string::npos) // { // //there are less than itemnum ; in string // return retstr; // } } //found itemnum ; int index=str.find(';',pos+1); retstr=str.substr(pos+1,index-(pos+1)); retstr=TrimPunctuation(retstr); return retstr; } else if(str.find(',')!=std::string::npos) { //This occurs when multiple objects were on the same line //eg {1, 56, 76} - only returns numbers //Commented out below 2 lines 10/09/2010 and replaced with new fnction to use comma delims and return anything //str=RemoveAllBut(str,"0123456789.-+ "); //return GetItemFromString(str,itemnum); str=RemoveAllOf(str,"{}"); return GetItemFromString(str,itemnum,','); } else { //unknown delimitng type. return ""; } } }
BOOL GetSystemName(PWSTR pBuf, SIZE_T cchBuf) { static const VENDOR_LONG_NAME LongNames[] = { { L"ASUSTeK", L"ASUS" }, { L"First International Computer", L"FIC" }, { L"Hewlett-Packard", L"HP" }, { L"MICRO-STAR", L"MSI" }, { L"SGI.COM", L"SGI" }, { L"Silicon Graphics International", L"SGI" }, { L"Intel(R) Client Systems", L"Intel" }, { L"InformationComputerSystems", L"ICS" }, { L"CHUWI INNOVATION AND TECHNOLOGY", L"CHUWI" }, { L"http://www.abit.com.tw/", L"ABIT" }, { L"www.abit.com.tw", L"ABIT" }, { L"CASPER BILGISAYAR SISTEMLERI A.S", L"Casper" }, { L"Colorful Technology And Development", L"Colorful" }, { L"Colorful Yu Gong Technology And Development", L"Colorful Yu Gong" }, { L"HaierComputer", L"Haier" }, { L"HELIOS BUSINESS COMPUTER", L"HELIOS" }, { L"Shanghai Zongzhi InfoTech", L"Zongzhi" }, { L"TSING HUA TONGFANG CO.,LTD", L"TSINGHUA TONGFANG" }, { L"Yeston Digital Technology Co.,LTD", L"Yeston" }, }; static const REDUNDANT_WORD RedundantWords[] = { { L"Corporation", FALSE }, { L"Communication", FALSE }, { L"Computer", FALSE }, { L"Computers", FALSE }, { L"Group", FALSE }, { L"Cloud", FALSE }, { L"Center", FALSE }, { L"Systems", FALSE }, { L"Microsystems", FALSE }, { L"Infosystems", FALSE }, { L"Electronics", FALSE }, { L"Electric", FALSE }, { L"Software", FALSE }, { L"Foundation", FALSE }, { L"International", FALSE }, { L"Interantonal", FALSE }, // on purpose (some MSI boards) { L"INTERANTIONAL", FALSE }, // on purpose (some MSI boards) { L"Industrial", FALSE }, { L"Information", FALSE }, { L"Informatica", FALSE }, { L"Technology", FALSE }, { L"Tecohnology", FALSE }, // on purpose (some Gigabyte boards) { L"Technologies", FALSE }, { L"Tecnologia", FALSE }, { L"Limited", FALSE }, { L"Int", FALSE }, { L"Inc", FALSE }, { L"Co", FALSE }, { L"Corp", FALSE }, { L"Crop", FALSE }, { L"Ltd", FALSE }, { L"LTDA", FALSE }, { L"GmbH", FALSE }, { L"S.p.A", FALSE }, { L"A.S.", FALSE }, { L"S.A", FALSE }, { L"S.A.S", FALSE }, { L"S/A", FALSE }, { L"SA", FALSE }, { L"SAS", FALSE }, { L"BV", FALSE }, { L"AG", FALSE }, { L"OOO", TRUE }, { L"CJSC", FALSE }, { L"INT'L", FALSE }, { L"plc", FALSE }, }; PVOID SMBiosBuf; PCHAR DmiStrings[ID_STRINGS_MAX] = { 0 }; WCHAR ven[512], dev[512]; CHAR tmpstr[512]; BOOL bTrimProduct, bTrimFamily, bGenericName, bRemove; UINT i; PWCHAR j; SMBiosBuf = LoadSMBiosData(DmiStrings); if (!SMBiosBuf) { return FALSE; } TrimNonPrintable(DmiStrings[SYS_VENDOR]); bTrimProduct = TrimNonPrintable(DmiStrings[SYS_PRODUCT]); TrimNonPrintable(DmiStrings[SYS_VERSION]); bTrimFamily = TrimNonPrintable(DmiStrings[SYS_FAMILY]); TrimNonPrintable(DmiStrings[BOARD_VENDOR]); TrimNonPrintable(DmiStrings[BOARD_NAME]); TrimNonPrintable(DmiStrings[BOARD_VERSION]); if (bTrimProduct) { if (DmiStrings[SYS_FAMILY] && !bTrimFamily) { DmiStrings[SYS_PRODUCT] = DmiStrings[SYS_FAMILY]; bTrimProduct = FALSE; } } GetSMBiosStringW(DmiStrings[SYS_VENDOR], ven, _countof(ven), TRUE); GetSMBiosStringW(DmiStrings[SYS_PRODUCT], dev, _countof(dev), TRUE); bGenericName = IsGenericSystemName(ven, dev, NULL) || bTrimProduct; if (wcslen(dev) == 0 || !wcscmp(dev, ven) || bGenericName) { BOOL bGenericVen = FALSE, bGenericDev = (wcslen(dev) == 0 || !wcscmp(dev, ven) || bTrimProduct); if (bGenericName && IsGenericSystemName(ven, NULL, &bRemove)) { if (bRemove) { *ven = 0; } bGenericVen = TRUE; } if (bGenericName && IsGenericSystemName(NULL, dev, &bRemove)) { if (bRemove) { *dev = 0; } bGenericDev = TRUE; } // system strings are unusable, use board strings if (DmiStrings[BOARD_VENDOR] != NULL || !bGenericName) { if ((DmiStrings[BOARD_VENDOR] && strlen(DmiStrings[BOARD_VENDOR]) >= 2 && strstr(DmiStrings[BOARD_VENDOR], " ") != DmiStrings[BOARD_VENDOR]) || bGenericVen) { GetSMBiosStringW(DmiStrings[BOARD_VENDOR], ven, _countof(ven), TRUE); } GetSMBiosStringW(DmiStrings[BOARD_NAME], dev, _countof(dev), TRUE); if (IsGenericSystemName(ven, NULL, &bRemove) && bRemove) { *ven = 0; } if (IsGenericSystemName(NULL, dev, &bRemove) && bRemove) { *dev = 0; if (!bGenericDev) { GetSMBiosStringW(DmiStrings[SYS_PRODUCT], dev, _countof(dev), TRUE); } } if (wcslen(dev) == 0 && DmiStrings[SYS_VERSION] != NULL) { GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE); if (IsGenericSystemName(NULL, dev, &bRemove) && bRemove) { *dev = 0; } } if (wcslen(dev) == 0 && DmiStrings[BOARD_VERSION] != NULL) { GetSMBiosStringW(DmiStrings[BOARD_VERSION], dev, _countof(dev), TRUE); if (IsGenericSystemName(NULL, dev, &bRemove) && bRemove) { *dev = 0; } } } if (wcslen(ven) == 0 && wcslen(dev) == 0) { // board strings are empty, use BIOS vendor string GetSMBiosStringW(DmiStrings[BIOS_VENDOR], ven, _countof(ven), TRUE); } } else { if (wcslen(ven) < 2) { GetSMBiosStringW(DmiStrings[BOARD_VENDOR], ven, _countof(ven), TRUE); if (IsGenericSystemName(ven, NULL, &bRemove) && bRemove) { *ven = 0; } } } // workaround for LORD ELECTRONICS if (((j = wcsstr(ven, L" ")) != NULL) && (j - ven > 2)) { i = j - ven; if (!wcsncmp(ven + wcslen(ven) - i, ven, i)) { ven[wcslen(ven) - i] = L'\0'; } } // make vendor strings shorter for (i = 0; i < _countof(LongNames); i++) { if (wcsstr(dev, LongNames[i].pwLongName) == dev) { // swap ven and dev StringCchCopyW(pBuf, cchBuf, ven); StringCchCopyW(ven, _countof(ven), dev); StringCchCopyW(dev, _countof(dev), pBuf); } wcsrep(ven, LongNames[i].pwLongName, LongNames[i].pwShortName, TRUE); } // remove redundant words for (i = 0; i < _countof(RedundantWords); i++) { wcsrep(ven, RedundantWords[i].pwStr, L"", RedundantWords[i].bReplaceFirstWord); } for (i = 0; i < _countof(RedundantWords); i++) { StringCchCopyW(pBuf, cchBuf, RedundantWords[i].pwStr); StringCchCatW(pBuf, cchBuf, L"."); wcsrep(ven, pBuf, L"", RedundantWords[i].bReplaceFirstWord); } // workaround for LENOVO notebooks if (!wcsicmp(ven, L"LENOVO")) { StringCchCopyW(ven, _countof(ven), L"Lenovo"); if (DmiStrings[SYS_VERSION] != NULL) { if (!strncmp(DmiStrings[SYS_VERSION], "ThinkPad ", 11)) { DmiStrings[SYS_VERSION][8] = L'\0'; } if (wcslen(dev) > 0 && (!strcmp(DmiStrings[SYS_VERSION], "IdeaCentre") || !strcmp(DmiStrings[SYS_VERSION], "ThinkPad"))) { DmiStrings[SYS_FAMILY] = DmiStrings[SYS_VERSION]; DmiStrings[SYS_VERSION] = NULL; } else { StringCchCopyA(tmpstr, _countof(tmpstr), DmiStrings[SYS_VERSION]); _strupr(tmpstr); } } if (DmiStrings[SYS_VERSION] != NULL && strcmp(tmpstr, " ") && strcmp(tmpstr, "LENOVO") && strstr(tmpstr, "LENOVO ") == NULL && strstr(tmpstr, "LENOVO PRODUCT") == NULL && strstr(tmpstr, "INVALID") == NULL && strncmp(tmpstr, " ", 3) && strstr(DmiStrings[SYS_VERSION], "Rev ") == NULL && strstr(DmiStrings[SYS_VERSION], "1.") == NULL && wcsistr(dev, L"System ") == NULL && // includes System x and ThinkSystem wcsistr(dev, L"IdeaPad ") == NULL && wcsistr(dev, L"ThinkServer ") == NULL) { GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE); } if (wcsstr(dev, L"Lenovo-") == dev) { // replace "-" with space dev[6] = L' '; } if (!wcscmp(dev, L"Lenovo")) { GetSMBiosStringW(DmiStrings[BOARD_NAME], dev, _countof(dev), TRUE); } } if (!wcscmp(ven, L"IBM") && DmiStrings[SYS_VERSION] != NULL && (strstr(DmiStrings[SYS_VERSION], "ThinkPad ") != NULL || strstr(DmiStrings[SYS_VERSION], "ThinkCentre ") != NULL)) { GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE); } // workaround for DEXP if (!wcscmp(ven, L"DEXP")) { if (DmiStrings[SYS_PRODUCT] != NULL && DmiStrings[SYS_VERSION] != NULL && (!stricmp(DmiStrings[SYS_PRODUCT], "Tablet PC") || !stricmp(DmiStrings[SYS_PRODUCT], "Notebook") || !stricmp(DmiStrings[SYS_PRODUCT], "Decktop"))) { GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE); } } // workaround for Razer Blade if (!wcscmp(ven, L"Razer") && !wcscmp(dev, L"Blade")) { if (DmiStrings[SYS_VERSION] != NULL) { StringCchCopyW(ven, _countof(ven), L"Razer Blade"); GetSMBiosStringW(DmiStrings[SYS_VERSION], dev, _countof(dev), TRUE); } } // workaround for MSI motherboards if (!wcscmp(ven, L"MSI") && wcsstr(dev, L"MS-") != NULL && DmiStrings[BOARD_NAME] != NULL && strstr(DmiStrings[BOARD_NAME], "(MS-") != NULL) { GetSMBiosStringW(DmiStrings[BOARD_NAME], dev, _countof(dev), TRUE); } if (wcslen(ven) == 0 && wcsstr(dev, L"MS-") == dev) { StringCchCopyW(ven, _countof(ven), L"MSI"); } // trim redundant characters TrimPunctuation(ven); TrimPunctuation(dev); if (wcsistr(dev, ven) == dev || (!wcscmp(ven, L"ASUS") && wcsstr(dev, L"ASUS") != NULL) || (!wcscmp(ven, L"HP") && wcsstr(dev, L" by HP") != NULL) || (!wcscmp(ven, L"INTEL") && wcsstr(dev, L" INTEL") != NULL)) { // device string contains vendor string, use second only StringCchCopyW(pBuf, cchBuf, dev); } else { if (wcslen(ven) > 0 && wcslen(dev) > 0 && (j = wcschr(dev, L' '))) { // check if vendor string ends with first word of device string i = j - dev; if (wcslen(ven) > i && !_wcsnicmp(ven + wcslen(ven) - i, dev, i)) { ven[wcslen(ven) - i] = L'\0'; TrimPunctuation(ven); } } StringCchCopyW(pBuf, cchBuf, ven); AppendSystemFamily(pBuf, cchBuf, DmiStrings, dev); if (wcslen(pBuf) > 0 && wcslen(dev) > 0) { StringCchCatW(pBuf, cchBuf, L" "); } StringCchCatW(pBuf, cchBuf, dev); } FreeSMBiosData(SMBiosBuf); return (wcslen(pBuf) > 0); }
//------------------------------------------------------------------------- // Function to read in the information from the header file and store in map // Keys are now converted to lowercase , Values are not converted //------------------------------------------------------------------------- int BinaryReader::ReadHeader() { //DEBUG statement DEBUGPRINT("Reading BIL Header file..."); //Open the header file //It is expected to be the same as filename(-'raw' + 'hdr') //std::string hdrfile=this->filename; //hdrfile.replace(this->filename.length() - 3, 3, "hdr"); std::string hdrfile; //Now try and open the file std::ifstream hdrin; int failcount=0,maxtypes=2; //Loop through the types of header file name to try and open until one works for(short int type=0;type<maxtypes;type++) { this->hdrfilename=GetHeaderFileName(type); hdrin.open(this->hdrfilename.c_str()); if(!hdrin.is_open()) { failcount++; //An error has occured //brinfo<<"An error has occurred opening the hdr file: "<<this->hdrfilename<<std::endl; continue; //return -1; } else break; } if(failcount==maxtypes) { //An error has occured brinfo<<"Unable to open a hdr file, does one exist?"<<std::endl; return -1; } else { //the hdr file has been opened successfully //Now search for the number of lines/samples/bands first std::string strbuffer;//buffer to hold lines from hdr file std::string strkey; //string for storing map key temporarally std::string strval; //string for storing map value temporarly bool multilineval=false; // bool to test if values are over multiple file lines while(!hdrin.eof()) { //get a line from the hdr file std::getline(hdrin,strbuffer); //std::cout<<strbuffer<<std::endl; //search for an equals if(strbuffer.find('=') != std::string::npos) { //An = has been found in this string if(strbuffer.find('{') != std::string::npos) { //A { has been found. This could mean that the value is on more than one line strkey=strbuffer.substr(0,strbuffer.find('=')); //can set key strval.assign(strbuffer.substr(strbuffer.find('=')+1)); //this is part of value //trim whitespace strkey=TrimWhitespace(strkey); strval=TrimWhitespace(strval); if(strbuffer.find('}')!=std::string::npos) // } also found on same line { //The value is only on one line this->Header[ToLowerCase(strkey)]=strval; strkey.clear(); strval.clear(); continue; } else multilineval=true;// value is spread over multiple lines of header file } else { //The value is only on this line //Split up string at equals strkey=strbuffer.substr(0,strbuffer.find('=')); strval=strbuffer.substr(strbuffer.find('=')+1); //trim whitespace strkey=TrimWhitespace(strkey); strval=TrimWhitespace(strval); this->Header[ToLowerCase(strkey)]=strval; strkey.clear(); strval.clear();//empty the string continue; } } else if(strbuffer.find('}') != std::string::npos) { //there is a }. This could mean that the end of a value has been reached //Check if found at the end of the line (whitespace removed) strbuffer=TrimWhitespace(strbuffer); if(strbuffer.find('}')==strbuffer.length()-1) { //At the end - get everything upto there as values - comma separates values strbuffer=ReplaceAllWith(&strbuffer,',',';'); strval=strval+";"+strbuffer; // add the string to the value string using ; as delimiter this->Header[ToLowerCase(strkey)]=strval; //add to the header map multilineval=false; //reset the bool strval.clear(); } else { //Not at the end - this is odd throw "Error. Problem with hdr file - contains data after '}' value: "+strbuffer; } } if((strbuffer.find('=') == std::string::npos) && (strbuffer.find('}') == std::string::npos)) // no = and no } { strbuffer=TrimWhitespace(strbuffer); //No = in this string if(multilineval==false)//For the moment will just pass this as the key AND value into the map { this->Header[strbuffer]=strbuffer; } else if(!strbuffer.empty()) { // 14/02/2011 TrimPunctuation and then remove commas and replace with ';' - this could be dangerous strbuffer=TrimPunctuation(strbuffer); strbuffer=ReplaceAllWith(&strbuffer,',',';'); strval=strval+";"+strbuffer; // add the string to the value string using ; as delimiter } } } //close the file hdrin.close(); hdrin.clear(); } return 1; }