tor  0.4.2.1-alpha-dev
smartlist_split.c
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
13 
14 #include "lib/err/torerr.h"
15 #include "lib/string/util_string.h"
17 #include "lib/malloc/malloc.h"
18 
19 #include <string.h>
20 
36 int
37 smartlist_split_string(smartlist_t *sl, const char *str, const char *sep,
38  int flags, int max)
39 {
40  const char *cp, *end, *next;
41  int n = 0;
42 
43  raw_assert(sl);
44  raw_assert(str);
45 
46  cp = str;
47  while (1) {
48  if (flags&SPLIT_SKIP_SPACE) {
49  while (TOR_ISSPACE(*cp)) ++cp;
50  }
51 
52  if (max>0 && n == max-1) {
53  end = strchr(cp,'\0');
54  } else if (sep) {
55  end = strstr(cp,sep);
56  if (!end)
57  end = strchr(cp,'\0');
58  } else {
59  for (end = cp; *end && *end != '\t' && *end != ' '; ++end)
60  ;
61  }
62 
63  raw_assert(end);
64 
65  if (!*end) {
66  next = NULL;
67  } else if (sep) {
68  next = end+strlen(sep);
69  } else {
70  next = end+1;
71  while (*next == '\t' || *next == ' ')
72  ++next;
73  }
74 
75  if (flags&SPLIT_SKIP_SPACE) {
76  while (end > cp && TOR_ISSPACE(*(end-1)))
77  --end;
78  }
79  if (end != cp || !(flags&SPLIT_IGNORE_BLANK)) {
80  char *string = tor_strndup(cp, end-cp);
81  if (flags&SPLIT_STRIP_SPACE)
82  tor_strstrip(string, " ");
83  smartlist_add(sl, string);
84  ++n;
85  }
86  if (!next)
87  break;
88  cp = next;
89  }
90 
91  return n;
92 }
void smartlist_add(smartlist_t *sl, void *element)
void tor_strstrip(char *s, const char *strip)
Definition: util_string.c:111
Header for smartlist_split.c.
Header for util_string.c.
Headers for util_malloc.c.
Top-level declarations for the smartlist_t dynamic array type.
Locale-independent character-type inspection (header)
Headers for torerr.c.
int smartlist_split_string(smartlist_t *sl, const char *str, const char *sep, int flags, int max)