-
Notifications
You must be signed in to change notification settings - Fork 2
/
strutils.c
215 lines (186 loc) · 5.96 KB
/
strutils.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
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
/***********************************************************************/ /**
* @file strutils.c
*
* Generic routines for string manipulation
*
* This file is part of the DataLink Library.
*
* Copyright (c) 2023 Chad Trabant, EarthScope Data Services
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
***************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "libdali.h"
/***********************************************************************/ /**
* @brief Parse/split a string on a specified delimiter
*
* Splits a 'string' on 'delim' and puts each part into a linked list
* pointed to by 'list' (a pointer to a pointer). The last entry has
* it's 'next' set to 0. All elements are NULL terminated strings.
*
* It is up to the caller to free the memory associated with the
* returned list. To facilitate freeing this special string list
* dl_strparse() can be called with both 'string' and 'delim' set to
* NULL and then the linked list is traversed and the memory used is
* free'd and the list pointer is set to NULL.
*
* @param string String to parse/split
* @param delim Delimiter to split string on
* @param list Returned list of sub-strings.
*
* @return The number of elements added to the list, or 0 when freeing
* the linked list.
***************************************************************************/
int
dl_strparse (const char *string, const char *delim, DLstrlist **list)
{
const char *beg; /* beginning of element */
const char *del; /* delimiter */
int stop = 0;
int count = 0;
int total;
DLstrlist *curlist = 0;
DLstrlist *tmplist = 0;
if (string != NULL && delim != NULL)
{
total = strlen (string);
beg = string;
while (!stop)
{
/* Find delimiter */
del = strstr (beg, delim);
/* Delimiter not found or empty */
if (del == NULL || strlen (delim) == 0)
{
del = string + strlen (string);
stop = 1;
}
tmplist = (DLstrlist *)malloc (sizeof (DLstrlist));
tmplist->next = 0;
tmplist->element = (char *)malloc (del - beg + 1);
strncpy (tmplist->element, beg, (del - beg));
tmplist->element[(del - beg)] = '\0';
/* Add this to the list */
if (count++ == 0)
{
curlist = tmplist;
*list = curlist;
}
else
{
curlist->next = tmplist;
curlist = curlist->next;
}
/* Update 'beg' */
beg = (del + strlen (delim));
if ((beg - string) > total)
break;
}
return count;
}
else
{
curlist = *list;
while (curlist != NULL)
{
tmplist = curlist->next;
free (curlist->element);
free (curlist);
curlist = tmplist;
}
*list = NULL;
return 0;
}
} /* End of dl_strparse() */
/***********************************************************************/ /**
* @brief Copy a string while removing space charaters
*
* Copy @a length characters from @a source to @a dest while removing
* all spaces. The result is left justified and always null
* terminated. The source string must have at least @a length
* characters and the destination string must have enough room needed
* for the non-space characters within @a length and the null
* terminator.
*
* @param dest Destination string
* @param source String to copy
* @param length Copy up to a maximum of this many characters to @a dest
*
* @return The number of characters (not including the null
* terminator) in the destination string.
***************************************************************************/
int
dl_strncpclean (char *dest, const char *source, int length)
{
int sidx, didx;
for (sidx = 0, didx = 0; sidx < length; sidx++)
{
if (*(source + sidx) != ' ')
{
*(dest + didx) = *(source + sidx);
didx++;
}
}
*(dest + didx) = '\0';
return didx;
} /* End of dl_strncpclean() */
/***********************************************************************/ /**
* @brief Concatinate one string to another growing the destination as needed
*
* Concatinate one string to another with a delimiter in-between
* growing the destination string as needed up to a maximum length.
*
* @param string Destination string to be added to
* @param add String to add to @a string
* @param delim Optional delimiter between added strings (cannot be NULL, but can be an empty string)
* @param maxlen Maximum number of bytes to grow @a string
*
* @return 0 on success, -1 on memory allocation error and -2 when
* string would grow beyond maximum length.
***************************************************************************/
int
dl_addtostring (char **string, char *add, char *delim, int maxlen)
{
int length;
char *ptr;
if (!string || !add)
return -1;
/* If string is empty, allocate space and copy the addition */
if (!*string)
{
length = strlen (add) + 1;
if (length > maxlen)
return -2;
if ((*string = (char *)malloc (length)) == NULL)
return -1;
strcpy (*string, add);
}
/* Otherwise tack on the addition with a delimiter */
else
{
length = strlen (*string) + strlen (delim) + strlen (add) + 1;
if (length > maxlen)
return -2;
if ((ptr = (char *)realloc (*string, length)) == NULL)
return -1;
*string = ptr;
strcat (*string, delim);
strcat (*string, add);
}
return 0;
} /* End of dl_addtostring() */