-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdl_chr.cpp
697 lines (580 loc) · 23 KB
/
dl_chr.cpp
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*===========================================================================
*
* File: DL_Chr.CPP
* Author: Dave Humphrey ([email protected])
* Created On: Sunday, May 06, 2001
*
* Implementation of character related functions for Dave's Library of
* common code.
*
* Version History
* -------------------------------------------------------------------------
* 2 December 2002 (Dave Humphrey)
* - Moved from regular char to TCHAR type to support wide characters
* under Windows. Successfully tested.
*
*=========================================================================*/
/* Include Files */
#include "dl_chr.h"
#include <ctype.h>
/*===========================================================================
*
* Begin Local Variable Definitions
*
*=========================================================================*/
DEFINE_FILE("DL_Chr.h");
/*===========================================================================
* End of Local Variable Definitions
*=========================================================================*/
/*===========================================================================
*
* Function - void chradd (pString, CharIndex, NewChar);
*
* Attempts to insert the specified character into the string at the
* given position CharIndex. First character in string is at position 0.
* Assumes the string is long enough to add another character. ASSERTs
* if any invalid input is received.
*
*=========================================================================*/
void chradd (TCHAR* pString, const size_t CharIndex, const TCHAR NewChar) {
DEFINE_FUNCTION("chradd()");
TCHAR* pInsertPosition;
size_t StringLength;
/* Ensure valid input */
ASSERT(pString != NULL);
StringLength = TSTRLEN(pString);
ASSERT(CharIndex <= StringLength);
/* Shift the characters after insert position right by one */
pInsertPosition = pString + CharIndex;
memmove (pInsertPosition + 1, pInsertPosition, (StringLength - CharIndex + 1)*sizeof(TCHAR));
/* Add the new character to the string */
*pInsertPosition = NewChar;
}
/*===========================================================================
* End of Function chradd()
*=========================================================================*/
/*===========================================================================
*
* Function - void chrcat (TCHAR* pString, const TCHAR NewChar);
*
* Attempts to add a character to the end of the given string. Assumes
* that the string is long enough to add another character. ASSERTs if
* any bad input is received.
*
*=========================================================================*/
void chrcat (TCHAR* pString, const TCHAR NewChar) {
DEFINE_FUNCTION("chrcat()");
TCHAR* pInsertPosition;
/* Ensure valid input */
ASSERT(pString != NULL);
/* Find the end of the string */
pInsertPosition = pString + TSTRLEN(pString);
/* Add the character to end of string, making sure we NULL terminate */
pInsertPosition[1] = NULL_CHAR;
pInsertPosition[0] = NewChar;
}
/*===========================================================================
* End of Function chrcat()
*=========================================================================*/
/*===========================================================================
*
* Function - int chrcount (pString, Char);
*
* Counts the number of characters in the given string. ASSERTs if given
* invalid input.
*
*=========================================================================*/
int chrcount (const TCHAR* pString, const TCHAR Char) {
DEFINE_FUNCTION("chrcount()");
int Count = 0;
/* Ensure valid input */
ASSERT(pString != NULL);
while (*pString != NULL_CHAR) {
if (*pString == Char) Count++;
pString++;
}
return (Count);
}
/*===========================================================================
* End of Function chrcount()
*=========================================================================*/
/*===========================================================================
*
* Function - void chrdel (pString, CharIndex);
*
* Attempts to delete the character at the given position from the string.
* Position 0 is the first character in the string. ASSERTs on any bad
* input.
*
*=========================================================================*/
void chrdel (TCHAR* pString, const size_t CharIndex) {
DEFINE_FUNCTION("chrdel()");
TCHAR* pDeletePosition;
size_t StringLength;
/* Ensure valid input */
ASSERT(pString != NULL);
StringLength = TSTRLEN(pString);
ASSERT(CharIndex <= StringLength);
/* Shift the characters left of the deletion position by one */
pDeletePosition = pString + CharIndex;
memmove (pDeletePosition, pDeletePosition + 1, (StringLength - CharIndex)*sizeof(TCHAR));
}
/*===========================================================================
* End of Function chrdel()
*=========================================================================*/
/*===========================================================================
*
* Function - void chrdellast (pString);
*
* Deletes the last character from the string. ASSERTs on bad input.
*
*=========================================================================*/
void chrdellast (TCHAR* pString) {
DEFINE_FUNCTION("chrdellast()");
size_t StringLength;
/* Ensure valid input */
ASSERT(pString != NULL);
/* Find last character in string, and ensure at least one exists */
StringLength = TSTRLEN(pString);
/* Delete the last character in the string, if any */
if (StringLength > 0) pString[StringLength - 1] = NULL_CHAR;
}
/*===========================================================================
* End of Function chrdellast()
*=========================================================================*/
/*===========================================================================
*
* Function - boolean chrrpunc (CharIndex, pString);
*
* Looks for the first punctuation character from the end of the string.
* Saves the character index if one is found (0 is the first character
* in the string) and returns TRUE. Returns FALSE if no matching character
* was found. ASSERTs on bad input.
*
*=========================================================================*/
boolean chrrpunc (size_t& CharIndex, const TCHAR* pString) {
DEFINE_FUNCTION("chrrpunc()");
size_t StringPosition;
/* Ensure valid input */
ASSERT(pString != NULL);
/* Start at the end of the string */
StringPosition = TSTRLEN(pString);
/* Look for a punctuating character until start of string is reached */
while (StringPosition != 0) {
StringPosition--;
if (ispunct(pString[StringPosition])) {
CharIndex = StringPosition;
return (TRUE);
}
}
/* character not found in string */
return (FALSE);
}
/*===========================================================================
* End of Function chrrpunc()
*=========================================================================*/
/*===========================================================================
*
* Function - TCHAR* chrtok (pString, TokenChar);
*
* Seperates the given string into substrings divided by the given token
* character, as strtok(). Returns NULL when no substrings are
* left. Uses an internal static variable which prevents use of nesting
* chrtok() calls. ASSERTs on invalid input. The TokenTCHAR cannot be
* the terminating NULL character.
*
* Unlike strtok(), chrtok() ignores multiple occurences of the TokenTCHAR,
* for example, the TestString "11x22x33xxx44"
* strtok(TestString, "x") = "11", "22", "33", "44"
* chrtok(TestString, 'x') = "11", "22", "33", "", "", "44"
* The use is otherwise identical to strok().
*
*=========================================================================*/
TCHAR* chrtok (TCHAR* pString, const TCHAR TokenChar) {
DEFINE_FUNCTION("chrtok()");
static TCHAR* pParse = NULL;
TCHAR* pSearchChar;
TCHAR* pResult;
/* Ensure valid input */
ASSERT(TokenChar != NULL_CHAR);
/* Check for end of parsed token string */
if (pString == NULL && pParse == NULL) return (NULL);
/* Start parsing a new string */
if (pString != NULL) pParse = pString;
/* Search for the next substring */
pSearchChar = TSTRCHR(pParse, TokenChar);
/* No more substrings */
if (pSearchChar == NULL) {
pResult = pParse;
pParse = NULL;
return (pResult);
}
/* Terminate substring */
*pSearchChar = NULL_CHAR;
pResult = pParse;
pParse = pSearchChar + 1;
return (pResult);
}
/*===========================================================================
* End of Function chrtok()
*=========================================================================*/
/*===========================================================================
*
* Function - TCHAR* chrtrunc (pString, TruncateChar);
*
* Truncates the character at the first occurence of the given character.
* Returns the string. ASSERTs if given invalid input.
*
*=========================================================================*/
TCHAR* chrtrunc (TCHAR* pString, const TCHAR TruncateChar) {
DEFINE_FUNCTION("chrtrunc()");
TCHAR* pFindChar;
/* Ensure valid input */
ASSERT(pString != NULL && TruncateChar != NULL_CHAR);
/* Attempt to find the first truncation character */
pFindChar = TSTRCHR(pString, TruncateChar);
if (pFindChar != NULL) *pFindChar = NULL_CHAR;
return (pString);
}
/*===========================================================================
* End of Function chrtrunc()
*=========================================================================*/
/*===========================================================================
*
* Function - TCHAR* chrrtrunc (pString, TruncateChar);
*
* Truncates the character at the last occurence of the given character.
* Returns the string. ASSERTs if given invalid input.
*
*=========================================================================*/
TCHAR* chrrtrunc (TCHAR* pString, const TCHAR TruncateChar) {
DEFINE_FUNCTION("chrrtrunc()");
TCHAR* pFindChar;
/* Ensure valid input */
ASSERT(pString != NULL && TruncateChar != NULL_CHAR);
/* Attempt to find the last truncation character */
pFindChar = TSTRRCHR(pString, TruncateChar);
if (pFindChar != NULL) *pFindChar = NULL_CHAR;
return (pString);
}
/*===========================================================================
* End of Function chrrtrunc()
*=========================================================================*/
/*===========================================================================
*
* Begin Module Test Routines
*
* Test routines for the functions in this module. Only defined in
* DEBUG builds.
*
*=========================================================================*/
#if defined(_DEBUG)
/*===========================================================================
*
* Function - void Test_chradd (void);
*
* Tests the chradd() function.
* 1. Ensures characters are correctly inserted in middle of string
* 2. Check adding characters at start of string
* 3. Check adding characters at end of string
*
*=========================================================================*/
void Test_chradd (void) {
DEFINE_FUNCTION("Test_chradd()");
TCHAR TestString[101] = _T("123456789");
SystemLog.Printf(stdout, _T("============= Testing chradd() ===================="));
/* Add a character in the middle of the string */
chradd(TestString, 4, '4');
ASSERT(TSTRCMP(TestString, _T("1234456789")) == 0);
/* Add a character at the start of the string */
chradd(TestString, 0, '0');
ASSERT(TSTRCMP(TestString, _T("01234456789")) == 0);
/* Add a character at the end of the string */
chradd(TestString, 11, '0');
ASSERT(TSTRCMP(TestString, _T("012344567890")) == 0);
}
/*===========================================================================
* End of Function Test_chradd()
*=========================================================================*/
/*===========================================================================
*
* Function - void Test_chrcat (void);
*
* Test the chrcat() function.
* 1. Tests adding a character to a regular string.
* 2. Tests adding a character to an empty string.
*
*=========================================================================*/
void Test_chrcat (void) {
DEFINE_FUNCTION("Test_chrcat()");
TCHAR TestString[101] = _T("123456789");
SystemLog.Printf(stdout, _T("============= Testing chrcat() ===================="));
/* Test adding character to regular string */
chrcat(TestString, '0');
ASSERT(TSTRCMP(TestString, _T("1234567890")) == 0);
/* Test adding character to empty string */
TestString[0] = NULL_CHAR;
chrcat(TestString, '0');
ASSERT(TSTRCMP(TestString, _T("0")) == 0);
}
/*===========================================================================
* End of Function Test_chrcat()
*=========================================================================*/
/*===========================================================================
*
* Function - void Test_chrdel (void);
*
* Tests the chrdel() function
* 1. Deletes characters from middle of regular string.
* 2. Delete characters from start/end of string
* 3. Test empty string deletion
*
*=========================================================================*/
void Test_chrdel (void) {
DEFINE_FUNCTION("Test_chrdel()");
TCHAR TestString[101] = _T("123456789");
SystemLog.Printf(stdout, _T("============= Testing chrdel() ===================="));
/* Delete characters from middle of string */
chrdel(TestString, 4);
ASSERT(TSTRCMP(TestString, _T("12346789")) == 0);
/* Delete characters from end of string */
chrdel(TestString, 7);
ASSERT(TSTRCMP(TestString, _T("1234678")) == 0);
/* Delete characters from start of string */
chrdel(TestString, 0);
ASSERT(TSTRCMP(TestString, _T("234678")) == 0);
/* Delete character from empty string */
TestString[0] = NULL_CHAR;
chrdel(TestString, 0);
ASSERT(TSTRCMP(TestString, _T("")) == 0);
}
/*===========================================================================
* End of Function Test_chrdel()
*=========================================================================*/
/*===========================================================================
*
* Function - void Test_chrdellast (void);
*
* Tests the chrdellast() function.
* 1. Delete the last character from a regular string
* 2. Delete character from empty string.
*
*=========================================================================*/
void Test_chrdellast (void) {
DEFINE_FUNCTION("Test_chrdellast()");
TCHAR TestString[101] = _T("123456789");
SystemLog.Printf(stdout, _T("============= Testing chrdellast() ===================="));
/* Delete characters from regular of string */
chrdellast(TestString);
ASSERT(TSTRCMP(TestString, _T("12345678")) == 0);
/* Delete character from empty string */
TestString[0] = NULL_CHAR;
chrdellast(TestString);
ASSERT(TSTRCMP(TestString, _T("")) == 0);
}
/*===========================================================================
* End of Function Test_chrdellast()
*=========================================================================*/
/*===========================================================================
*
* Function - void Test_chrtok (void);
*
* Tests the chrtok() function.
* 1. Test a regular string, comparing results with strtok()
* 2. Check the function enhanced cabapility.
*
*=========================================================================*/
void Test_chrtok (void) {
DEFINE_FUNCTION("Test_chrtok()");
TCHAR TestString1[] = _T("111x222x333x444x555");
TCHAR TestString2[] = _T("111x222x333x444x555");
TCHAR* pChrPtr;
TCHAR* pStrPtr;
SystemLog.Printf(stdout, _T("============= Testing chrtok() ===================="));
/* Compare chrtok() results with strtok() for a normal string */
pChrPtr = chrtok(TestString1, 'x');
pStrPtr = TSTRTOK(TestString2, _T("x"));
while (pChrPtr != NULL && pStrPtr != NULL) {
SystemLog.Printf (_T("\t chrtok() / strtok() = %s / %s"), pChrPtr, pStrPtr);
ASSERT(TSTRCMP(pChrPtr, pStrPtr) == 0);
pChrPtr = chrtok(NULL, 'x');
pStrPtr = TSTRTOK(NULL, _T("x"));
}
ASSERT(pChrPtr == NULL && pStrPtr == NULL);
/* Test the enhanced feature of chrtok() */
TSTRCPY (TestString1, _T("111x222xx333xx"));
pChrPtr = chrtok(TestString1, 'x');
ASSERT(TSTRCMP(pChrPtr, _T("111")) == 0);
pChrPtr = chrtok(NULL, 'x');
ASSERT(TSTRCMP(pChrPtr, _T("222")) == 0);
pChrPtr = chrtok(NULL, 'x');
ASSERT(TSTRCMP(pChrPtr, _T("")) == 0);
pChrPtr = chrtok(NULL, 'x');
ASSERT(TSTRCMP(pChrPtr, _T("333")) == 0);
pChrPtr = chrtok(NULL, 'x');
ASSERT(TSTRCMP(pChrPtr, _T("")) == 0);
pChrPtr = chrtok(NULL, 'x');
ASSERT(TSTRCMP(pChrPtr, _T("")) == 0);
pChrPtr = chrtok(NULL, 'x');
ASSERT(pChrPtr == NULL);
}
/*===========================================================================
* End of Function Test_chrtok()
*=========================================================================*/
/*===========================================================================
*
* Function - void Test_chrrpunc (void);
*
* Tests the chrrpunc() function.
* 1. Ensures the function finds a punctuation in middle/start/end of string
* 2. Check multiple punctuation case
* 3. Check case where string contains no punctuation
* 4. Check empty string case.
*
*=========================================================================*/
void Test_chrrpunc (void) {
DEFINE_FUNCTION("Test_chrrpunc()");
TCHAR TestString[101] = _T("0123=5678");
boolean Result;
size_t TCHARIndex;
SystemLog.Printf(stdout, _T("============= Testing chrrpunc() ===================="));
/* Test for punctuation in middle of string */
Result = chrrpunc(TCHARIndex, TestString);
ASSERT(Result);
ASSERT(TCHARIndex == 4);
/* Test for punctuation at end of string */
TSTRCPY (TestString, _T("012345678."));
Result = chrrpunc(TCHARIndex, TestString);
ASSERT(Result);
ASSERT(TCHARIndex == 9);
/* Test for punctuation at start of string */
TSTRCPY (TestString, _T(".123456789"));
Result = chrrpunc(TCHARIndex, TestString);
ASSERT(Result);
ASSERT(TCHARIndex == 0);
/* Test for multiple punctuations in of string */
TSTRCPY (TestString, _T("01234.67.0"));
Result = chrrpunc(TCHARIndex, TestString);
ASSERT(Result);
ASSERT(TCHARIndex == 8);
/* Test for no punctuation in string */
TSTRCPY (TestString, _T("0123456789"));
Result = chrrpunc(TCHARIndex, TestString);
ASSERT(Result == FALSE);
/* Test empty string case */
TSTRCPY (TestString, _T(""));
Result = chrrpunc(TCHARIndex, TestString);
ASSERT(Result == FALSE);
}
/*===========================================================================
* End of Function Test_chrrpunc()
*=========================================================================*/
/*===========================================================================
*
* Function - void Test_chrtrunc (void);
*
* Tests the chrtrunc() function.
* 1. Test function with regular string with truncation character in
* middle/start/end of string
* 2. Check with no truncation character in string
* 3. Test with empty string
*
*=========================================================================*/
void Test_chrtrunc (void) {
DEFINE_FUNCTION("Test_chrtrunc()");
TCHAR TestString[101];
SystemLog.Printf(stdout, _T("============= Testing chrtrunc() ===================="));
/* Check with truncation character in middle of string */
TSTRCPY(TestString, _T("123456789"));
chrtrunc(TestString, '5');
ASSERT(TSTRCMP(TestString, _T("1234")) == 0);
/* Check with truncation character at end of string */
TSTRCPY(TestString, _T("123456789"));
chrtrunc(TestString, '9');
ASSERT(TSTRCMP(TestString, _T("12345678")) == 0);
/* Check with truncation character at start of string */
TSTRCPY(TestString, _T("123456789"));
chrtrunc(TestString, '1');
ASSERT(TSTRCMP(TestString, _T("")) == 0);
/* Check with no truncation character in string */
TSTRCPY(TestString, _T("123456789"));
chrtrunc(TestString, 'a');
ASSERT(TSTRCMP(TestString, _T("123456789")) == 0);
/* Check with empty string */
TSTRCPY(TestString, _T(""));
chrtrunc(TestString, '5');
ASSERT(TSTRCMP(TestString, _T("")) == 0);
}
/*===========================================================================
* End of Function Test_chrtrunc()
*=========================================================================*/
/*===========================================================================
*
* Function - void Test_chrtrunc (void);
*
* Tests the chrrtrunc() function.
* 1. Test function with regular string with truncation character in
* middle/start/end of string
* 2. Check with no truncation character in string
* 3. Test with empty string
*
*=========================================================================*/
void Test_chrrtrunc (void) {
DEFINE_FUNCTION("Test_chrrtrunc()");
TCHAR TestString[101];
SystemLog.Printf(stdout, _T("============= Testing chrrtrunc() ===================="));
/* Check with truncation character in middle of string */
TSTRCPY(TestString, _T("123456789"));
chrrtrunc(TestString, '5');
ASSERT(TSTRCMP(TestString, _T("1234")) == 0);
/* Check with truncation character at end of string */
TSTRCPY(TestString, _T("123456789"));
chrrtrunc(TestString, '9');
ASSERT(TSTRCMP(TestString, _T("12345678")) == 0);
/* Check with truncation character at start of string */
TSTRCPY(TestString, _T("123456789"));
chrrtrunc(TestString, '1');
ASSERT(TSTRCMP(TestString, _T("")) == 0);
/* Check with no truncation character in string */
TSTRCPY(TestString, _T("123456789"));
chrrtrunc(TestString, 'a');
ASSERT(TSTRCMP(TestString, _T("123456789")) == 0);
/* Check with empty string */
TSTRCPY(TestString, _T(""));
chrrtrunc(TestString, '5');
ASSERT(TSTRCMP(TestString, _T("")) == 0);
}
/*===========================================================================
* End of Function Test_chrrtrunc()
*=========================================================================*/
/*===========================================================================
*
* Function - void Test_DLChr (void);
*
* Tests all the functions in this module.
* 1. Tests the chradd() function
* 2. Tests the chrcat() function
* 3. Tests the chrdel() and chrdellast() functions
* 4. Tests the chrtok() function
* 5. Tests the chrrpunc() function
* 6. Tests the chrtrunc() and chrrtrunc() functions
*
*=========================================================================*/
void Test_DLChr (void) {
//DEFINE_FUNCTION("Test_DLChr()");
Test_chradd();
Test_chrcat();
Test_chrdel();
Test_chrdellast();
Test_chrtok();
Test_chrrpunc();
Test_chrtrunc();
Test_chrrtrunc();
}
/*===========================================================================
* End of Function Test_DLChr()
*=========================================================================*/
#endif
/*===========================================================================
* End of Module Test Routines
*=========================================================================*/