-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwordsplit.c
More file actions
150 lines (136 loc) · 3.03 KB
/
Copy pathwordsplit.c
File metadata and controls
150 lines (136 loc) · 3.03 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
#include "wordsplit.h"
#include "utlist.h"
#include <ctype.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef struct ws_list {
char *word;
struct ws_list *next, *prev;
} WS_List;
char *str_iterate(char *str) {
assert(str != NULL);
while(*str != '\0' && isspace(*str)) str++;
if(*str == '\0') return NULL;
int in_quote = 0;
int mid_word = 0;
if(*str == '"'){
in_quote = 1;
str++;
}
for(int i = 0; str[i] != '\0' && !isspace(str[i]); i++) {
if(str[i] == '"'){
in_quote = 1;
mid_word = 1;
break;
}
}
if(mid_word) {
while(*str != '"') {
if(*str == '\0') return NULL;
str++;
}
str++;
}
if(in_quote) {
while(*str != '"') {
if(*str == '\0') return NULL;
str++;
}
}
while(!isspace(*str)) {
if(*str == '\0') return NULL;
str++;
}
while(isspace(*str)) {
if(*str == '\0') return NULL;
str++;
}
return str;
}
char *end_of_word(char *str) {
assert(str != NULL);
char *next_word = str_iterate(str);
if(next_word == NULL) {
while(*str != '\0') str++;
return str;
}
next_word--;
while(isspace(*next_word)) {
next_word--;
}
next_word++;
return next_word;
}
void shuffle_down(char *str) {
assert(str);
char *next = str+1;
while(*str != '\0') {
*str = *next;
str++;
next++;
}
}
void remove_quotes(char *str) {
assert(str);
while(*str != '\0') {
if(*str == '"') {
shuffle_down(str);
} else {
str++;
}
}
}
WS_List *string_split(char *str) {
if(str == NULL) return NULL;
WS_List *words = NULL;
while(isspace(*str)) str++;
int len = strlen(str)-1;
for(int i = len; i <= 0 && isspace(str[i]); i--) {
str[i] = '\0';
}
char *start = str;
char *end;
while(start != NULL && *start != '\0') {
char *copy = strdup(start);
end = end_of_word(copy);
assert(end != NULL);
*end = '\0';
remove_quotes(copy);
WS_List *item = malloc(sizeof(WS_List));
assert(item);
item->word = copy;
DL_APPEND(words, item);
start = str_iterate(start);
}
return words;
}
void ws_free(WS_List *list) {
WS_List *word, *tmp;
DL_FOREACH_SAFE(list, word, tmp) {
DL_DELETE(list, word);
free(word->word);
free(word);
}
}
int ws_split(char *str, char ***words) {
assert(str && words);
WS_List *ws = string_split(str);
WS_List *tmp;
int size = 0;
DL_COUNT(ws, tmp, size);
*words = malloc(sizeof(char*) * size);
int count = 0;
DL_FOREACH(ws, tmp) {
(*words)[count++] = strdup(tmp->word);
}
ws_free(ws);
return size;
}
int ws_len(char *str) {
if(str == NULL) return -1;
int count = 0;
while((str = str_iterate(str)) != NULL) count++;
return count;
}