1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
| #ifndef STORAGE_H
#define STORAGE_H
#include <windows.h>
#pragma once
class CStorage
{
public:
HRESULT hRes;
WCHAR * lpszW;
IStorage * pStorage;
void * lpBuf;
FILE * pFile;
HWND m_hWnd;
WIN32_FIND_DATA * pDATA;
int iINDENT;
char szTMP[MAX_PATH];
DWORD SectorsPerCluster;
DWORD BytesPerSector;
DWORD NumberOfFreeClusters;
DWORD TotalNumberOfClusters;
ULONG nStgElm;
ULONG uStgLen;
CStorage();
~CStorage();
inline GetHResult(){return hRes;}
LPWSTR _L(LPCTSTR str);
HRESULT MkIStorage(LPCTSTR szStgName);
HRESULT StgAddPath(LPCTSTR szName, IStorage * pStorage, IStorage ** lpStorage);
HRESULT StgAddFile(LPCTSTR szName, IStorage * pStorage, IStream ** lpStream);
HRESULT PutDataInStream(IStream * pStream, FILE * pFile);
void PumpMessages(HWND hWND);
WIN32_FIND_DATA * MkPathList(LPCTSTR szPath);
};
typedef CStorage * pSTG;
CStorage::CStorage()
{
CoInitialize(NULL);
GetTempPath(MAX_PATH,szTMP);
GetShortPathName(szTMP,szTMP,MAX_PATH);
if(GetDiskFreeSpace
(
szTMP,
&SectorsPerCluster,
&BytesPerSector,
&NumberOfFreeClusters,
&TotalNumberOfClusters
)
)
BytesPerSector = BytesPerSector < 1 ? 1024: BytesPerSector;
lpszW = (WCHAR *)malloc(sizeof(WCHAR));
lpBuf = malloc(sizeof(char));
pDATA = (WIN32_FIND_DATA *)malloc(sizeof(WIN32_FIND_DATA));
pFile = NULL;
m_hWnd= NULL;
nStgElm= 0;
uStgLen= 0;
pDATA[nStgElm].dwReserved1 = 0;
iINDENT = 0;
}
CStorage::~CStorage()
{
pStorage->Release();
CoUninitialize();
lpszW = (WCHAR *)realloc((void *)lpszW,sizeof(WCHAR));
lpBuf = realloc((void *)lpBuf,sizeof(char));
pDATA = (WIN32_FIND_DATA *)realloc((void *)pDATA,sizeof(WIN32_FIND_DATA));
}
LPWSTR CStorage::_L(LPCTSTR str)
{
long uLen = MultiByteToWideChar(CP_ACP, 0, str, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, str, uLen, lpszW =
(WCHAR *)realloc((void *)lpszW,sizeof(WCHAR)*uLen),
uLen);
return lpszW;
}
HRESULT CStorage::MkIStorage(LPCTSTR szStgName)
{
char szPath[MAX_PATH];
sprintf(szPath,"%s%s.stg",szTMP,szStgName);
hRes = StgCreateStorageEx(
_L(szPath),
STGM_CREATE |
STGM_READWRITE |
STGM_SHARE_EXCLUSIVE,
STGFMT_STORAGE,
0, NULL, NULL,
IID_IStorage,
(void **)&pStorage);
return hRes;
}
HRESULT CStorage::StgAddPath(LPCTSTR szName, IStorage * pStorage, IStorage ** lpStorage)
{
hRes = pStorage->CreateStorage
(
_L(szName),
STGM_CREATE |
STGM_DIRECT |
STGM_READWRITE |
STGM_SHARE_EXCLUSIVE,
0, 0, lpStorage
);
return hRes;
}
HRESULT CStorage::StgAddFile(LPCTSTR szName, IStorage * pStorage, IStream ** lpStream)
{
hRes = pStorage->CreateStream
(
_L(szName),
STGM_CREATE |
STGM_DIRECT |
STGM_READWRITE |
STGM_SHARE_EXCLUSIVE/* |
STGM_DELETEONRELEASE*/,
0, 0, lpStream
);
return hRes;
}
HRESULT CStorage::PutDataInStream(IStream * pStream, FILE * pFile)
{
ULONG nBytes = 0;
ULONG uBufLen= uBufLen = SectorsPerCluster*BytesPerSector;
if(uBufLen < 1)
uBufLen = 4096;
if(pFile != 0)
{
lpBuf = realloc(lpBuf,uBufLen*sizeof(char));
while(!feof(pFile))
{
if(0 < (nBytes = fread(lpBuf,1,uBufLen,pFile)))
{
if(nBytes < uBufLen)
{
uBufLen = nBytes;
lpBuf = realloc(lpBuf,uBufLen*sizeof(char));
}
hRes = pStream->Write(lpBuf,uBufLen,&nBytes);
}
if(nBytes < 0)
break;
}
fclose(pFile);
}
if(pStream)
hRes = pStream->Release();
return hRes;
}
void CStorage::PumpMessages(HWND hWND)
{
MSG msg;
// Handle dialog messages
while(PeekMessage(&msg, hWND, 0, 0, PM_REMOVE))
if(!IsDialogMessage(hWND, &msg) && TranslateMessage(&msg))
DispatchMessage(&msg);
}
WIN32_FIND_DATA * CStorage::MkPathList(LPCTSTR szPath)
{
WIN32_FIND_DATA pFILE_DATA;ULONG uIND = nStgElm;
pDATA[nStgElm].dwFileAttributes = GetFileAttributes(szPath);
pDATA[nStgElm].dwReserved1 = iINDENT;
HANDLE hFile = CreateFile(
strcpy(pDATA[nStgElm].cFileName,szPath),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if(hFile != INVALID_HANDLE_VALUE)
{
uStgLen += (pDATA[nStgElm].nFileSizeLow = GetFileSize(hFile,NULL));
CloseHandle(hFile);
pDATA = (WIN32_FIND_DATA *)realloc
(
(void *)pDATA,
sizeof(WIN32_FIND_DATA)*(1 + (nStgElm = nStgElm + 1))
);
}
else
{
pFILE_DATA.cFileName[0] = '\0';
if(strcmp(pDATA[nStgElm].cFileName,"") != 0)
sprintf(pFILE_DATA.cFileName,"%s\\*.*",pDATA[nStgElm].cFileName);
hFile = FindFirstFile(pFILE_DATA.cFileName,&pFILE_DATA);
if(hFile != INVALID_HANDLE_VALUE)
{
iINDENT++;
pDATA[(uIND = nStgElm)].nFileSizeLow = uStgLen;
pDATA = (WIN32_FIND_DATA *)realloc
(
(void *)pDATA,
sizeof(WIN32_FIND_DATA)*(1 + (nStgElm = nStgElm + 1))
);
do
{
if(strcmp(pFILE_DATA.cFileName,".") == 0 ||
strcmp(pFILE_DATA.cFileName,"..") == 0)
continue;
else
{
//GetShortPathName(pDATA[uIND].cFileName,pDATA[nStgElm].cFileName,MAX_PATH);
sprintf
(
pDATA[nStgElm].cFileName,
"%s\\%s",
pDATA[uIND].cFileName,
pFILE_DATA.cFileName
);
SetWindowText(m_hWnd,pFILE_DATA.cFileName);
pDATA[nStgElm].dwReserved1 = iINDENT;
pDATA[nStgElm].nFileSizeLow = pFILE_DATA.nFileSizeLow;
pDATA[nStgElm].dwFileAttributes = pFILE_DATA.dwFileAttributes;
if(FILE_ATTRIBUTE_DIRECTORY & pFILE_DATA.dwFileAttributes)
MkPathList(pDATA[nStgElm].cFileName);
else
{
uStgLen += pFILE_DATA.nFileSizeLow;
pDATA = (WIN32_FIND_DATA *)realloc
(
(void *)pDATA,
sizeof(WIN32_FIND_DATA)*(1 + (nStgElm = nStgElm + 1))
);
}
PumpMessages(m_hWnd);
}
}
while(FindNextFile(hFile,&pFILE_DATA) != 0);
FindClose(hFile);
pDATA[uIND].nFileSizeLow = uStgLen - pDATA[uIND].nFileSizeLow;
iINDENT--;
}
}
return pDATA;
}
#endif |