summaryrefslogtreecommitdiff
path: root/orkbasecxx/serializers/SingleLineSerializer.cpp
blob: f60156d8d4885b9f693217f1b7e1a9abcf053add (plain)
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
 * Oreka -- A media capture and retrieval platform
 * 
 * Copyright (C) 2005, orecx LLC
 *
 * http://www.orecx.com
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License.
 * Please refer to http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "ace/OS_NS_ctype.h"
#include "Utils.h"
#include "SingleLineSerializer.h"


void SingleLineSerializer::AddString(const char* key, CStdString& value)
{
	CStdString pair;
	CStdString escapedValue;
	EscapeSingleLine(value, escapedValue);
	pair.Format("%s=%s ", key, (PCSTR)escapedValue);
	m_output += pair;
}

CStdString SingleLineSerializer::Serialize()
{
	m_deSerialize = false;		// Set Serialize mode
	m_object->Define(this);
	return m_output;
}

typedef enum {SingleLineStartState, SingleLineKeyState, SingleLineValueState, SingleLineErrorState} SingleLineState;

void SingleLineSerializer::DeSerialize(CStdString& input)
{
	// read string and extract values into map
	SingleLineState state = SingleLineStartState;
	CStdString key;
	CStdString value;
	// #### Additionally, errorDescription should be converted to Exceptions
	CStdString errorDescription;

	input.Trim();

	for(unsigned int i=0; i<input.length() && state!= SingleLineErrorState; i++)
	{
		TCHAR character = input[i];

		switch(state)
		{
		case SingleLineStartState:
			if(character == ' ')
			{
				;	// ignore spaces
			}
			else if ( ACE_OS::ace_isalnum(character) )
			{
				state = SingleLineKeyState;
				key = character;
			}
			else
			{
				state = SingleLineErrorState;
				errorDescription = "Cannot find key start, keys must be alphanum";
			}
			break;
		case SingleLineKeyState:
			if( ACE_OS::ace_isalnum(character) )
			{
				key += character;
			}
			else if (character == '=')
			{
				state = SingleLineValueState;
				value.Empty();
			}
			else
			{
				state = SingleLineErrorState;
				errorDescription = "Invalid key character, keys must be alphanum";
			}
			break;
		case SingleLineValueState:
			if( character == '=')
			{
				state = SingleLineErrorState;
				errorDescription = "Value followed by = sign, value should always be followed by space sign";				
			}
			else if (character == ' ')
			{
				state = SingleLineStartState;
			}
			else
			{
				value += character;
			}
			break;
		default:
			state = SingleLineErrorState;
			errorDescription = "Non-existing state";
		}	// switch(state)

		if ( (state == SingleLineStartState) || (i == (input.length()-1)) )
		{
			if (!key.IsEmpty())
			{
				// SingleLine unescape
				CStdString unescapedValue;
				UnEscapeSingleLine(value, unescapedValue);

				// Add pair to key-value map
				m_map.insert(std::make_pair(key, unescapedValue));
				key.Empty();
				value.Empty();
			}
		}
	}	// for(int i=0; i<input.length() && state!= SingleLineErrorState; i++)

	Serializer::DeSerialize();
}

// Escape the space, equals and percent characters for serializing to Key-Value-Pair text
void SingleLineSerializer::EscapeSingleLine(CStdString& in, CStdString& out)
{
	for(unsigned int i=0; i<in.length();i++)
	{
		TCHAR c = in[i];
		if (c == ' ')
		{
			out+= "%s";
		}
		else if (c == '%')
		{
			out+= "%p";
		}
		else if (c == '=')
		{
			out+= "%e";
		}
		else
		{
			out+= c;
		}
	}
}

// Unescape the space, equals and percent characters for serializing to Key-Value-Pair text
void SingleLineSerializer::UnEscapeSingleLine(CStdString& in, CStdString& out)
{
	unsigned int iin = 0;

	while(iin<in.length())
	{ 
		if ( in[iin] == '%')
		{
			iin++;

			switch (in[iin])
			{
			case 's':
				out += ' ';
				break;
			case 'p':
				out += '%';
				break;
			case 'e':
				out += '=';
				break;
			}
		}
		else
		{
			out += in[iin];
		}
		iin++;
	}
}

CStdString SingleLineSerializer::FindClass(CStdString& input)
{
	CStdString result;
	int equalsPostion = input.Find('=');
	if (equalsPostion != -1)
	{
		int spacePostion = input.Find(' ');
		if (spacePostion > equalsPostion)
		{
			result = input.Mid(equalsPostion+1, spacePostion-equalsPostion-1);
		}
		else
		{
			result = input.Mid(equalsPostion+1, input.GetLength() - equalsPostion - 1);
		}
	}
	result.ToLower();
	return result;
}