BOOL BBXParser::LoadFile( LPWSTR _FileName ) { Cleanup(); // //CDebugConsole::GetInstance()->Messagef( L"BBXParser\n" ); // //CDebugConsole::GetInstance()->Message( _FileName ); // //CDebugConsole::GetInstance()->Messagef( L"\n" ); FILE* fp; fp = _wfopen( _FileName, L"rt" ); TCHAR sLine[ 1024 ]; ReadLineFromFile( fp, sLine ); if( !CheckFile( fp, sLine ) ) return FALSE; while( !feof( fp ) ) { ReadLineFromFile( fp, sLine ); GetNumBoundBox( sLine ); BeginBoundBoxList( fp, sLine ); } // 렌더링 될 충돌박스를 만든다 CreateCube( m_iNumBoundBox, m_pData ); fclose( fp ); return TRUE; }
void ReadFile(int index) { long fileSize=0; char *tempInputCommand; fileSize=GetFileSize(_FilesList[index]); do{ tempInputCommand=ReadLineFromFile(_FilesList[index],fileSize); if(tempInputCommand==NULL) break; tempInputCommand= StringTrim(tempInputCommand); if(AnalyzeText(tempInputCommand)==TRUE) /*analyze line */ { AsmQueue_AddRecord(tempInputCommand,_AssemblerCommandsQueue[index]); } } while(1); }
int main(int argc, char *argv[]) { if (argc != 3) { printf("Wrong number of arguments"); exit(1); } FILE *inFile = fopen(argv[1], "rt"); if(inFile == NULL) { printf("No inFile\n"); exit(1); } FILE *outFile = fopen(argv[2], "w"); static char buf[512]; int i; for(i = 0; !feof(inFile); i++) { int a = ReadLineFromFile(inFile, buf); WriteLineToFile(outFile, buf, a); } return 0; }
//Get system memory total size static unsigned long long GetSystemMemorySize () { std::string memory_info = ReadLineFromFile ("/proc/meminfo"); size_t line_off = 0; if (!memory_info.empty () && (line_off = memory_info.find ("MemTotal")) != std::string::npos) { line_off = memory_info.substr (line_off).find (':') + 1; memory_info = memory_info.substr (line_off, memory_info.substr (line_off).find ("kB") - 1); line_off = 0; // trim whitespace and append 000 to replace kB while (isspace (memory_info.at (line_off))) { ++line_off; } memory_info = memory_info.substr (line_off); if (!memory_info.empty ()) { return strtoull (memory_info.c_str (), NULL, 10); } } return 0; }
int ReadLine(void) { int r; /* If we're at the end of a file, pop */ while (!CLine && !fp) { r = PopFile(); if (r) return r; } /* If it's cached, read line from the cache */ if (CLine) { CurLine = CLine->text; LineNo = CLine->LineNo; CLine = CLine->next; FreshLine = 1; if (DebugFlag & DB_ECHO_LINE) OutputLine(ErrFp); return OK; } /* Not cached. Read from the file. */ return ReadLineFromFile(); }
BOOL BBXParser::BeginBoundBoxList( FILE* _fp, LPWSTR _sLine ) { if( 0 == wcsnicmp( _sLine, m_pKeyword[ BBXKEY::BOUNDBOX_LIST ].sKey, m_pKeyword[ BBXKEY::BOUNDBOX_LIST ].iKey ) ) { while( !feof( _fp ) ) { ReadLineFromFile( _fp, _sLine ); if( L'}' == _sLine[ 0 ] ) break; GetBoundBoxName( _sLine ); GetBoundBoxPivot( _sLine ); GetBoundBoxPlusSize( _sLine ); GetBoundBoxMinusSize( _sLine ); GetBoundBoxColor( _sLine ); } m_iCurrentIndex++; return TRUE; } return FALSE; }
static int CacheFile(char const *fname) { int r; CachedFile *cf; CachedLine *cl; char const *s; if (DebugFlag & DB_TRACE_FILES) { fprintf(ErrFp, "Caching file `%s' in memory\n", fname); } cl = NULL; /* Create a file header */ cf = NEW(CachedFile); cf->cache = NULL; if (!cf) { ShouldCache = 0; FCLOSE(fp); return E_NO_MEM; } cf->filename = StrDup(fname); if (!cf->filename) { ShouldCache = 0; FCLOSE(fp); free(cf); return E_NO_MEM; } if (RunDisabled & RUN_NOTOWNER) { cf->ownedByMe = 0; } else { cf->ownedByMe = 1; } /* Read the file */ while(fp) { r = ReadLineFromFile(); if (r) { DestroyCache(cf); ShouldCache = 0; FCLOSE(fp); return r; } /* Skip blank chars */ s = DBufValue(&LineBuffer); while (isempty(*s)) s++; if (*s && *s!=';' && *s!='#') { /* Add the line to the cache */ if (!cl) { cf->cache = NEW(CachedLine); if (!cf->cache) { DBufFree(&LineBuffer); DestroyCache(cf); ShouldCache = 0; FCLOSE(fp); return E_NO_MEM; } cl = cf->cache; } else { cl->next = NEW(CachedLine); if (!cl->next) { DBufFree(&LineBuffer); DestroyCache(cf); ShouldCache = 0; FCLOSE(fp); return E_NO_MEM; } cl = cl->next; } cl->next = NULL; cl->LineNo = LineNo; cl->text = StrDup(s); DBufFree(&LineBuffer); if (!cl->text) { DestroyCache(cf); ShouldCache = 0; FCLOSE(fp); return E_NO_MEM; } } } /* Put the cached file at the head of the queue */ cf->next = CachedFiles; CachedFiles = cf; return OK; }
//////////////////////////////////////////////////////////////////////////// // Parameter: // 1, CFile& : hex file preference, to be read // 2, char* : a memory pointer, has been allocate in heep, it's a valid memory space. // 3, int : parameter 2, memory space's size. // return: // int : it's the truely length of the used buffer, also useful byte number after hex file been parsed. int CHexFileParser::ReadNormalHexFile( CFile& hexFile, char* pBuf, int nBufLen) { ASSERT(nBufLen >= 0xFFFF); //CString m_get_data_from_file; int nBufCount = 0; //*****************inspect the file********************* unsigned int nLineNum=0; char a[256]; ZeroMemory(a, 256); //while(NULL!=ar.ReadString(m_get_data_from_file))//循环读取文件,直到文件结束 while(ReadLineFromFile(hexFile, a)) {//get a line from the file,check "crc" for total file #ifdef _DEBUG if (a[8] != '0') { int n = 0; } #endif nLineNum++; //the line number that the wrong hex file; TS_UC get_hex[128]={0}; //get hex data,it is get from the line char UINT i = 0; for( i=0;i<strlen(a); i++) //get a line//去掉第一个 { a[i] = a[i+1]; } int nLen = strlen(a)-2; // 减去回车换行符号 if(strlen(a)%2==0) turn_hex_file_line_to_unsigned_char(a); // turn every char to int else { wchar_t p_c_temp[74]={'\0'}; swprintf_s(p_c_temp,_T("Error: the hex file had error at %d line!"),nLineNum); nBufCount = 0; if(!auto_flash_mode) AfxMessageBox(p_c_temp); //close_com(); return 0; } turn_int_to_unsigned_char(a,nLen,get_hex); //turn to hex if(get_hex[3]==1) //for to seektobegin() function,because to end of the file break; TS_UC uctemp2=0; for(int j=0;j<(nLen/2);j++) uctemp2+=get_hex[j]; if(uctemp2%256!=0) { wchar_t p_c_temp[74]={'\0'}; swprintf_s(p_c_temp,_T("Error: the hex file had error at %d line!"),nLineNum); nBufCount = 0; if(!auto_flash_mode) AfxMessageBox(p_c_temp,MB_OK); //close_com(); return 0; } if(get_hex[1]==0 && get_hex[2]==0) get_hex[4]=255; //for the 0000 register to write 255 unsigned int ltemp; ltemp=get_hex[1]*256+get_hex[2]; for(int j=0;j<get_hex[0];j++) { pBuf[ltemp+j]=get_hex[4+j];//get the data } if((UINT)nBufCount<(ltemp+get_hex[0])) { nBufCount=ltemp+get_hex[0]-1; } ZeroMemory(a, 256); }//end while hexFile.Close();//close the file return nBufCount; }
//////////////////////////////////////////////////////////////////////////// // Parameter: // 1, CFile& : hex file preference, to be read // 2, char* : a memory pointer, has been allocate in heep, it's a valid memory space. // 3, int : specify parameter 2, memory space's size. // return: // int : it's the truely length of the used buffer, also useful byte number after hex file been parsed. // 读扩展线性地址记录的hex file-最大32位的地址空间 BOOL CHexFileParser::ReadExtLinearHexFile(CFile& hexfile, char* pBuf, int nBufLen) { //CArchive ar(&m_hex_file,CArchive::load); //ASSERT(nBufLen >= 0xFFFF); if (pBuf) { delete[] pBuf; pBuf = NULL; } pBuf = new char[c_nHexFileBufLen]; CString strGetData; int nBufCount = 0; //*****************inspect the file********************* unsigned int nLineNumErr=0; DWORD dwHiAddr = 0; // 高位地址 char a[256]; ZeroMemory(a, 256); //while(NULL!=ar.ReadString(strGetData)) //循环读取文件,直到文件结束 while(ReadLineFromFile(hexfile, a)) { // 取得高位地址,可能不止一处扩展 if( a[8] == '4') { CString strTemp(a); dwHiAddr = GetHighAddrFromFile(strTemp); dwHiAddr <<= 16; if( nBufCount != 0) { m_szFlags.push_back(nBufCount); } // do CRC continue; } //get a line from the file,check "crc" for total file nLineNumErr++; //the line number that the wrong hex file; TS_UC get_hex[128]={0};//get hex data,it is get from the line char //the number is (i-1) //int nLen = strGetData.GetLength(); for(UINT i=0; i<strlen(a); i++) // 去掉冒号 { a[i]=a[i+1]; } int nLen = strlen(a)-2; // 不算回车换行的长度 if(strlen(a)%2==0) turn_hex_file_line_to_unsigned_char(a);//turn every char to int else { wchar_t p_c_temp[74]={'\0'}; swprintf_s(p_c_temp,_T("Error: the hex file had error at %d line!"),nLineNumErr); //AddStringToOutPuts(p_c_temp); nBufCount = 0; AfxMessageBox(p_c_temp); //close_com(); goto END; return 0; } turn_int_to_unsigned_char(a,nLen,get_hex);//turn to hex if(get_hex[3]==1) //for to seektobegin() function,because to end of the file break; if(!DoCRC( get_hex, nLen/2)) { wchar_t p_c_temp[74]={'\0'}; swprintf_s(p_c_temp,_T("Error: the hex file had error at %d line!"),nLineNumErr); //AddStringToOutPuts(p_c_temp); nBufCount = 0; AfxMessageBox(p_c_temp, MB_OK); //close_com(); goto END; return 0; } // if(get_hex[1]==0 && get_hex[2]==0) // get_hex[4]=255;//for the 0000 register to write 255 int temp; char temp_buf[32]; if (nLineNumErr==9) { for (int i=0;i<32;i++) { temp_buf[i]=a[i+8]; } for (int i=0;i<20;i++) { temp=temp_buf[2*i]*16+temp_buf[2*i+1]; m_DeviceInfor[i]=temp; } //m_softwareRev = (temp_buf[30] + temp_buf[31]*256)/10.0; //m_ProductModel=m_DeviceInfor[0]; m_softwareRev= ((a[38]*16+a[39]) + (a[40]*16 + a[41])*256)/10.0; m_ProductName.Empty(); for (int i=0;i<10;i++) { CString temp1; temp1.Format(_T("%C"),m_DeviceInfor[5+i]); m_ProductName = m_ProductName + temp1; } } unsigned int ltemp; ltemp=get_hex[1]*256+get_hex[2] + dwHiAddr; // if (m_IsRAM) // { // ltemp-=0x00008000; // } for(int j=0;j<get_hex[0];j++) pBuf[ltemp+j]=get_hex[4+j];//get the data if((UINT)nBufCount<(ltemp+get_hex[0])) nBufCount=ltemp+get_hex[0]; ZeroMemory(a, 256); }//end while m_szFlags.push_back(nBufCount); END: hexfile.Close();//close the file return nBufCount; //return 0 ; }
//Determine Linux distro and version static void GetLinuxDistro (std::string& name, std::string& version) { FILE* pfd = nullptr; bool found = false; char buf[512] = {'\0'}; // try lsb file first if (0 == access ("/etc/lsb-release", 0)) { pfd = fopen ("/etc/lsb-release", "r"); } if (nullptr != pfd) { while (nullptr != fgets (buf, 511, pfd) && !feof (pfd)) { buf[strlen (buf) < 1 ? 0 : strlen (buf) - 1] = '\0'; if (0 == strncmp (buf, "DISTRIB_ID", 10)) { name = buf + 11; continue; } if (0 == strncmp (buf, "DISTRIB_RELEASE", 15)) { version = buf + 16; continue; } if (!name.empty () && !version.empty ()) { found = true; break; } } fclose (pfd); if (found) { return; } } // not found // try known flat-text file locations std::vector <std::string> paths; found = false; paths.push_back ("/etc/system-release"); paths.push_back ("/etc/redhat-release"); paths.push_back ("/etc/gentoo-release"); paths.push_back ("/etc/novell-release"); paths.push_back ("/etc/gentoo-release"); paths.push_back ("/etc/SuSE-release"); paths.push_back ("/etc/SUSE-release"); paths.push_back ("/etc/sles-release"); paths.push_back ("/etc/debian_release"); paths.push_back ("/etc/slackware-version"); paths.push_back ("/etc/centos-release"); std::vector<std::string>::iterator it = paths.begin (); for (; it != paths.end (); ++it) { if (0 == access (it->c_str (), 0)) { found = true; break; } } if (found) { pfd = fopen (it->c_str (), "r"); if (nullptr == pfd) { bzero (buf, sizeof(buf)); if (nullptr != fgets (buf, 511, pfd)) { buf[strlen (buf) < 1 ? 0 : strlen (buf) - 1] = '\0'; name = buf; } fclose (pfd); version = "Kernel "; version += ReadLineFromFile ("/proc/sys/kernel/osrelease"); } } }