root/trunk/contrib/dbcEditer/thOpenSource.cpp @ 2

Revision 2, 5.3 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1//---------------------------------------------------------------------------
2
3#include <vcl.h>
4#pragma hdrstop
5
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//---------------------------------------------------------------------------
14
15//   Important: Methods and properties of objects in VCL can only be
16//   used in a method called using Synchronize, for example:
17//
18//      Synchronize(UpdateCaption);
19//
20//   where UpdateCaption could look like:
21//
22//      void __fastcall thOpenFile::UpdateCaption()
23//      {
24//        Form1->Caption = "Updated in a thread";
25//      }
26//---------------------------------------------------------------------------
27
28__fastcall thOpenFile::thOpenFile(bool CreateSuspended)
29        : TThread(CreateSuspended)
30{
31     
32}
33//---------------------------------------------------------------------------
34void __fastcall thOpenFile::Execute()
35{
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;
45
46}
47//---------------------------------------------------------------------------
48void __fastcall thOpenFile::RunOpen()
49{
50        LoadAndModify(FrmMain->OpenDialog1->FileName.c_str());
51        //OpenOk=true;
52}
53
54void thOpenFile::ReadAndModifyFromBuff(char *pBuff, DWORD dwSize, const char* pszFileName)
55{
56        char szErrorMsg[MAX_PATH];
57        char szNewFileName[MAX_PATH];
58        DWORD w;
59        TIniFile *ini;
60       
61
62        TypePtr p(pBuff);
63        if('WDBC' != TAG(*p.dw))
64        {
65                _snprintf(szErrorMsg, 512, "[%s]Not Wow's dbc file!", pszFileName);
66                ShowMessage(szErrorMsg);
67                return;
68        }
69        p.dw++;
70
71        DWORD dwRows, dwCols, dwRowLen, dwTextLen;
72        dwRows = *p.dw++;
73        dwCols = *p.dw++;
74        dwRowLen = *p.dw++;
75        dwTextLen = *p.dw++;
76
77        FrmMain->sgEdit->RowCount = dwRows+1;
78        FrmMain->sgEdit->ColCount = dwCols+1;
79
80        for(int i=0; i<FrmMain->sgEdit->RowCount; i++){
81            FrmMain->sgEdit->Cells[0][i]=IntToStr(i);
82            if(Terminated) return;
83        }
84        //Éè¶šÁбêÌâ
85        AnsiString  iniSetFile=ExtractFilePath(Application->ExeName)+"BcdEditer.ini";
86        AnsiString SectionName=ExtractFileName(FrmMain->CurrentOpenFile);
87
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;
94        }
95        delete ini;
96
97        //int   *ColType = new int[dwCols];
98       
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;
108
109        ini = new TIniFile( iniSetFile );
110
111        for(i=0; i<dwRows; i++)
112        {
113                for(j=0; j<dwCols; j++)
114                {
115                    //SleepEx(0,0);
116                    if(Terminated) return;
117                    lTemp = *p.l;
118                    newTmp = *p.f;
119                    memcpy(&fTemp, &newTmp, 4);
120
121                    if(j==0)    //ID
122                        FrmMain->sgEdit->Cells[j+1][i+1]=IntToStr(lTemp);
123                    else{
124
125                        //ColType= ini->ReadInteger(SectionName,"ColType"+IntToStr(j),0);
126
127                        switch (ColType[j+1])
128                        {
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]="žÃÁв»ÊÇÎıŸ";
141                             }
142                          break;
143                          default: //ÕûÐÍ
144                             FrmMain->sgEdit->Cells[j+1][i+1]=IntToStr(lTemp);
145                        }
146                    }
147                    p.c += 4;
148                }
149        }
150
151        delete [] pbString;
152        //delete []  ColType;
153        delete ini;
154
155}
156
157void thOpenFile::LoadAndModify(const char * pszFileName)
158{
159        HANDLE hFile = NULL;
160        hFile = CreateFile(pszFileName, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
161        if(hFile == INVALID_HANDLE_VALUE)return;
162
163        DWORD r = 0, nFileSize = 0;
164        nFileSize = GetFileSize(hFile, NULL);
165        char* pTmpBuf = new char[nFileSize];
166        if(pTmpBuf==NULL)
167        {
168                CloseHandle(hFile);
169                return;
170        }
171        ReadFile(hFile, pTmpBuf, nFileSize, &r, NULL);
172
173        FrmMain->CurrentOpenFile=pszFileName;
174        FrmMain->btSave->Enabled=true;
175
176        ReadAndModifyFromBuff(pTmpBuf, nFileSize, pszFileName);
177
178        //SAFE_DELETE_ARRAY(pTmpBuf);
179        delete [] pTmpBuf;
180        CloseHandle(hFile);
181
182}
Note: See TracBrowser for help on using the browser.