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
|
/** \file
* \brief System Dependent Binary File Access.
*
* See Copyright Notice in im_lib.h
* $Id: im_sysfile_win32.cpp,v 1.1 2008/10/17 06:10:16 scuri Exp $
*/
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <assert.h>
#include "im_util.h"
#include "im_binfile.h"
class imBinSystemFile: public imBinFileBase
{
protected:
HANDLE FileHandle;
int Error;
unsigned long ReadBuf(void* pValues, unsigned long pSize);
unsigned long WriteBuf(void* pValues, unsigned long pSize);
public:
virtual void Open(const char* pFileName);
virtual void New(const char* pFileName);
virtual void Close();
unsigned long FileSize();
int HasError() const;
void SeekTo(unsigned long pOffset);
void SeekOffset(long pOffset);
void SeekFrom(long pOffset);
unsigned long Tell() const;
int EndOfFile() const;
};
imBinFileBase* iBinSystemFileNewFunc()
{
return new imBinSystemFile();
}
void imBinSystemFile::Open(const char* pFileName)
{
this->FileHandle = CreateFile(pFileName, GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
this->Error = (this->FileHandle == INVALID_HANDLE_VALUE)? 1: 0;
SetLastError(NO_ERROR);
InitByteOrder(imBinCPUByteOrder());
this->IsNew = 0;
}
void imBinSystemFile::New(const char* pFileName)
{
this->FileHandle = CreateFile(pFileName, GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
this->Error = (this->FileHandle == INVALID_HANDLE_VALUE)? 1: 0;
SetLastError(NO_ERROR);
InitByteOrder(imBinCPUByteOrder());
this->IsNew = 1;
}
void imBinSystemFile::Close()
{
if (this->FileHandle != INVALID_HANDLE_VALUE)
CloseHandle(this->FileHandle);
this->FileHandle = INVALID_HANDLE_VALUE;
this->Error = 1;
}
unsigned long imBinSystemFile::FileSize()
{
assert(this->FileHandle != INVALID_HANDLE_VALUE);
this->Error = 0;
DWORD Size = GetFileSize(this->FileHandle, NULL);
if (Size == INVALID_FILE_SIZE)
this->Error = 1;
return Size;
}
unsigned long imBinSystemFile::ReadBuf(void* pValues, unsigned long pSize)
{
assert(this->FileHandle != INVALID_HANDLE_VALUE);
this->Error = 0;
DWORD dwSize = 0;
ReadFile(this->FileHandle, pValues, pSize, &dwSize, NULL);
if (dwSize != pSize)
this->Error = 1;
return dwSize;
}
unsigned long imBinSystemFile::WriteBuf(void* pValues, unsigned long pSize)
{
assert(this->FileHandle != INVALID_HANDLE_VALUE);
this->Error = 0;
DWORD dwSize = 0;
WriteFile(this->FileHandle, pValues, pSize, &dwSize, NULL);
if (dwSize != pSize)
this->Error = 1;
return dwSize;
}
int imBinSystemFile::HasError() const
{
return this->Error;
}
void imBinSystemFile::SeekTo(unsigned long pOffset)
{
assert(this->FileHandle != INVALID_HANDLE_VALUE);
this->Error = 0;
DWORD ret = SetFilePointer(this->FileHandle, pOffset, NULL, FILE_BEGIN);
if (ret == INVALID_SET_FILE_POINTER)
this->Error = 1;
}
void imBinSystemFile::SeekOffset(long pOffset)
{
assert(this->FileHandle != INVALID_HANDLE_VALUE);
this->Error = 0;
DWORD ret = SetFilePointer(this->FileHandle, pOffset, NULL, FILE_CURRENT);
if (ret == INVALID_SET_FILE_POINTER)
this->Error = 1;
}
void imBinSystemFile::SeekFrom(long pOffset)
{
assert(this->FileHandle != INVALID_HANDLE_VALUE);
this->Error = 0;
DWORD ret = SetFilePointer(this->FileHandle, pOffset, NULL, FILE_END);
if (ret == INVALID_SET_FILE_POINTER)
this->Error = 1;
}
unsigned long imBinSystemFile::Tell() const
{
assert(this->FileHandle != INVALID_HANDLE_VALUE);
return SetFilePointer(this->FileHandle, 0, NULL, FILE_CURRENT);
}
int imBinSystemFile::EndOfFile() const
{
assert(this->FileHandle != INVALID_HANDLE_VALUE);
DWORD cur_pos = SetFilePointer(this->FileHandle, 0, NULL, FILE_CURRENT);
DWORD end_pos = SetFilePointer(this->FileHandle, 0, NULL, FILE_END);
SetFilePointer(this->FileHandle, cur_pos, NULL, FILE_CURRENT);
return (cur_pos == end_pos)? 1: 0;
}
class imBinSystemFileHandle: public imBinSystemFile
{
public:
virtual void Open(const char* pFileName);
virtual void New(const char* pFileName);
virtual void Close();
};
imBinFileBase* iBinSystemFileHandleNewFunc()
{
return new imBinSystemFileHandle();
}
void imBinSystemFileHandle::Open(const char* pFileName)
{
// the file was successfully opened already the client
HANDLE file_handle = (HANDLE)pFileName;
this->FileHandle = file_handle;
InitByteOrder(imBinCPUByteOrder());
this->IsNew = 0;
this->Error = 0;
}
void imBinSystemFileHandle::New(const char* pFileName)
{
// the file was successfully opened already the client
HANDLE file_handle = (HANDLE)pFileName;
this->FileHandle = file_handle;
InitByteOrder(imBinCPUByteOrder());
this->IsNew = 1;
this->Error = 0;
}
void imBinSystemFileHandle::Close()
{
// does nothing, the client must close the file
}
|