Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
244 chris 1
#ifndef __PAKINTERFACE_H__
2
#define __PAKINTERFACE_H__
3
 
4
#include <map>
5
#include <list>
6
#include <string>
7
 
8
#define WIN32_LEAN_AND_MEAN
9
#include <windows.h>
10
 
11
class PakCollection;
12
 
13
class PakRecord
14
{
15
public:
16
        PakCollection*                  mCollection;
17
        std::string                             mFileName;
18
        FILETIME                                mFileTime;
19
        int                                             mStartPos;
20
        int                                             mSize; 
21
};
22
 
23
typedef std::map<std::string, PakRecord> PakRecordMap;
24
 
25
class PakCollection
26
{
27
public:
28
        HANDLE                                  mFileHandle;
29
        HANDLE                                  mMappingHandle;
30
        void*                                   mDataPtr;
31
};
32
 
33
typedef std::list<PakCollection> PakCollectionList;
34
 
35
struct PFILE
36
{
37
        PakRecord*                              mRecord;
38
        int                                             mPos;
39
        FILE*                                   mFP;
40
};
41
 
42
struct PFindData
43
{
44
        HANDLE                                  mWHandle;
45
        std::string                             mLastFind;
46
        std::string                             mFindCriteria;
47
};
48
 
49
class PakInterfaceBase
50
{
51
public:
52
        virtual PFILE*                  FOpen(const char* theFileName, const char* theAccess) = 0;
53
        virtual PFILE*                  FOpen(const wchar_t* theFileName, const wchar_t* theAccess) { return NULL; }
54
        virtual int                             FClose(PFILE* theFile) = 0;
55
        virtual int                             FSeek(PFILE* theFile, long theOffset, int theOrigin) = 0;
56
        virtual int                             FTell(PFILE* theFile) = 0;
57
        virtual size_t                  FRead(void* thePtr, int theElemSize, int theCount, PFILE* theFile) = 0;
58
        virtual int                             FGetC(PFILE* theFile) = 0;
59
        virtual int                             UnGetC(int theChar, PFILE* theFile) = 0;
60
        virtual char*                   FGetS(char* thePtr, int theSize, PFILE* theFile) = 0;
61
        virtual wchar_t*                FGetS(wchar_t* thePtr, int theSize, PFILE* theFile) { return thePtr; }
62
        virtual int                             FEof(PFILE* theFile) = 0;
63
 
64
        virtual HANDLE                  FindFirstFile(LPCTSTR lpFileName, LPWIN32_FIND_DATA lpFindFileData) = 0;       
65
        virtual BOOL                    FindNextFile(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData) = 0;
66
        virtual BOOL                    FindClose(HANDLE hFindFile) = 0;
67
};
68
 
69
class PakInterface : public PakInterfaceBase
70
{
71
public:
72
        PakCollectionList               mPakCollectionList;    
73
        PakRecordMap                    mPakRecordMap;
74
 
75
public:
76
        bool                                    PFindNext(PFindData* theFindData, LPWIN32_FIND_DATA lpFindFileData);
77
 
78
public:
79
        PakInterface();
80
        ~PakInterface();
81
 
82
        bool                                    AddPakFile(const std::string& theFileName);
83
        PFILE*                                  FOpen(const char* theFileName, const char* theAccess);
84
        int                                             FClose(PFILE* theFile);
85
        int                                             FSeek(PFILE* theFile, long theOffset, int theOrigin);
86
        int                                             FTell(PFILE* theFile);
87
        size_t                                  FRead(void* thePtr, int theElemSize, int theCount, PFILE* theFile);
88
        int                                             FGetC(PFILE* theFile);
89
        int                                             UnGetC(int theChar, PFILE* theFile);
90
        char*                                   FGetS(char* thePtr, int theSize, PFILE* theFile);
91
        int                                             FEof(PFILE* theFile);
92
 
93
        HANDLE                                  FindFirstFile(LPCTSTR lpFileName, LPWIN32_FIND_DATA lpFindFileData);
94
        BOOL                                    FindNextFile(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData);
95
        BOOL                                    FindClose(HANDLE hFindFile);
96
};
97
 
98
extern PakInterface* gPakInterface;
99
 
100
static HANDLE gPakFileMapping = NULL;
101
static PakInterfaceBase** gPakInterfaceP = NULL;
102
 
103
static PakInterfaceBase* GetPakPtr()
104
{
105
        if (gPakFileMapping == NULL)
106
        {
107
                char aName[256];
108
                sprintf(aName, "gPakInterfaceP_%d", GetCurrentProcessId());
109
                gPakFileMapping = ::CreateFileMappingA((HANDLE)INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(PakInterface*), aName);
110
                gPakInterfaceP = (PakInterfaceBase**) MapViewOfFile(gPakFileMapping, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(PakInterface*));        
111
        }
112
        return *gPakInterfaceP;
113
}
114
 
115
static PFILE* p_fopen(const char* theFileName, const char* theAccess)
116
{
117
        if (GetPakPtr() != NULL)
118
                return (*gPakInterfaceP)->FOpen(theFileName, theAccess);       
119
        FILE* aFP = fopen(theFileName, theAccess);
120
        if (aFP == NULL)
121
                return NULL;
122
        PFILE* aPFile = new PFILE();
123
        aPFile->mRecord = NULL;
124
        aPFile->mPos = 0;
125
        aPFile->mFP = aFP;
126
        return aPFile;
127
}
128
 
129
static PFILE* p_fopen(const wchar_t* theFileName, const wchar_t* theAccess)
130
{
131
        if (GetPakPtr() != NULL)
132
                return (*gPakInterfaceP)->FOpen(theFileName, theAccess);       
133
        FILE* aFP = _wfopen(theFileName, theAccess);
134
        if (aFP == NULL)
135
                return NULL;
136
        PFILE* aPFile = new PFILE();
137
        aPFile->mRecord = NULL;
138
        aPFile->mPos = 0;
139
        aPFile->mFP = aFP;
140
        return aPFile;
141
}
142
 
143
static int p_fclose(PFILE* theFile)
144
{
145
        if (GetPakPtr() != NULL)
146
                return (*gPakInterfaceP)->FClose(theFile);
147
        int aResult = fclose(theFile->mFP);
148
        delete theFile;
149
        return aResult;
150
}
151
 
152
static int p_fseek(PFILE* theFile, long theOffset, int theOrigin)
153
{
154
        if (GetPakPtr() != NULL)
155
                return (*gPakInterfaceP)->FSeek(theFile, theOffset, theOrigin);
156
        return fseek(theFile->mFP, theOffset, theOrigin);
157
}
158
 
159
static int p_ftell(PFILE* theFile)
160
{
161
        if (GetPakPtr() != NULL)
162
                return (*gPakInterfaceP)->FTell(theFile);
163
        return ftell(theFile->mFP);
164
}
165
 
166
static size_t p_fread(void* thePtr, int theSize, int theCount, PFILE* theFile)
167
{
168
        if (GetPakPtr() != NULL)
169
                return (*gPakInterfaceP)->FRead(thePtr, theSize, theCount, theFile);
170
        return fread(thePtr, theSize, theCount, theFile->mFP);
171
}
172
 
173
static size_t p_fwrite(const void* thePtr, int theSize, int theCount, PFILE* theFile)
174
{      
175
        if (theFile->mFP == NULL)
176
                return 0;
177
        return fwrite(thePtr, theSize, theCount, theFile->mFP);
178
}
179
 
180
static int p_fgetc(PFILE* theFile)
181
{
182
        if (GetPakPtr() != NULL)
183
                return (*gPakInterfaceP)->FGetC(theFile);
184
        return fgetc(theFile->mFP);
185
}
186
 
187
static int p_ungetc(int theChar, PFILE* theFile)
188
{
189
        if (GetPakPtr() != NULL)
190
                return (*gPakInterfaceP)->UnGetC(theChar, theFile);
191
        return ungetc(theChar, theFile->mFP);
192
}
193
 
194
static char* p_fgets(char* thePtr, int theSize, PFILE* theFile)
195
{
196
        if (GetPakPtr() != NULL)
197
                return (*gPakInterfaceP)->FGetS(thePtr, theSize, theFile);
198
        return fgets(thePtr, theSize, theFile->mFP);
199
}
200
 
201
static wchar_t* p_fgets(wchar_t* thePtr, int theSize, PFILE* theFile)
202
{
203
        if (GetPakPtr() != NULL)
204
                return (*gPakInterfaceP)->FGetS(thePtr, theSize, theFile);
205
        return fgetws(thePtr, theSize, theFile->mFP);
206
}
207
 
208
static int p_feof(PFILE* theFile)
209
{
210
        if (GetPakPtr() != NULL)
211
                return (*gPakInterfaceP)->FEof(theFile);
212
        return feof(theFile->mFP);
213
}
214
 
215
static HANDLE p_FindFirstFile(LPCTSTR lpFileName, LPWIN32_FIND_DATA lpFindFileData)
216
{
217
        if (GetPakPtr() != NULL)
218
                return (*gPakInterfaceP)->FindFirstFile(lpFileName, lpFindFileData);
219
        return FindFirstFile(lpFileName, lpFindFileData);
220
}
221
 
222
static BOOL p_FindNextFile(HANDLE hFindFile, LPWIN32_FIND_DATA lpFindFileData)
223
{
224
        if (GetPakPtr() != NULL)
225
                return (*gPakInterfaceP)->FindNextFile(hFindFile, lpFindFileData);
226
        return FindNextFile(hFindFile, lpFindFileData);
227
}
228
 
229
static BOOL p_FindClose(HANDLE hFindFile)
230
{
231
        if (GetPakPtr() != NULL)
232
                return (*gPakInterfaceP)->FindClose(hFindFile);
233
        return FindClose(hFindFile);
234
}
235
 
236
 
237
#endif //__PAKINTERFACE_H__