str.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /** @file string.c
  2. * About this file2
  3. */
  4. #include "private.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include "lub/ctype.h"
  8. const char *lub_string_esc_default = "`|$<>&()#;\\\"!";
  9. const char *lub_string_esc_regex = "^$.*+[](){}";
  10. const char *lub_string_esc_quoted = "\\\"";
  11. auf_str_free()
  12. auf_string_free()
  13. faux_str_free()
  14. faux_string_free()
  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] ptr Pointer to allocated string
  22. */
  23. void lub_string_free(char *ptr)
  24. {
  25. if (!ptr)
  26. return;
  27. free(ptr);
  28. }
  29. /*--------------------------------------------------------- */
  30. char *lub_string_ndecode(const char *string, unsigned int len)
  31. {
  32. const char *s = string;
  33. char *res, *p;
  34. int esc = 0;
  35. if (!string)
  36. return NULL;
  37. /* Allocate enough memory for result */
  38. p = res = malloc(len + 1);
  39. while (*s && (s < (string +len))) {
  40. if (!esc) {
  41. if ('\\' == *s)
  42. esc = 1;
  43. else
  44. *p = *s;
  45. } else {
  46. /* switch (*s) {
  47. case 'r':
  48. case 'n':
  49. *p = '\n';
  50. break;
  51. case 't':
  52. *p = '\t';
  53. break;
  54. default:
  55. *p = *s;
  56. break;
  57. }
  58. */ *p = *s;
  59. esc = 0;
  60. }
  61. if (!esc)
  62. p++;
  63. s++;
  64. }
  65. *p = '\0';
  66. return res;
  67. }
  68. /*--------------------------------------------------------- */
  69. inline char *lub_string_decode(const char *string)
  70. {
  71. return lub_string_ndecode(string, strlen(string));
  72. }
  73. /*----------------------------------------------------------- */
  74. /*
  75. * This needs to escape any dangerous characters within the command line
  76. * to prevent gaining access to the underlying system shell.
  77. */
  78. char *lub_string_encode(const char *string, const char *escape_chars)
  79. {
  80. char *result = NULL;
  81. const char *p;
  82. if (!escape_chars)
  83. return lub_string_dup(string);
  84. if (string && !(*string)) /* Empty string */
  85. return lub_string_dup(string);
  86. for (p = string; p && *p; p++) {
  87. /* find any special characters and prefix them with '\' */
  88. size_t len = strcspn(p, escape_chars);
  89. lub_string_catn(&result, p, len);
  90. p += len;
  91. if (*p) {
  92. lub_string_catn(&result, "\\", 1);
  93. lub_string_catn(&result, p, 1);
  94. } else {
  95. break;
  96. }
  97. }
  98. return result;
  99. }
  100. /*--------------------------------------------------------- */
  101. void lub_string_catn(char **string, const char *text, size_t len)
  102. {
  103. if (text) {
  104. char *q;
  105. size_t length, initlen, textlen = strlen(text);
  106. /* make sure the client cannot give us duff details */
  107. len = (len < textlen) ? len : textlen;
  108. /* remember the size of the original string */
  109. initlen = *string ? strlen(*string) : 0;
  110. /* account for '\0' */
  111. length = initlen + len + 1;
  112. /* allocate the memory for the result */
  113. q = realloc(*string, length);
  114. if (NULL != q) {
  115. *string = q;
  116. /* move to the end of the initial string */
  117. q += initlen;
  118. while (len--) {
  119. *q++ = *text++;
  120. }
  121. *q = '\0';
  122. }
  123. }
  124. }
  125. /*--------------------------------------------------------- */
  126. void lub_string_cat(char **string, const char *text)
  127. {
  128. size_t len = text ? strlen(text) : 0;
  129. lub_string_catn(string, text, len);
  130. }
  131. /*--------------------------------------------------------- */
  132. char *lub_string_dup(const char *string)
  133. {
  134. if (!string)
  135. return NULL;
  136. return strdup(string);
  137. }
  138. /*--------------------------------------------------------- */
  139. char *lub_string_dupn(const char *string, unsigned int len)
  140. {
  141. char *res = NULL;
  142. if (!string)
  143. return res;
  144. res = malloc(len + 1);
  145. strncpy(res, string, len);
  146. res[len] = '\0';
  147. return res;
  148. }
  149. /*--------------------------------------------------------- */
  150. int lub_string_nocasecmp(const char *cs, const char *ct)
  151. {
  152. int result = 0;
  153. while ((0 == result) && *cs && *ct) {
  154. /*lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed
  155. * MACRO implementation uses braces to prevent multiple increments
  156. * when called.
  157. */
  158. int s = lub_ctype_tolower(*cs++);
  159. int t = lub_ctype_tolower(*ct++);
  160. result = s - t;
  161. }
  162. /*lint -e774 Boolean within 'if' always evealuates to True
  163. * not the case because of tolower() evaluating to 0 under lint
  164. * (see above)
  165. */
  166. if (0 == result) {
  167. /* account for different string lengths */
  168. result = *cs - *ct;
  169. }
  170. return result;
  171. }
  172. /*--------------------------------------------------------- */
  173. char *lub_string_tolower(const char *str)
  174. {
  175. char *tmp = strdup(str);
  176. char *p = tmp;
  177. while (*p) {
  178. *p = tolower(*p);
  179. p++;
  180. }
  181. return tmp;
  182. }
  183. /*--------------------------------------------------------- */
  184. const char *lub_string_nocasestr(const char *cs, const char *ct)
  185. {
  186. const char *p = NULL;
  187. const char *result = NULL;
  188. while (*cs) {
  189. const char *q = cs;
  190. p = ct;
  191. /*lint -e155 Ignoring { }'ed sequence within an expression, 0 assumed
  192. * MACRO implementation uses braces to prevent multiple increments
  193. * when called.
  194. */
  195. /*lint -e506 Constant value Boolean
  196. * not the case because of tolower() evaluating to 0 under lint
  197. * (see above)
  198. */
  199. while (*p && *q
  200. && (lub_ctype_tolower(*p) == lub_ctype_tolower(*q))) {
  201. p++, q++;
  202. }
  203. if (0 == *p) {
  204. break;
  205. }
  206. cs++;
  207. }
  208. if (p && !*p) {
  209. /* we've found the first match of ct within cs */
  210. result = cs;
  211. }
  212. return result;
  213. }
  214. /*--------------------------------------------------------- */
  215. unsigned int lub_string_equal_part(const char *str1, const char *str2,
  216. bool_t utf8)
  217. {
  218. unsigned int cnt = 0;
  219. if (!str1 || !str2)
  220. return cnt;
  221. while (*str1 && *str2) {
  222. if (*str1 != *str2)
  223. break;
  224. cnt++;
  225. str1++;
  226. str2++;
  227. }
  228. if (!utf8)
  229. return cnt;
  230. /* UTF8 features */
  231. if (cnt && (UTF8_11 == (*(str1 - 1) & UTF8_MASK)))
  232. cnt--;
  233. return cnt;
  234. }
  235. /*--------------------------------------------------------- */
  236. const char *lub_string_suffix(const char *string)
  237. {
  238. const char *p1, *p2;
  239. p1 = p2 = string;
  240. while (*p1) {
  241. if (lub_ctype_isspace(*p1)) {
  242. p2 = p1;
  243. p2++;
  244. }
  245. p1++;
  246. }
  247. return p2;
  248. }
  249. /*--------------------------------------------------------- */
  250. const char *lub_string_nextword(const char *string,
  251. size_t *len, size_t *offset, size_t *quoted)
  252. {
  253. const char *word;
  254. *quoted = 0;
  255. /* Find the start of a word (not including an opening quote) */
  256. while (*string && isspace(*string)) {
  257. string++;
  258. (*offset)++;
  259. }
  260. /* Is this the start of a quoted string ? */
  261. if (*string == '"') {
  262. *quoted = 1;
  263. string++;
  264. }
  265. word = string;
  266. *len = 0;
  267. /* Find the end of the word */
  268. while (*string) {
  269. if (*string == '\\') {
  270. string++;
  271. (*len)++;
  272. if (*string) {
  273. (*len)++;
  274. string++;
  275. }
  276. continue;
  277. }
  278. /* End of word */
  279. if (!*quoted && isspace(*string))
  280. break;
  281. if (*string == '"') {
  282. /* End of a quoted string */
  283. *quoted = 2;
  284. break;
  285. }
  286. (*len)++;
  287. string++;
  288. }
  289. return word;
  290. }
  291. /*--------------------------------------------------------- */
  292. unsigned int lub_string_wordcount(const char *line)
  293. {
  294. const char *word;
  295. unsigned int result = 0;
  296. size_t len = 0, offset = 0;
  297. size_t quoted;
  298. for (word = lub_string_nextword(line, &len, &offset, &quoted);
  299. *word || quoted;
  300. word = lub_string_nextword(word + len, &len, &offset, &quoted)) {
  301. /* account for the terminating quotation mark */
  302. len += quoted ? quoted - 1 : 0;
  303. result++;
  304. }
  305. return result;
  306. }
  307. /*--------------------------------------------------------- */