-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable.cpp
executable file
·239 lines (213 loc) · 5.44 KB
/
table.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
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
/*
* Copyright (C) 2016, 王超 [wabcwang] , <[email protected]>.
* All Rights Reserved.
*/
#include "table.h"
int Row::getInt(const std::string& key)
{
int iret = 0;
std::stringstream stream(m_row[key]);
stream>>iret;
return iret;
}
long Row::getLong(const std::string& key)
{
long lret = 0;
std::stringstream stream(m_row[key]);
stream>>lret;
return lret;
}
float Row::getFloat(const std::string& key)
{
float fret = 0;
std::stringstream stream(m_row[key]);
stream>>fret;
return fret;
}
double Row::getDouble(const std::string& key)
{
double dret = 0;
std::stringstream stream(m_row[key]);
stream>>dret;
return dret;
}
void Row::setValue(const std::string& key, const std::string& value)
{
m_row.insert(SPair(key,value));
return;
}
void Row::setValue(const std::string& key, int value)
{
std::stringstream value_stream;
value_stream<<value;
m_row.insert(SPair(key,value_stream.str()));
return;
}
void Row::setValue(const std::string& key, long value)
{
std::stringstream value_stream;
value_stream<<value;
m_row.insert(SPair(key,value_stream.str()));
return;
}
void Row::setValue(const std::string& key, double value)
{
std::stringstream value_stream;
value_stream<<value;
m_row.insert(SPair(key,value_stream.str()));
return;
}
void Row::setValue(const std::string& key, float value)
{
std::stringstream value_stream;
value_stream<<value;
m_row.insert(SPair(key,value_stream.str()));
return;
}
std::string& trim(std::string &s)
{
if (s.empty())
{
return s;
}
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
return s;
}
Table::Table(const char* in,char field_sep, char row_sep)
{
int cur_pos = 0;
int last_pos = 0;
const char* temp = in;
std::string filed = "";
bool end_filed = false;
for(cur_pos=0; (0 != temp[cur_pos]) && (true != end_filed); cur_pos++)
{
switch(temp[cur_pos])
{
case FIELD_SEPARTOR: //字段分隔符
m_field.push_back(filed);
filed="";
break;
case ROW_SEPARTOR: //行分隔符
m_field.push_back(filed);
filed="";
end_filed = true;
break;
default:
filed += temp[cur_pos];
break;
}
}
std::string value = "";
Row *row = NULL;
int filed_index = 0;
for(cur_pos; 0 != temp[cur_pos]; cur_pos++)
{
switch(temp[cur_pos])
{
case FIELD_SEPARTOR: //字段分隔符
if(NULL == row){
row = new Row; //内存耗尽的问题不考虑
}
if(filed_index < m_field.size()){
row->setValue(m_field[filed_index],value);
}
value="";
filed_index++;
break;
case ROW_SEPARTOR: //行分隔符
if(NULL == row){
row = new Row; //内存耗尽的问题不考虑
}
if(filed_index < m_field.size()){
row->setValue(m_field[filed_index],value);
}
addRow(row);
row = NULL;
value = "";
filed_index = 0;
break;
default:
value += temp[cur_pos];
break;
}
}
return;
}
Table::~Table()
{
while(true != m_row.empty())
{
Row* row = m_row.back();
m_row.pop_back();
delete row;
}
}
/* 删除指定行的信息,索引从0开始 */
void Table::deleteRow(int index)
{
if((index >= 0) && (index < m_row.size()) && (true != empty()))
{
Row* row = m_row[index];
m_row.erase(m_row.begin()+index);
delete row;
}
}
/* 删除索引从begin开始到end结束的信息,end这一行不删除,索引从0开始 */
void Table::deleteRow(int begin, int end)
{
if((0 <= begin) && (begin < end) && (end < m_row.size()) && (true != empty()))
{
for(int index=begin; index<end; index++)
{
Row* row = m_row[index];
m_row[index] = NULL;
delete row;
}
m_row.erase(m_row.begin()+begin,m_row.begin()+end);
}
}
Row* Table::getRow(int index)
{
if((index >= 0) && (index < m_row.size()) && (true != empty()))
{
return m_row[index];
}
return NULL;
}
Row* Table::pop_back()
{
if(true == empty()){
return NULL;
}
Row* row = m_row.back();
m_row.pop_back();
return row;
}
Row* Table::pop_front()
{
if(true == empty()){
return NULL;
}
Row* row = m_row[0];
deleteRow(0);
return row;
}
Row* Table::find(const std::string &key, const std::string &value)
{
if(true == empty()){
return NULL;
}
Row* row = NULL;
for (std::vector<Row*>::iterator it = m_row.begin() ; it != m_row.end(); ++it)
{
row = *it;
if(value == row->getValue(key))
{
break;
}
row = NULL;
}
return row;
}