[7146] Trailing whitespace code cleanup
[getmangos.git] / contrib / dbcEditer / thOpenSource.cpp
blob7df4debfe31e56c4d552d1dbb88cb2260538aee4
1 //---------------------------------------------------------------------------
3 #include <vcl.h>
4 #pragma hdrstop
6 #include "thOpenSource.h"
7 #include "dbcedit.h"
8 #include "stdio.h"
9 #include <dir.h>
10 #include <inifiles.hpp>
11 #include <process.h>
12 #pragma package(smart_init)
13 //---------------------------------------------------------------------------
15 // Important: Methods and properties of objects in VCL can only be
16 // used in a method called using Synchronize, for example:
18 // Synchronize(UpdateCaption);
20 // where UpdateCaption could look like:
22 // void __fastcall thOpenFile::UpdateCaption()
23 // {
24 // Form1->Caption = "Updated in a thread";
25 // }
26 //---------------------------------------------------------------------------
28 __fastcall thOpenFile::thOpenFile(bool CreateSuspended)
29 : TThread(CreateSuspended)
33 //---------------------------------------------------------------------------
34 void __fastcall thOpenFile::Execute()
36 //---- Place thread code here ----
37 //if(!Terminated){
38 // FrmMain->LoadAndModify(FrmMain->OpenDialog1->FileName.c_str());
39 // FrmMain->OpenOk=true;
40 //}
41 thEnd=false;
42 RunOpen();
43 FrmMain->OpenOk=true;
44 thEnd=true;
47 //---------------------------------------------------------------------------
48 void __fastcall thOpenFile::RunOpen()
50 LoadAndModify(FrmMain->OpenDialog1->FileName.c_str());
51 //OpenOk=true;
54 void thOpenFile::ReadAndModifyFromBuff(char *pBuff, DWORD dwSize, const char* pszFileName)
56 char szErrorMsg[MAX_PATH];
57 char szNewFileName[MAX_PATH];
58 DWORD w;
59 TIniFile *ini;
62 TypePtr p(pBuff);
63 if('WDBC' != TAG(*p.dw))
65 _snprintf(szErrorMsg, 512, "[%s]Not Wow's dbc file!", pszFileName);
66 ShowMessage(szErrorMsg);
67 return;
69 p.dw++;
71 DWORD dwRows, dwCols, dwRowLen, dwTextLen;
72 dwRows = *p.dw++;
73 dwCols = *p.dw++;
74 dwRowLen = *p.dw++;
75 dwTextLen = *p.dw++;
77 FrmMain->sgEdit->RowCount = dwRows+1;
78 FrmMain->sgEdit->ColCount = dwCols+1;
80 for(int i=0; i<FrmMain->sgEdit->RowCount; i++){
81 FrmMain->sgEdit->Cells[0][i]=IntToStr(i);
82 if(Terminated) return;
84 //É趨ÁбêÌâ
85 AnsiString iniSetFile=ExtractFilePath(Application->ExeName)+"BcdEditer.ini";
86 AnsiString SectionName=ExtractFileName(FrmMain->CurrentOpenFile);
88 ini = new TIniFile( iniSetFile );
89 for(int j=0; j<FrmMain->sgEdit->ColCount; j++){
90 FrmMain->sgEdit->Cells[j][0]= ini->ReadString(SectionName,"ColTitle"+IntToStr(j),IntToStr(j));
91 //sgEdit->Cells[j][0]=IntToStr(j);
92 ColType[j]=ini->ReadInteger(SectionName,"ColType"+IntToStr(j),0);
93 if(Terminated) return;
95 delete ini;
97 //int *ColType = new int[dwCols];
99 DWORD dwTextStartPos = dwRows*dwRowLen+20;
100 char* pTextPtr = pBuff + dwTextStartPos;
101 char pszTemp[MAX_PATH];
102 float fTemp;
103 long lTemp;
104 DWORD i, j;
105 BOOL* pbString = new BOOL[dwRows*dwCols];
106 float newTmp;
107 //int ColType;
109 ini = new TIniFile( iniSetFile );
111 for(i=0; i<dwRows; i++)
113 for(j=0; j<dwCols; j++)
115 //SleepEx(0,0);
116 if(Terminated) return;
117 lTemp = *p.l;
118 newTmp = *p.f;
119 memcpy(&fTemp, &newTmp, 4);
121 if(j==0) //ID
122 FrmMain->sgEdit->Cells[j+1][i+1]=IntToStr(lTemp);
123 else{
125 //ColType= ini->ReadInteger(SectionName,"ColType"+IntToStr(j),0);
127 switch (ColType[j+1])
129 case 0: //ÕûÐÍ
130 FrmMain->sgEdit->Cells[j+1][i+1]=IntToStr(lTemp);
131 break;
132 case 1: //¸¡µã
133 FrmMain->sgEdit->Cells[j+1][i+1]=FloatToStr(fTemp);
134 break;
135 case 2: //Îı¾ Îı¾ÀàÐÍÖ»ÄÜ¿´£¬²»Äܱ༭
136 if(dwTextStartPos + lTemp < dwSize){
137 pTextPtr = pBuff + dwTextStartPos + lTemp;
138 FrmMain->sgEdit->Cells[j+1][i+1]=pTextPtr;
139 }else{
140 FrmMain->sgEdit->Cells[j+1][i+1]="¸ÃÁв»ÊÇÎı¾";
142 break;
143 default: //ÕûÐÍ
144 FrmMain->sgEdit->Cells[j+1][i+1]=IntToStr(lTemp);
147 p.c += 4;
151 delete [] pbString;
152 //delete [] ColType;
153 delete ini;
157 void thOpenFile::LoadAndModify(const char * pszFileName)
159 HANDLE hFile = NULL;
160 hFile = CreateFile(pszFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
161 if(hFile == INVALID_HANDLE_VALUE)return;
163 DWORD r = 0, nFileSize = 0;
164 nFileSize = GetFileSize(hFile, NULL);
165 char* pTmpBuf = new char[nFileSize];
166 if(pTmpBuf==NULL)
168 CloseHandle(hFile);
169 return;
171 ReadFile(hFile, pTmpBuf, nFileSize, &r, NULL);
173 FrmMain->CurrentOpenFile=pszFileName;
174 FrmMain->btSave->Enabled=true;
176 ReadAndModifyFromBuff(pTmpBuf, nFileSize, pszFileName);
178 //SAFE_DELETE_ARRAY(pTmpBuf);
179 delete [] pTmpBuf;
180 CloseHandle(hFile);