-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmstring.c
167 lines (112 loc) · 2.84 KB
/
mstring.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
/*
Lisenced under GPL V.2 or later
Implements the split() and join() functions, like the ones in modern programming
languages.
There is already a strtok function in string.h that splits the string, but it
modifies its argument and requires repeated calls.
Author: github.com/quakehead
ChangeLog:
* 2/10/13
- (after a long time!)
- one malloc didn't have +1 for null char
- added free_parts()
* 4/7/09
- added join()
* 4/4/09 initial version
- uses realloc for each new token. (allocates a char pointer)
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
// This function returns an array of tokens.
// The input string is unmodified.
char** split(const char* cstr, const char* delim) {
/* returns { "1", "2", "3", NULL }
*
* reallocate 1 new pointer at a time.
*/
char* str;
char** pieces = NULL;
char* piece;
int count = 0;
// make a copy of the string
str = malloc(strlen(cstr) + 1);
if (str != NULL) {
strcpy(str, cstr);
}
piece = strtok(str, delim);
while (piece != NULL) {
count++;
// change size of pointer array - idea from cplusplus.com
pieces = (char**) realloc(pieces, count * sizeof(char*));
if (pieces == NULL) {
fprintf(stderr, "split(): Error allocating memory");
return NULL;
}
// allocate space for str
pieces[count-1] = malloc(strlen(piece) + 1);
strcpy(pieces[count-1], piece);
piece = strtok(NULL, delim);
}
// pices = {"one", "two", NULL}
pieces = (char**) realloc(pieces, (count+1) * sizeof(char*));
if (pieces == NULL) {
fprintf(stderr, "split(): Error allocating memory");
return NULL;
}
// last element
pieces[count] = NULL;
free(str);
return pieces;
}
char* join(const char** parts, const char* delim) {
const char** part;
char* result = NULL;
int delim_len;
int result_len;
if ((NULL == parts) || (NULL == *parts)) {
return NULL;
}
if (NULL == delim) {
delim_len = 0;
}
else {
delim_len = strlen(delim);
}
// 0x1 0x2 0x3
// pices = {"one", "two", NULL}; delim = " "
// result = "one two"
// count the length of result
result_len = -delim_len;
for (part = parts; NULL != *part; part++) {
result_len += delim_len + strlen(*part);
}
result = (char*) malloc((result_len+1) * sizeof(char));
if (NULL == result) {
// if running on 64k
fprintf(stderr, "join(): Error allocating memory");
return NULL;
}
result[0] = '\0';
// construct result
for (part = parts; NULL != *part; part++) {
// if last part, don't append delim
if (*(part+1) != NULL) {
strcat(result, *part);
strcat(result, (delim != NULL)? delim : "");
}
else {
strcat(result, *part);
}
}
return result;
}
void free_parts(char** parts) {
char** part;
// free the strings
for (part = parts; NULL != *part; part++) {
free(*part);
}
// free the pointer array
free(parts);
}