-
Notifications
You must be signed in to change notification settings - Fork 0
/
cStrFun.c
444 lines (371 loc) · 15 KB
/
cStrFun.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
/*#########################################################
# Name: cStrFun
# Use:
# o Holds functions for copying or manipualting c-strings
# Dependencies:
# - <string.h> (memcpy for line wrap functions)
#########################################################*/
#include "cStrFun.h"
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' cStrFun SOF: Start Of Functions
' - fun-1 cStrCpInvsDelm:
' o Copy c-string till tab, newline, or null
' - fun-2 cpParmAndArg:
' o Copies two strings, adding a space between each
' - fun-03 cStrWrapCp:
' o Copys a c-string and if needed breaks the c-string
' into separate lines
' - fun-04 cStrCpNoWrapSentence:
' o Copies a c-string and if needed will break at the
' start of the copied c-string. This is for repeated
' copying.
' - fun-05 trimNewLineAtEnd:
' o Trims a new line or "\r\n" of the end of a buffer
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*--------------------------------------------------------\
| Output:
| - Modifies:
| o cpToCStr to hold the copied C-string
| - Returns:
| o pointer to null at end of cpToCStr
\--------------------------------------------------------*/
char * cStrCpInvsDelm(
char *cpToCStr, /*C-string to copy values to*/
char *cpFromCStr /*C-string to copy*/
){ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-01 TOC: Sec-1 Sub-1: cStrCpInvsDelim
' - Copy c-string till tab, newline, or null
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
while(*cpFromCStr > 31)
{ /*While have a c-string to copy*/
*cpToCStr = *cpFromCStr;
++cpToCStr;
++cpFromCStr;
} /*While have a c-string to copy*/
*cpToCStr = '\0';
return cpToCStr;
} /*cStrCpInvsDelim*/
/*--------------------------------------------------------\
| Output:
| - Modifies:
| o cpToCStr to hold space, parameter, space, & argument
| - Returns:
| o pointer to null at end of cpToCStr
\--------------------------------------------------------*/
char * cpParmAndArg(
char *cpToCStr, /*Holds copied parameter & argement*/
char *cpParmCStr,/*Paramater to copy*/
char *cpArgCStr /*Argument to copy*/
){ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-02 TOC: Sec-1 Sub-1: cpSpaceCStr
' - Copies two strings, adding a space between each
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
*cpToCStr = ' ';
++cpToCStr;
while(*cpParmCStr > 31)
{ /*While have a c-string to copy*/
*cpToCStr = *cpParmCStr;
++cpToCStr;
++cpParmCStr;
} /*While have a c-string to copy*/
*cpToCStr = ' ';
++cpToCStr;
while(*cpArgCStr > 31)
{ /*While have a c-string to copy*/
*cpToCStr = *cpArgCStr;
++cpToCStr;
++cpArgCStr;
} /*While have a c-string to copy*/
*cpToCStr = '\0';
return cpToCStr;
} /*cpParmAndArg*/
/*--------------------------------------------------------\
| output:
| - modifies:
| o dupcstr to hold the contents of cpcstr
| o cntus to have number of chars in current line. this
| allows copying of multiple strings to dupcstr.
| o numcharindupul to have the number chars in dupcstr
| - returns:
| o char pointer to '\0' at end of dupcstr
| - note:
| o copies cpcstr at dupcstr + numcharindupul
| - When numCharInDupUL = 0, it will overwrite dupCStr
| - numCharInDupUL > dupCStr whill result in overflow
\--------------------------------------------------------*/
char * cStrWrapCp(
char *dupCStr, // Buffer to copy c-string to
char *cpCStr, // C-string to copy
char *padCStr, // Padding to add to start of breaks
unsigned char lenPadUC,// Length of padding (index 1)
unsigned short *cntUS,
// Number characters in current line of dpuCStr
unsigned short wrapUS, // Max characters per line
char *lineBreakCStr, // What to break a line with
unsigned char lenBreakUC, // length of lineBreakCStr
unsigned long *numCharInDupUL
// Number of characters in dupCStr
){ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-03 TOC: cStrWrapCp
' - Copys a c-string and if needed breaks the c-string
' into separate lines
' o fun-03 sec-1: Variable declerations
' o fun-03 sec-2: Check if need to add a new line
' o fun-03 sec-3: Copy the c-string & add needed breaks
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-03 Sec-1: Sub-1: Variable declerations
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
char *tmpCStr = padCStr;
// These variables are for adding in padding. In these
// cases I may need to maintain a temporary buffer.
char swapCAry[lenPadUC + 2]; // swap buffer
unsigned short numSwapCharUS = 0; // characters to swap
unsigned short swapElmOnUS = 0; // were at in swap
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-03 Sec-2: Sub-1: Check if need to add a new line
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
dupCStr += *numCharInDupUL; // Move to last character
if(*cntUS + 1 >= wrapUS)
{ // If I need to start a new line
while(*dupCStr < 33)
{ // While I am on white space
--dupCStr;
--(*numCharInDupUL);
} // While I am on white space
++dupCStr;
++(*numCharInDupUL);
// Add in the line break
memcpy(dupCStr, lineBreakCStr, lenBreakUC);
dupCStr += lenBreakUC;
// Add in the padding
memcpy(dupCStr, padCStr, lenPadUC);
dupCStr += lenPadUC; // Move past padding
// Adjust the counters
(*numCharInDupUL) += lenPadUC + lenBreakUC;
*cntUS = lenPadUC;
} // If I need to start a new line
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-03 Sec-3: Copy the c-string
^ o fun-03 sec-3 sub-1: Need to break a line?
^ o fun-03 sec-3 sub-2: Add wrap to end of line
^ o fun-03 sec-3 sub-3: 1 character after break point
^ o fun-03 sec-3 sub-4: > 1 character after break
^ o fun-03 sec-3 sub-5: Add new character to buffer
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
while(*cpCStr != '\0')
{ // While not at the end of the string
/*************************************************\
* Fun-03 Sec-3 Sub-1: Need to break a line?
\*************************************************/
if(*cntUS >= wrapUS - 1)
{ // If I need to wrap the string
*cntUS = lenPadUC;
*dupCStr = '\0';
tmpCStr = dupCStr - 1;
while(*tmpCStr > 32)
{ // While not on white space
--tmpCStr;
++(*cntUS); // number of char after break
} // While not on white space
/*********************************************\
* Fun-03 Sec-3 Sub-2: Add wrap to end of line
\*********************************************/
// Check if line is 1 word (can not break up)
// Or if I can just add a break at the end
if(*cntUS + lenPadUC >= wrapUS ||
*cntUS <= lenPadUC
) { // If I am wraping at the end of the line
while(*dupCStr < 33)
{ // While on white space
--dupCStr;
--(*numCharInDupUL);
} // While on white space
++dupCStr;
++(*numCharInDupUL);
// Add in the line break
memcpy(dupCStr, lineBreakCStr, lenBreakUC);
dupCStr += lenBreakUC; // Move past break
// Add in the padding
memcpy(dupCStr, padCStr, lenPadUC);
dupCStr += lenPadUC; // Move past padding
// Adjust the counters
(*numCharInDupUL) += lenPadUC + lenBreakUC;
*cntUS = lenPadUC;
} // If I am wraping at the end of the line
/*********************************************\
* Fun-03 Sec-3 Sub-4: > 1 character after break
\*********************************************/
else
{ // Else I can break up the line
// Add in the line break
dupCStr = tmpCStr;
tmpCStr = lineBreakCStr;
numSwapCharUS = 0;
while(numSwapCharUS < lenBreakUC)
{ // Loop till I have copied the break
*dupCStr = *tmpCStr;
++tmpCStr;
++dupCStr;
swapCAry[numSwapCharUS] = *dupCStr;
++numSwapCharUS;
} // Loop till I have copied the break
// Add the padding after the break
tmpCStr = padCStr;
while(numSwapCharUS < lenBreakUC +lenPadUC)
{ // Loop till I have copied the break
*dupCStr = *tmpCStr;
++tmpCStr;
++dupCStr;
swapCAry[numSwapCharUS] = *dupCStr;
if(*dupCStr == '\0')
*(dupCStr + 1) = '\0';
++numSwapCharUS;
} // Loop till I have copied the break
// Remake the line
//--numSwapCharUS; // convert to 0 index
swapCAry[numSwapCharUS] = *dupCStr;
tmpCStr = dupCStr;
++dupCStr;
swapElmOnUS = 0;
while(swapCAry[swapElmOnUS] != '\0')
{ // While I have chars to add back in
*tmpCStr = swapCAry[swapElmOnUS];
swapCAry[swapElmOnUS] = *dupCStr;
++swapElmOnUS; // Move to the next swap
++tmpCStr;
++dupCStr;
// Do I need to go back to index 0?
if(swapElmOnUS >= numSwapCharUS)
swapElmOnUS = 0;
} // While I have chars to add back in
// Currently is one element to far foward
--dupCStr;
(*numCharInDupUL) +=
lenPadUC
+ lenBreakUC
- 1; // -1 for the space I used
} // Else I can break up the line
if(*cpCStr == '\0') break; // done
} // If I need to wrap the string
/*************************************************\
* Fun-03 Sec-3 Sub-6: Add new character to buffer
\*************************************************/
*dupCStr = *cpCStr;
++dupCStr;
++cpCStr;
++(*numCharInDupUL);
++(*cntUS);
} // While not at the end of the string
*dupCStr = '\0'; // Mark end of buffer
return dupCStr;
} // cStrWrapCp
/*--------------------------------------------------------\
| Output:
| - Modifies:
| o dupcstr to hold the contents of cpcstr
| o cntus to have number of chars in current line. this
| allows copying of multiple strings to dupcstr.
| - Returns:
| o char pointer to '\0' at end of dupcstr
| - Note:
| o copies cpcstr at dupcstr + numcharindupul
| - When numCharInDupUL = 0, it will overwrite dupCStr
| - When numCharInDupUL > dupCStr will overflow dupCStr
\--------------------------------------------------------*/
char * cStrCpNoWrapBuff(
char *dupCStr, // Buffer to copy c-string to
char *cpCStr, // C-string to copy
unsigned short lenCpUS,
char *padCStr, // Padding to add to start of breaks
unsigned char lenPadUC,// Length of padding (index 1)
char *endStrCStr, // Ending c-string to add in
unsigned char lenEndUC,// Length of the ending c-string
unsigned short *cntUS,
// Number characters in current line of dpuCStr
unsigned short wrapUS, // Max characters per line
char *lineBreakCStr, // What to break a line with
unsigned char lenBreakUC, // length of lineBreakCStr
unsigned long *numCharInDupUL
// Number of characters in dupCStr
){/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-04 TOC: cStrCpNoWrapSentence
' o Copies a c-string and if needed will break at the
' start of the copied c-string.
' o fun-04 sec-1: Need to add new entry on a new line?
' o fun-04 sec-2: Copy the new entry to the buffer
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-04 Sec-1: Sub-1: Should I add entry on new line?
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
dupCStr += *numCharInDupUL;
if(*cntUS + lenCpUS + lenEndUC >= wrapUS)
{ // If I need to wrap a line
while(*dupCStr < 33)
{ // While have trailing white space
--dupCStr;
--(*numCharInDupUL);
} // While have trailing white space
// Acount for one shift off
++dupCStr;
++(*numCharInDupUL);
// Add in the line break
memcpy(dupCStr, lineBreakCStr, lenBreakUC);
dupCStr += lenBreakUC;
// Add in the padding
memcpy(dupCStr, padCStr, lenPadUC);
dupCStr += lenPadUC; // Move past padding
// Adjust the counters
(*numCharInDupUL) += lenPadUC + lenBreakUC;
*cntUS = lenPadUC;
} // If I need to wrap a line
/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\
^ Fun-04 Sec-2: Sub-1: Copy the new entry to the buffer
\<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
// Copy in the new string and the ending
memcpy(dupCStr, cpCStr, lenCpUS);
dupCStr += lenCpUS;
(*numCharInDupUL) += lenCpUS;
*cntUS += lenCpUS;
if(lenEndUC > 0)
{ // If have an ending to add in
memcpy(dupCStr, endStrCStr, lenEndUC);
dupCStr += lenEndUC;
(*numCharInDupUL) += lenEndUC;
*cntUS += lenEndUC;
} // If have an ending to add in
*dupCStr = '\0';
return dupCStr;
} // cStrCpNoWrapBuff
/*--------------------------------------------------------\
| Output:
| - Modifies:
| o buffCStr to replace '\n', '\r', or "\r\n" with '\0'
| o lenBuffUL to account for removal of new line
\--------------------------------------------------------*/
void trimNewLineAtEnd(
char *buffCStr, // Buffer to check for new line at end
unsigned long *lenBuffUL
){ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\
' Fun-05 TOC: Sec-1 Sub-1: trimNewLineAtEnd
' - Trims a new line off the end of a buffer
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
if(*(buffCStr + *lenBuffUL - 1) == '\n')
{ // If I have a new line ending I need to remove
--(*lenBuffUL); // Buffer will be one char shorter
if(*(buffCStr + *lenBuffUL - 1) == '\r')
{ // If I need to remove the new line
*(buffCStr + *lenBuffUL - 1) = '\0';
--(*lenBuffUL);
} // If I need to remove the new line
else *(buffCStr + *lenBuffUL) = '\0';
return;
} // If I have a new line ending I need to remove
if(*(buffCStr + *lenBuffUL - 1) == '\r')
{ // If I need to remove the carriage return
*(buffCStr + *lenBuffUL - 1) = '\0';
--(*lenBuffUL);
return;
} // If I need to remove the carriage return
return;
} // trimNewLineAtEnd