str.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /** @file str.c
  2. * @brief String related functions
  3. *
  4. * This file implements some often used string functions.
  5. * Some functions are more portable versions of standard
  6. * functions but others are original ones.
  7. */
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include <stdio.h>
  12. #include <stdarg.h>
  13. #include "faux/ctype.h"
  14. #include "faux/str.h"
  15. /** @brief Free the memory allocated for the string.
  16. *
  17. * Safely free the memory allocated for the string. You can use NULL
  18. * pointer with this function. POSIX's free() checks for the NULL pointer
  19. * but not all systems do so.
  20. *
  21. * @param [in] str String to free
  22. */
  23. void faux_str_free(char *str)
  24. {
  25. faux_free(str);
  26. }
  27. /** @brief Duplicates the string.
  28. *
  29. * Duplicates the string. Same as standard strdup() function. Allocates
  30. * memory with malloc(). Checks for NULL pointer.
  31. *
  32. * @warning Resulting string must be freed by faux_str_free().
  33. *
  34. * @param [in] str String to duplicate.
  35. * @return Pointer to allocated string or NULL.
  36. */
  37. char *faux_str_dup(const char *str)
  38. {
  39. if (!str)
  40. return NULL;
  41. return strdup(str);
  42. }
  43. /** @brief Duplicates the first n bytes of the string.
  44. *
  45. * Duplicates at most n bytes of the string. Allocates
  46. * memory with malloc(). Checks for NULL pointer. Function will allocate
  47. * n + 1 bytes to store string and terminating null byte.
  48. *
  49. * @warning Resulting string must be freed by faux_str_free().
  50. *
  51. * @param [in] str String to duplicate.
  52. * @param [in] n Number of bytes to copy.
  53. * @return Pointer to allocated string or NULL.
  54. */
  55. char *faux_str_dupn(const char *str, size_t n)
  56. {
  57. char *res = NULL;
  58. size_t len = 0;
  59. if (!str)
  60. return NULL;
  61. // Search for terminating '\0' among first n bytes
  62. // Don't use strlen() because it can be not null-terminated.
  63. for (len = 0; len < n; len++)
  64. if ('\0' == str[len])
  65. break;
  66. len = (len < n) ? len : n;
  67. res = faux_zmalloc(len + 1);
  68. if (!res)
  69. return NULL;
  70. strncpy(res, str, len);
  71. res[len] = '\0';
  72. return res;
  73. }
  74. /** @brief Generates lowercase copy of input string.
  75. *
  76. * Allocates the copy of input string and convert that copy to lowercase.
  77. *
  78. * @warning Resulting string must be freed by faux_str_free().
  79. *
  80. * @param [in] str String to convert.
  81. * @return Pointer to lowercase string copy or NULL.
  82. */
  83. char *faux_str_tolower(const char *str)
  84. {
  85. char *res = faux_str_dup(str);
  86. char *p = res;
  87. if (!res)
  88. return NULL;
  89. while (*p) {
  90. *p = faux_ctype_tolower(*p);
  91. p++;
  92. }
  93. return res;
  94. }
  95. /** @brief Generates uppercase copy of input string.
  96. *
  97. * Allocates the copy of input string and convert that copy to uppercase.
  98. *
  99. * @warning Resulting string must be freed by faux_str_free().
  100. *
  101. * @param [in] str String to convert.
  102. * @return Pointer to lowercase string copy or NULL.
  103. */
  104. char *faux_str_toupper(const char *str)
  105. {
  106. char *res = faux_str_dup(str);
  107. char *p = res;
  108. if (!res)
  109. return NULL;
  110. while (*p) {
  111. *p = faux_ctype_toupper(*p);
  112. p++;
  113. }
  114. return res;
  115. }
  116. /** @brief Add n bytes of text to existent string.
  117. *
  118. * Concatenate two strings. Add n bytes of second string to the end of the
  119. * first one. The first argument is address of string pointer. The pointer
  120. * can be changed due to realloc() features. The first pointer can be NULL.
  121. * In this case the memory will be malloc()-ed and stored to the first pointer.
  122. *
  123. * @param [in,out] str Address of first string pointer.
  124. * @param [in] text Text to add to the first string.
  125. * @param [in] n Number of bytes to add.
  126. * @return Pointer to resulting string or NULL.
  127. */
  128. char *faux_str_catn(char **str, const char *text, size_t n)
  129. {
  130. size_t str_len = 0;
  131. size_t text_len = 0;
  132. char *res = NULL;
  133. char *p = NULL;
  134. if (!text)
  135. return *str;
  136. str_len = (*str) ? strlen(*str) : 0;
  137. text_len = strlen(text);
  138. text_len = (text_len < n) ? text_len : n;
  139. res = realloc(*str, str_len + text_len + 1);
  140. if (!res)
  141. return NULL;
  142. p = res + str_len;
  143. strncpy(p, text, text_len);
  144. p[text_len] = '\0';
  145. *str = res;
  146. return res;
  147. }
  148. /** @brief Add some text to existent string.
  149. *
  150. * Concatenate two strings. Add second string to the end of the first one.
  151. * The first argument is address of string pointer. The pointer can be
  152. * changed due to realloc() features. The first pointer can be NULL. In this
  153. * case the memory will be malloc()-ed and stored to the first pointer.
  154. *
  155. * @param [in,out] str Address of first string pointer.
  156. * @param [in] text Text to add to the first string.
  157. * @return Pointer to resulting string or NULL.
  158. */
  159. char *faux_str_cat(char **str, const char *text)
  160. {
  161. size_t len = 0;
  162. if (!text)
  163. return *str;
  164. len = strlen(text);
  165. return faux_str_catn(str, text, len);
  166. }
  167. /** @brief Add multiply text strings to existent string.
  168. *
  169. * Concatenate multiply strings. Add next string to the end of the previous one.
  170. * The first argument is address of string pointer. The pointer can be
  171. * changed due to realloc() features. The first pointer can be NULL. In this
  172. * case the memory will be malloc()-ed and stored to the first pointer.
  173. * The last argument must be 'NULL'. It marks the last argument within
  174. * variable arguments list.
  175. *
  176. * @warning If last argument is not 'NULL' then behaviour is undefined.
  177. *
  178. * @param [in,out] str Address of first string pointer.
  179. * @param [in] text Text to add to the first string.
  180. * @return Pointer to resulting string or NULL.
  181. */
  182. char *faux_str_mcat(char **str, ...)
  183. {
  184. va_list ap;
  185. const char *arg = NULL;
  186. char *retval = *str;
  187. va_start(ap, str);
  188. while ((arg = va_arg(ap, const char *))) {
  189. retval = faux_str_cat(str, arg);
  190. }
  191. va_end(ap);
  192. return retval;
  193. }
  194. /** @brief Allocates memory and vsprintf() to it.
  195. *
  196. * Function tries to find out necessary amount of memory for specified format
  197. * string and arguments. Format is same as for vsprintf() function. Then
  198. * function allocates memory for resulting string and vsprintf() to it. So
  199. * user doesn't need to allocate buffer himself. Function returns allocated
  200. * string that need to be freed by faux_str_free() function later.
  201. *
  202. * @warning The returned pointer must be free by faux_str_free().
  203. *
  204. * @param [in] fmt Format string like the sprintf()'s fmt.
  205. * @param [in] ap The va_list argument.
  206. * @return Allocated resulting string or NULL on error.
  207. */
  208. char *faux_str_vsprintf(const char *fmt, va_list ap)
  209. {
  210. int size = 1;
  211. char calc_buf[1] = "";
  212. char *line = NULL;
  213. va_list ap2;
  214. // Calculate buffer size
  215. va_copy(ap2, ap);
  216. size = vsnprintf(calc_buf, size, fmt, ap2);
  217. va_end(ap2);
  218. // The snprintf() prior to 2.0.6 glibc version returns -1 if string
  219. // was truncated. The later glibc returns required buffer size.
  220. // The calc_buf can be NULL and size can be 0 for recent glibc but
  221. // probably some exotic implementations can break on it. So use
  222. // minimal buffer with length = 1.
  223. if (size < 0)
  224. return NULL;
  225. size++; // Additional byte for '\0'
  226. line = faux_zmalloc(size);
  227. if (!line) // Memory problems
  228. return NULL;
  229. // Format real string
  230. size = vsnprintf(line, size, fmt, ap);
  231. if (size < 0) { // Some problems
  232. faux_str_free(line);
  233. return NULL;
  234. }
  235. return line;
  236. }
  237. /** @brief Allocates memory and sprintf() to it.
  238. *
  239. * Function tries to find out necessary amount of memory for specified format
  240. * string and arguments. Format is same as for sprintf() function. Then
  241. * function allocates memory for resulting string and sprintf() to it. So
  242. * user doesn't need to allocate buffer himself. Function returns allocated
  243. * string that need to be freed by faux_str_free() function later.
  244. *
  245. * @warning The returned pointer must be free by faux_str_free().
  246. *
  247. * @param [in] fmt Format string like the sprintf()'s fmt.
  248. * @param [in] arg Number of arguments.
  249. * @return Allocated resulting string or NULL on error.
  250. */
  251. char *faux_str_sprintf(const char *fmt, ...)
  252. {
  253. char *line = NULL;
  254. va_list ap;
  255. va_start(ap, fmt);
  256. line = faux_str_vsprintf(fmt, ap);
  257. va_end(ap);
  258. return line;
  259. }
  260. /** @brief Service function to compare to chars in right way.
  261. *
  262. * The problem is char type can be signed or unsigned on different
  263. * platforms. So stright comparision can return different results.
  264. *
  265. * @param [in] char1 First char
  266. * @param [in] char2 Second char
  267. * @return
  268. * < 0 if char1 < char2
  269. * = 0 if char1 = char2
  270. * > 0 if char1 > char2
  271. */
  272. static int faux_str_cmp_chars(char char1, char char2)
  273. {
  274. unsigned char ch1 = (unsigned char)char1;
  275. unsigned char ch2 = (unsigned char)char2;
  276. return (int)ch1 - (int)ch2;
  277. }
  278. /** @brief Compare n first characters of two strings.
  279. *
  280. * @param [in] str1 First string to compare.
  281. * @param [in] str2 Second string to compare.
  282. * @param [in] n Number of characters to compare.
  283. * @return < 0, 0, > 0, see the strcasecmp().
  284. */
  285. int faux_str_cmpn(const char *str1, const char *str2, size_t n)
  286. {
  287. if (!str1 && !str2) // Empty strings are equal
  288. return 0;
  289. if (!str1) // Consider NULL string to be less then empty string
  290. return -1;
  291. if (!str2) // Consider NULL string to be less then empty string
  292. return 1;
  293. return strncmp(str1, str2, n);
  294. }
  295. /** @brief Compare two strings.
  296. *
  297. * @param [in] str1 First string to compare.
  298. * @param [in] str2 Second string to compare.
  299. * @return < 0, 0, > 0, see the strcmp().
  300. */
  301. int faux_str_cmp(const char *str1, const char *str2)
  302. {
  303. if (!str1 && !str2) // Empty strings are equal
  304. return 0;
  305. if (!str1) // Consider NULL string to be less then empty string
  306. return -1;
  307. if (!str2) // Consider NULL string to be less then empty string
  308. return 1;
  309. return strcmp(str1, str2);
  310. }
  311. /** @brief Compare n first characters of two strings ignoring case.
  312. *
  313. * The difference beetween this function an standard strncasecmp() is
  314. * faux function uses faux ctype functions. It can be important for
  315. * portability.
  316. *
  317. * @param [in] str1 First string to compare.
  318. * @param [in] str2 Second string to compare.
  319. * @param [in] n Number of characters to compare.
  320. * @return < 0, 0, > 0, see the strcasecmp().
  321. */
  322. int faux_str_casecmpn(const char *str1, const char *str2, size_t n)
  323. {
  324. const char *p1 = str1;
  325. const char *p2 = str2;
  326. size_t num = n;
  327. while (*p1 != '\0' && *p2 != '\0' && num != 0) {
  328. int res = faux_str_cmp_chars(
  329. faux_ctype_tolower(*p1), faux_ctype_tolower(*p2));
  330. if (res != 0)
  331. return res;
  332. p1++;
  333. p2++;
  334. num--;
  335. }
  336. if (0 == n) // It means n first characters are equal.
  337. return 0;
  338. return faux_str_cmp_chars(
  339. faux_ctype_tolower(*p1), faux_ctype_tolower(*p2));
  340. }
  341. /** @brief Compare two strings ignoring case.
  342. *
  343. * The difference beetween this function an standard strcasecmp() is
  344. * faux function uses faux ctype functions. It can be important for
  345. * portability.
  346. *
  347. * @param [in] str1 First string to compare.
  348. * @param [in] str2 Second string to compare.
  349. * @return < 0, 0, > 0, see the strcasecmp().
  350. */
  351. int faux_str_casecmp(const char *str1, const char *str2)
  352. {
  353. const char *p1 = str1;
  354. const char *p2 = str2;
  355. if (!p1 && !p2) // Empty strings are equal
  356. return 0;
  357. if (!p1) // Consider NULL string to be less then empty string
  358. return -1;
  359. if (!p2) // Consider NULL string to be less then empty string
  360. return 1;
  361. while (*p1 != '\0' && *p2 != '\0') {
  362. int res = faux_str_cmp_chars(
  363. faux_ctype_tolower(*p1), faux_ctype_tolower(*p2));
  364. if (res != 0)
  365. return res;
  366. p1++;
  367. p2++;
  368. }
  369. return faux_str_cmp_chars(
  370. faux_ctype_tolower(*p1), faux_ctype_tolower(*p2));
  371. }
  372. /** @brief Finds the first occurrence of the substring in the string
  373. *
  374. * Function is a faux version of strcasestr() function.
  375. *
  376. * @param [in] haystack String to find substring in it.
  377. * @param [in] needle Substring to find.
  378. * @return
  379. * Pointer to first occurence of substring in the string.
  380. * NULL on error
  381. */
  382. char *faux_str_casestr(const char *haystack, const char *needle)
  383. {
  384. const char *ptr = haystack;
  385. size_t ptr_len = 0;
  386. size_t needle_len = 0;
  387. assert(haystack);
  388. assert(needle);
  389. if (!haystack || !needle)
  390. return NULL;
  391. ptr_len = strlen(haystack);
  392. needle_len = strlen(needle);
  393. while ((*ptr != '\0') && (ptr_len >= needle_len)) {
  394. int res = faux_str_casecmpn(ptr, needle, needle_len);
  395. if (0 == res)
  396. return (char *)ptr;
  397. ptr++;
  398. ptr_len--;
  399. }
  400. return NULL; // Not found
  401. }
  402. /** Prepare string for embedding to C-code (make escaping).
  403. *
  404. * @warning The returned pointer must be freed by faux_str_free().
  405. * @param [in] src String for escaping.
  406. * @return Escaped string or NULL on error.
  407. */
  408. char *faux_str_c_esc(const char *src)
  409. {
  410. const char *src_ptr = src;
  411. char *dst = NULL;
  412. char *dst_ptr = NULL;
  413. char *escaped = NULL;
  414. size_t src_len = 0;
  415. size_t dst_len = 0;
  416. assert(src);
  417. if (!src)
  418. return NULL;
  419. src_len = strlen(src);
  420. // Calculate max destination string size.
  421. // The worst case is when each src character will be replaced by
  422. // something like '\xff'. So it's 4 dst chars for 1 src one.
  423. dst_len = (src_len * 4) + 1; // one byte for '\0'
  424. dst = faux_zmalloc(dst_len);
  425. assert(dst);
  426. if (!dst)
  427. return NULL;
  428. dst_ptr = dst;
  429. while (*src_ptr != '\0') {
  430. char *esc = NULL; // escaped replacement
  431. char buf[5]; // longest 'char' (4 bytes) + '\0'
  432. size_t len = 0;
  433. switch (*src_ptr) {
  434. case '\n':
  435. esc = "\\n";
  436. break;
  437. case '\"':
  438. esc = "\\\"";
  439. break;
  440. case '\\':
  441. esc = "\\\\";
  442. break;
  443. case '\'':
  444. esc = "\\\'";
  445. break;
  446. case '\r':
  447. esc = "\\r";
  448. break;
  449. case '\t':
  450. esc = "\\t";
  451. break;
  452. default:
  453. // Check is the symbol control character. Control
  454. // characters has codes from 0x00 to 0x1f.
  455. if (((unsigned char)*src_ptr & 0xe0) == 0) { // control
  456. snprintf(buf, sizeof(buf), "\\x%02x",
  457. (unsigned char)*src_ptr);
  458. buf[4] = '\0'; // for safety
  459. } else {
  460. buf[0] = *src_ptr; // Common character
  461. buf[1] = '\0';
  462. }
  463. esc = buf;
  464. break;
  465. }
  466. len = strlen(esc);
  467. memcpy(dst_ptr, esc, len); // zmalloc() nullify the rest
  468. dst_ptr += len;
  469. src_ptr++;
  470. }
  471. escaped = faux_str_dup(dst); // Free some memory
  472. faux_str_free(dst); // 'dst' size >= 'escaped' size
  473. return escaped;
  474. }
  475. #define BYTE_CONV_LEN 4 // Length of one byte converted to string
  476. /** Prepare binary block for embedding to C-code.
  477. *
  478. * @warning The returned pointer must be freed by faux_str_free().
  479. * @param [in] src Binary block for conversion.
  480. * @return C-string or NULL on error.
  481. */
  482. char *faux_str_c_bin(const char *src, size_t n)
  483. {
  484. const char *src_ptr = src;
  485. char *dst = NULL;
  486. char *dst_ptr = NULL;
  487. size_t dst_len = 0;
  488. assert(src);
  489. if (!src)
  490. return NULL;
  491. // Calculate destination string size.
  492. // Each src character will be replaced by
  493. // something like '\xff'. So it's 4 dst chars for 1 src char.
  494. dst_len = (n * BYTE_CONV_LEN) + 1; // one byte for '\0'
  495. dst = faux_zmalloc(dst_len);
  496. assert(dst);
  497. if (!dst)
  498. return NULL;
  499. dst_ptr = dst;
  500. while (src_ptr < (src + n)) {
  501. char buf[BYTE_CONV_LEN + 1]; // longest 'char' (4 bytes) + '\0'
  502. snprintf(buf, sizeof(buf), "\\x%02x", (unsigned char)*src_ptr);
  503. memcpy(dst_ptr, buf, BYTE_CONV_LEN); // zmalloc() nullify the rest
  504. dst_ptr += BYTE_CONV_LEN;
  505. src_ptr++;
  506. }
  507. return dst;
  508. }
  509. /** @brief Search the n-th chars of string for one of the specified chars.
  510. *
  511. * The function search for any of specified characters within string.
  512. * The search is limited to first n characters of the string. If
  513. * terminating '\0' is before n-th character then search will stop on
  514. * it. Can be used with raw memory block.
  515. *
  516. * @param [in] str String (or memory block) to search in.
  517. * @param [in] chars_to_string Chars enumeration to search for.
  518. * @param [in] n Maximum number of bytes to search within.
  519. * @return Pointer to the first occurence of one of specified chars.
  520. * NULL on error.
  521. */
  522. char *faux_str_charsn(const char *str, const char *chars_to_search, size_t n)
  523. {
  524. const char *current_char = str;
  525. size_t len = n;
  526. assert(str);
  527. assert(chars_to_search);
  528. if (!str || !chars_to_search)
  529. return NULL;
  530. while ((*current_char != '\0') && (len > 0)) {
  531. if (strchr(chars_to_search, *current_char))
  532. return (char *)current_char;
  533. current_char++;
  534. len--;
  535. }
  536. return NULL;
  537. }
  538. /** @brief Search string for one of the specified chars.
  539. *
  540. * The function search for any of specified characters within string.
  541. *
  542. * @param [in] str String to search in.
  543. * @param [in] chars_to_string Chars enumeration to search for.
  544. * @return Pointer to the first occurence of one of specified chars.
  545. * NULL on error.
  546. */
  547. char *faux_str_chars(const char *str, const char *chars_to_search)
  548. {
  549. assert(str);
  550. if (!str)
  551. return NULL;
  552. return faux_str_charsn(str, chars_to_search, strlen(str));
  553. }
  554. /** @brief Remove escaping. Convert string to internal view.
  555. *
  556. * Find backslashes (before escaped symbols) and remove it. Escaped symbol
  557. * will not be analyzed so `\\` will lead to `\`.
  558. *
  559. * @param [in] string Escaped string.
  560. * @param [in] len Length of string to de-escape.
  561. * @return Allocated de-escaped string
  562. * @warning Returned value must be freed by faux_str_free() later.
  563. */
  564. static char *faux_str_deesc(const char *string, size_t len)
  565. {
  566. const char *s = string;
  567. char *res = NULL;
  568. char *p = NULL;
  569. bool_t escaped = BOOL_FALSE;
  570. assert(string);
  571. if (!string)
  572. return NULL;
  573. if (0 == len)
  574. return NULL;
  575. res = faux_zmalloc(len + 1);
  576. assert(res);
  577. if (!res)
  578. return NULL;
  579. p = res;
  580. while ((*s != '\0') && (s < (string +len))) {
  581. if (('\\' == *s) && !escaped) {
  582. escaped = BOOL_TRUE;
  583. s++;
  584. continue;
  585. }
  586. escaped = BOOL_FALSE;
  587. *p = *s;
  588. s++;
  589. p++;
  590. }
  591. *p = '\0';
  592. return res;
  593. }
  594. /*--------------------------------------------------------- */
  595. /** @brief Find next word or quoted substring within string
  596. *
  597. * The quotation can be of several different kinds.
  598. *
  599. * The first kind is standard double quoting. In this case the internal (within
  600. * quotation) `"` and `\` symbols must be escaped. But symbols will be deescaped
  601. * before writing to internal buffers.
  602. *
  603. * The second kind of quotation is alternative quotation. Any symbol can become
  604. * quote sign. For example "`" and "'" can be considered as a quotes. To use
  605. * some symbols as a quote them must be specified by `alt_quotes` function
  606. * parameter. The single symbol can be considered as a start of quotation or
  607. * a sequence of the same symbols can be considered as a start of quotation. In
  608. * this case the end of quotation is a sequence of the same symbols. The same
  609. * symbol can appear inside quotation but number of symbols (sequence) must be
  610. * less than opening quote sequence. The example of alternatively quoted string
  611. * is ```some text``and anothe`r```. The backslash has no special meaning inside
  612. * quoted string.
  613. *
  614. * The substring can be unquoted string without spaces. The space, backslash and
  615. * quote can be escaped by backslash.
  616. *
  617. * Parts of text with different quotes can be glued together to get single
  618. * substring like this: aaa"inside dbl quote"bbb``alt quote"`here``ccc.
  619. *
  620. * @param [in] str String to parse.
  621. * @param [out] saveptr Pointer to first symbol after found substring.
  622. * @param [in] alt_quotes Possible alternative quotes.
  623. * @param [out] qclosed Flag is quote closed.
  624. * @return Allocated buffer with found substring (without quotes).
  625. * @warning Returned alocated buffer must be freed later by faux_str_free()
  626. */
  627. char *faux_str_nextword(const char *str, const char **saveptr,
  628. const char *alt_quotes, bool_t *qclosed)
  629. {
  630. const char *string = str;
  631. const char *word = NULL;
  632. size_t len = 0;
  633. const char dbl_quote = '"';
  634. bool_t dbl_quoted = BOOL_FALSE;
  635. char alt_quote = '\0';
  636. unsigned int alt_quote_num = 0; // Number of opening alt quotes
  637. bool_t alt_quoted = BOOL_FALSE;
  638. char *result = NULL;
  639. // Find the start of a word (not including an opening quote)
  640. while (*string && isspace(*string))
  641. string++;
  642. word = string; // Suppose not quoted string
  643. while (*string != '\0') {
  644. // Standard double quotation
  645. if (dbl_quoted) {
  646. // End of word
  647. if (*string == dbl_quote) {
  648. if (len > 0) {
  649. char *s = faux_str_deesc(word, len);
  650. faux_str_cat(&result, s);
  651. faux_str_free(s);
  652. }
  653. dbl_quoted = BOOL_FALSE;
  654. string++;
  655. word = string;
  656. len = 0;
  657. // Escaping
  658. } else if (*string == '\\') {
  659. // Skip escaping
  660. string++;
  661. len++;
  662. // Skip escaped symbol
  663. if (*string) {
  664. string++;
  665. len++;
  666. }
  667. } else {
  668. string++;
  669. len++;
  670. }
  671. // Alternative multi quotation
  672. } else if (alt_quoted) {
  673. unsigned int qnum = alt_quote_num;
  674. while (string && (*string == alt_quote) && qnum) {
  675. string++;
  676. len++;
  677. qnum--;
  678. }
  679. if (0 == qnum) { // End of word was found
  680. // Quotes themselfs are not a part of a word
  681. len -= alt_quote_num;
  682. if (len > 0)
  683. faux_str_catn(&result, word, len);
  684. alt_quoted = BOOL_FALSE;
  685. word = string;
  686. len = 0;
  687. } else if (qnum == alt_quote_num) { // No quote syms
  688. string++;
  689. len++;
  690. }
  691. // Not quoted
  692. } else {
  693. // Start of a double quoted string
  694. if (*string == dbl_quote) {
  695. if (len > 0) {
  696. char *s = faux_str_deesc(word, len);
  697. faux_str_cat(&result, s);
  698. faux_str_free(s);
  699. }
  700. dbl_quoted = BOOL_TRUE;
  701. string++;
  702. word = string;
  703. len = 0;
  704. // Start of alt quoted string
  705. } else if (alt_quotes && strchr(alt_quotes, *string)) {
  706. if (len > 0) {
  707. char *s = faux_str_deesc(word, len);
  708. faux_str_cat(&result, s);
  709. faux_str_free(s);
  710. }
  711. alt_quoted = BOOL_TRUE;
  712. alt_quote = *string;
  713. alt_quote_num = 0;
  714. while (string && (*string == alt_quote)) {
  715. string++;
  716. alt_quote_num++; // Count starting quotes
  717. }
  718. word = string;
  719. len = 0;
  720. // End of word
  721. } else if (isspace(*string)) {
  722. if (len > 0) {
  723. char *s = faux_str_deesc(word, len);
  724. faux_str_cat(&result, s);
  725. faux_str_free(s);
  726. }
  727. word = string;
  728. len = 0;
  729. break;
  730. // Escaping
  731. } else if (*string == '\\') {
  732. // Skip escaping
  733. string++;
  734. len++;
  735. // Skip escaped symbol
  736. if (*string) {
  737. string++;
  738. len++;
  739. }
  740. } else {
  741. string++;
  742. len++;
  743. }
  744. }
  745. }
  746. if (len > 0) {
  747. if (alt_quoted) {
  748. faux_str_catn(&result, word, len);
  749. } else {
  750. char *s = faux_str_deesc(word, len);
  751. faux_str_cat(&result, s);
  752. faux_str_free(s);
  753. }
  754. }
  755. if (saveptr)
  756. *saveptr = string;
  757. if (qclosed)
  758. *qclosed = ! (dbl_quoted || alt_quoted);
  759. return result;
  760. }
  761. /** @brief Indicates is string is empty.
  762. *
  763. * @param [in] str String to analyze.
  764. * @return BOOL_TRUE if pointer is NULL or empty, BOOL_FALSE if not empty.
  765. */
  766. bool_t faux_str_is_empty(const char *str)
  767. {
  768. if (!str)
  769. return BOOL_TRUE;
  770. if ('\0' == *str)
  771. return BOOL_TRUE;
  772. return BOOL_FALSE;
  773. }
  774. /** @brief Gets line from multiline string.
  775. *
  776. * @param [in] str String to analyze.
  777. * @param [out] saveptr Pointer to the position after found EOL.
  778. * @return Allocated line or NULL if string is empty.
  779. */
  780. char *faux_str_getline(const char *str, const char **saveptr)
  781. {
  782. const char *find_pos = NULL;
  783. const char *eol = "\n\r";
  784. assert(str);
  785. if (!str)
  786. return NULL;
  787. if ('\0' == *str) {
  788. if (saveptr)
  789. *saveptr = str;
  790. return NULL;
  791. }
  792. find_pos = faux_str_chars(str, eol);
  793. if (find_pos) {
  794. size_t len = find_pos - str;
  795. char *res = NULL;
  796. res = faux_zmalloc(len + 1);
  797. if (len > 0)
  798. memcpy(res, str, len);
  799. if (saveptr)
  800. *saveptr = find_pos + 1;
  801. return res;
  802. }
  803. // Line without EOL
  804. if (saveptr)
  805. *saveptr = str + strlen(str);
  806. return faux_str_dup(str);
  807. }