/
ConversionFunctions.c
290 lines (230 loc) · 7.48 KB
/
ConversionFunctions.c
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
/*
* BASIC support library: conversion functions.
*
* Copyright (c) 1998 Ryan C. Gordon and Gregory S. Read.
*/
#include <stdio.h>
#include <ctype.h>
#include "ConversionFunctions.h"
#define __round(STATEARGS, x) ((long) x + 0.5)
int vbiS_asc(STATEPARAMS, PBasicString pBasicStr)
/*
* Get the ASCII value of the first character of (str)...
*
* params : str == string where ASCII value is desired.
* returns : ASCII value of first char in string.
*/
{
unsigned int retVal = 0;
if (pBasicStr->length == 0) /* blank string? */
__runtimeError(STATEARGS, ERR_ILLEGAL_FUNCTION_CALL);
else
retVal = (unsigned int) pBasicStr->data[0];
return(retVal);
} /* vbiS_asc */
PBasicString vbSi_chr_DC_(STATEPARAMS, int asciiValue)
/*
* Returns a new basic string of one character length based on the ascii
* value passed.
*
* params : asciiValue == ascii code to convert to a string.
* returns : one char string of ascii value.
*/
{
PBasicString retVal = NULL;
if ((asciiValue < 0) || (asciiValue > 255))
__runtimeError(STATEARGS, ERR_ILLEGAL_FUNCTION_CALL);
else
{
retVal = __allocString(STATEARGS, 1, false);
retVal->data[0] = (char) asciiValue;
} /* else */
return(retVal);
} /* vbSi_chr_DC_ */
PBasicString vbSd_str_DC_(STATEPARAMS, double numeric)
/*
* Convert a numeric to a BASIC string. If (numeric) is positive, the
* string contains a leading blank (' ') character. If negative, that
* blank character will be a '-' char. Decimal points and places are only
* placed in the string if needed. For example, str$(1.0) will return " 1".
*
* params : numeric == number to convert to a BASIC string.
* returns : newly allocated BASIC string.
*/
{
#ifdef BROKEN
PBasicString retVal;
int allocated = 10;
int decimalPlaces = 0;
char *buffer = __memAlloc(STATEARGS, allocated);
double i;
int index = 1;
int number;
if (numeric < 0.0) /* negative? */
{
buffer[0] = '-';
numeric = -numeric; /* make number positive. */
} /* if */
else
buffer[0] = ' ';
for (i = 1.0; i < numeric; i *= 10.0, decimalPlaces--);
for (i /= 10.0; numeric != 0.0; i /= 10.0)
{
if (i == 0.1) /* decimal places? */
{
buffer[index] = '.'; /* add decimal point to string. */
index++;
} /* if */
number = (int) (numeric / i);
numeric -= (number * i);
buffer[index] = ('0' + number);
index++;
decimalPlaces++;
if (index + 1 >= allocated)
{
allocated += 10;
buffer = __memRealloc(STATEARGS, buffer, allocated);
} /* if */
} /* for */
buffer[index] = '\0'; /* null-terminate string for conversion. */
retVal = __createString(STATEARGS, buffer, false);
__memFree(STATEARGS, buffer);
return(retVal);
#else
#warning "str_DC_() is still a kludge!"
/* !!! fix this. */
char buffer[100];
sprintf(buffer, "%f", numeric);
return(__createString(STATEARGS, buffer, false));
#endif
} /* vbSd_str_DC_ */
int __valEndOfNumberString(STATEPARAMS, PBasicString pBasicStr)
/*
* Used by val() to find index in string where number ends.
*
* params : pBasicStr == BASIC string to convert.
* returns : index in string where number to convert ends.
*/
{
boolean pastFirst = false; /* so extra '-' chars don't sneak in. */
boolean foundDecimal = false; /* so extra '.' chars don't sneak in. */
boolean foundEnd = false; /* loop control variable. */
int max = pBasicStr->length; /* loop control variable. */
int i; /* loop control variable. */
char *str = pBasicStr->data; /* the actual string data. */
int ch; /* for individual chars in string. */
for (i = 0; (i < max) && (!foundEnd); i++)
{
ch = (int) str[i];
if (!isdigit(ch))
{
switch (ch)
{
case ' ': /* whitespace that val() ignores. */
case '\t':
case '\n':
break;
case '-': /* negative number? */
if (pastFirst)
foundEnd = true;
else
pastFirst = true;
break;
case '.': /* decimal place? */
if (foundDecimal)
foundEnd = true;
else
foundDecimal = true;
pastFirst = true;
break;
default: /* ...everything else. */
pastFirst = foundEnd = true;
break;
} /* switch */
} /* if */
else
pastFirst = true;
} /* for */
return(i - 1); /* minus one since for-loop will bump us past end... */
} /* __valEndOfNumberString */
double vbdS_val(STATEPARAMS, PBasicString pBasicStr)
/*
* Convert a BASIC string into a numeric. This function stops conversion
* at the end of the string, or when it runs into a character it can't
* recognize as numeric. Spaces (' '), tabs ('\t') and linefeeds ('\n') are
* ignored. So "1615 198th Street" would return (1615198).
*
* params : pBasicStr == pointer to BASIC string to convert. ("") accepted.
* returns : see above.
*/
{
#warning "val() still isn't right!"
double retVal = 0.0;
int strEndIndex = __valEndOfNumberString(STATEARGS, pBasicStr);
int decimalPlace = 1;
int ch;
int i;
for (i = strEndIndex; i >= 0; i--)
{
ch = (int) pBasicStr->data[i];
if (isdigit(ch))
{
retVal += ((ch - '0') * decimalPlace);
decimalPlace *= 10;
} /* if */
else
{
switch (ch)
{
case ' ': /* ignore these. */
case '\t':
case '\n':
break;
case '-':
retVal = -retVal;
break;
case '.':
retVal /= (double) decimalPlace;
decimalPlace = 1;
break;
} /* switch */
} /* else */
} /* for */
return(retVal);
} /* vbdS_val */
PBasicString vbSl_hex_DC_(STATEPARAMS, long x)
/*
* Convert a numeric to a string in Hexadecimal format. Numeric is rounded
* as necessary.
*
* params : x == numeric to convert.
* returns : newly allocated BASIC string with hex version of (x).
*/
{
unsigned int rounded;
/* !!! this is a kludge. Really write this... */
char buffer[20];
rounded = __round(STATEARGS, x);
sprintf(buffer, "%X", rounded);
return(__createString(STATEARGS, buffer, false));
} /* vbSd_hex_DC_ */
PBasicString vbSl_oct_DC_(STATEPARAMS, long x)
/*
* Convert a numeric to a string in Octal format. Numeric is rounded
* as necessary.
*
* params : x == numeric to convert.
* returns : newly allocated BASIC string with octal version of (x).
*/
{
unsigned int rounded;
/* !!! this is a kludge. Really write this... */
char buffer[20];
rounded = __round(STATEARGS, x);
sprintf(buffer, "%o", rounded);
return(__createString(STATEARGS, buffer, false));
} /* vbSd_oct_DC_ */
/* !!! still need: */
/* cvi, cvl, etc... mkd$, mkl$, etc... */
/* swap? */
/* end of ConversionFunctions.c ... */