-
Notifications
You must be signed in to change notification settings - Fork 3
/
DSVRecord.cc
251 lines (220 loc) · 4.37 KB
/
DSVRecord.cc
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
//
// Copyright 2009-2017 M. Shulhan (ms@kilabit.info). All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "DSVRecord.hh"
namespace vos {
/**
* @method : DSVRecord::DSVRecord
* @desc : DSVRecord object constructor.
*/
DSVRecord::DSVRecord() : Buffer(),
_next_col(NULL),
_last_col(this),
_next_row(NULL),
_last_row(this)
{}
/**
* @method : DSVRecord::~DSVRecord
* @desc : DSVRecord object destructor.
*/
DSVRecord::~DSVRecord()
{
if (_next_col)
delete _next_col;
if (_next_row)
delete _next_row;
}
/**
* @method : DSVRecord::dump
* @desc : print content of DSVRecord object to standard output.
*/
void DSVRecord::dump()
{
Buffer o;
DSVRecord* row = this;
DSVRecord* col = NULL;
o.append_raw("[vos::DSVRecord__] dump:\n");
while (row) {
col = row;
while (col) {
o.append(col);
o.appendc('|');
col = col->_next_col;
}
o.appendc('\n');
row = row->_next_row;
}
printf("%s", o.chars());
}
/**
* @method : DSVRecord::get_column
* @param :
* > n : index of column, started from zero.
* @return :
* < DSVRecord : pointer to DSVRecord at column 'n'.
* @desc : get record in column 'n'.
*/
DSVRecord* DSVRecord::get_column(int n)
{
DSVRecord* p = this;
for (; n > 0 && p; --n) {
p = p->_next_col;
}
return p;
}
/**
* @method : DSVRecord::set_column
* @param :
* > n : index of column to set to, started from zero.
* > bfr : content for new column value.
* @return :
* < 0 : success.
* < <0 : fail.
* @desc : set column 'n' value to 'bfr'.
*/
int DSVRecord::set_column(int n, Buffer* bfr)
{
if (!bfr) {
return 0;
}
DSVRecord* p = this;
for (; n > 0 && p; --n) {
p = p->_next_col;
}
if (n < 0 || NULL == p) {
return -1;
}
if (! bfr->is_empty()) {
Error err = p->copy_raw(bfr->v(), bfr->len());
if (err != NULL) {
return -1;
}
}
return 0;
}
/**
* @method : DSVRecord::set_column_number
* @param :
* > n : index of column to set to, started from zero.
* > number : content for a new column value.
* @return :
* < 0 : success.
* < <0 : fail.
* @desc : set column 'n' value to 'number'.
*/
int DSVRecord::set_column_number(int n, const int number)
{
DSVRecord* p = this;
for (; n > 0 && p; --n) {
p = p->_next_col;
}
if (n < 0 || NULL == p) {
return -1;
}
p->reset();
Error err = p->appendi(number);
if (err != NULL) {
return -1;
}
return 0;
}
/**
* @method : DSVRecord::set_column_ulong
* @param :
* > n : index of column to set to, started from zero.
* > number : content for a new column value.
* @return :
* < 0 : success.
* < <0 : fail.
* @desc : set column 'n' value to 'number'.
*/
int DSVRecord::set_column_ulong(int n, const unsigned long number)
{
DSVRecord* p = this;
for (; n > 0 && p; --n) {
p = p->_next_col;
}
if (n < 0 || NULL == p) {
return -1;
}
p->reset();
Error err = p->appendui(number);
if (err != NULL) {
return -1;
}
return 0;
}
/**
* @method : DSVRecord::columns_reset
* @desc : empties all columns.
*/
void DSVRecord::columns_reset()
{
DSVRecord* p = this;
while (p) {
p->reset();
p = p->_next_col;
}
}
/**
* @method : DSVRecord::ADD_COL
* @param :
* > row : header or row.
* > col : a new column to be added to 'row'.
* @desc : add a new column 'col' to 'row'.
*/
void DSVRecord::ADD_COL(DSVRecord** row, DSVRecord* col)
{
if (! (*row)) {
(*row) = col;
} else {
(*row)->_last_col->_next_col = col;
(*row)->_last_col = col;
}
}
/**
* @method : DSVRecord::ADD_ROW
* @param :
* > rows : pointer to the first row.
* > row : a new row to be added to list of 'rows'.
* @desc : add a new 'row' to the list of 'rows'.
*/
void DSVRecord::ADD_ROW(DSVRecord** rows, DSVRecord* row)
{
if (! (*rows)) {
(*rows) = row;
} else {
(*rows)->_last_row->_next_row = row;
(*rows)->_last_row = row;
}
}
/**
* @method : DSVRecord::INIT_ROW
* @param :
* > row : head of row or pointer to first column.
* > n_col : number of column to be added to 'row'
* @return :
* < 0 : success.
* < <0 : fail.
* @desc : add 'n_col' number of column to the 'row'.
*/
int DSVRecord::INIT_ROW(DSVRecord** row, int n_col)
{
if (0 == n_col) {
return 0;
}
DSVRecord* col = NULL;
for (; n_col > 0; --n_col) {
col = new DSVRecord();
if (!col) {
delete row;
return -1;
}
ADD_COL(row, col);
}
return 0;
}
} /* namespace::vos */
// vi: ts=8 sw=8 tw=78: