sstr.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * Copyright (C) 2005 iptelorg GmbH
  3. *
  4. * This file is part of ser, a free SIP server.
  5. *
  6. * ser is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version
  10. *
  11. * For a license to use the ser software under conditions
  12. * other than those described here, or to purchase support for this
  13. * software, please contact iptel.org by e-mail at the following addresses:
  14. * [email protected]
  15. *
  16. * ser is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25. #include <cds/sstr.h>
  26. #include <cds/memory.h>
  27. #include <cds/dstring.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <cds/logger.h>
  32. /** returns 1 if the string is empty */
  33. int is_str_empty(const str_t *s)
  34. {
  35. if (!s) return 1;
  36. if ((!s->s) || (s->len < 1)) return 1;
  37. return 0;
  38. }
  39. int str_cmp_zt(const str_t *a, const char *b)
  40. {
  41. int i;
  42. if (!a) {
  43. if (b) return 1;
  44. else return 0;
  45. }
  46. for (i = 0; (i < a->len) && (b[i]); i++) {
  47. if (a->s[i] < b[i]) return -1;
  48. if (a->s[i] > b[i]) return 1;
  49. }
  50. if (i < a->len) return 1;
  51. return 0;
  52. }
  53. int str_prefix(const str_t *a, const str_t *b)
  54. {
  55. int i;
  56. if (!b) return 0;
  57. if (!a) return -1;
  58. if (b->len > a->len) return -1;
  59. for (i = 0; i < b->len; i++) {
  60. if (a->s[i] != b->s[i]) return -1;
  61. }
  62. return 0;
  63. }
  64. str_t zt2str(char *str)
  65. {
  66. str_t s;
  67. s.s = str;
  68. if (str) s.len = strlen(str);
  69. else s.len = 0;
  70. return s;
  71. }
  72. int str_dup_dbg(str_t* dst, const str_t* src, const char *file, int line)
  73. {
  74. if (!dst) return -1;
  75. dst->len = 0;
  76. dst->s = NULL;
  77. if (!src) return 0;
  78. if ( (!src->s) || (src->len < 1)) return 0;
  79. /* ERROR_LOG("can't allocate memory (%d bytes)\n", src->len); */
  80. DEBUG_LOG("str_dup called from %s:%d\n", file, line);
  81. dst->s = cds_malloc(src->len);
  82. if (!dst->s) {
  83. /* ERROR_LOG("can't allocate memory (%d bytes)\n", src->len); */
  84. return -1;
  85. }
  86. memcpy(dst->s, src->s, src->len);
  87. dst->len = src->len;
  88. return 0;
  89. }
  90. int str_dup_impl(str_t* dst, const str_t* src)
  91. {
  92. if (!dst) return -1;
  93. dst->len = 0;
  94. dst->s = NULL;
  95. if (!src) return 0;
  96. if ( (!src->s) || (src->len < 1)) return 0;
  97. dst->s = cds_malloc(src->len);
  98. if (!dst->s) {
  99. /* ERROR_LOG("can't allocate memory (%d bytes)\n", src->len); */
  100. return -1;
  101. }
  102. memcpy(dst->s, src->s, src->len);
  103. dst->len = src->len;
  104. return 0;
  105. }
  106. str_t *str_dup_new(const str_t* src)
  107. {
  108. str_t *dst = cds_malloc(sizeof(str_t));
  109. if (dst) str_dup(dst, src);
  110. return dst;
  111. }
  112. int str_dup_zt(str_t* dst, const char* src)
  113. {
  114. int len;
  115. if (!dst) return -1;
  116. dst->len = 0;
  117. dst->s = NULL;
  118. if (!src) return 0;
  119. len = strlen(src);
  120. if (len < 1) return 0;
  121. dst->s = cds_malloc(len);
  122. if (!dst->s) return -1;
  123. memcpy(dst->s, src, len);
  124. dst->len = len;
  125. return 0;
  126. }
  127. char *zt_strdup(const char* src)
  128. {
  129. int len;
  130. char *dst;
  131. len = strlen(src);
  132. if (len < 0) return NULL;
  133. dst = cds_malloc(len + 1);
  134. if (dst) memcpy(dst, src, len + 1);
  135. return dst;
  136. }
  137. int str_nocase_equals(const str_t *a, const str_t *b)
  138. {
  139. int i;
  140. if (!a) {
  141. if (!b) return 0;
  142. else return (b->len == 0) ? 0 : 1;
  143. }
  144. if (!b) return (a->len == 0) ? 0 : 1;
  145. if (a->len != b->len) return 1;
  146. for (i = 0; i < a->len; i++)
  147. if (tolower(a->s[i]) != tolower(b->s[i])) return 1;
  148. return 0;
  149. }
  150. int str_case_equals(const str_t *a, const str_t *b)
  151. {
  152. int i;
  153. if (!a) {
  154. if (!b) return 0;
  155. else return (b->len == 0) ? 0 : 1;
  156. }
  157. if (!b) return (a->len == 0) ? 0 : 1;
  158. if (a->len != b->len) return 1;
  159. for (i = 0; i < a->len; i++)
  160. if (a->s[i] != b->s[i]) return 1;
  161. return 0;
  162. }
  163. /* void str_free_content(str_t *s)
  164. {
  165. if (!s) return;
  166. if ((s->len > 0) && (s->s)) cds_free(s->s);
  167. s->len = 0;
  168. s->s = NULL;
  169. }
  170. void str_free(str_t *s)
  171. {
  172. if (s) {
  173. str_free_content(s);
  174. cds_free(s);
  175. }
  176. }
  177. void str_clear(str_t *s)
  178. {
  179. if (s) {
  180. s->s = NULL;
  181. s->len = 0;
  182. }
  183. } */
  184. char *str_strchr(const str_t *s, char c)
  185. {
  186. if (s) {
  187. int i;
  188. for (i = 0; i < s->len; i++)
  189. if (s->s[i] == c) return s->s + i;
  190. }
  191. return NULL;
  192. }
  193. char *str_str(const str_t *s, const str_t *search_for)
  194. {
  195. int i, j;
  196. /* FIXME: reimplement using better algorithm */
  197. if (is_str_empty(search_for)) return s->s;
  198. if (is_str_empty(s)) return NULL;
  199. if (search_for->len > s->len) return NULL;
  200. j = 0;
  201. i = 0;
  202. while (i < s->len) {
  203. if (s->s[i] == search_for->s[j]) {
  204. j++;
  205. i++;
  206. if (j == search_for->len) return s->s + i - j;
  207. }
  208. else {
  209. i = i - j + 1;
  210. j = 0;
  211. }
  212. }
  213. return NULL;
  214. }
  215. /* creates new string as concatenation of a and b */
  216. int str_concat(str_t *dst, str_t *a, str_t *b)
  217. {
  218. int al;
  219. int bl;
  220. if (!dst) return -1;
  221. al = str_len(a);
  222. bl = str_len(b);
  223. dst->len = al + bl;
  224. if (dst->len > 0) {
  225. dst->s = (char *)cds_malloc(dst->len);
  226. if (!dst->s) {
  227. dst->len = 0;
  228. return -1;
  229. }
  230. }
  231. else {
  232. dst->s = NULL;
  233. dst->len = 0;
  234. return 0;
  235. }
  236. if (al) memcpy(dst->s, a->s, al);
  237. if (bl) memcpy(dst->s + al, b->s, bl);
  238. return 0;
  239. }
  240. int replace_str(const str_t *src, str_t *dst, const str_t *sample, const str_t *value)
  241. {
  242. str_t s;
  243. char *c;
  244. dstring_t str;
  245. int res, len;
  246. /* if (!dst) return -1;
  247. if (!src) {
  248. str_clear(dst);
  249. return -1;
  250. } */
  251. if (is_str_empty(sample)) {
  252. str_clear(dst);
  253. return -1;
  254. }
  255. if (is_str_empty(src)) {
  256. str_clear(dst);
  257. return 0;
  258. }
  259. s = *src;
  260. dstr_init(&str, src->len + 32);
  261. do {
  262. c = str_str(&s, sample);
  263. if (c) {
  264. len = c - s.s;
  265. dstr_append(&str, s.s, len);
  266. dstr_append_str(&str, value);
  267. s.len = s.len - len - sample->len;
  268. s.s = c + sample->len;
  269. if (s.len <= 0) break;
  270. }
  271. else dstr_append_str(&str, &s);
  272. } while (c);
  273. res = dstr_get_str(&str, dst);
  274. dstr_destroy(&str);
  275. return res;
  276. }