Repository navigation
Expand file tree
/
Copy pathstringUtils.c
More file actions
177 lines (140 loc) · 4.18 KB
/
Copy pathstringUtils.c
File metadata and controls
177 lines (140 loc) · 4.18 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
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include "stringutils.h"
int stringCompareIgnoreCase(char *a, char *b) {
int aLength = strlen(a);
int bLength = strlen(b);
int maxLength = (aLength > bLength) ? aLength : bLength;
int i;
for (i = 0; i < maxLength; ++i) {
if (i > aLength)
return -1;
if (i > bLength)
return 1;
char aChar = tolower(a[i]);
char bChar = tolower(b[i]);
if (aChar > bChar)
return 1;
if (bChar > aChar)
return -1;
}
return 0;
}
int stripNewLines(char* input){
int length = strlen(input);
int i = 0;
for(; i < length; ++i){
if(input[i] == '\n'){
input[i] = '\0';
return 1;
}
}
return 0;
}
int countTokens(char* input){
int length = strlen(input);
char leadChar, followChar;
int tokenCount = 0;
int i = 0;
for(; i <= length; ++i){
leadChar = input[i + 1];
followChar = input[i];
int leadOnToken = !isspace((int)leadChar) && leadChar != '\0';
int followOnToken = !isspace((int)followChar) && followChar != '\0';
// we are just stepping onto a token
if(!followOnToken && leadOnToken){
// do nothing
}
// we are in the middle of a token
if(followOnToken && leadOnToken){
// do nothing
}
// we are stepping off of a token
if(followOnToken && !leadOnToken){
++ tokenCount;
}
// we are not touching a token
if(!followOnToken && !leadOnToken){
// do nothing
}
}
return tokenCount;
}
/*
* input[in] : The string to be tokenized
* tokenCount[out] : The number of tokens found in the string
*/
char ** tokenize(char *input, int *tokenCount){
if(input == NULL)
fprintf(stderr, "Error: tokenize() received a NULL pointer for input\n");
if(tokenCount == NULL)
fprintf(stderr, "Error: tokenize() received a NULL pointer for tokenCount\n");
if(!(input || tokenCount))
return NULL;
(*tokenCount) = countTokens(input);
char** output = (char**) calloc(sizeof(char*), *tokenCount);
if(output == NULL){
fprintf(stderr, "Error: stringCopy was unable to allocate memory using calloc()\n");
return NULL;
}
int length = strlen(input);
int currentTokenLength = 0;
int tokenIndex = 0;
char leadChar, followChar;
int i;
for(i = 0; i <= length; ++i){
leadChar = input[i + 1];
followChar = input[i];
int leadOnToken = !isspace((int)leadChar) && leadChar != '\0';
int followOnToken = !isspace((int)followChar) && followChar != '\0';
// we are just stepping onto a token
if(!followOnToken && leadOnToken){
// do nothing
}
// we are in the middle of a token
if(followOnToken && leadOnToken){
++currentTokenLength;
}
// we are stepping off of a token
if(followOnToken && !leadOnToken){
++currentTokenLength;
char* tokenStartPos = input + (i - (currentTokenLength - 1));
// make a dynamic copy of this token
char* thisToken = stringCopy(tokenStartPos, currentTokenLength);
output[tokenIndex++] = thisToken;
currentTokenLength = 0;
}
// we are not touching a token
if(!followOnToken && !leadOnToken){
}
}
return output;
}
char* stringCopy(char* input, int length){
if(length == 0 || input == NULL)
return NULL;
char* output = (char*)calloc(sizeof(char), (length + 1));
if(output == NULL){
fprintf(stderr, "Error: stringCopy was unable to allocate memory using calloc()\n");
return NULL;
}
int i = 0;
for(; i < length; ++i){
output[i] = input[i];
}
output[i] = '\0';
return output;
}
int findFirstOccurence(char* input, char target){
if(input == NULL)
return -1;
int i = 0;
int length = strlen(input);
for(; i < length; ++i){
if(input[i] == target)
return i;
}
return -1;
}