strv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. *
  3. * Embedded Linux library
  4. *
  5. * Copyright (C) 2011-2014 Intel Corporation. All rights reserved.
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #ifdef HAVE_CONFIG_H
  23. #include <config.h>
  24. #endif
  25. #define _GNU_SOURCE
  26. #include <string.h>
  27. #include "strv.h"
  28. #include "private.h"
  29. #include "useful.h"
  30. /**
  31. * SECTION:strv
  32. * @short_description: String array functions
  33. *
  34. * String array functions
  35. */
  36. /**
  37. * l_strfreev:
  38. * @strlist: String list to free
  39. *
  40. * Frees a list of strings
  41. **/
  42. LIB_EXPORT void l_strfreev(char **strlist)
  43. {
  44. l_strv_free(strlist);
  45. }
  46. /**
  47. * l_strsplit:
  48. * @str: String to split
  49. * @sep: The delimiter character
  50. *
  51. * Splits a string into pieces which do not contain the delimiter character.
  52. * As a special case, an empty string is returned as an empty array, e.g.
  53. * an array with just the NULL element.
  54. *
  55. * Note that this function only works with ASCII delimiters.
  56. *
  57. * Returns: A newly allocated %NULL terminated string array. This array
  58. * should be freed using l_strfreev().
  59. **/
  60. LIB_EXPORT char **l_strsplit(const char *str, const char sep)
  61. {
  62. int len;
  63. int i;
  64. const char *p;
  65. char **ret;
  66. if (unlikely(!str))
  67. return NULL;
  68. if (str[0] == '\0')
  69. return l_new(char *, 1);
  70. for (p = str, len = 1; *p; p++)
  71. if (*p == sep)
  72. len += 1;
  73. ret = l_new(char *, len + 1);
  74. i = 0;
  75. p = str;
  76. len = 0;
  77. while (p[len]) {
  78. if (p[len] != sep) {
  79. len += 1;
  80. continue;
  81. }
  82. ret[i++] = l_strndup(p, len);
  83. p += len + 1;
  84. len = 0;
  85. }
  86. ret[i++] = l_strndup(p, len);
  87. return ret;
  88. }
  89. /**
  90. * l_strsplit_set:
  91. * @str: String to split
  92. * @separators: A set of delimiters
  93. *
  94. * Splits a string into pieces which do not contain the delimiter characters
  95. * that can be found in @separators.
  96. * As a special case, an empty string is returned as an empty array, e.g.
  97. * an array with just the NULL element.
  98. *
  99. * Note that this function only works with ASCII delimiters.
  100. *
  101. * Returns: A newly allocated %NULL terminated string array. This array
  102. * should be freed using l_strfreev().
  103. **/
  104. LIB_EXPORT char **l_strsplit_set(const char *str, const char *separators)
  105. {
  106. int len;
  107. int i;
  108. const char *p;
  109. char **ret;
  110. bool sep_table[256];
  111. if (unlikely(!str))
  112. return NULL;
  113. if (str[0] == '\0')
  114. return l_new(char *, 1);
  115. memset(sep_table, 0, sizeof(sep_table));
  116. for (p = separators; *p; p++)
  117. sep_table[(unsigned char) *p] = true;
  118. for (p = str, len = 1; *p; p++)
  119. if (sep_table[(unsigned char) *p] == true)
  120. len += 1;
  121. ret = l_new(char *, len + 1);
  122. i = 0;
  123. p = str;
  124. len = 0;
  125. while (p[len]) {
  126. if (sep_table[(unsigned char) p[len]] != true) {
  127. len += 1;
  128. continue;
  129. }
  130. ret[i++] = l_strndup(p, len);
  131. p += len + 1;
  132. len = 0;
  133. }
  134. ret[i++] = l_strndup(p, len);
  135. return ret;
  136. }
  137. /**
  138. * l_strjoinv:
  139. * @str_array: a %NULL terminated array of strings to join
  140. * @delim: Delimiting character
  141. *
  142. * Joins strings contanied in the @str_array into one long string delimited
  143. * by @delim.
  144. *
  145. * Returns: A newly allocated string that should be freed using l_free()
  146. */
  147. LIB_EXPORT char *l_strjoinv(char **str_array, const char delim)
  148. {
  149. size_t len = 0;
  150. unsigned int i;
  151. char *ret;
  152. char *p;
  153. if (unlikely(!str_array))
  154. return NULL;
  155. if (!str_array[0])
  156. return l_strdup("");
  157. for (i = 0; str_array[i]; i++)
  158. len += strlen(str_array[i]);
  159. len += 1 + i - 1;
  160. ret = l_malloc(len);
  161. p = stpcpy(ret, str_array[0]);
  162. for (i = 1; str_array[i]; i++) {
  163. *p++ = delim;
  164. p = stpcpy(p, str_array[i]);
  165. }
  166. return ret;
  167. }
  168. /**
  169. * l_strv_new:
  170. *
  171. * Returns: new emptry string array
  172. **/
  173. LIB_EXPORT char **l_strv_new(void)
  174. {
  175. return l_new(char *, 1);
  176. }
  177. /**
  178. * l_strv_free:
  179. * @str_array: a %NULL terminated array of strings
  180. *
  181. * Frees strings in @str_array and @str_array itself
  182. **/
  183. LIB_EXPORT void l_strv_free(char **str_array)
  184. {
  185. if (likely(str_array)) {
  186. int i;
  187. for (i = 0; str_array[i]; i++)
  188. l_free(str_array[i]);
  189. l_free(str_array);
  190. }
  191. }
  192. /**
  193. * l_strv_length:
  194. * @str_array: a %NULL terminated array of strings
  195. *
  196. * Returns: the number of strings in @str_array
  197. */
  198. LIB_EXPORT unsigned int l_strv_length(char **str_array)
  199. {
  200. unsigned int i = 0;
  201. if (unlikely(!str_array))
  202. return 0;
  203. while (str_array[i])
  204. i += 1;
  205. return i;
  206. }
  207. /**
  208. * l_strv_contains:
  209. * @str_array: a %NULL terminated array of strings
  210. * @item: An item to search for, must be not %NULL
  211. *
  212. * Returns: #true if @str_array contains item
  213. */
  214. LIB_EXPORT bool l_strv_contains(char **str_array, const char *item)
  215. {
  216. unsigned int i = 0;
  217. if (unlikely(!str_array || !item))
  218. return false;
  219. while (str_array[i]) {
  220. if (!strcmp(str_array[i], item))
  221. return true;
  222. i += 1;
  223. }
  224. return false;
  225. }
  226. /**
  227. * l_strv_append:
  228. * @str_array: a %NULL terminated array of strings or %NULL
  229. * @str: A string to be appened at the end of @str_array
  230. *
  231. * Returns: New %NULL terminated array of strings with @str added
  232. */
  233. LIB_EXPORT char **l_strv_append(char **str_array, const char *str)
  234. {
  235. char **ret;
  236. unsigned int i, len;
  237. if (unlikely(!str))
  238. return str_array;
  239. len = l_strv_length(str_array);
  240. ret = l_new(char *, len + 2);
  241. for (i = 0; i < len; i++)
  242. ret[i] = str_array[i];
  243. ret[i] = l_strdup(str);
  244. l_free(str_array);
  245. return ret;
  246. }
  247. LIB_EXPORT char **l_strv_append_printf(char **str_array,
  248. const char *format, ...)
  249. {
  250. va_list args;
  251. char **ret;
  252. va_start(args, format);
  253. ret = l_strv_append_vprintf(str_array, format, args);
  254. va_end(args);
  255. return ret;
  256. }
  257. LIB_EXPORT char **l_strv_append_vprintf(char **str_array,
  258. const char *format, va_list args)
  259. {
  260. char **ret;
  261. unsigned int i, len;
  262. if (unlikely(!format))
  263. return str_array;
  264. len = l_strv_length(str_array);
  265. ret = l_new(char *, len + 2);
  266. for (i = 0; i < len; i++)
  267. ret[i] = str_array[i];
  268. ret[i] = l_strdup_vprintf(format, args);
  269. l_free(str_array);
  270. return ret;
  271. }
  272. /**
  273. * l_strv_copy:
  274. * @str_array: a %NULL terminated array of strings or %NULL
  275. *
  276. * Returns: An independent copy of @str_array.
  277. */
  278. LIB_EXPORT char **l_strv_copy(char **str_array)
  279. {
  280. int i, len;
  281. char **copy;
  282. if (unlikely(!str_array))
  283. return NULL;
  284. for (len = 0; str_array[len]; len++);
  285. copy = l_malloc(sizeof(char *) * (len + 1));
  286. for (i = len; i >= 0; i--)
  287. copy[i] = l_strdup(str_array[i]);
  288. return copy;
  289. }