-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstrfuncs.c
357 lines (262 loc) · 8.77 KB
/
strfuncs.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
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
/*
This source file is part of the TRS-8051 project. BAS51 is a work alike
rewrite of the Microsoft Level 2 BASIC for the TRS-80 that is intended to run
on an 8051 microprocessor. This version is in intended to also run under
Windows for testing purposes.
Copyright (C) 2017 Dennis Hawkins
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
If you use this code in any way, I would love to hear from you. My email
address is: dennis@galliform.com
*/
/*
STRFUNCS.C - Various internal BASIC string manipulation functions.
*/
#include "Bas51.h"
// Return the string length.
// Strings can be 0 to 255 chars.
BYTE StringLen(BYTE ArgNum)
{
// return length
if (uValArg[ArgNum].sVal.sPtr) return(uValArg[ArgNum].sVal.sLen);
else return(0);
}
// Test and/or correct string bounds of argnum.
// Return TRUE if uncorrectable.
BIT StringBounds(BYTE ArgNum)
{
if (uValArg[ArgNum].LVal < 0) uValArg[ArgNum].LVal = 0;
if (uValArg[ArgNum].LVal > 255) // uncorrectable error
{
SyntaxErrorCode = ERROR_STRING_LENGTH;
return(TRUE);
}
return(FALSE);
}
// Compare two strings
// Return 0 if equal.
// Return -1 if arg0 is less than arg1.
// Return 1 if arg0 is greater than arg1.
// Shorter strings are less than the same but longer string ("we" < "weed").
// Compare is not case sensitive.
signed char StringCmp(void)
{
BYTE Len1, Len2, B1, B2;
// get string lengths
Len1 = StringLen(0);
Len2 = StringLen(1);
// special case for null strings
if (Len1 == 0 && Len2 == 0) return(0); // two null strings are equal
if (Len1 == 0) return(-1); // 1st arg null
if (Len2 == 0) return(1); // 2nd arg null
// Niether string is null iof here
while (Len1 && Len2)
{
B1 = (BYTE) toupper(ReadRandom51(uValArg[0].sVal.sPtr++));
B2 = (BYTE) toupper(ReadRandom51(uValArg[1].sVal.sPtr++));
if (B1 < B2) return(-1);
if (B1 > B2) return(1);
Len1--;
Len2--;
}
// Check for equal
if (Len1 == Len2) return(0); // both are the same
// If here, one string is the same but shorter.
return((signed char)((Len1 < Len2) ? -1 : 1));
}
// Concat Arg[0] and Arg[1]
// One of the args might not be a string.
void ConcatStr(void)
{
BYTE Len1, Len2;
WORD LenT, StrPtr;
// Calculate lengths of strings
Len1 = StringLen(0);
Len2 = StringLen(1);
LenT = (WORD)((WORD) Len1 + Len2); // get total length
if (LenT > 255)
{
SyntaxErrorCode = ERROR_STRING_LENGTH;
return;
}
// initialize combined string
uData.LVal = 0;
if (LenT == 0) return;
// allocate new space
StrPtr = TempAlloc((BYTE)LenT);
if (!StrPtr) return; // Not enough memory - error code already set
uData.sVal.sLen = LenT;
uData.sVal.sPtr = StrPtr;
// copy in the strings
MemMove51(StrPtr, uValArg[0].sVal.sPtr, Len1);
MemMove51((WORD)(StrPtr + Len1), uValArg[1].sVal.sPtr, Len2);
}
// INSTR(BigStr$, LittleStr$) - Searches for the first occurence of LittleStr$
// in BigStr$ and returns the position where the match was found.
// Returns 0 if not found. Returns 1 if LittleStr$ is empty.
// Arg[0] = BigStr$, Arg[1] = LittleStr.
BYTE InstrFunction(void)
{
BYTE LenBig, LenSub;
BYTE BigIdx, SubIdx;
WORD BigStr, SubStr;
LenBig = StringLen(0);
LenSub = StringLen(1);
if (LenBig == 0 || LenBig < LenSub) return(0); // Can't be found
if (LenSub == 0) return(1); // null small string
BigStr = (WORD)uValArg[0].sVal.sPtr;
SubStr = (WORD)uValArg[1].sVal.sPtr;
for (BigIdx = 0; BigIdx < LenBig; BigIdx++)
{
for (SubIdx = 0; SubIdx < LenSub; SubIdx++)
{
BYTE B, S;
B = ReadRandom51((WORD)(BigStr + BigIdx + SubIdx));
S = ReadRandom51((WORD)(SubStr + SubIdx));
if (toupper(B) != toupper(S)) break;
}
if (SubIdx == LenSub) // Did we find it?
return((BYTE)(BigIdx + 1)); // YES.
}
return(0); // Not found
}
// Returns a Float converted from a string in Arg[0].
void ValFunction(void)
{
BYTE Len;
uData.fVal = 0;
Len = StringLen(0);
if (Len > 0)
{
// Copy string from main memory to XDATA memory
ReadBlock51(TokBuf, (WORD)uValArg[0].sVal.sPtr, Len);
TokBuf[Len] = 0; // convert to C-string
uData.fVal = strtod((char *) TokBuf, NULL);
}
}
// Mid$ function
// Also called by Left$ and Right$
void MidsFunction(void)
{
BYTE LenBig;
BYTE Start, Len;
uData.LVal = 0; // initialize return string
if (StringBounds(1) || StringBounds(2)) return;
Start = (BYTE) uValArg[1].LVal;
Len = (BYTE) uValArg[2].LVal;
LenBig = StringLen(0);
// Null string or Starts after it ends
if (Len == 0 || LenBig == 0 || Start == 0 || Start > LenBig) return;
// Normalize start
Start--;
// Correct length if they ask for more than we have
if (Start + Len > LenBig) Len = (BYTE)(LenBig - Start);
// allocate new space
uData.sVal.sPtr = TempAlloc(Len);
if (!uData.sVal.sPtr) return; // Not enough memory - error code already set
uData.sVal.sLen = Len;
// Copy substring over
MemMove51(uData.sVal.sPtr, (WORD)(uValArg[0].sVal.sPtr + Start), Len);
}
void LeftFunction(void)
{
uValArg[2].LVal = uValArg[1].LVal; // Length
uValArg[1].LVal = 1; // Start
MidsFunction(); // Start at 1 to length
}
void RightFunction(void)
{
BYTE LenStr;
BYTE ReqLen;
uData.LVal = 0; // initialize return string
if (StringBounds(1)) return;
uValArg[2].LVal = ReqLen = (BYTE) uValArg[1].LVal; // length
LenStr = StringLen(0); // actual length of input string
if (ReqLen > LenStr) ReqLen = LenStr;
uValArg[1].LVal = LenStr - ReqLen + 1;
MidsFunction(); // Start at pos to end
}
// Do ASC BASIC function
// Return ASCII code of first char of string in Arg[0]
// Return value in uData
void AscFunction(void)
{
if (StringLen(0))
uData.LVal = ReadRandom51((WORD)(uValArg[0].sVal.sPtr));
else uData.LVal = 0;
}
// Returns a 1-char string defined by code.
void ChrFunction(void)
{
uData.LVal = 0; // null string
uData.sVal.sPtr = TempAlloc(1); // Alloc temp string space
if (uData.sVal.sPtr == NULL) return;
WriteRandom51(uData.sVal.sPtr, (BYTE)uValArg[0].LVal);
uData.sVal.sLen = 1;
}
// Converts Float into string.
void StrFunction(void)
{
BYTE Len;
uData.LVal = 0; // null string
Len = (BYTE) sprintf((char *) TokBuf, "%G", uValArg[0].fVal);
uData.sVal.sPtr = TempAlloc(Len);
if (uData.sVal.sPtr == NULL) return;
uData.sVal.sLen = Len;
WriteBlock51(uData.sVal.sPtr, TokBuf, Len);
}
// Returns a string of Arg1[0] characters with a length of Arg0.
// Arg0 = Length, Arg1 = String, Return Temp String in uData
void StringFunction(void)
{
BYTE Len, FirstChar;
uData.LVal = 0; // initialize to null string
if (StringBounds(0)) return;
Len = (BYTE)uValArg[0].LVal;
if (Len != 0 && StringLen(1) != 0)
{
FirstChar = ReadRandom51(uValArg[1].sVal.sPtr); // Get 1st char
uData.sVal.sPtr = TempAlloc(Len);
if (uData.sVal.sPtr == NULL) return;
uData.sVal.sLen = Len;
while (Len--)
{
WriteRandom51((WORD)(uData.sVal.sPtr + Len), FirstChar);
}
}
}
// Return last char in keyboard buffer or a null string if not there.
void InkeyFunction(void)
{
uData.LVal = 0; // initialize to NULL string
if (KeyAvailable())
{
WORD key;
BYTE B;
key = GetKeyWord();
uData.sVal.sPtr = TempAlloc(2); // Get 2 temp bytes
if (uData.sVal.sPtr == NULL) return;
B = (BYTE)(key & 0xFF);
if (B != 0)
{
WriteRandom51(uData.sVal.sPtr, B);
uData.sVal.sLen = 1;
}
else // extended char
{
WriteRandom51(uData.sVal.sPtr, 'X');
B = (BYTE)(key >> 8); // upper byte
WriteRandom51((WORD)(uData.sVal.sPtr + 1), B);
uData.sVal.sLen = 2;
}
}
}