-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvalue.cpp
More file actions
92 lines (89 loc) · 2.7 KB
/
cvalue.cpp
File metadata and controls
92 lines (89 loc) · 2.7 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "cvalue.h"
//-------------------------------------------------------------------------------------------------
CValue::CValue(const char *str)
{
_buf = strdup(str);
}
//-------------------------------------------------------------------------------------------------
CValue::CValue(char *str, size_t len)
{
_buf = (char*) malloc(len + 1);
memcpy(_buf, str, len);
_buf[len] = '\0';
}
//-------------------------------------------------------------------------------------------------
CValue::~CValue()
{
if(_buf) { free(_buf); _buf = NULL; }
if(_form_buf) { free(_form_buf); _form_buf = NULL; }
}
//-------------------------------------------------------------------------------------------------
CValue::CValue(const CValue& other)
{
_buf = strdup(other._buf);
}
//-------------------------------------------------------------------------------------------------
CValue& CValue::operator=(const CValue& rhs)
{
if (this == &rhs) return *this; // handle self assignment
//assignment operator
return *this;
}
//-------------------------------------------------------------------------------------------------
char* CValue::text()
{
return _buf;
}
//-------------------------------------------------------------------------------------------------
const char* CValue::format(const char *fmt, int roundto)
{
if(_form_buf)
{
free(_form_buf);
_form_buf = NULL;
}
if(strchr(fmt, 'd'))
{
// integer output
int vali = strtol(_buf, NULL, 10);
int len = snprintf(NULL, 0, fmt, vali);
if(len > 0)
{
_form_buf = (char*) malloc(len + 1);
sprintf(_form_buf, fmt, vali);
}
}
if(strchr(fmt, 'f'))
{
// real output
double valf = to_double(roundto);
int len = snprintf(NULL, 0, fmt, valf);
if(len > 0)
{
_form_buf = (char*) malloc(len + 1);
sprintf(_form_buf, fmt, valf);
}
}
return _form_buf;
}
//-------------------------------------------------------------------------------------------------
double CValue::to_double()
{
return strtod(_buf, NULL);
}
//-------------------------------------------------------------------------------------------------
double CValue::to_double(int round_digits)
{
double p = pow(10.0, round_digits);
return round(to_double() * p) / p;
}
//-------------------------------------------------------------------------------------------------
size_t CValue::get_length()
{
return strlen(_buf);
}
//-------------------------------------------------------------------------------------------------