tycho / crisscross

A C++ library with various uses.

This URL has Read+Write access

crisscross / source / core_io_reader.cpp
100644 293 lines (248 sloc) 7.309 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* CrissCross
* A multi-purpose cross-platform library.
*
* A product of Uplink Laboratories.
*
* (c) 2006-2009 Steven Noonan.
* Licensed under the New BSD License.
*
*/
 
#include <crisscross/universal_include.h>
 
#include <crisscross/debug.h>
#include <crisscross/core_io.h>
#include <crisscross/system.h>
 
using namespace CrissCross::System;
 
namespace CrissCross
{
namespace IO
{
CoreIOReader::CoreIOReader(FILE * _fileBuffer, bool _isUnicode, LineEndingType _lnEnding) : m_fileInputPointer(_fileBuffer),
m_unicode(_isUnicode)
{
SetLineEndings(_lnEnding);
}
 
CoreIOReader::~CoreIOReader()
{
}
 
bool CoreIOReader::EndOfFile()
{
CoreAssert(this != NULL);
 
if (!m_fileInputPointer)
return true;
 
return (feof(m_fileInputPointer) != 0);
}
 
void CoreIOReader::Flush()
{
CoreAssert(this != NULL);
if (!IsOpen()) return;
 
#ifndef __GNUC__
MutexHolder mh(&m_ioMutex);
#endif
fflush(m_fileInputPointer);
}
 
bool CoreIOReader::IsOpen()
{
CoreAssert(this != NULL);
 
if (m_fileInputPointer == NULL)
return false;
else
return true;
}
 
int CoreIOReader::Forward(cc_int64_t _position)
{
CoreAssert(this != NULL);
if (!IsOpen()) return CC_ERR_INVALID_BUFFER;
 
int res = Seek(_position, SEEK_CUR);
return (res == 0);
}
 
cc_int64_t CoreIOReader::Position()
{
CoreAssert(this != NULL);
CoreAssert(IsOpen());
#ifdef HAS_FPOS64
fpos64_t lastpos;
#if defined(TARGET_COMPILER_VC) || defined(TARGET_COMPILER_BORLAND) || \
(defined(TARGET_COMPILER_ICC) && defined(TARGET_OS_WINDOWS))
lastpos = _ftelli64(m_fileInputPointer);
return lastpos;
#elif defined (TARGET_OS_MACOSX) || defined (TARGET_OS_NETBSD) || \
defined (TARGET_OS_FREEBSD) || defined (TARGET_OS_OPENBSD)
fgetpos(m_fileInputPointer, &lastpos);
return lastpos;
#else
fgetpos64(m_fileInputPointer, &lastpos);
return lastpos.__pos;
#endif
#else
fpos_t lastpos;
lastpos = ftell(m_fileInputPointer);
return lastpos;
#endif
}
 
cc_int64_t CoreIOReader::Length()
{
CoreAssert(this != NULL);
CoreAssert(IsOpen());
 
#ifndef __GNUC__
MutexHolder mh(&m_ioMutex);
#endif
 
#ifdef HAS_FPOS64
fpos64_t lastpos, endpos;
#if defined(TARGET_COMPILER_VC) || defined(TARGET_COMPILER_BORLAND) || \
(defined(TARGET_COMPILER_ICC) && defined(TARGET_OS_WINDOWS))
lastpos = _ftelli64(m_fileInputPointer);
_fseeki64(m_fileInputPointer, 0, SEEK_END);
endpos = _ftelli64(m_fileInputPointer);
_fseeki64(m_fileInputPointer, lastpos, SEEK_SET);
#elif defined (TARGET_OS_MACOSX) || defined (TARGET_OS_NETBSD) || \
defined (TARGET_OS_FREEBSD) || defined (TARGET_OS_OPENBSD)
fgetpos(m_fileInputPointer, &lastpos);
fseek(m_fileInputPointer, 0, SEEK_END);
fgetpos(m_fileInputPointer, &endpos);
fsetpos(m_fileInputPointer, &lastpos);
#else
fgetpos64(m_fileInputPointer, &lastpos);
fseeko64(m_fileInputPointer, 0, SEEK_END);
fgetpos64(m_fileInputPointer, &endpos);
fsetpos64(m_fileInputPointer, &lastpos);
#endif
#else
fpos_t lastpos, endpos;
lastpos = ftell(m_fileInputPointer);
fseek(m_fileInputPointer, 0, SEEK_END);
endpos = ftell(m_fileInputPointer);
fseek(m_fileInputPointer, lastpos, SEEK_SET);
#endif
 
#if defined (TARGET_OS_WINDOWS) || defined (TARGET_OS_MACOSX) || defined (TARGET_OS_FREEBSD) || \
defined (TARGET_OS_NETBSD) || defined (TARGET_OS_OPENBSD) || defined (TARGET_COMPILER_CYGWIN) || \
defined (TARGET_OS_NDSFIRMWARE)
return endpos;
#elif defined (TARGET_OS_LINUX)
return endpos.__pos;
#endif
}
 
int CoreIOReader::Read(char *_buffer, size_t _bufferLength, size_t _bufferIndex, size_t _count)
{
CoreAssert(this != NULL);
if (!IsOpen()) return CC_ERR_INVALID_BUFFER;
 
size_t retval;
 
CoreAssert(_buffer != NULL);
CoreAssert(_bufferLength - _bufferIndex <= _count);
CoreAssert(_count > 0);
#ifndef __GNUC__
MutexHolder mh(&m_ioMutex);
#endif
retval = fread(&_buffer[_bufferIndex], sizeof(char), _count, m_fileInputPointer);
return (int)retval;
}
 
int CoreIOReader::ReadLine(char *_buffer, size_t _bufferLength)
{
CoreAssert(this != NULL);
if (!IsOpen()) return CC_ERR_INVALID_BUFFER;
 
#ifndef __GNUC__
MutexHolder mh(&m_ioMutex);
#endif
 
/* We use fgets because it detects line endings. */
_buffer[0] = '\x0';
char *ret = fgets(_buffer, (int)_bufferLength, m_fileInputPointer);
if (ret != _buffer)
return -1;
 
/* Detect line endings. */
char *endl = NULL;
char *cr = strchr(_buffer, '\r');
char *lf = strchr(_buffer, '\n');
char *crlf = strstr(_buffer, "\r\n");
if (crlf) {
SetLineEndings(CC_LN_CRLF); endl = crlf;
} else if (cr) {
SetLineEndings(CC_LN_CR); endl = cr;
} else if (lf) {
SetLineEndings(CC_LN_LF); endl = lf;
}
 
if (endl)
*endl = '\x0';
 
return (int)strlen(_buffer);
}
 
/* TODO: This function uses fgetc() which incurs unnecessary function call overhead. Find a suitable replacement. */
int CoreIOReader::ReadLine(std::string &_string)
{
CoreAssert(this != NULL);
if (!IsOpen()) return CC_ERR_INVALID_BUFFER;
 
#ifndef __GNUC__
MutexHolder mh(&m_ioMutex);
#endif
char c = (char)fgetc(m_fileInputPointer);
 
if (c == (char)EOF)
return 0;
 
std::string buffer;
 
while (c != (char)EOF && c != '\n') {
buffer += c;
c = (char)fgetc(m_fileInputPointer);
}
 
int len = (int)buffer.length();
 
if (len && buffer[len - 1] == '\r')
buffer.resize(len - 1);
 
_string = buffer;
 
return (int)_string.length() * sizeof(char);
}
 
int CoreIOReader::Seek(cc_int64_t _position, int _origin)
{
CoreAssert(this != NULL);
if (!IsOpen()) return CC_ERR_INVALID_BUFFER;
 
#ifndef __GNUC__
MutexHolder mh(&m_ioMutex);
#endif
#ifdef HAS_FPOS64
#if defined(TARGET_COMPILER_VC) || defined(TARGET_COMPILER_BORLAND) || \
(defined(TARGET_COMPILER_ICC) && defined(TARGET_OS_WINDOWS))
int res = _fseeki64(m_fileInputPointer, _position, _origin);
#elif defined (TARGET_OS_MACOSX) || defined (TARGET_OS_NETBSD) || \
defined (TARGET_OS_FREEBSD) || defined (TARGET_OS_OPENBSD)
int res = fseek(m_fileInputPointer, _position, _origin);
#else
int res = fseeko64(m_fileInputPointer, _position, _origin);
#endif
#else
int res = fseek(m_fileInputPointer, _position, _origin);
#endif
return res;
}
 
int CoreIOReader::Seek(cc_int64_t _position)
{
CoreAssert(this != NULL);
if (!IsOpen()) return CC_ERR_INVALID_BUFFER;
 
int res = Seek(_position, SEEK_SET);
return (res == 0);
}
 
CrissCross::Errors CoreIOReader::SetLineEndings(LineEndingType _ending)
{
CoreAssert(this != NULL);
 
if (_ending == CC_LN_NATIVE) {
#if defined (TARGET_OS_WINDOWS)
_ending = CC_LN_CRLF;
#elif defined (TARGET_OS_LINUX) || defined (TARGET_OS_MACOSX) || defined (TARGET_OS_FREEBSD) || \
defined (TARGET_OS_NETBSD) || defined (TARGET_OS_OPENBSD) || defined (TARGET_OS_NDSFIRMWARE)
_ending = CC_LN_LF;
#else
#error You are not using a supported OS.
#endif
}
 
switch (_ending)
{
case CC_LN_CR:
sprintf(m_lineEnding, "\r");
break;
case CC_LN_LF:
sprintf(m_lineEnding, "\n");
break;
case CC_LN_CRLF:
sprintf(m_lineEnding, "\r\n");
break;
default:
return CC_ERR_BADPARAMETER;
}
return CC_ERR_NONE;
}
}
}