-
Notifications
You must be signed in to change notification settings - Fork 0
/
Value.cpp
88 lines (69 loc) · 1.78 KB
/
Value.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
// Value.cpp - SHilScript values
#include "Globals.h"
#include "Value.h"
#include <ctype.h>
_USING_SHILSCRIPT
// TODO: Recognize [whitespace] [digits] [.] [digits] [d D e E] [sign] [digits]
// TODO: This needs to be done ASAP. Or we need an altogether different way of
// TODO: handling number <-> text conversion. This really cripples SHilScript
// TODO: as far as high numbers.
void Value::CalcNumericality()
{
if(!m_strok)
CalcStrFromNum();
m_isnumok = true;
// If the string is empty, return that this is numerical
// (atof() will return zero anyway)
if(!m_str.size())
{
m_isnum = true;
return;
}
// Check if it's all digits plus at most one decimal
int decimal=0;
// For each character
for(int i=0; i<(signed)m_str.size(); i++)
{
// If it's a decimal
if(m_str[i] == '.')
{
// Increase the decimal count
decimal++;
// If the # of decimals is greater than 1
if(decimal > 1)
{
// This can't be a number
m_isnum = false;
return;
}
}
// Otherwise, if this character's not a digit
else if(!isdigit(m_str[i]))
{
// This can't be a number
m_isnum = false;
return;
}
}
// It has to be a number!
m_isnum = true;
}
void Value::CalcNumFromStr()
{
// If it's numerical, convert it to a floating-point
// value; otherwise return one. Note that IsNumerical()
// returns true if the string is empty and therefore
// the null string "" is equivalent to zero.
if(IsNumerical()) m_num = (Real)atof(m_str.c_str());
else m_num = 1.0f;
m_numok = true;
}
void Value::CalcStrFromNum()
{
// Convert from float to string
char buf[64];
sprintf(buf, "%g", (double)m_num);
// Save this string in the buffer
m_str = SSString(buf);
m_strok = true;
}