scalar.cpp 3.37 KB
Newer Older
1
2
3
4
5
6
7
#include "crt.h"
#include "scalar.h"
#include "scanner.h"
#include "token.h"
#include "exceptions.h"
#include "node.h"
#include <sstream>
8
#include <algorithm>
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

namespace YAML
{
	Scalar::Scalar()
	{
	}

	Scalar::~Scalar()
	{
	}

	void Scalar::Parse(Scanner *pScanner, const ParserState& state)
	{
		Token& token = pScanner->peek();
		m_data = token.value;
		pScanner->pop();
	}

	void Scalar::Write(std::ostream& out, int indent, bool startedLine, bool onlyOneCharOnLine)
	{
		out << "\"";
		for(unsigned i=0;i<m_data.size();i++) {
			switch(m_data[i]) {
				case '\\': out << "\\\\"; break;
				case '\t': out << "\\t"; break;
				case '\n': out << "\\n"; break;
				case '\r': out << "\\r"; break;
				default: out << m_data[i]; break;
			}
		}
		out << "\"\n";
	}

42
	bool Scalar::Read(std::string& s) const
43
44
	{
		s = m_data;
45
		return true;
46
47
	}

48
	bool Scalar::Read(int& i) const
49
50
51
	{
		std::stringstream data(m_data);
		data >> i;
52
		return !data.fail();
53
54
	}

55
	bool Scalar::Read(unsigned& u) const
56
57
58
	{
		std::stringstream data(m_data);
		data >> u;
59
		return !data.fail();
60
61
	}

62
	bool Scalar::Read(long& l) const
63
64
65
	{
		std::stringstream data(m_data);
		data >> l;
66
		return !data.fail();
67
68
	}
	
69
	bool Scalar::Read(float& f) const
70
71
72
	{
		std::stringstream data(m_data);
		data >> f;
73
		return !data.fail();
74
75
	}
	
76
	bool Scalar::Read(double& d) const
77
78
79
	{
		std::stringstream data(m_data);
		data >> d;
80
		return !data.fail();
81
82
	}

83
	bool Scalar::Read(char& c) const
84
85
86
	{
		std::stringstream data(m_data);
		data >> c;
87
		return !data.fail();
88
89
	}

90
91
92
93
94
95
96
97
98
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
164
	namespace
	{
		// we're not gonna mess with the mess that is all the isupper/etc. functions
		bool IsLower(char ch) { return 'a' <= ch && ch <= 'z'; }
		bool IsUpper(char ch) { return 'A' <= ch && ch <= 'Z'; }
		char ToLower(char ch) { return IsUpper(ch) ? ch + 'a' - 'A' : ch; }

		std::string tolower(const std::string& str)
		{
			std::string s(str);
			std::transform(s.begin(), s.end(), s.begin(), ToLower);
			return s;
		}

		template <typename T>
		bool IsEntirely(const std::string& str, T func)
		{
			for(unsigned i=0;i<str.size();i++)
				if(!func(str[i]))
					return false;

			return true;
		}

		// IsFlexibleCase
		// . Returns true if 'str' is:
		//   . UPPERCASE
		//   . lowercase
		//   . Capitalized
		bool IsFlexibleCase(const std::string& str)
		{
			if(str.empty())
				return true;

			if(IsEntirely(str, IsLower))
				return true;

			bool firstcaps = IsUpper(str[0]);
			std::string rest = str.substr(1);
			return firstcaps && (IsEntirely(rest, IsLower) || IsEntirely(rest, IsUpper));
		}
	}

	bool Scalar::Read(bool& b) const
	{
		// we can't use iostream bool extraction operators as they don't
		// recognize all possible values in the table below (taken from
		// http://yaml.org/type/bool.html)
		static const struct {
			std::string truename, falsename;
		} names[] = {
			{ "y", "n" },
			{ "yes", "no" },
			{ "true", "false" },
			{ "on", "off" },
		};

		if(!IsFlexibleCase(m_data))
			return false;

		for(unsigned i=0;i<sizeof(names)/sizeof(names[0]);i++) {
			if(names[i].truename == tolower(m_data)) {
				b = true;
				return true;
			}

			if(names[i].falsename == tolower(m_data)) {
				b = false;
				return true;
			}
		}

		return false;
	}

165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
	int Scalar::Compare(Content *pContent)
	{
		return -pContent->Compare(this);
	}

	int Scalar::Compare(Scalar *pScalar)
	{
		if(m_data < pScalar->m_data)
			return -1;
		else if(m_data > pScalar->m_data)
			return 1;
		else
			return 0;
	}
}