-
Notifications
You must be signed in to change notification settings - Fork 0
/
qualified.c
55 lines (51 loc) · 1.65 KB
/
qualified.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TRUE 1;
#define FALSE 0;
/*
* strqsep works similarly to the standard lib function 'strsep'.
* It accepts a third parameter, qual, which is a text qualifier.
* The qualifier is often a double or single quote, and wraps a field
* in delimited data that MIGHT contain the delimiter character itself.
*
* This allows the caller to get the first char pointer to a field (token)
* from the input in an effort to parse fields from delimited data; and also
* disregards the delimiter character IF it is within the qualifier.
*
* The input 'strp' WILL be modified. Similarly to strsep, the delimiter
* of the input will be lost and replaced with a null terminator '\0'.
* The char pointer returned will be a pointer to the next token, not including
* the delimiter. If NULL was provided as input, or the end of strp was met,
* then NULL shall be returned.
*/
char *
strqsep(char **strp, const char *delim, const char *qual) {
char *field;
int in_quotes = FALSE;
size_t count = 0;
if (strp == NULL || *strp == NULL || **strp == '\0') {
return NULL;
}
field = *strp;
do {
if (**strp == *qual) {
in_quotes ^= 1;
(*strp)++;
count++;
continue;
}
if (**strp == *delim) {
if (in_quotes) {
(*strp)++;
count++;
continue;
}
*(*strp)++ = '\0'; // replace delimiter to terminate string for the returned field
break;
}
(*strp)++;
count++;
} while (**strp != '\0');
return field;
}