-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringfuncs.c
More file actions
164 lines (131 loc) · 3.57 KB
/
stringfuncs.c
File metadata and controls
164 lines (131 loc) · 3.57 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
UNIX Shell implementation
Solution for Cow Shell Lab, Operating Systems
Williams College Department of Computer Science
string utility functions
Copyright 2006, James D. Teresco
*/
#include <string.h>
#include <stdlib.h>
#include "stringfuncs.h"
/*
Function to remove leading and trailing spaces from a C string.
This function is destructive in that the null terminator will be
moved forward through any white space. The return value is the
first non-whitespace character at the start of the string
*/
char *trim_leading_trailing_spaces(char *str) {
char *ptr;
/* start with trailing spaces */
ptr = str;
while (*ptr) ptr++;
/* we are pointing at the null terminator */
if (ptr != str) {
ptr--;
while ((ptr >= str) &&
((*ptr == ' ') || (*ptr == '\t'))) {
*ptr = '\0';
ptr--;
}
}
/* we have eliminated trailing whitespace, now eliminate at the start */
ptr = str;
while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
ptr++;
return ptr;
}
/*
Function to allocate a copy of a C string, but with leading and
trailing spaces removed. The original string is unmodified. The
return value should be deallocated using free();
*/
char *trimmed_strdup(char *str) {
char *ptr, *retval;
/* skip over white space at the start */
ptr = str;
while (*ptr && ((*ptr == ' ') || (*ptr == '\t')))
ptr++;
/* now duplicate the string (so the duplicate will have space for
trailing whitespace */
retval = strdup(ptr);
/* eliminate trailing whitespace */
ptr = retval;
while (*ptr) ptr++;
/* we are pointing at the null terminator */
if (ptr != retval) {
ptr--;
while ((ptr >= retval) &&
((*ptr == ' ') || (*ptr == '\t'))) {
*ptr = '\0';
ptr--;
}
}
return retval;
}
/* string list utility functions */
/* construct a new (empty) stringlist */
struct stringlist *new_stringlist() {
struct stringlist *retval;
retval = (struct stringlist *)malloc(sizeof(struct stringlist));
retval->size = 0;
retval->head = NULL;
return retval;
}
/* construct a new (empty) stringlist */
static struct stringlist_entry *new_stringlist_entry(char *val) {
struct stringlist_entry *retval;
retval = (struct stringlist_entry *)malloc(sizeof(struct stringlist_entry));
retval->val = val;
retval->next = NULL;
return retval;
}
/* append a new string to the end of an existing stringlist (which cannot
be empty) */
void stringlist_append(struct stringlist *sl, char *str) {
struct stringlist_entry *finger;
if (sl->head) {
finger = sl->head;
/* find the end */
while (finger->next) {
finger = finger->next;
}
/* create a new entry at the end */
finger->next = new_stringlist_entry(str);
}
else {
sl->head = new_stringlist_entry(str);
}
}
/* convert the stringlist into an array of strings suitable for
passing as an argument list. The returned pointer should be
returned to the system with free() */
char **stringlist_getarray(struct stringlist *sl) {
int len, i;
struct stringlist_entry *finger;
char **retval;
len = 0;
finger = sl->head;
while (finger) {
len++;
finger = finger->next;
}
retval = (char **)malloc((len+1)*sizeof(char *));
finger = sl->head;
for (i=0; i<len; i++) {
retval[i] = finger->val;
finger = finger->next;
}
retval[len] = NULL;
return retval;
}
/* clean up a stringlist */
void stringlist_free(struct stringlist *sl) {
struct stringlist_entry *finger, *prev;
finger = sl->head;
while (finger) {
prev = finger;
finger = finger->next;
free(prev);
}
free(sl);
}