BOOL DBCFile::WriteToBinFile(const CHAR* szFileName) { assert(szFileName); FILE* fp = fopen(szFileName, "wb"); if(fp == 0) { return FALSE; } //write file head FILE_HEAD theHead; theHead.m_Identify = 0XDDBBCC00; theHead.m_nFieldsNum = GetFieldsNum(); theHead.m_nRecordsNum = GetRecordsNum(); theHead.m_nStringBlockSize = m_nStringBufSize; if(1 != fwrite(&theHead, sizeof(FILE_HEAD), 1, fp)) { fclose(fp); return FALSE; } //write field type if(m_theType.size() != fwrite((const void*)(&(m_theType[0])), sizeof(FIELD_TYPE), m_theType.size(), fp)) { fclose(fp); return FALSE; } DATA_BUF bufTemp = m_vDataBuf; //convert string runtime to offset for(int i=0; i<(int)bufTemp.size(); i++) { if(m_theType[i%m_nFieldsNum] == T_STRING) { bufTemp[i].iValue -= (INT)(INT_PTR)m_pStringBuf; } } //write data block if(m_vDataBuf.size() != fwrite((const void*)(&(bufTemp[0])), sizeof(FIELD), bufTemp.size(), fp)) { fclose(fp); return FALSE; } //write string block if(1 != fwrite(m_pStringBuf, m_nStringBufSize, 1, fp)) { fclose(fp); return FALSE; } fclose(fp); return TRUE; }
const DBCFile::FIELD* DBCFile::Search_Posistion(INT nRecordLine, INT nColumNum) const { INT nPosition = nRecordLine*GetFieldsNum() + nColumNum; if(nPosition <0 || nColumNum >= (INT)m_vDataBuf.size()) return NULL; return &(m_vDataBuf[nPosition]); }
const DBCFile::FIELD* DBCFile::Search_Posistion(INT nRecordLine, INT nColumNum) const { INT nPosition = nRecordLine*GetFieldsNum() + nColumNum; if(nPosition <0 || nColumNum >= (INT)m_vDataBuf.size()) { char szTemp[260]; tsnprintf(szTemp, MAX_FILE_PATH, "DBC:Invalid search request[rec=%d, col=%d]", nRecordLine, nColumNum); #ifdef GAME_CLIENT throw std::string(szTemp); #else //AssertEx(FALSE, szTemp); #endif return NULL; } return &(m_vDataBuf[nPosition]); }
BOOL DBCFile::OpenFromMemoryImpl_Binary(const CHAR* pMemory, const CHAR* pDeadEnd, const CHAR* szFileName) { register const char* pMem = pMemory; //---------------------------------------------------- //Read Head FILE_HEAD theHead; memcpy(&theHead, pMem, sizeof(FILE_HEAD)); if(theHead.m_Identify != 0XDDBBCC00 ) { return FALSE; } //check memory size if( sizeof(FILE_HEAD) + sizeof(UINT)*theHead.m_nFieldsNum + sizeof(FIELD)*theHead.m_nRecordsNum * theHead.m_nFieldsNum + theHead.m_nStringBlockSize > (DWORD)(pDeadEnd-pMemory)) { return FALSE; } pMem += sizeof(FILE_HEAD); //---------------------------------------------------- //Init m_nRecordsNum = theHead.m_nRecordsNum; m_nFieldsNum = theHead.m_nFieldsNum; m_nStringBufSize = theHead.m_nStringBlockSize; //--------------------------------------------- //Create String Blok m_pStringBuf = new CHAR[theHead.m_nStringBlockSize]; if(!m_pStringBuf) return FALSE; //------------------------------------------------------ // Read Field Types vector< UINT > vFieldType; vFieldType.resize(theHead.m_nFieldsNum); memcpy(&(vFieldType[0]), pMem, sizeof(UINT)*theHead.m_nFieldsNum); pMem += sizeof(UINT)*theHead.m_nFieldsNum; //Check it! m_theType.resize(theHead.m_nFieldsNum); for(INT i=0; i<(INT)theHead.m_nFieldsNum; i++) { switch(vFieldType[i]) { case T_INT: case T_FLOAT: case T_STRING: m_theType[i] = (FIELD_TYPE)vFieldType[i]; break; default: SAFE_DELETE_ARRAY(m_pStringBuf); return FALSE; } } //------------------------------------------------------ // Read All Field m_vDataBuf.resize(theHead.m_nRecordsNum * theHead.m_nFieldsNum); memcpy(&(m_vDataBuf[0]), pMem, sizeof(FIELD)*theHead.m_nRecordsNum * theHead.m_nFieldsNum); pMem += sizeof(FIELD)*theHead.m_nRecordsNum * theHead.m_nFieldsNum; //------------------------------------------------------ // Read String Block memcpy( m_pStringBuf, pMem, m_nStringBufSize ); m_pStringBuf[ m_nStringBufSize - 1 ] = 0; //------------------------------------------------------ // TO runtime address for( INT j = 0; j < (INT)theHead.m_nFieldsNum; j++ ) { if( vFieldType[j] != T_STRING ) continue; string str; INT nFieldsNum; for( INT i = 0; i < (INT)theHead.m_nRecordsNum; i++ ) { nFieldsNum = GetFieldsNum(); m_vDataBuf[ i * nFieldsNum + j ].pString += reinterpret_cast<unsigned long long>( m_pStringBuf ); // str = m_vDataBuf[ i * nFieldsNum + j ].pString; // Decrypt( str.c_str(), const_cast< char* >( m_vDataBuf[ i * nFieldsNum + j ].pString ) ); } } //------------------------------------------------------ //生成索引列 CreateIndex(0, szFileName); return TRUE; }
void FileUtils::qsortfile(string m_fname1,char delimiter) { // TODO: Add your control notification handler code here char line[10240]; Ref* refseq=NULL; if(m_fname1 == "") { cout<<"please input a file for sort!"<<endl; return; } int ret; int total = CountLines(m_fname1); ret = OpenFileForRead(m_fname1); if(!ret) return; ret = OpenFileForWrite(m_fname1); if(!ret) return; ret = AllocBuffer(); if(!ret) return; ret = FirstRead(); if(!ret) { Release(); return; } refseq = new Ref[50000]; if(!refseq) { Release(); return; } cout<<"Begin to process:"<<endl; int k = 0; int count = 0; while(k < total) { ret = GetLine(line,10240); string buffer = line; count = GetFieldsNum(buffer,delimiter); for(int j=0;j<count;j++) { string field = GetField(buffer,j+1,delimiter); strcpy(refseq[k].field[j],field.c_str()); } cout<<"..."; k++; } qsort(refseq,k,sizeof(Ref),cmp); char line1[1024]; line1[0] = '\0'; //write the sorted resuts to file for(int j=0; j<k;j++) { strcpy(line1,""); for(int kk=0;kk<count-1;kk++) { strcat(line1,refseq[j].field[kk]); strcat(line1,"\t"); } strcat(line1,refseq[j].field[count-1]); WriteLine(line1); } LastWrite(); delete[] refseq; Release(); }