-
Notifications
You must be signed in to change notification settings - Fork 0
/
mystropt.c
396 lines (362 loc) · 9.14 KB
/
mystropt.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
//
// mystropt.c
//
// Module Name:
// Manage String Options
//
// Description:
//
// Revision History:
// T. David Wong 05-05-2005 Original Author
// T. David Wong 05-15-2005 Implemented and tested TESTDRIVER
// T. David Wong 04-02-2012 Compiled on Mac OS/X
//
// TODO:
// 1. better string handle in case insensitive condition
//
// -- MSVC command
// cl -Zi -W3 -D_TESTDRIVER_ mystropt.c
// -- GCC command
// gcc -g -o mystropt -D_TESTDRIVER_ mystropt.c
//
#include <stdio.h>
#include <stdlib.h> // malloc
#include <string.h>
#include "mystropt.h"
/* internal defines
*/
#define LIST_ITEM_PER_LINE 6
/* internal data structure
*/
typedef struct TableOfStrings
{
unsigned int count; /* # of strings in the table */
char **tbl;
} strTbl;
/*
* str_InitTable
*
* IN:
* kflag - keep flag
*
* OUT:
* ptr - pointer to table structure
*/
static void *str_InitTable(void)
{
strTbl *tbl = malloc(sizeof(strTbl));
if (tbl == NULL) return NULL;
tbl->count = 0;
tbl->tbl = NULL;
// tbl->flag = kflag;
return tbl;
}
/*
* str_FreeTable - release table
*
* IN:
* ptr - pointer to table structure
*/
void str_FreeTable(void *ptr)
{
strTbl *tbl = (strTbl*)ptr;
unsigned int ix;
/* sanity check */
if (tbl == NULL) return;
for (ix = 0; ix < tbl->count; ix++) {
/* free table entry */
if (tbl->tbl[ix]) free(tbl->tbl[ix]);
}
/* free table itself */
free(tbl->tbl);
free(tbl);
return;
}
/*
* str_InsertToTable - insert entry to table
*
* IN:
* str - string to be inserted
* ptr - pointer to table structure
* kflag - keep flag (when match occurs which, longer or shorter string, to keep)
* OUT:
* opaque pointer to table structure
*/
void *str_InsertToTable(char *str, void *ptr, int kflag)
{
strTbl *tbl = (strTbl *)ptr;
unsigned int icase =
#ifdef _MSC_VER
1
#else
0
#endif
;
unsigned int ix;
unsigned int count;
unsigned int slen = strlen(str);
int (*cmpfunc)();
/* sanity check */
if (tbl == NULL) {
/* there is NO table yet */
tbl = str_InitTable();
}
/* use only lowercase if case is insensitive */
if (icase) strlwr(str);
/* search for sub-string match
*/
count = tbl->count;
for (ix = 0; ix < count; ix++)
{
unsigned int elen = strlen(tbl->tbl[ix]);
/*
* - check exact match
*/
if (strlen(str) == strlen(tbl->tbl[ix]))
{
/* select appropriate compare function */
cmpfunc = icase ? stricmp : strcmp;
if (cmpfunc(str, tbl->tbl[ix]) == 0) {
/* no insertion needed */
return (void*)tbl;
}
}
else if (kflag == STR_KEEP_LONGER_STRING)
{
if (elen > slen && strstr(tbl->tbl[ix], str) != NULL) {
/* longer string already exists */
return (void*)tbl;
}
else
if (slen > elen && strstr(str, tbl->tbl[ix]) != NULL) {
/* replace with newer longer string */
free(tbl->tbl[ix]);
tbl->tbl[ix] = strdup(str);
return (void*)tbl;
}
}
else /* if (kflag == STR_KEEP_SHORTER_STRING) */
{
if (slen > elen && strstr(str, tbl->tbl[ix]) != NULL) {
/* shorter string already exists */
return (void*)tbl;
}
else
if (elen > slen && strstr(tbl->tbl[ix], str) != NULL) {
/* replace with newer shorter string */
free(tbl->tbl[ix]);
tbl->tbl[ix] = strdup(str);
return (void*)tbl;
}
}
} /* for (ix = 0; ix < count; ix++) */
/* no match found in the table
*/
(tbl->count)++;
tbl->tbl = (char **)realloc(tbl->tbl, ((tbl->count)+1)*sizeof(char*));
tbl->tbl[count] = strdup(str);
tbl->tbl[tbl->count] = NULL; /* make sure last entry is NULL */
return (void*)tbl;
}
/*
* str_FindInTable - find matched entry in table
*
* IN:
* str - string to search
* ptr - pointer to table structure
* icase - ignore case if icase != 0
*/
char *str_FindInTable(char *str, void *ptr, int icase)
{
strTbl *tbl = (strTbl *)ptr;
unsigned int ix;
char *cStr = str;
/* sanity check */
if (tbl == NULL) return NULL;
if (icase) { cStr = strlwr(strdup(str)); }
//
for (ix = 0; ix <= tbl->count; ix++) {
if (tbl->tbl[ix] == NULL) continue;
/* match excluded substring */
if (icase == 0) {
if (strstr(str, tbl->tbl[ix]) != NULL) { return tbl->tbl[ix]; }
}
else {
/* handle case insensitive condition */
char *cTblStr = strlwr(strdup(tbl->tbl[ix]));
char *sStr = strstr(cStr, cTblStr);
free(cTblStr);
if (sStr != NULL) {
free(cStr);
return tbl->tbl[ix];
}
}
}
return NULL;
}
/*
* str_ResolveConflicts - resolve con conflict patterns in "contain" and "exclude" tables
*
* IN:
* cptr - pointer to "contain" table structure
* xptr - pointer to "exclude" table structure
*/
void str_ResolveConflicts(void *cptr, void *eptr)
{
strTbl *ctbl = (strTbl *)cptr;
strTbl *etbl = (strTbl *)eptr;
unsigned int cix, eix;
/* sanity check */
if (ctbl == NULL || etbl == NULL) return;
if (ctbl->count == 0 || etbl->count == 0) return;
for (cix = 0; cix < ctbl->count; cix++)
{
if (ctbl->tbl[cix] == NULL) continue;
for (eix = 0; eix < etbl->count; eix++)
{
/* "contain" pattern takes precedence!!!
*/
if (etbl->tbl[eix] == NULL) continue;
if (strstr(ctbl->tbl[cix], etbl->tbl[eix]) != 0) {
//-dbg- fprintf(stderr, "contain=%s exclude=%s\n", ctbl->tbl[cix], etbl->tbl[eix]);
/* move last entry to current slot */
free(etbl->tbl[eix]); // free current entry
--(etbl->count); // decrease entry count
etbl->tbl[eix] = (etbl->count) ? etbl->tbl[etbl->count] : NULL;
--eix; // allow current slot to be checked again
break;
}
} /* for (eix = 0; eix < etbl->count; eix++) */
} /* for (cix = 0; cix < ctbl->count; cix++) */
return;
}
/*
* str_ListTable - list table content
*
* IN:
* desc - description string
* ptr - pointer to table structure
*/
void str_ListTable(char *desc, void *ptr)
{
strTbl *tbl = (strTbl *)ptr;
unsigned int ix;
/* sanity check */
if (tbl == NULL) return;
//
fprintf(stdout, "%s\n", desc);
if (tbl->count == 0) {
// no entry available
fprintf(stdout, "<none>\n");
return;
}
for (ix = 0; ix <= tbl->count; ix++) {
if (tbl->tbl[ix] == NULL) continue;
fprintf(stdout, "[%d] %s ", ix, tbl->tbl[ix]);
if (ix && (ix % LIST_ITEM_PER_LINE) == 0) { fprintf(stdout, "\n"); }
}
fprintf(stdout, "\n");
return;
}
#ifndef _WIN32
char *strlwr(char *str)
{
char *ptr = str;
while (*ptr) {
if ('A' <= *ptr && *ptr <= 'Z') {
*ptr += ('a' - 'A');
}
ptr++;
}
return str;
}
char *strupr(char *str)
{
char *ptr = str;
while (*ptr) {
if ('a' <= *ptr && *ptr <= 'z') {
*ptr -= ('a' - 'A');
}
ptr++;
}
return str;
}
#endif
/* ------------------------------------------------------------
*/
#ifdef _TESTDRIVER_
void *gContainsStr = NULL,
*gExcludedStr = NULL;
int gIgnoreCase = 0;
//
static int usage(char *progname)
{
fprintf(stderr, "Usage: %s [options] str...\n", progname);
fprintf(stderr, " -c<incstr> string to include\n");
fprintf(stderr, " -x<excstr> string to exclude\n");
fprintf(stderr, " -i ignore case\n");
return 0;
}
int main (int argc, char **argv)
{
char *progname = argv[0];
int ix;
/* no arguments */
if (argc == 1) {
usage(progname);
return 0;
}
/* parse options */
for (ix = 1; ix < argc; ix++) {
// fprintf(stderr, "[%d] %s\n", ix, argv[ix]);
if (strcmp(argv[ix], "-i") == 0) {
gIgnoreCase++;
continue;
}
else if (strncmp(argv[ix], "-c", 2) == 0) {
gContainsStr = str_InsertToTable(&argv[ix][2], gContainsStr, STR_KEEP_LONGER_STRING);
if (gContainsStr == NULL) {
fprintf(stderr, "error: str_InsertToTable to gContainsStr failed\n");
}
continue;
}
else if (strncmp(argv[ix], "-x", 2) == 0) {
gExcludedStr = str_InsertToTable(&argv[ix][2], gExcludedStr, STR_KEEP_SHORTER_STRING);
if (gExcludedStr == NULL) {
fprintf(stderr, "error: str_InsertToTable to gExcludedStr failed\n");
}
continue;
}
else if (argv[ix][0] != '-') {
/* no more option */
break;
}
}
/* list all */
fprintf(stderr, "-- before RESOLVE CONFILCTS:\n");
str_ListTable("string contains:", gContainsStr);
str_ListTable("string excluded:", gExcludedStr);
str_ResolveConflicts(gContainsStr, gExcludedStr);
/* list all */
fprintf(stderr, "-- after RESOLVE CONFILCTS:\n");
str_ListTable("string contains:", gContainsStr);
str_ListTable("string excluded:", gExcludedStr);
/* handle arguments */
for (/*no-op*/; ix < argc; ix++) {
char *str;
fprintf(stderr, "-%d- %s\n", ix, argv[ix]);
if (str = str_FindInTable(argv[ix], gContainsStr, gIgnoreCase)) {
fprintf(stderr, "-%d- %s - contains WANTED sub-string (%s)\n", ix, argv[ix], str);
}
if (str = str_FindInTable(argv[ix], gExcludedStr, gIgnoreCase)) {
fprintf(stderr, "-%d- %s - should be EXCLUDED due to (%s)\n", ix, argv[ix], str);
}
}
/* release all */
str_FreeTable(gContainsStr);
str_FreeTable(gExcludedStr);
return 0;
}
#endif /* _TESTDRIVER_ */
/*
* ------------------------------------------------------------ */