-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringUtils.c
525 lines (432 loc) · 15 KB
/
StringUtils.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
Provided by Dr. Michael Leverington NAU CS480 Fall2023
*/
#include "StringUtils.h"
/*
Name: compareString
Process: compares two strings with the following results:
if left string less than right string, returns less than zero
if left string greater than right string, returns greater than zero
if left string equals right string, returns zero
- equals test includes length
Function Input/Parameters: c-style left and right strings (char *)
Function Output/Parameters: none
Function Output/Returned: result as specified (int)
Device Input/device: none
Device Output/device: none
Dependencies: getStringLength
*/
int compareString( const char *oneStr, const char *otherStr )
{
// initialize funtion/variables
int diff, index = 0;
// loop to end of shortest string,
// with overrun protection
while( oneStr[ index ] != NULL_CHAR
&& otherStr[ index ] != NULL_CHAR
&& index < MAX_STR_LEN )
{
// get difference in characters
diff = oneStr[ index ] - otherStr[ index ];
// check for difference between characters
if( diff != 0 )
{
// return difference
return diff;
}
// increment index
index++;
}
// end loop
// return difference if lengths, if any
// function: getStringLength
return getStringLength( oneStr ) - getStringLength( otherStr );
}
/*
Name: concatenateString
Process: appends one string onto another
Function Input/Parameters: c-style source string (char *)
Function Output/Parameters: c-style destination string (char *)
Function Output/Returned: none
Device Input/device: none
Device Output/device: none
Dependencies: getStringLength
*/
void concatenateString( char *destStr, const char *sourceStr )
{
// initialize function/variables
// set destination index
// function: getStringLength
int destIndex = getStringLength( destStr );
// get source string length
// function: getStringLength
int sourceStrLen = getStringLength( sourceStr );
// create temporary string pointer
char *tempStr;
// create other variables
int sourceIndex = 0;
// copy source string in case of aliasings
// malloc, copyString
tempStr = (char *)malloc( sizeof( sourceStrLen + 1 ) );
copyString( tempStr, sourceStr );
// loop to end of source string
while( tempStr[ sourceIndex ] != NULL_CHAR && destIndex < MAX_STR_LEN )
{
// assign characters to end of destination string
destStr[ destIndex ] = tempStr[ sourceIndex ];
// update indices
destIndex++; sourceIndex++;
// set temporary end of destination string
destStr[ destIndex ] = NULL_CHAR;
}
// end loop
// release memory used for temp string
// function: free
free( tempStr );
}
/*
Name: copyString
Process: copies one string into another,
overwriting data in the destination string
Function Input/Parameters: c-style source string (char *)
Function Output/Parameters: c-style destination string (char *)
Function Output/Returned: none
Device Input/device: none
Device Output/device: none
Dependencies: getStringLength
*/
void copyString( char *destStr, const char *sourceStr )
{
// initialize functions/variables
int index = 0;
// check for source/destination not the same (aliasing)
if( destStr != sourceStr )
{
// loop to end of source string
while( sourceStr[ index ] != NULL_CHAR && index + 1 < MAX_STR_LEN )
{
// assign characters to end of destination string
destStr[ index ] = sourceStr[ index ];
// update index
index++;
// set temporary end of destination string
destStr[ index ] = NULL_CHAR;
}
// set end of destination string (case sourceStr is empty)
destStr[ index ] = NULL_CHAR;
}
// end loop
}
/*
Name: findSubString
Process: linear search for given substring within another string
Function Input/Parameters: c-style source test string (char *),
c-style source search string (char *)
Function Output/Parameters: none
Function Output/Returned: index fo found substring, or
SUBSTRING_NOT_FOUND constant
if string not found
Device Input/device: none
Device Output/device: none
Dependencies: getStringLength
*/
int findSubString( const char *testStr, const char *searchSubStr )
{
// initialize function/variables
// initialize test string length
// function: getStringLength
int testStrLen = getStringLength( testStr );
// initailize master index - location of sub string start point
int masterIndex = 0;
// initialize other variables
int searchIndex, internalIndex;
// loop across test string
while( masterIndex < testStrLen )
{
// set internal loop index to current test string index
internalIndex = masterIndex;
// set internal search index to zero
searchIndex = 0;
// loop to end of test string
// while test sttring/sub string characters are the same
while( internalIndex <= testStrLen
&& testStr[ internalIndex ] == searchSubStr[ searchIndex ] )
{
// increment test string, substring indices
internalIndex++; searchIndex++;
// check for end of substring (search completed)
if( searchSubStr[ searchIndex ] == NULL_CHAR )
{
// return beginning location of sub string
return masterIndex;
}
}
// end internal comparison loop
// increment current beginning location index
masterIndex++;
}
// end loop across test string
// assume test have failed at this point, return SUBSTRING_NOT_FOUND
return SUBSTRING_NOT_FOUND;
}
/*
Name: getStringConstrained
Process: captures a string from the input stream
with various constants
Function Input/Parameters: input stream (FILE *)
clears leading non printable (bool),
clears leading space (bool),
stops at non printable (bool),
stops at specified delimiter (char)
Note: consumes delimiter
Function Output/Parameters: string returned (char *)
Function Output/Returned: success of operation (bool)
Device Input/device: none
Device Output/device: none
Dependencies: fgetc
*/
bool getStringConstrained(
FILE *inStream,
bool clearLeadingNonPrintable,
bool clearLeadingSpace,
bool stopAtNonPrintable,
char delimiter,
char *capturedString
)
{
// initialize variables
int intChar = EOF, index = 0;
// initialize output string
capturedString[ index ] = NULL_CHAR;
// capture first value in stream
// function: fgetc
intChar = fgetc( inStream );
// loop to clear non printable or spac, if indicated
while( ( intChar != EOF )
&& ( ( clearLeadingNonPrintable && intChar < (int)SPACE )
|| ( clearLeadingSpace && intChar == (int)SPACE ) )
)
{
// get next character
// function: fgetc
intChar = fgetc( inStream );
}
// end clear non printable/space loop
// check for end of file found
if( intChar == EOF )
{
// return failed operation
return false;
}
// loop to capture input
while(
// continues if not at end of file and max string length not reached
( intChar != EOF && index < MAX_STR_LEN - 1 )
// AND
// continues if not printable flag set and characters are printable
// OR continues if not printable flag not set
&& ( ( stopAtNonPrintable && intChar >= (int)SPACE )
|| ( !stopAtNonPrintable ) )
// AND
// continues if specified delimiter is not found
&& ( intChar != (char)delimiter )
)
{
// place character in array element
capturedString[ index ] = (char)intChar;
// increment array index
index++;
// set next element to null character / end of c-string
capturedString[ index ] = NULL_CHAR;
// get next character as integer
// function: fgetc
intChar = fgetc( inStream );
}
// end loop
// return successful operation
return true;
}
/*
Name: getStringLength
Process: finds the length of a string
by counting characters up to the NULL_CHAR character
Function Input/Parameters: c-style string (char *)
Function Output/Parameters: none
Function Output/Returned: length of string
Device Input/device: none
Device Output/device: none
Dependencies: none
*/
int getStringLength( const char *testStr )
{
// initialize function/variables
int index = 0;
// loop to end of string, protect from overflow
while( index < STD_STR_LEN && testStr[ index ] != NULL_CHAR )
{
// update index
index++;
}
// end loop
// return index/length
return index;
}
/*
Name: getStringToDelimiter
Process: captures a string from the input stream
to a specified delimiter;
Note: consumes delimiter
Function Input/Parameters: input stream (FILE *)
stops at specified delimiter (char),
Function Output/Parameters: string returned (char *)
Function Output/Returned: success of operation (bool)
Device Input/device: none
Device Output/device: none
Dependencies: getStringConstrained(
*/
bool getStringToDelimiter(
FILE *inStream,
char delimiter,
char *capturedString
)
{
// call engine function with delimiter
// fucntion getStringConstrained
return getStringConstrained(
inStream, // file stream pointer
true, // clears leading non printable character
true, // bool clearLeadingSpace
true, // stops at non printable
delimiter, // stops at delimiter
capturedString // returns string
);
}
/*
Name: getStringToLineEnd
Process: captures a string from the input stream
to the end of the line
Function Input/Parameters: input stream (FILE *)
Function Output/Parameters: string returned (char *)
Function Output/Returned: success of operation (bool)
Device Input/Keyboard: none
Device Output/Monitor: none
Dependencies: getStringConstrained
*/
bool getStringToLineEnd(
FILE *inStream,
char *capturedString
)
{
// call engine function with delimiter
// fucntion getStringConstrained
return getStringConstrained(
inStream, // file stream pointer
true, // clears leading non printable character
true, // bool clearLeadingSpace
true, // stops at non printable
NON_PRINTABLE_CHAR, // non printable delimiter
capturedString // returns string
);
}
/*
Name: getSubString
Process: captures sub string within larger string
between two inclusive indices.
returns empty string if either index is out of range,
assumes enough memory in destination string
Function Input/Parameters: c-style source string (char *),
start and end indices (int)
Function Output/Parameters: c-style destination string (char *)
Function Output/Returned: none
Device Input/device: none
Device Output/device: none
Dependencies: getStringLength, malloc, copyString, free
*/
void getSubString( char *destStr, const char *sourceStr,
int startIndex, int endIndex )
{
// initialize function/variables
// set length of source string
// function: getStringLength
int sourceStrLen = getStringLength( sourceStr );
// initialize the destination index to zero
int destIndex = 0;
// intialize source index to start index (parameter)
int sourceIndex = startIndex;
// create pointer for temp string
char *tempStr;
// check for indices within limits
if( startIndex >= 0 && startIndex <= endIndex && endIndex < sourceStrLen )
{
// create temporary string
// function: malloc, copyString
tempStr = (char *)malloc( sourceStrLen + 1 );
copyString( tempStr, sourceStr );
// loop across requested substring (indices)
while( sourceIndex <= endIndex )
{
// assign source character to destination element
destStr[ destIndex ] = tempStr[ sourceIndex ];
// increment indices
destIndex++; sourceIndex++;
// set temporary end of destination string
destStr[ destIndex ] = NULL_CHAR;
}
// end loop
}
// return memory for temporary string
// fucntion: free
free( tempStr );
}
/*
Name: setStrToLowerCase
Process: iterates through string, sets any upper case letter
to lower case; no effect on other letters
Function Input/Parameters: c-style source string (char *)
Function Output/Parameters: c-style destination string (char *)
Function Output/Returned: none
Device Input/device: none
Device Output/device: none
Dependencies: toLowerCase
*/
void setStrToLowerCase( char *destStr, const char *sourceStr )
{
// initialize function/variables
// create other variables
int index = 0;
// loop across source string
while( sourceStr[ index ] != NULL_CHAR && index < MAX_STR_LEN )
{
// set individual character to lower case as needed,
// assign to destination string
destStr[ index ] = toLowerCase( sourceStr[ index ] );
// update index
index++;
// set temporary end of destination string
destStr[ index ] = NULL_CHAR;
}
// end loop
}
/*
Name: toLowerCase
Process: if character is upper case, sets it to lower case;
otherwise returns character unchanged
Function Input/Parameters: test character (char)
Function Output/Parameters: none
Function Output/Returned: character to set to lower case, if appropriate
Device Input/device: none
Device Output/device: none
Dependencies: none
*/
char toLowerCase( char testChar )
{
// check for upper case letter
if( testChar >= 'A' && testChar <= 'Z' )
{
// return lower case letter
return testChar - 'A' + 'a';
}
// otherwise, assume no upper case letter,
// return character unchanged
return testChar;
}