forked from xNVSE/NVSE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIFileStream.cpp
More file actions
232 lines (187 loc) · 4.35 KB
/
Copy pathIFileStream.cpp
File metadata and controls
232 lines (187 loc) · 4.35 KB
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
#include "IFileStream.h"
#include "IDebugLog.h"
#include "IErrors.h"
#include <direct.h>
IFileStream::IFileStream()
:theFile(NULL)
{
}
IFileStream::IFileStream(const char * name)
:theFile(NULL)
{
Open(name);
}
IFileStream::~IFileStream()
{
Close();
}
/**
* Opens a file for reading and attaches it to the stream
*/
bool IFileStream::Open(const char * name)
{
Close();
theFile = CreateFile(name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(theFile != INVALID_HANDLE_VALUE)
{
LARGE_INTEGER temp;
GetFileSizeEx(theFile, &temp);
streamLength = temp.QuadPart;
streamOffset = 0;
}
return theFile != INVALID_HANDLE_VALUE;
}
static UINT_PTR CALLBACK BrowseEventProc(HWND window, UINT msg, WPARAM wParam, LPARAM lParam)
{
return 0;
}
bool IFileStream::BrowseOpen(void)
{
bool result = false;
OPENFILENAME info;
char path[4096];
path[0] = 0;
info.lStructSize = sizeof(info);
info.hwndOwner = NULL;
info.hInstance = NULL;
info.lpstrFilter = NULL;
info.lpstrCustomFilter = NULL;
info.nMaxCustFilter = 0;
info.nFilterIndex = 0;
info.lpstrFile = path;
info.nMaxFile = sizeof(path);
info.lpstrFileTitle = NULL;
info.nMaxFileTitle = 0;
info.lpstrInitialDir = NULL;
info.lpstrTitle = NULL;
info.Flags = OFN_EXPLORER | OFN_ENABLESIZING | OFN_FILEMUSTEXIST | OFN_ENABLEHOOK | OFN_NOCHANGEDIR;
info.lpstrDefExt = NULL;
info.lCustData = NULL;
info.lpfnHook = BrowseEventProc;
info.lpTemplateName = NULL;
// info.pvReserved = NULL;
// info.dwReserved = NULL;
// info.FlagsEx = 0;
if(GetOpenFileName(&info))
{
result = Open(path);
}
return result;
}
/**
* Creates a new file for writing, overwriting any previously-existing files,
* and attaches it to the stream
*/
bool IFileStream::Create(const char * name)
{
Close();
theFile = CreateFile(name, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(theFile != INVALID_HANDLE_VALUE)
{
streamLength = 0;
streamOffset = 0;
}
return theFile != INVALID_HANDLE_VALUE;
}
bool IFileStream::BrowseCreate(const char * defaultName, const char * defaultPath, const char * title)
{
bool result = false;
OPENFILENAME info;
char path[4096];
if(defaultName)
strcpy_s(path, sizeof(path), defaultName);
info.lStructSize = sizeof(info);
info.hwndOwner = NULL;
info.hInstance = NULL;
info.lpstrFilter = NULL;
info.lpstrCustomFilter = NULL;
info.nMaxCustFilter = 0;
info.nFilterIndex = 0;
info.lpstrFile = path;
info.nMaxFile = sizeof(path);
info.lpstrFileTitle = NULL;
info.nMaxFileTitle = 0;
info.lpstrInitialDir = defaultPath;
info.lpstrTitle = title;
info.Flags = OFN_EXPLORER | OFN_ENABLESIZING | OFN_ENABLEHOOK |
OFN_NOCHANGEDIR | OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
info.lpstrDefExt = NULL;
info.lCustData = NULL;
info.lpfnHook = BrowseEventProc;
info.lpTemplateName = NULL;
// info.pvReserved = NULL;
// info.dwReserved = NULL;
// info.FlagsEx = 0;
if(GetSaveFileName(&info))
{
result = Create(path);
}
return result;
}
/**
* Closes the current file
*/
void IFileStream::Close(void)
{
if(theFile)
{
CloseHandle(theFile);
theFile = NULL;
}
}
void IFileStream::ReadBuf(void * buf, UInt32 inLength)
{
UInt32 bytesRead;
ReadFile(theFile, buf, inLength, &bytesRead, NULL);
streamOffset += bytesRead;
}
void IFileStream::WriteBuf(const void * buf, UInt32 inLength)
{
UInt32 bytesWritten;
// check for file expansion
if(streamOffset > streamLength)
SetEndOfFile(theFile);
WriteFile(theFile, buf, inLength, &bytesWritten, NULL);
streamOffset += bytesWritten;
if(streamLength < streamOffset)
streamLength = streamOffset;
}
void IFileStream::SetOffset(SInt64 inOffset)
{
LARGE_INTEGER temp;
temp.QuadPart = inOffset;
SetFilePointerEx(theFile, temp, NULL, FILE_BEGIN);
streamOffset = inOffset;
}
// ### TODO: get rid of buf
void IFileStream::MakeAllDirs(const char * path)
{
char buf[1024];
char * traverse = buf;
while(1)
{
char data = *path++;
if(!data)
break;
if((data == '\\') || (data == '/'))
{
*traverse = 0;
_mkdir(buf);
}
*traverse++ = data;
}
}
char * IFileStream::ExtractFileName(char * path)
{
char * traverse = path;
char * lastSlash = NULL;
while(1)
{
char data = *traverse++;
if((data == '\\') || (data == '/'))
lastSlash = traverse;
if(!data)
break;
}
return lastSlash;
}