-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdata_file.hh
193 lines (163 loc) · 5.98 KB
/
data_file.hh
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
/*
* Copyright ViewTouch, Inc., 1995, 1996, 1997, 1998
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* data_file.hh - revision 15 (8/25/98)
* Functions for the reading & writing of data files
*/
#ifndef _DATA_FILE_HH
#define _DATA_FILE_HH
#include "utility.hh"
#include <zlib.h>
#include <string>
#include <cstdint> // intXX_t and uintXX_t types
#define BLOCKSIZE 16384
/**** Types ****/
/*********************************************************************
* InputDataFile:
********************************************************************/
class InputDataFile
{
gzFile fp;
int old_format;
std::string filename;
public:
int end_of_file;
// Constructor
InputDataFile();
// Destructor
~InputDataFile() { Close(); }
// Member Functions
int Open(const std::string &filename, int &version);
int Close();
int GetToken(char* buffer, int max_len = 0);
uint64_t GetValue();
// using the following conversions
// char ... 1 byte
// short ... 2 byte
// int ... 4 byte
// long ... 8 byte
// long long ... 8 byte
int Read( int8_t &val) { val = static_cast< int8_t >(GetValue()); return 0; }
int Read(uint8_t &val) { val = static_cast<uint8_t >(GetValue()); return 0; }
int Read( int16_t &val) { val = static_cast< int16_t>(GetValue()); return 0; }
int Read(uint16_t &val) { val = static_cast<uint16_t>(GetValue()); return 0; }
int Read( int32_t &val) { val = static_cast< int32_t>(GetValue()); return 0; }
int Read(uint32_t &val) { val = static_cast<uint32_t>(GetValue()); return 0; }
int Read( int64_t &val) { val = static_cast< int64_t>(GetValue()); return 0; }
int Read(uint64_t &val) { val = static_cast<uint64_t>(GetValue()); return 0; }
int Read(Flt &val);
int Read(Str &val);
int Read(TimeInfo &val);
// conditional reads (won't read if pointer is NULL)
int Read(int *val);
int Read(Flt *val);
int Read(Str *val);
int PeekTokens();
const char* ShowTokens(char* buffer = NULL, int lines = 1);
const std::string &FileName() { return filename; }
};
/*********************************************************************
* OutputDataFile
********************************************************************/
class OutputDataFile
{
void *fp = nullptr;
int compress = 0;
std::string filename;
public:
// Constructor
OutputDataFile();
// Destructor
~OutputDataFile() { Close(); }
// Member Functions
int Open(const std::string &filename, int version, int use_compression = 0);
int Close();
int PutValue(uint64_t val, int bk);
// using the following conversions
// char ... 1 byte
// short ... 2 byte
// int ... 4 byte
// long ... 8 byte
// long long ... 8 byte
int Write( int8_t val, int bk = 0) { return PutValue(static_cast<uint64_t>(val), bk); }
int Write(uint8_t val, int bk = 0) { return PutValue(static_cast<uint64_t>(val), bk); }
int Write( int16_t val, int bk = 0) { return PutValue(static_cast<uint64_t>(val), bk); }
int Write(uint16_t val, int bk = 0) { return PutValue(static_cast<uint64_t>(val), bk); }
int Write( int32_t val, int bk = 0) { return PutValue(static_cast<uint64_t>(val), bk); }
int Write(uint32_t val, int bk = 0) { return PutValue(static_cast<uint64_t>(val), bk); }
int Write( int64_t val, int bk = 0) { return PutValue(static_cast<uint64_t>(val), bk); }
int Write(uint64_t val, int bk = 0) { return PutValue(static_cast<uint64_t>(val), bk); }
int Write(Str &val, int bk = 0) { return Write(val.Value(), bk); }
int Write(Flt val, int bk = 0);
int Write(TimeInfo &val, int bk = 0);
// conditional writes (won't write if pointer is NULL)
int Write(int *val, int bk = 0);
int Write(const char* val, int bk = 0);
int Write(Flt *val, int bk = 0);
int Write(Str *val, int bk = 0);
std::string FileName() { return filename; }
};
/*********************************************************************
* KeyValueInputFile:
********************************************************************/
class KeyValueInputFile
{
private:
int filedes = -1;
int bytesread = 0;
int keyidx = 0;
int validx = 0;
int buffidx = 0;
int comment = 0;
int getvalue = 0;
char delimiter = ':';
char buffer[BLOCKSIZE] = {'\0'};
std::string inputfile;
public:
KeyValueInputFile();
KeyValueInputFile(const int fd);
KeyValueInputFile(const std::string &filename);
bool Open();
bool Open(const std::string &filename);
bool IsOpen();
int Set(const int fd);
int Set(const std::string &filename);
char SetDelim(const char delim);
int Close();
int Reset();
int Read(char* key, char* value, int maxlen);
};
/*********************************************************************
* KeyValueOutputFile:
********************************************************************/
class KeyValueOutputFile
{
private:
int filedes = -1;
char delimiter = ':';
std::string outputfile;
public:
KeyValueOutputFile();
KeyValueOutputFile(const int fd);
KeyValueOutputFile(const std::string &filename);
int Open();
int Open(const std::string &filename);
int IsOpen();
char SetDelim(const char delim);
int Close();
int Reset();
int Write(const std::string &key, const std::string &value);
};
#endif