-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringUtils.c
More file actions
320 lines (257 loc) · 9 KB
/
stringUtils.c
File metadata and controls
320 lines (257 loc) · 9 KB
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
#include "stringUtils.h"
int getStringLength( const char *testStr )
{
// Initialize index variable
int index = 0;
// Loop over the string until null char is found or index passes STD_STR_LEN
while( index < STD_STR_LEN && testStr[ index ] != NULL_CHAR )
{
// Increment index
index++;
}
// Return string length
return index;
}
int compareString( const char *oneStr, const char *otherStr )
{
// Initialize variables
int diff, index = 0;
// Loop to end of the shorter string
while( oneStr[ index ] != NULL_CHAR
&& otherStr[ index ] != NULL_CHAR
&& index < MAX_STR_LEN )
{
// Calculate the difference in characters
diff = oneStr[ index ] - otherStr[ index ];
// Check for a difference between characters
if( diff != 0 )
{
// Return the difference
return diff;
}
// Increment the index
index++;
}
// If no character difference was found, return difference of lengths
return getStringLength( oneStr ) - getStringLength( otherStr );
}
void concatenateString( char *destStr, const char *sourceStr )
{
// Initalize variables
int destIndex = getStringLength( destStr );
int sourceIndex = 0;
int sourceStrLen = getStringLength( sourceStr );
// Create temporary string pointer
char *tempString;
// Copy source string
tempString = (char *)malloc( ( sourceStrLen + 1 ) * sizeof(char) );
copyString( tempString, sourceStr );
// Loop to the end of the source string
while( tempString[ sourceIndex ] != NULL_CHAR && destIndex < MAX_STR_LEN )
{
// Assign characters to end of destination string
destStr[ destIndex ] = tempString[ sourceIndex ];
// Update indicies
destIndex++;
sourceIndex++;
// Set temporary end of destination string
destStr[ destIndex ] = NULL_CHAR;
}
// Free temporary string
free(tempString);
}
void copyString( char *destStr, const char *sourceStr )
{
// Initalize variables
int index = 0;
// Check for source/dest pointer not the same
if( destStr != sourceStr )
{
// Loop across source string
while( sourceStr[ index ] != NULL_CHAR && index < MAX_STR_LEN )
{
// Assign characters to end of dest string
destStr[ index ] = sourceStr[ index ];
// Increment index
index++;
// Set temporary end of dest string
destStr[ index ] = NULL_CHAR;
}
}
}
int findSubString( const char *testStr, const char *searchSubStr )
{
// Initialize variables
int testStrLen = getStringLength( testStr );
int masterIndex = 0;
int searchIndex, internalIndex;
// Loop across testStr
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( internalIndex <= testStrLen
&& testStr[ internalIndex ] == searchSubStr[ searchIndex ] )
{
// Increment test string and substring indicies
internalIndex++;
searchIndex++;
// Check for end of the substring
if( searchSubStr[ searchIndex ] == NULL_CHAR )
{
// Return beginning location of the sub string
return masterIndex;
}
}
// Increment master index
masterIndex++;
}
// Assume test has failed, return SUBSTRING_NOT_FOUND
return SUBSTRING_NOT_FOUND;
}
bool getStringConstrained( FILE *inStream, bool clearLeadingNonPrintable,
bool clearLeadingSpace, bool stopAtNonPrintable, char delimiter,
char *capturedString )
{
// Initalize variables
int intChar = EOF;
int index = 0;
// Initialze output string
capturedString[ index ] = NULL_CHAR;
// Capture first value in stream
intChar = fgetc( inStream );
// Loop to clear non printable or space, if indicated
while( ( intChar != EOF) && ( ( clearLeadingNonPrintable && intChar
< (int)SPACE ) || ( clearLeadingSpace && intChar == (int)SPACE ) ) )
{
// Get next character
intChar = fgetc( inStream );
}
// Check if file is empty
if( intChar == EOF )
{
// String was not found, return failure
return false;
}
// Loop across input
// ( Not at EOF and not reach max string length )
// AND ( (if stopAtNonPrintable flag is set AND intChar is nonPrintable)
// OR stopAtNonPrintable is false ) AND intChar is not the delimiter
while( ( intChar != EOF && index < MAX_STR_LEN - 1 )
&& ( ( stopAtNonPrintable && intChar >= (int)SPACE )
|| ( !stopAtNonPrintable ) ) && ( intChar != (char)delimiter ) )
{
// Place character in array
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 (reprime)
intChar = fgetc( inStream );
}
// Return success
return true;
}
bool getStringToDelimiter(FILE *inStream, char delimiter, char *capturedString)
{
// Call engine with delimiter
return getStringConstrained(inStream, true, true, true, delimiter,
capturedString);
}
bool getStringToLineEnd( FILE *inStream, char *capturedString )
{
// Call engine with delimiter
return getStringConstrained(inStream, true, true, true, NON_PRINTABLE_CHAR,
capturedString);
}
void getSubString( char *destStr, const char *sourceStr, int startIndex,
int endIndex )
{
// Initialize variables
int sourceStrLen = getStringLength( sourceStr );
int destIndex = 0;
int sourceIndex = startIndex;
// Create char pointer for temporary string
char *tempStr;
// Check if indicies are valid
if( startIndex >= 0 && startIndex <= endIndex && endIndex < sourceStrLen)
{
// Allocate memory for temporary string and copy from source string
tempStr = (char *)malloc( sizeof(char) * ( sourceStrLen + 1 ) );
copyString( tempStr, sourceStr );
// Loop over start to end index
while( sourceIndex <= endIndex )
{
// Copy character from temp to dest strings
destStr[ destIndex ] = tempStr[ sourceIndex ];
// Increment indicies
destIndex++;
sourceIndex++;
// Add null character for c style string
destStr[ destIndex ] = NULL_CHAR;
}
// Free temporary string
free( tempStr );
}
}
void setStrToLowerCase( char *destStr, const char *sourceStr )
{
// Initialize variables
int sourceStrLen = getStringLength( sourceStr );
int index = 0;
// Create temporary string pointer and copy source string
char *tempStr = (char *)malloc( sizeof(char) * ( sourceStrLen + 1 ) );
copyString( tempStr, sourceStr );
// Loop over temp string
while( tempStr[ index ] != NULL_CHAR && index < MAX_STR_LEN )
{
// Copy lower case character
destStr[ index ] = toLowerCase( tempStr[ index ] );
// Increment index
index++;
// Terminate c style string
destStr[ index ] = NULL_CHAR;
}
// Free temp string
free( tempStr );
}
char toLowerCase( char inChar )
{
// Check for lowercase character
if( inChar >= 'A' && inChar <= 'Z' )
{
// Return lowercase
return inChar - 'A' + 'a';
}
// Otherwise, character is lowercase
return inChar;
}
void utilitiesCheck()
{
// Utilities check
printf( "The string '123' is %d characters long\n",
getStringLength( "123" ) );
printf( "The strings '123' and '1234' have the difference of %d\n",
compareString( "123", "1234" ) );
printf( "The strings '1234' and '123' have the difference of %d\n",
compareString( "1234", "123" ) );
char testStr[MAX_STR_LEN] = "1234";
concatenateString( testStr, "567" );
printf( "The strings '1234' and '567' concatted creates %s\n", testStr);
char testStrTwo[MAX_STR_LEN] = "Ploop";
copyString( testStr, testStrTwo );
printf( "Copied strings: %s %s\n", testStr, testStrTwo );
copyString( testStr, "Hello World!" );
copyString( testStrTwo, "World!" );
printf( "Substring '%s' is found at index %d in '%s'\n", testStrTwo,
findSubString( testStr, testStrTwo ), testStr );
//getStringConstrained( somefile , false, false, false, ',', testStr );
getSubString( testStrTwo, testStr, 6, 11);
printf( "Substring: '%s'\n", testStrTwo );
setStrToLowerCase( testStrTwo, testStr );
printf( "Lowercase: '%s' and '%c'\n", testStrTwo, toLowerCase('Z'));
}