aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/StringConversions.cpp
blob: 3a46820bf857c31fc4439951d3ae3b59a7345ae8 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1999-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
\* ===================================================================== */

#include "EmCommon.h"
#include "StringConversions.h"

#include "EmDirRef.h"			// EmDirRef
#include "EmFileRef.h"			// EmFileRef
#include "EmTransport.h"		// EmTransportDescriptor
#include "Platform.h"			// _stricmp

#include <ctype.h>				// isprint
#include <stdio.h>				// sscanf, sprintf

// Handy macro for performing some common unsigned string compares.

#define CHECK_STRING(s1, s2, v) 		\
	if (_stricmp (s1.c_str(), s2) == 0) \
	{									\
		value = v;						\
		return true;					\
	}



// We convert a lot of numbers into strings; this value is the
// minimum buffer size to hold the converted string.

const int kMaxIntStringSize = 12;	// sign + 4xxxyyyzzz + NULL




// ----------------------------------------------------------------------
//	Our preferences routines need to convert our preference data to and
//	from string format (the format in which the data is stored on disk).
//	Following are a bunch of overloaded routines for converting strings
//	into all the data types we use for preferences.
// ----------------------------------------------------------------------

bool FromString (const string& s, bool& value)
{
	if (s == "0" || _stricmp (s.c_str (), "false") == 0)
		value = false;
	else
		value = true;

	return true;
}


bool FromString (const string& s, char& value)
{
	if (s.size () == 1 && isprint (s[0]))
	{
		value = s[0];
		return true;
	}

	return false;
}


bool FromString (const string& s, signed char& value)
{
	short	temp;
	int result = sscanf (s.c_str (), "%hd", &temp);
	if (result == 1)
		value = (signed char) temp;
	return result == 1;
}


bool FromString (const string& s, unsigned char& value)
{
	unsigned short	temp;
	int result = sscanf (s.c_str (), "%hu", &temp);
	if (result == 1)
		value = (unsigned char) temp;
	return result == 1;
}


bool FromString (const string& s, signed short& value)
{
	int result = sscanf (s.c_str (), "%hd", &value);
	return result == 1;
}


bool FromString (const string& s, unsigned short& value)
{
	int result = sscanf (s.c_str (), "%hu", &value);
	return result == 1;
}


bool FromString (const string& s, signed int& value)
{
	int result = sscanf (s.c_str (), "%d", &value);
	return result == 1;
}


bool FromString (const string& s, unsigned int& value)
{
	int result = sscanf (s.c_str (), "%u", &value);
	return result == 1;
}


bool FromString (const string& s, signed long& value)
{
	int result = sscanf (s.c_str (), "%ld", &value);
	return result == 1;
}


bool FromString (const string& s, unsigned long& value)
{
	int result = sscanf (s.c_str (), "%lu", &value);
	return result == 1;
}


bool FromString (const string& s, string& value)
{
	value = s;
	return true;
}


bool FromString (const string& s, char* value)
{
	strcpy (value, s.c_str ());
	return true;
}


/*
	// Seems to conflict with standard scalar types
bool FromString (const string& s, Bool& value)
{
	bool	temp;
	if (::FromString (s, temp))
	{
		value = (Bool) temp;
		return true;
	}
	
	return false;
}
*/


bool FromString (const string& s, CloseActionType& value)
{
	CHECK_STRING (s, "SaveAlways", kSaveAlways)
	CHECK_STRING (s, "SaveAsk", kSaveAsk)
	CHECK_STRING (s, "SaveNever", kSaveNever)

	return false;
}


bool FromString (const string& s, EmDevice& value)
{
	value = EmDevice (s);

	return value.Supported () != 0;
}


bool FromString (const string& s, EmDirRef& value)
{
	return value.FromPrefString (s);
}


bool FromString (const string& s, EmErrorHandlingOption& value)
{
	CHECK_STRING (s, "ShowDialog",	kShow)
	CHECK_STRING (s, "Continue",	kContinue)
	CHECK_STRING (s, "Quit",		kQuit)
	CHECK_STRING (s, "NextGremlin",	kSwitch)

	return false;
}


bool FromString (const string& s, EmFileRef& value)
{
	return value.FromPrefString (s);
}


bool FromString (const string& s, EmTransportDescriptor& value)
{
	value = EmTransportDescriptor (s);
	return true;
}


// ----------------------------------------------------------------------
//	Our preferences routine need to convert our preference data to and
//	from string format (the format in which the data is stored on disk).
//	Following are a bunch of overloaded routines for converting data
//	(in all the types we use for preferences) into strings.
// ----------------------------------------------------------------------

string ToString (bool value)
{
	if (value)
		return "1";
	return "0";
}


string ToString (char value)
{
	return string (1, value);
}


string ToString (signed char value)
{
	char	buffer[kMaxIntStringSize];
	sprintf (buffer, "%hd", (short) value);
	return buffer;
}


string ToString (unsigned char value)
{
	char	buffer[kMaxIntStringSize];
	sprintf (buffer, "%hu", (unsigned short) value);
	return buffer;
}


string ToString (signed short value)
{
	char	buffer[kMaxIntStringSize];
	sprintf (buffer, "%hd", value);
	return buffer;
}


string ToString (unsigned short value)
{
	char	buffer[kMaxIntStringSize];
	sprintf (buffer, "%hu", value);
	return buffer;
}


string ToString (signed int value)
{
	char	buffer[kMaxIntStringSize];
	sprintf (buffer, "%d", value);
	return buffer;
}


string ToString (unsigned int value)
{
	char	buffer[kMaxIntStringSize];
	sprintf (buffer, "%u", value);
	return buffer;
}


string ToString (signed long value)
{
	char	buffer[kMaxIntStringSize];
	sprintf (buffer, "%ld", value);
	return buffer;
}


string ToString (unsigned long value)
{
	char	buffer[kMaxIntStringSize];
	sprintf (buffer, "%lu", value);
	return buffer;
}


string ToString (const string& value)
{
	return value;
}


string ToString (const char* value)
{
	return string (value);
}


/*

 // Seems to conflict with standard scalar types
string ToString (Bool value)
{
	return ::ToString ((bool) value);
}

*/


string ToString (CloseActionType value)
{
	switch (value)
	{
		case kSaveAlways:		return "SaveAlways";
		case kSaveAsk:			return "SaveAsk";
		case kSaveNever:		return "SaveNever";
	}

	return "";
}


string ToString (const EmDevice& value)
{
	return value.GetIDString ();
}


string ToString (const EmDirRef& value)
{
	return value.ToPrefString ();
}


string ToString (EmErrorHandlingOption value)
{
	switch (value)
	{
		case kShow:			return "ShowDialog";
		case kContinue:		return "Continue";
		case kQuit:			return "Quit";
		case kSwitch:		return "NextGremlin";
	}

	return "";
}


string ToString (const EmFileRef& value)
{
	return value.ToPrefString ();
}


string ToString (const EmTransportDescriptor& value)
{
	return value.GetDescriptor ();
}