Blame view

server/kernel/src/DataRecord/DataRecord.cpp 3.65 KB
346b85c6   Benjamin Renard   First commit with...
1
2
3
4
5
6
#include "DataRecord.h"

#include <cstring>
#include <cstdlib>
#include <climits>
#include <algorithm>
150096dc   Benjamin Renard   Fix some bugs + i...
7
8
9
#include <cstdio>
#include <cfloat>
#include <cmath>
346b85c6   Benjamin Renard   First commit with...
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

namespace TREPS
{
	namespace DataRecord
	{
		DataRecordClass::DataRecordClass(void) : tag(0), nextRecord(NULL)
		{
			memset(&this->time,0,sizeof(t_Time));
			this->values.clear();
		}
		
		DataRecordClass::~DataRecordClass(void)
		{
			this->values.clear();
		}
	
		void DataRecordClass::setValue(const char *key, const char *val)
		{
			if (std::find(keysOrder.begin(),keysOrder.end(),key) == keysOrder.end())
				keysOrder.push_back(key);
			this->values[key] = val;
		}

		string DataRecordClass::getValue(const char *key)
		{
			if (this->valueExist(key))
				return this->values[key];
			return "";
		}

		string DataRecordClass::getMultiValues(const char *firstKey, int nbParts)
		{
			std::list<std::string>::iterator it = std::find(keysOrder.begin(),keysOrder.end(),firstKey);
			int treatedPart = 0;
			string allValues = "";
			while ((it != keysOrder.end()) && (treatedPart < nbParts))
			{
				if (treatedPart > 0)
					allValues += " ";
				allValues += getValue(it->c_str());
				++treatedPart;
				++it;
			}
			return allValues;
		}

		double DataRecordClass::getDoubleValue(const char *key)
		{
			//convert value to double
			string value = this->getValue(key);
			if (value.compare("") == 0)
				return 0.;
			return atof(value.c_str());
		}

		float DataRecordClass::getFloatValue(const char *key)
		{
			//convert value to float
			string value = this->getValue(key);
			if (value.compare("") == 0)
				return 0.;
150096dc   Benjamin Renard   Fix some bugs + i...
71
72
73
74
			double d = atof(value.c_str());
                        if (isnan(d) || (d > FLT_MAX))
                                return NAN;
			return (float)d;
346b85c6   Benjamin Renard   First commit with...
75
76
77
78
79
80
81
82
		}

		int DataRecordClass::getIntValue(const char *key)
		{
			//convert value to int
			string value = this->getValue(key);
			if (value.compare("") == 0)
				return 0;
150096dc   Benjamin Renard   Fix some bugs + i...
83
84
85
86
			double d = atof(value.c_str());
                        if (isnan(d) || (d > INT_MAX))
                                return 0;
			return (int)d;
346b85c6   Benjamin Renard   First commit with...
87
88
89
90
91
92
93
94
		}

		long DataRecordClass::getLongValue(const char *key)
		{
			//convert value to long
			string value = this->getValue(key);
			if (value.compare("") == 0)
				return 0;
150096dc   Benjamin Renard   Fix some bugs + i...
95
96
97
98
			double d = atof(value.c_str());
			if (isnan(d) || (d > LONG_MAX))
				return 0;
			return (long)d;
346b85c6   Benjamin Renard   First commit with...
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
		}

		long long DataRecordClass::getLongLongValue(const char *key)
		{
			//convert value to long long
			string value = this->getValue(key);
			if (value.compare("") == 0)
				return 0;
			return atoll(value.c_str());
		}

		short DataRecordClass::getShortValue(const char *key)
		{
			//convert value to short
			string value = this->getValue(key);
			if (value.compare("") == 0)
				return 0;
			int i = atoi(value.c_str());
			//be sure that the cast is possible
			if ((i > SHRT_MAX) || (i < SHRT_MIN))
				return 0;
			return (short)i;
		}

		map<string,string> DataRecordClass::getValuesMap(void)
		{
			return this->values;
		}

		bool DataRecordClass::valueExist(const char *key)
		{
			return (this->values.count(key) > 0);
		}

		void DataRecordClass::setTime(t_Time t)
		{
			this->time = t;
		}

		t_Time DataRecordClass::getTime(void)
		{
			return this->time;
		}

		void DataRecordClass::setTag(long int tag)
		{
			this->tag = tag;
		}

		long int DataRecordClass::getTag(void)
		{
			return this->tag;
		}

		void DataRecordClass::setNextRecord(DataRecordClass *nextRecord)
		{
			this->nextRecord = nextRecord;
		}

		DataRecordClass *DataRecordClass::getNextRecord(void)
		{
			return this->nextRecord;
		}
	}
}