-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathFileIO.h
executable file
·249 lines (217 loc) · 4.79 KB
/
FileIO.h
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
#ifndef _FILEIO_H_
#define _FILEIO_H_
//////FILEIO WRAPPER///////
#include <string.h>
#include <stdio.h>
//char* ScanString(char* line, char* string);
class CFileIO
{
private:
FILE* fPTR;//file pointer
char m_szFileName[256];//remember the file for re-opening
char m_szReadMode[12];//read/write mode
long m_lFileOFFSET;//stored file offset from last read
char m_szLine[256];//last read line
bool FileOpen()
{
if(m_szFileName==NULL)
return false;
if(!(fPTR=fopen(m_szFileName,m_szReadMode)))
return false;
return true;
}
bool AppendFile()
{
if(m_szFileName==NULL)
return false;
if(!(fPTR=fopen(m_szFileName,"a")))
return false;
return true;
}
bool AppendFileBinary()
{
if(m_szFileName==NULL)
return false;
if(!(fPTR=fopen(m_szFileName,"ab")))
return false;
return true;
}
public:
CFileIO()
{
fPTR = NULL;
}
~CFileIO()
{}
void ShutFile()
{
//get new file OFFSET
fseek(fPTR,0,SEEK_CUR);
m_lFileOFFSET = ftell(fPTR);
//close file and return line
fclose(fPTR);
}
//open a file for reading or writing
bool GetFile(char* filename,char* ReadMode )
{
strcpy(m_szFileName,filename);
strcpy(m_szReadMode,ReadMode);
if(!FileOpen())
return false;
fseek(fPTR,0,SEEK_SET);
m_lFileOFFSET=ftell(fPTR);
fclose(fPTR);
return true;
}
char* ReadLine()
{
if(!FileOpen())
return NULL;//cant open file
//make sure its not the EOF
fseek(fPTR,0,SEEK_END);
if(m_lFileOFFSET==ftell(fPTR))
return NULL;//end of file
//move to file offset and get line
fseek(fPTR,m_lFileOFFSET,SEEK_SET);//move to file offset
fgets(m_szLine,256,fPTR);//read line from file
//get new file OFFSET
fseek(fPTR,0,SEEK_CUR);
m_lFileOFFSET = ftell(fPTR);
//close file and return line
fclose(fPTR);
return m_szLine;
}
char* FindStringInFile(char* string)//returns remaining line
{
if(!FileOpen())
return NULL;//cant open file
fseek(fPTR,0,SEEK_END);
long end = ftell(fPTR);
m_lFileOFFSET=0;
fseek(fPTR,m_lFileOFFSET,SEEK_SET);//beginning of file
char* line;
while(m_lFileOFFSET!=end)
{
line = ReadLine();
if(!line)
continue;
char* tok = strtok(line," ");
if(!strcmp(tok,string))
{
m_lFileOFFSET=0;
tok = strtok(NULL,"\n");
return tok;
}
}
m_lFileOFFSET=0;
fclose(fPTR);
return NULL;
}
bool WriteLine(char* txt)
{
if(!AppendFile())
return false;//cant open file
fwrite(txt,sizeof(char),strlen(txt),fPTR);//write the line
//get new file OFFSET
fseek(fPTR,0,SEEK_CUR);
m_lFileOFFSET = ftell(fPTR);
//close file and return line
fclose(fPTR);
return true;
}
#ifdef AMIGAOS4
// generic little->big conversion
template <class T> inline void littleBigEndian (T *x) {
unsigned char *toConvert = reinterpret_cast<unsigned char *>(x);
unsigned char tmp;
const int sz = sizeof(T);
for (size_t i = 0; i < sz/2; ++i) {
tmp = toConvert[i];
toConvert[i] = toConvert[sz - i - 1];
toConvert[sz - i - 1] = tmp;
}
}
#endif
template<class T>
bool WriteBinary(T* data)
{
if(!AppendFileBinary())
return false;//cant open file
#ifdef AMIGAOS4
if(sizeof(T)>1) {
T tmp;
memcpy(&tmp, data, sizeof(tmp));
littleBigEndian(&tmp);
fwrite(&tmp,sizeof(T),1,fPTR);//write the line
} else
#endif
fwrite(data,sizeof(T),1,fPTR);//write the line
//get new file OFFSET
fseek(fPTR,0,SEEK_CUR);
m_lFileOFFSET = ftell(fPTR);
//close file and return line
fclose(fPTR);
return true;
}
template<class T>
bool WriteBinary(T* data, int size)
{
if(!AppendFileBinary())
return false;//cant open file
#ifdef AMIGAOS4
if(sizeof(T)>1) {
T tmp;
for(int i=0; i<size; ++i) {
memcpy(&tmp, data+i, sizeof(tmp));
littleBigEndian(&tmp);
fwrite(&tmp,sizeof(T),1,fPTR);//write the line
}
} else
#endif
fwrite(data,sizeof(T),size,fPTR);//write the line
//get new file OFFSET
fseek(fPTR,0,SEEK_CUR);
m_lFileOFFSET = ftell(fPTR);
//close file and return line
fclose(fPTR);
return true;
}
template<class T>
bool ReadBinary(T* data)
{
if(!FileOpen())
return NULL;//cant open file
fseek(fPTR,m_lFileOFFSET,SEEK_SET);//move to file offset
fread(data,sizeof(T),1,fPTR);//write the line
//get new file OFFSET
fseek(fPTR,0,SEEK_CUR);
m_lFileOFFSET = ftell(fPTR);
//close file and return line
fclose(fPTR);
#ifdef AMIGAOS4
littleBigEndian(data);
#endif
return true;
}
template<class T>
bool ReadBinary(T* data, int size)
{
if(!FileOpen())
return NULL;//cant open file
fseek(fPTR,m_lFileOFFSET,SEEK_SET);//move to file offset
fread(data,sizeof(T),size,fPTR);//write the line
//get new file OFFSET
fseek(fPTR,0,SEEK_CUR);
m_lFileOFFSET = ftell(fPTR);
//close file and return line
fclose(fPTR);
#ifdef AMIGAOS4
for(int i=0; i<size; ++i)
littleBigEndian(data+i);
#endif
return true;
}
void ReleaseFile()
{}
};
#endif