-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamWriter.cpp
182 lines (155 loc) · 3.94 KB
/
StreamWriter.cpp
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
/*
* File: StreamWriter.cpp
* Author: kavlar
*
* Created on 20140821
*/
#include "StreamWriter.h"
MetCode::StreamWriter::StreamWriter()
{
pos = NULL;
}
MetCode::StreamWriter::StreamWriter(const StreamWriter& orig)
{
}
MetCode::StreamWriter::~StreamWriter()
{
ofs.close();
}
void MetCode::StreamWriter::reset() throw ()
{
cache.clear();
cache.str("");
n_line = 0;
n_group = 0;
}
bool MetCode::StreamWriter::log(const std::string& level, const std::string& text) const throw (IOException)
{
std::cerr << "<< [" << level << "]" << text << std::endl;
return true;
}
bool MetCode::StreamWriter::open(const std::string& filename) throw (IOException)
{
ofs.open(filename.c_str(), std::ofstream::out | std::ofstream::binary);
if (ofs.fail())
{
ofs.close();
log("E", "Failed to open file: " + filename);
throw IOException();
return false;
}
pos = static_cast<std::ostream*> (&ofs);
reset();
return true;
}
bool MetCode::StreamWriter::open(std::ostream& os) throw (IOException)
{
if (os.fail())
{
log("E", "Failed to write istream");
throw IOException();
return false;
}
pos = static_cast<std::ostream*> (&os);
reset();
return true;
}
bool MetCode::StreamWriter::isEOF() throw (IOException)
{
return pos->fail();
}
extern void trim(std::string& str, const std::string chars);
bool MetCode::StreamWriter::put(const std::string& str, unsigned long& line,
const bool new_line,
const bool skip_empty_line,
const std::string& delimit) throw (IOException)
{
bool result = false;
if (cache)
{
if (new_line)
{
if (skip_empty_line && str.size() == 0)
{
}
else
{
n_line++;
cache << delimit;
}
}
cache << str;
line = n_line;
if (cache.good())
{
result = true;
}
}
//std::cerr << ">> PUTLINE (" << line << ") : " << str.size() << "[" << str << "]" << std::endl;
return result;
}
bool MetCode::StreamWriter::put(const std::string& str, unsigned long& group, unsigned long& line,
const bool new_group, const bool new_line,
const bool skip_empty_group,
const bool skip_empty_line,
const std::string& group_delimit,
const std::string& line_delimit) throw (IOException)
{
bool result = false;
if (cache)
{
if (new_line)
{
result = put(str, line, new_line, skip_empty_line, line_delimit);
}
else
{
if (new_group)
{
if (skip_empty_group && str.size() == 0)
{
}
else
{
n_group++;
cache << group_delimit;
}
}
cache << str;
group = n_group;
if (cache.good())
{
result = true;
}
}
}
//std::cerr << ">> PUTGROUP (" << line << "," << group << ") : " << str.size() << "[" << str << "]" << std::endl;
return result;
}
bool MetCode::StreamWriter::flush(bool prefix_size, const std::string& type) throw (IOException)
{
if (*pos)
{
if (prefix_size)
{
char buf[20];
::sprintf(buf, "%08d\0", cache.str().size());
*pos << buf;
}
*pos << type.substr(0, 2);
*pos << cache.str();
cache.clear();
cache.str("");
}
else
{
throw IOException();
return false;
}
}
void MetCode::StreamWriter::close() throw (IOException)
{
ofs.close();
pos = NULL;
reset();
}