-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfield.cc
101 lines (88 loc) · 2.1 KB
/
field.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
/*
* $Id: field.cc,v 2.7 2005/10/20 11:24:12 martin Exp $
* Copyright (c) 2004, 2005, Voidsoft AB
*/
#include <iostream>
#include <sstream>
#include "field.hh"
#include "tdspp.hh" // for TDSPP::Exception
/** Class Field */
/** Constructor */
Field::Field()
: data(NULL) {
}
Field::Field(string name, int size, int datatype_) {
colname = name;
data = new char[size];
data[0] = 0;
datatype = datatype_;
}
/** Destructor */
Field::~Field() {
delete [] data;
}
string Field::to_str() {
if (!data) throw TDSPP::Exception("Field::tostr: Data not initialized");
return data;
}
long Field::to_int() {
if (!data) throw TDSPP::Exception("Field::toint: Data not initialized");
int i = 0;
stringstream ss(data);
ss >> i;
return i;
}
long Field::to_int64() {
if (!data) throw TDSPP::Exception("Field::toint: Data not initialized");
int64_t i = 0;
stringstream ss(data);
ss >> i;
return i;
}
/* Not precise! */
double Field::to_double() {
if (!data) throw TDSPP::Exception("Field::to_double: Data not initialized");
switch (datatype) {
case CS_REAL_TYPE: case CS_FLOAT_TYPE: case CS_MONEY_TYPE: case CS_MONEY4_TYPE: case CS_NUMERIC_TYPE: case CS_DECIMAL_TYPE:
{
double d = 0;
stringstream ss(data);
ss >> d;
return d;
}
default:
return 0;
}
}
/** Class Rows */
/** Constructor */
Rows::Rows()
: currentrow(0) {
}
/** Destructor */
Rows::~Rows() {
clear();
}
void Rows::clear(void) {
while (rows.size()) {
while (rows[rows.size()-1].size()) {
delete rows[rows.size()-1].back();
rows[rows.size()-1].pop_back();
}
rows.pop_back();
}
}
void Rows::printheader(void) {
cout << "| ";
for (unsigned int i=0; i < rows[currentrow].size(); i++) {
cout << rows[currentrow][i]->colname << " | ";
}
cout << endl;
}
void Rows::print(void) {
cout << "| ";
for (unsigned int i=0; i < rows[currentrow].size(); i++) {
cout << rows[currentrow][i]->to_str() << " | ";
}
cout << endl;
}