forked from WohlSoft/PGE-File-Library-STL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pge_file_lib_globs.h
474 lines (435 loc) · 13.3 KB
/
pge_file_lib_globs.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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*
* PGE File Library - a library to process file formats, part of Moondust project
*
* Copyright (c) 2014-2022 Vitaly Novichkov <[email protected]>
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/*! \file pge_file_lib_globs.h
\brief Contains internal settings and references for PGE File Library
All defined here macroses are allows to build PGE File Library for both
Qt and STL libraries set.
*/
#pragma once
#ifndef PGE_FILE_LIB_GLOBS_H_
#define PGE_FILE_LIB_GLOBS_H_
/*! \def PGE_FILES_QT
\brief If this macro is defined, Qt version of PGE File Library will be built
*/
/*! \def PGEChar
\brief A macro which equal to 'char' if PGE File Library built in the STL mode
and equal to QChar if PGE File Library built in the Qt mode
*/
/*! \def PGESTRING
\brief A macro which equal to std::string if PGE File Library built in the STL mode
and equal to QString if PGE File Library built in the Qt mode
*/
/*! \def PGESTRINGList
\brief A macro which equal to std::vector<std::string> if PGE File Library built in the STL mode
and equal to QStringList if PGE File Library built in the Qt mode
*/
/*! \def PGELIST
\brief A macro which equal to std::vector if PGE File Library built in the STL mode
and equal to QList if PGE File Library built in the Qt mode
*/
/*! \def PGEVECTOR
\brief A macro which equal to std::vector if PGE File Library built in the STL mode
and equal to QVector if PGE File Library built in the Qt mode
*/
#include <cstdint>
#ifdef PGE_FILES_QT
#include <QString>
#include <QStringList>
#include <QList>
#include <QVector>
#include <QPair>
#include <QFile>
#include <QMap>
#include <QTextStream>
#define PGE_FILES_INHERED : public QObject
typedef QString PGESTRING;
typedef QStringList PGESTRINGList;
typedef QChar PGEChar;
#define PGELIST QList
#define PGEVECTOR QVector
#define PGEPAIR QPair
#define PGEMAP QMap
#define PGEMAPKEY(it) (it.key())
#define PGEMAPVAL(it) (it.value())
#else /* PGE_FILES_QT */
#include <string>
#include <vector>
#include <cstdio>
#include <utility>
#include <map>
#define PGE_FILES_INHERED
typedef std::string PGESTRING;
typedef std::vector<std::string> PGESTRINGList;
typedef char PGEChar;
#define PGELIST std::vector
#define PGEVECTOR std::vector
#define PGEPAIR std::pair
#define PGEMAP std::map
#define PGEMAPKEY(it) (it->first)
#define PGEMAPVAL(it) (it->second)
#endif /* PGE_FILES_QT */
/*!
* Misc I/O classes used by PGE File Library internally
*/
namespace PGE_FileFormats_misc
{
/**
* @brief Check the header to identify valid SMBX64 file format
* @param src Header source string
* @return true if the header was identified as SMBX64 file
*/
bool PGE_DetectSMBXFile(PGESTRING src);
/*!
* \brief Provides cross-platform file path calculation for a file names or paths
*/
class FileInfo
{
public:
/*!
* \brief Constructor
*/
FileInfo();
/*!
* \brief Constructor with pre-opening of a file
* \param filepath relative or absolute file path
*/
FileInfo(const PGESTRING &filepath);
/*!
* \brief Sets file which will be used to calculate file information
* \param filepath
*/
void setFile(const PGESTRING &filepath);
/*!
* \brief Returns file extension (last part of filename after last dot)
* \return file suffix or name extension (last part of filename after last dot)
*/
PGESTRING suffix();
/*!
* \brief Returns full filename without path
* \return full filename without path
*/
PGESTRING filename();
/*!
* \brief Returns absolute path to file
* \return absolute path to file
*/
PGESTRING fullPath();
/*!
* \brief Returns base name part (first part of file name before first dot)
* \return base name part (first part of file name before first dot)
*/
PGESTRING basename();
/*!
* \brief Returns full directory path where actual file is located
* \return full directory path where actual file is located
*/
PGESTRING dirpath();
private:
/*!
* \brief Recalculates all internal fields
*/
void rebuildData();
/*!
* \brief Current filename
*/
PGESTRING m_filePath;
/*!
* \brief Current filename without directory path
*/
PGESTRING m_fileName;
/*!
* \brief File name suffix (last part of file name after last dot)
*/
PGESTRING m_suffix;
/*!
* \brief Base name (first part of file name before first dot)
*/
PGESTRING m_baseName;
/*!
* \brief Full directory path where file is located
*/
PGESTRING m_dirPath;
};
class TextInput
{
public:
/*!
* \brief Relative positions of carriage
*/
enum positions
{
//! Relative to current position
current = 0,
//! Relative to begin of file
begin,
//! Relative to end of file
end
};
TextInput();
virtual ~TextInput();
virtual PGESTRING read(int64_t len);
virtual PGESTRING readLine();
virtual PGESTRING readCVSLine();
virtual PGESTRING readAll();
virtual bool eof();
virtual int64_t tell();
virtual int seek(int64_t pos, positions relativeTo);
virtual PGESTRING getFilePath();
virtual void setFilePath(const PGESTRING &path);
virtual long getCurrentLineNumber();
virtual bool reOpen(bool utf8);
protected:
PGESTRING m_filePath;
long m_lineNumber = 0;
};
class TextOutput
{
public:
/*!
* \brief Relative positions of carriage
*/
enum positions
{
//! Relative to current position
current = 0,
//! Relative to begin of file
begin,
//! Relative to end of file
end
};
enum outputMode
{
truncate = 0,
append,
overwrite
};
TextOutput();
virtual ~TextOutput();
virtual int write(PGESTRING buffer);
virtual int64_t tell();
virtual int seek(int64_t pos, positions relativeTo);
virtual PGESTRING getFilePath();
virtual void setFilePath(const PGESTRING &path);
virtual long getCurrentLineNumber();
TextOutput &operator<<(const PGESTRING &s);
TextOutput &operator<<(const char *s);
protected:
PGESTRING m_filePath;
long m_lineNumber = 0;
};
class RawTextInput: public TextInput
{
public:
RawTextInput();
RawTextInput(PGESTRING *rawString, const PGESTRING &filepath = PGESTRING());
virtual ~RawTextInput();
bool open(PGESTRING *rawString, const PGESTRING &filepath = PGESTRING());
void close();
virtual PGESTRING read(int64_t len);
virtual PGESTRING readLine();
virtual PGESTRING readCVSLine();
virtual PGESTRING readAll();
virtual bool eof();
virtual int64_t tell();
virtual int seek(int64_t pos, positions relativeTo);
private:
int64_t m_pos = 0;
PGESTRING *m_data = nullptr;
bool m_isEOF = false;
};
class RawTextOutput: public TextOutput
{
public:
RawTextOutput();
RawTextOutput(PGESTRING *rawString, outputMode mode = truncate);
virtual ~RawTextOutput();
bool open(PGESTRING *rawString, outputMode mode = truncate);
void close();
int write(PGESTRING buffer);
int64_t tell();
int seek(int64_t pos, positions relativeTo);
private:
long long m_pos = 0ll;
PGESTRING *m_data = nullptr;
};
/*!
* \brief Provides cross-platform text file reading interface
*/
class TextFileInput: public TextInput
{
public:
/*!
* \brief Checks is requested file exist
* \param filePath Full or relative path to the file
* \return true if file exists
*/
static bool exists(PGESTRING filePath);
/*!
* \brief Constructor
*/
TextFileInput();
/*!
* \brief Constructor with pre-opening of the file
* \param filePath Full or relative path to the file
* \param utf8 Use UTF-8 encoding or will be used local 8-bin encoding
*/
TextFileInput(PGESTRING filePath, bool utf8 = false);
/*!
* \brief Destructor
*/
virtual ~TextFileInput();
/*!
* \brief Opening of the file
* \param filePath Full or relative path to the file
* \param utf8 Use UTF-8 encoding or will be used local 8-bin encoding
*/
bool open(PGESTRING filePath, bool utf8 = false);
/*!
* \brief Re-open opened file with or without UTF8 mode enabled
* \param utf8 Use UTF-8 encoding or will be used local 8-bin encoding
*/
bool reOpen(bool utf8 = false);
/*!
* \brief Close currently opened file
*/
void close();
/*!
* \brief Reads requested number of characters from a file
* \param Maximal lenght of characters to read from file
* \return string contains requested line of characters
*/
PGESTRING read(int64_t len);
/*!
* \brief Reads whole line before line feed character
* \return string contains gotten line
*/
PGESTRING readLine();
/*!
* \brief Reads whole line before line feed character or before first unquoted comma
* \return string contains gotten line
*/
PGESTRING readCVSLine();
/*!
* \brief Reads all data from a file at current position of carriage
* \return
*/
PGESTRING readAll();
/*!
* \brief Is carriage position at end of file
* \return true if carriage position at end of file
*/
bool eof();
/*!
* \brief Returns current position of carriage relative to begin of file
* \return current position of carriage relative to begin of file
*/
int64_t tell();
/*!
* \brief Changes position of carriage to specific file position
* \param pos Target position of carriage
* \param relativeTo defines relativity of target position of carriage (current position, begin of file or end of file)
*/
int seek(int64_t pos, positions relativeTo);
private:
#ifdef PGE_FILES_QT
//! Read as UTF8 or as ANSI
bool m_utf8 = true;
//! File handler used in Qt version of PGE file Library
QFile file;
//! File input stream used in Qt version of PGE file Library
QTextStream stream;
#else
//! File input stream used in STL version of PGE file Library
FILE *stream = nullptr;
#endif
};
class TextFileOutput: public TextOutput
{
public:
/*!
* \brief Checks is requested file exist
* \param filePath Full or relative path to the file
* \return true if file exists
*/
static bool exists(PGESTRING filePath);
/*!
* \brief Constructor
*/
TextFileOutput();
/*!
* \brief Constructor with pre-opening of the file
* \param filePath Full or relative path to the file
* \param utf8 Use UTF-8 encoding or will be used local 8-bin encoding
*/
TextFileOutput(PGESTRING filePath, bool utf8 = false, bool forceCRLF = false, outputMode mode = truncate);
/*!
* \brief Destructor
*/
virtual ~TextFileOutput();
/*!
* \brief Opening of the file
* \param filePath Full or relative path to the file
* \param utf8 Use UTF-8 encoding or will be used local 8-bin encoding
*/
bool open(PGESTRING filePath, bool utf8 = false, bool forceCRLF = false, outputMode mode = truncate);
/*!
* \brief Close currently opened file
*/
void close();
/*!
* \brief Reads requested number of characters from a file
* \param Maximal lenght of characters to read from file
* \return string contains requested line of characters
*/
int write(PGESTRING buffer);
/*!
* \brief Returns current position of carriage relative to begin of file
* \return current position of carriage relative to begin of file
*/
int64_t tell();
/*!
* \brief Changes position of carriage to specific file position
* \param pos Target position of carriage
* \param relativeTo defines relativity of target position of carriage (current position, begin of file or end of file)
*/
int seek(int64_t pos, positions relativeTo);
private:
//! Enfoce CRLF line ending in a file, even on non-Windows platforms
bool m_forceCRLF = false;
#ifdef PGE_FILES_QT
//! Write in UTF8 or in ANSI
bool m_utf8 = true;
//! File handler used in Qt version of PGE file Library
QFile file;
//! File input stream used in Qt version of PGE file Library
QTextStream stream;
#else
//! File input stream used in STL version of PGE file Library
FILE *stream = nullptr;
#endif
};
}
#endif // PGE_FILE_LIB_GLOBS_H_