DataRecord.cpp 3.65 KB
#include "DataRecord.h"

#include <cstring>
#include <cstdlib>
#include <climits>
#include <algorithm>
#include <cstdio>
#include <cfloat>
#include <cmath>

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.;
			double d = atof(value.c_str());
                        if (isnan(d) || (d > FLT_MAX))
                                return NAN;
			return (float)d;
		}

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

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

		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;
		}
	}
}