-
Notifications
You must be signed in to change notification settings - Fork 4
/
csplit.h
579 lines (509 loc) · 18.7 KB
/
csplit.h
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
/********************************************************************************
* MIT License
*
* Copyright (c) 2019 Jakub Wlodek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*********************************************************************************/
/**
* @defgroup intern Internal
* @brief Internal functions, not intended to be used by users
* @defgroup set Setup
* @brief Setup and Diagnostic functions used by csplit
* @defgroup core csplit Core
* @brief Core functions included in csplit
*/
/**
* A single-header C library for string manipulation and splitting.
*
* Author: Jakub Wlodek
* Created: 02-Aug-2019
*/
// Include guard - avoid redefinition
#ifndef CSPLIT_H
#define CSPLIT_H
#ifdef __cplusplus
extern "C" {
#endif
// some basic includes
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#ifdef _MSC_VER
# define _CSPLIT_FUNC static __inline
#elif !defined __STDC_VERSION__ || __STDC_VERSION__ < 199901L
# define _CSPLIT_FUNC static __inline__
#else
# define _CSPLIT_FUNC static inline
#endif
/**
* Enum type for error codes for csplit
* @ingroup set
*/
typedef enum CSPLIT_ERROR {
CSPLIT_SUCCESS = 0, /**< No Error */
CSPLIT_TOO_SHORT = -1, /**< Input string is too short */
CSPLIT_NO_SUCH_INDEX = -2, /**< Index out of range */
CSPLIT_UNIMPLEMENTED = -3, /**< Function unimplemented */
CSPLIT_BUFF_EXCEEDED = -4, /**< Buffer size exceeded */
} CSplitError_t;
/**
* Struct for an individual string fragment result from csplit
* @ingroup intern
*/
typedef struct CSPLIT_FRAGMENT {
char* text; /**< Text of the fragment. */
struct CSPLIT_FRAGMENT* next; /**< Next fragment in the linked list */
struct CSPLIT_FRAGMENT* prev; /**< Previous fragment in the linked list */
} CSplitFragment_t;
/**
* Struct that stores the csplit linked list. Can be used as an arbitrary linked list
* for strings, but is intended for use with csplit strtok replacement functions
* @ingroup core
*/
typedef struct CSPLIT_LIST {
int num_elems; /**< Number of elements in the list */
CSplitFragment_t* head; /**< Head of the linked list (first element) */
CSplitFragment_t* tail; /**< Tail of the linked list (last element) */
} CSplitList_t;
/* Function Declarations */
_CSPLIT_FUNC
CSplitList_t* csplit_init_list();
_CSPLIT_FUNC
void csplit_clear_list(CSplitList_t* list);
_CSPLIT_FUNC
CSplitError_t csplit_push_to_list(CSplitList_t* list, CSplitFragment_t* fragment, size_t buff_size);
_CSPLIT_FUNC
void csplit_print_list_info(CSplitList_t* list, FILE* fp);
#ifdef CSPLIT_DEBUG
_CSPLIT_FUNC
void print_csplit_fragment_info(CSplitFragment_t* fragment, FILE* fp);
#endif
_CSPLIT_FUNC
char* csplit_get_fragment_at_index(CSplitList_t* list, int index);
_CSPLIT_FUNC
CSplitError_t csplit_reverse_list(CSplitList_t* list);
_CSPLIT_FUNC
char* csplit_strip(char* input_str);
_CSPLIT_FUNC
char* csplit_remove_whitespace(char* input_str);
_CSPLIT_FUNC
int csplit_startswith(char* input_str, char* starts_with);
_CSPLIT_FUNC
int csplit_endswith(char* input_str, char* ends_with);
_CSPLIT_FUNC
CSplitError_t csplit_rstr(CSplitList_t* list, char* input_str, char* token, int max_splits);
_CSPLIT_FUNC
CSplitError_t csplit_str(CSplitList_t* list, char* input_str, char* token, int max_splits);
_CSPLIT_FUNC
CSplitError_t csplit_lim(CSplitList_t* list, char* input_str, char* token, int max_splits);
_CSPLIT_FUNC
CSplitError_t csplit(CSplitList_t* list, char* input_str, char* token);
_CSPLIT_FUNC
CSplitError_t rcsplit(CSplitList_t* output_list, char* input_str, char* token);
/* Function Definitions */
/**
* @brief Function for initializing a csplit list
* @ingroup set
*
* @params[in]: buff_size -> user set buffer size. Make sure this is large enough for your largest fragment
* @return: list -> an allocated csplit list
*/
_CSPLIT_FUNC
CSplitList_t* csplit_init_list(){
CSplitList_t* list = (CSplitList_t*) calloc(1, sizeof(CSplitList_t));
list->num_elems = 0;
return list;
}
/**
* @brief Clears all memory for an allocated csplit list
* @ingroup set
*
* @params[in]: list -> a previously allocated csplit list to be freed
*/
_CSPLIT_FUNC
void csplit_clear_list(CSplitList_t* list){
CSplitFragment_t* current_fragment = list->head;
while(current_fragment != NULL){
CSplitFragment_t* temp = current_fragment->next;
free(current_fragment->text);
free(current_fragment);
current_fragment = temp;
}
free(list);
}
/**
* @brief Function that pushes a new CSplitFragment to the end of the list, and allocates memory for the text,
* with size list->BUFF_SIZE
* @ingroup intern
*
* @params[out]: list -> The list with fragment appended to the tail
* @params[in]: fragment -> fragment to append to the list. fragment->text will be allocated based on list->BUFF_SIZE
*/
_CSPLIT_FUNC
CSplitError_t csplit_push_to_list(CSplitList_t* list, CSplitFragment_t* fragment, size_t buff_size){
// first make sure neither is null
if(list == NULL || fragment == NULL){
return CSPLIT_TOO_SHORT;
}
else{
// then update the number of elements, and the pointers
list->num_elems = list->num_elems + 1;
if(list->head == NULL){
list->head = fragment;
list->tail = fragment;
}
else{
list->tail->next = fragment;
fragment->prev = list->tail;
list->tail = fragment;
}
// allocate fragment text field
fragment->text = (char*) calloc(1, buff_size);
}
return CSPLIT_SUCCESS;
}
/**
* @brief Function that prints information about a csplit list
* @ingroup set
*
* @params[in]: list -> list for which to print info
* @params[in]: fp -> file pointer to print into.
*/
_CSPLIT_FUNC
void csplit_print_list_info(CSplitList_t* list, FILE* fp){
if(list == NULL || fp == NULL) return;
fprintf(fp, "List contains %d elements\n", list->num_elems);
fprintf(fp, "Supports indexes -%d to %d.\n", list->num_elems, list->num_elems -1);
CSplitFragment_t* current_fragment = list->head;
while(current_fragment != NULL){
fprintf(fp, "--%s--\n", current_fragment->text);
current_fragment = current_fragment->next;
}
}
#ifdef CSPLIT_DEBUG
/**
* @brief Function that prints information about a csplit fragment - intended for internal use only
* @ingroup intern
*
* @params[in]: fragment -> fragment for which to print info
* @params[in]: fp -> file pointer to print into
*/
_CSPLIT_FUNC
void print_csplit_fragment_info(CSplitFragment_t* fragment, FILE* fp){
if(fragment == NULL || fp == NULL) return;
fprintf(fp, "Fragment has text %s\n", fragment->text);
if(fragment->next != NULL) fprintf(fp, "Fragment has next with text %s\n", fragment->next->text);
if(fragment->prev != NULL) fprintf(fp, "Fragment has prev with text %s\n", fragment->prev->text);
}
#endif
/**
* @brief Function that returns the string fragment at a certain index in the list
* @ingroup core
*
* @params[in]: list -> list generated by csplit
* @params[in]: index -> index to search for (can be negative for getting at index from back of list)
* @return: text -> string at the given index or NULL if index out of range.
*/
_CSPLIT_FUNC
char* csplit_get_fragment_at_index(CSplitList_t* list, int index){
// convert index into absolute index (if negative);
int target_index;
if(index < 0){
target_index = index + list->num_elems;
}
else{
target_index = index;
}
// if index is out of range return null
if(list->num_elems <= target_index || target_index < 0){
return NULL;
}
else{
// iterate over list until index found
int counter = 0;
CSplitFragment_t* current_fragment = list->head;
while(counter < target_index){
current_fragment = current_fragment->next;
counter++;
}
// return the text field
return current_fragment->text;
}
}
/**
* @brief Function that reverses the list generated by csplit
* @ingroup set
*
* @params[out]: list -> list to reverse
* @return: err -> error code if there is an error
*/
_CSPLIT_FUNC
CSplitError_t csplit_reverse_list(CSplitList_t* list){
int i;
// iterate over list and swap next and previous fields
CSplitFragment_t* temp = list->head;
for(i = 0; i < list->num_elems; i++){
CSplitFragment_t* temp2 = temp->next;
temp->next = temp->prev;
temp->prev = temp2;
temp = temp2;
}
// swap head and tail
temp = list->head;
list->head = list->tail;
list->tail = temp;
return CSPLIT_SUCCESS;
}
/**
* @brief Function that strips a given string into an output string. Will remove whitespace character:
* \n, \r, \t, space will be removed from the start and end of each string.
* @ingroup core
*
* @params[in]: input_str -> the input string to strip
* @return: output_str -> the string with whitespace removed from the ends. Must be freed.
*/
_CSPLIT_FUNC
char* csplit_strip(char* input_str){
char* output_str;
// check if input is null
if(input_str == NULL)
output_str = NULL;
else{
int len = strlen(input_str);
char* end = input_str + len - 1;
char* start = input_str;
while(isspace(*start)){
if(*start == '\0' || start == end) return NULL;
else start++;
}
while(isspace(*end))
end--;
size_t buff_size = end - start + 1;
output_str = (char*) calloc(1, buff_size + 1);
strncpy(output_str, start, buff_size);
}
return output_str;
}
/**
* @breifFunction that removes all whitespace characters of a given string into an output string.
* Note that resulting char* must be free'd after it is no longer used
* @ingroup core
*
* @params[in]: input_str -> the input string to strip
* @return: output_str -> the string with whitespace removed.
*/
_CSPLIT_FUNC
char* csplit_remove_whitespace(char* input_str){
char* output_str;
if(input_str == NULL)
output_str = NULL;
else{
int len = strlen(input_str);
output_str = (char*) calloc(1, len);
int counter = 0;
int output_counter = 0;
// read through but don't copy whitespace
while(counter < len){
if(!isspace(input_str[counter])){
output_str[output_counter] = input_str[counter];
output_counter++;
}
counter++;
}
}
return output_str;
}
/**
* @brief Function that checks if a given string starts with another given string.
* @ingroup core
*
* @params[in]: input_str -> string to check against
* @params[in]: starts_with -> string to try to match with start of input string
* @return: int -> -2 if input is invalid, -1 if doesn't start with given string, or 0 if it does
*/
_CSPLIT_FUNC
int csplit_startswith(char* input_str, char* starts_with){
if(input_str == NULL || starts_with == NULL) return -2;
else if(strstr(input_str, starts_with) == input_str) return 0;
else return -1;
}
/**
* @brief Function that checks if a given string ends with another given string.
* @ingroup core
*
* @params[in]: input_str -> string to check against
* @params[in]: ends_with -> string to try to match with end of input string
* @return: int -> -2 if input is invalid, -1 if doesn't end with given string, or 0 if it does
*/
_CSPLIT_FUNC
int csplit_endswith(char* input_str, char* ends_with){
if(input_str == NULL || ends_with == NULL){
return -2;
}
else{
int ewith_len = strlen(ends_with);
int istr_len = strlen(input_str);
if(ewith_len > istr_len) return -1;
else if(strcmp(input_str + istr_len - ewith_len, ends_with) == 0) return 0;
else return -1;
}
}
/**
* @brief Function that runs csplit on a particular string from the end of the input. Called if max_splits < 0
* @ingroup intern
*
* @params[out]: list -> split input string into this list structure
* @params[in]: input_str -> input string
* @params[in]: token -> character on which to split
* @params[in]: max_splits -> maximum number of splits. If negative will split from end of string
* @return: err -> error code if there was a problem with csplitting.
*/
_CSPLIT_FUNC
CSplitError_t csplit_rstr(CSplitList_t* list, char* input_str, char* token, int max_splits){
CSplitError_t err = CSPLIT_SUCCESS;
int in_len = strlen(input_str);
int counter = 0;
int num_splits = 0;
int token_len = strlen(token);
char* current_location = input_str;
char* arr[in_len];
while((arr[counter] = strstr(current_location, token)) != NULL){
current_location = arr[counter] + 1;
counter++;
}
char* last_location = input_str + in_len;
while(counter >= 0 && num_splits >= max_splits){
current_location = arr[counter - 1] + token_len;
CSplitFragment_t* fragment = (CSplitFragment_t*) calloc(1, sizeof(CSplitFragment_t));
size_t req_buff_size;
if(num_splits == max_splits || counter == 0){
req_buff_size = in_len - strlen(last_location);
current_location = input_str;
}
else
req_buff_size = last_location - current_location;
err = csplit_push_to_list(list, fragment, req_buff_size);
strncpy(fragment->text, current_location, req_buff_size);
last_location = current_location - token_len;
num_splits--;
counter--;
}
err = csplit_reverse_list(list);
return err;
}
/**
* @brief Function that splits a given input string based on another string.
* @ingroup intern
*
* @params[out]: list -> output list splitting input str on string token
* @params[in]: input_str -> input string which will be split
* @params[in]: token -> string on which to split
* @params[in]: max_splits -> max number of splits to perform. Negative if starting from end of string.
* @return: err -> error code if there was a problem with csplitting.
*/
_CSPLIT_FUNC
CSplitError_t csplit_str(CSplitList_t* list, char* input_str, char* token, int max_splits){
if(max_splits < 0)
return csplit_rstr(list, input_str, token, max_splits);
CSplitError_t err = CSPLIT_SUCCESS;
int in_len = strlen(input_str);
int num_splits = 0;
int token_len = strlen(token);
char* current_location = input_str;
char* next_location;
while(current_location != NULL && num_splits <= max_splits){
CSplitFragment_t* fragment = (CSplitFragment_t*) calloc(1, sizeof(CSplitFragment_t));
next_location = strstr(current_location, token);
size_t req_buff_size;
if(next_location == NULL || (num_splits == max_splits))
req_buff_size = in_len - (current_location - input_str);
else if(next_location != NULL)
req_buff_size = next_location - current_location;
err = csplit_push_to_list(list, fragment, req_buff_size);
strncpy(fragment->text, current_location, req_buff_size);
if(next_location != NULL)
current_location = next_location + token_len;
else{
current_location = next_location;
}
num_splits++;
}
return err;
}
/**
* @brief Function that allows user to split based on a limited number of splits, either forward
* or in reverse. A max_splits >= len(input_str) will guarantee all possible splits
* @ingroup core
*
* @params[out]: list -> output list splitting input str on string token
* @params[in]: input_str -> input string which will be split
* @params[in]: token -> string on which to split
* @params[in]: max_splits -> max number of splits to perform. Negative if starting from end of string.
* @return: err -> error code if there was a problem with csplitting.
*/
_CSPLIT_FUNC
CSplitError_t csplit_lim(CSplitList_t* list, char* input_str, char* token, int max_splits){
CSplitError_t err = CSPLIT_SUCCESS;
// first check to see if input string is valid and token is valid
if(strlen(input_str) < 1 || strlen(token) < 1){
err = CSPLIT_TOO_SHORT;
}
else{
// otherwise we split on the given string
err = csplit_str(list, input_str, token, max_splits);
}
return err;
}
/**
* @brief Top level csplit function call. Outputs a csplit list split on a string token. Calls
* csplit_lim with max_splits = len(input_str), ensuring that all possible splits will be made.
* @ingroup core
*
* @params[out]: list -> output list splitting input str on string token
* @params[in]: input_str -> input string which will be split
* @params[in]: token -> string on which to split
* @return: err -> error code if there was a problem with csplitting.
*/
_CSPLIT_FUNC
CSplitError_t csplit(CSplitList_t* list, char* input_str, char* token){
return csplit_lim(list, input_str, token, (int) strlen(input_str));
}
/**
* @brief Function that runs csplit and then reverses the output.
* @ingroup core
*
* @params[out]: output_list -> output list splitting input str on string token
* @params[in]: input_str -> input string which will be split
* @params[in]: token -> string on which to split
* @return: err -> error code if there was a problem with csplitting.
*/
_CSPLIT_FUNC
CSplitError_t rcsplit(CSplitList_t* output_list, char* input_str, char* token){
CSplitError_t err = csplit(output_list, input_str, token);
if(err != CSPLIT_SUCCESS)
return err;
err = csplit_reverse_list(output_list);
return err;
}
#ifdef __cplusplus
}
#endif
#endif