aboutsummaryrefslogtreecommitdiffstats
path: root/common/stringop.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/stringop.c')
-rw-r--r--common/stringop.c115
1 files changed, 0 insertions, 115 deletions
diff --git a/common/stringop.c b/common/stringop.c
index 709be684..b9c9a193 100644
--- a/common/stringop.c
+++ b/common/stringop.c
@@ -150,29 +150,6 @@ void free_argv(int argc, char **argv) {
150 free(argv); 150 free(argv);
151} 151}
152 152
153char *code_strstr(const char *haystack, const char *needle) {
154 /* TODO */
155 return strstr(haystack, needle);
156}
157
158char *code_strchr(const char *str, char delimiter) {
159 int in_string = 0, in_character = 0;
160 int i = 0;
161 while (str[i] != '\0') {
162 if (str[i] == '"' && !in_character) {
163 in_string = !in_string;
164 } else if (str[i] == '\'' && !in_string) {
165 in_character = !in_character;
166 } else if (!in_character && !in_string) {
167 if (str[i] == delimiter) {
168 return (char *)str + i;
169 }
170 }
171 ++i;
172 }
173 return NULL;
174}
175
176int unescape_string(char *string) { 153int unescape_string(char *string) {
177 /* TODO: More C string escapes */ 154 /* TODO: More C string escapes */
178 int len = strlen(string); 155 int len = strlen(string);
@@ -276,84 +253,6 @@ char *join_args(char **argv, int argc) {
276 return res; 253 return res;
277} 254}
278 255
279static bool has_whitespace(const char *str) {
280 while (*str) {
281 if (isspace(*str)) {
282 return true;
283 }
284 ++str;
285 }
286 return false;
287}
288
289/**
290 * Add quotes around any argv with whitespaces.
291 */
292void add_quotes(char **argv, int argc) {
293 int i;
294 for (i = 0; i < argc; ++i) {
295 if (has_whitespace(argv[i])) {
296 int len = strlen(argv[i]) + 3;
297 char *tmp = argv[i];
298 argv[i] = malloc(len * sizeof(char));
299 snprintf(argv[i], len, "\"%s\"", tmp);
300 free(tmp);
301 }
302 }
303}
304
305/*
306 * Join a list of strings, adding separator in between. Separator can be NULL.
307 */
308char *join_list(list_t *list, char *separator) {
309 if (!sway_assert(list != NULL, "list != NULL") || list->length == 0) {
310 return NULL;
311 }
312
313 size_t len = 1; // NULL terminator
314 size_t sep_len = 0;
315 if (separator != NULL) {
316 sep_len = strlen(separator);
317 len += (list->length - 1) * sep_len;
318 }
319
320 for (int i = 0; i < list->length; i++) {
321 len += strlen(list->items[i]);
322 }
323
324 char *res = malloc(len);
325
326 char *p = res + strlen(list->items[0]);
327 strcpy(res, list->items[0]);
328
329 for (int i = 1; i < list->length; i++) {
330 if (sep_len) {
331 memcpy(p, separator, sep_len);
332 p += sep_len;
333 }
334 strcpy(p, list->items[i]);
335 p += strlen(list->items[i]);
336 }
337
338 *p = '\0';
339
340 return res;
341}
342
343char *cmdsep(char **stringp, const char *delim) {
344 // skip over leading delims
345 char *head = *stringp + strspn(*stringp, delim);
346 // Find end token
347 char *tail = *stringp += strcspn(*stringp, delim);
348 // Set stringp to beginning of next token
349 *stringp += strspn(*stringp, delim);
350 // Set stringp to null if last token
351 if (!**stringp) *stringp = NULL;
352 // Nullify end of first token
353 *tail = 0;
354 return head;
355}
356
357char *argsep(char **stringp, const char *delim) { 256char *argsep(char **stringp, const char *delim) {
358 char *start = *stringp; 257 char *start = *stringp;
359 char *end = start; 258 char *end = start;
@@ -389,17 +288,3 @@ char *argsep(char **stringp, const char *delim) {
389 found: 288 found:
390 return start; 289 return start;
391} 290}
392
393const char *strcasestr(const char *haystack, const char *needle) {
394 size_t needle_len = strlen(needle);
395 const char *pos = haystack;
396 const char *end = pos + strlen(haystack) - needle_len;
397
398 while (pos <= end) {
399 if (strncasecmp(pos, needle, needle_len) == 0) {
400 return pos;
401 }
402 ++pos;
403 }
404 return NULL;
405}