PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/std/c_string.c

https://gitlab.com/rpdev/csync
C | 365 lines | 275 code | 67 blank | 23 comment | 83 complexity | fc1664fd61a6a8e2d9d70759d67dcbf7 MD5 | raw file
  1. /*
  2. * cynapses libc functions
  3. *
  4. * Copyright (c) 2008-2013 by Andreas Schneider <asn@cryptomilk.org>
  5. * Copyright (c) 2012-2013 by Klaas Freitag <freitag@owncloud.com>
  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 Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "config.h"
  22. #include <assert.h>
  23. #include <errno.h>
  24. #include <ctype.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27. #include <limits.h>
  28. #include <sys/types.h>
  29. #include <wchar.h>
  30. #include "c_string.h"
  31. #include "c_alloc.h"
  32. #include "c_macro.h"
  33. #ifdef _WIN32
  34. #include <windows.h>
  35. #endif
  36. #if defined(HAVE_ICONV) && defined(WITH_ICONV)
  37. # ifdef HAVE_ICONV_H
  38. # include <iconv.h>
  39. # endif
  40. # ifdef HAVE_SYS_ICONV_H
  41. # include <sys/iconv.h>
  42. # endif
  43. typedef struct {
  44. iconv_t to;
  45. iconv_t from;
  46. } iconv_conversions;
  47. CSYNC_THREAD iconv_conversions _iconvs = { NULL, NULL };
  48. int c_setup_iconv(const char* to) {
  49. _iconvs.to = iconv_open(to, "UTF-8");
  50. _iconvs.from = iconv_open("UTF-8", to);
  51. if (_iconvs.to == (iconv_t)-1 || _iconvs.from == (iconv_t)-1)
  52. return -1;
  53. return 0;
  54. }
  55. int c_close_iconv() {
  56. int ret_to = 0;
  57. int ret_from = 0;
  58. if( _iconvs.to != (iconv_t) NULL ) {
  59. ret_to = iconv_close(_iconvs.to);
  60. }
  61. if( _iconvs.from != (iconv_t) NULL ) {
  62. ret_from = iconv_close(_iconvs.from);
  63. }
  64. if (ret_to == -1 || ret_from == -1)
  65. return -1;
  66. _iconvs.to = (iconv_t) 0;
  67. _iconvs.from = (iconv_t) 0;
  68. return 0;
  69. }
  70. enum iconv_direction { iconv_from_native, iconv_to_native };
  71. static char *c_iconv(const char* str, enum iconv_direction dir)
  72. {
  73. #ifdef HAVE_ICONV_CONST
  74. const char *in = str;
  75. #else
  76. char *in = discard_const(str);
  77. #endif
  78. size_t size;
  79. size_t outsize;
  80. char *out;
  81. size_t ret;
  82. if (str == NULL) {
  83. return NULL;
  84. }
  85. if(_iconvs.from == NULL && _iconvs.to == NULL) {
  86. #ifdef __APPLE__
  87. c_setup_iconv("UTF-8-MAC");
  88. #else
  89. return c_strdup(str);
  90. #endif
  91. }
  92. size = strlen(in);
  93. outsize = size*2;
  94. out = c_malloc(outsize);
  95. if (out == NULL) {
  96. return NULL;
  97. }
  98. if (dir == iconv_to_native) {
  99. ret = iconv(_iconvs.to, &in, &size, &out, &outsize);
  100. } else {
  101. ret = iconv(_iconvs.from, &in, &size, &out, &outsize);
  102. }
  103. if (ret == (size_t)-1) {
  104. SAFE_FREE(out);
  105. return NULL;
  106. }
  107. return out;
  108. }
  109. #endif /* defined(HAVE_ICONV) && defined(WITH_ICONV) */
  110. int c_streq(const char *a, const char *b) {
  111. register const char *s1 = a;
  112. register const char *s2 = b;
  113. if (s1 == NULL || s2 == NULL) {
  114. return 0;
  115. }
  116. while (*s1 == *s2++) {
  117. if (*s1++ == '\0') {
  118. return 1;
  119. }
  120. }
  121. return 0;
  122. }
  123. c_strlist_t *c_strlist_new(size_t size) {
  124. c_strlist_t *strlist = NULL;
  125. if (size == 0) {
  126. errno = EINVAL;
  127. return NULL;
  128. }
  129. strlist = c_malloc(sizeof(c_strlist_t));
  130. if (strlist == NULL) {
  131. return NULL;
  132. }
  133. strlist->vector = (char **) c_malloc(size * sizeof(char *));
  134. if (strlist->vector == NULL) {
  135. SAFE_FREE(strlist);
  136. return NULL;
  137. }
  138. strlist->count = 0;
  139. strlist->size = size;
  140. return strlist;
  141. }
  142. c_strlist_t *c_strlist_expand(c_strlist_t *strlist, size_t size) {
  143. if (strlist == NULL || size == 0) {
  144. errno = EINVAL;
  145. return NULL;
  146. }
  147. if (strlist->size >= size) {
  148. return strlist;
  149. }
  150. strlist->vector = (char **) c_realloc(strlist->vector, size * sizeof(char *));
  151. if (strlist->vector == NULL) {
  152. return NULL;
  153. }
  154. strlist->size = size;
  155. return strlist;
  156. }
  157. int c_strlist_add(c_strlist_t *strlist, const char *string) {
  158. if (strlist == NULL || string == NULL) {
  159. return -1;
  160. }
  161. if (strlist->count < strlist->size) {
  162. strlist->vector[strlist->count] = c_strdup(string);
  163. if (strlist->vector[strlist->count] == NULL) {
  164. return -1;
  165. }
  166. strlist->count++;
  167. } else {
  168. errno = ENOBUFS;
  169. return -1;
  170. }
  171. return 0;
  172. }
  173. void c_strlist_destroy(c_strlist_t *strlist) {
  174. size_t i = 0;
  175. if (strlist == NULL) {
  176. return;
  177. }
  178. for (i = 0; i < strlist->count; i++) {
  179. SAFE_FREE(strlist->vector[i]);
  180. }
  181. SAFE_FREE(strlist->vector);
  182. SAFE_FREE(strlist);
  183. }
  184. char *c_strreplace(char *src, const char *pattern, const char *repl) {
  185. char *p = NULL;
  186. while ((p = strstr(src, pattern)) != NULL) {
  187. size_t of = p - src;
  188. size_t l = strlen(src);
  189. size_t pl = strlen(pattern);
  190. size_t rl = strlen(repl);
  191. if (rl > pl) {
  192. src = (char *) c_realloc(src, strlen(src) + rl - pl + 1);
  193. }
  194. if (rl != pl) {
  195. memmove(src + of + rl, src + of + pl, l - of - pl + 1);
  196. }
  197. strncpy(src + of, repl, rl);
  198. }
  199. return src;
  200. }
  201. char *c_uppercase(const char* str) {
  202. char *new;
  203. char *p;
  204. if (str == NULL) {
  205. return NULL;
  206. }
  207. new = c_strdup(str);
  208. if (new == NULL) {
  209. return NULL;
  210. }
  211. for (p = new; *p; p++) {
  212. *p = toupper(*p);
  213. }
  214. return new;
  215. }
  216. char *c_lowercase(const char* str) {
  217. char *new;
  218. char *p;
  219. if (str == NULL) {
  220. return NULL;
  221. }
  222. new = c_strdup(str);
  223. if (new == NULL) {
  224. return NULL;
  225. }
  226. for (p = new; *p; p++) {
  227. *p = tolower(*p);
  228. }
  229. return new;
  230. }
  231. /* Convert a wide multibyte String to UTF8 */
  232. char* c_utf8_from_locale(const mbchar_t *wstr)
  233. {
  234. char *dst = NULL;
  235. #ifdef _WIN32
  236. char *mdst = NULL;
  237. int size_needed;
  238. size_t len;
  239. #endif
  240. if (wstr == NULL) {
  241. return NULL;
  242. }
  243. #ifdef _WIN32
  244. len = wcslen(wstr);
  245. /* Call once to get the required size. */
  246. size_needed = WideCharToMultiByte(CP_UTF8, 0, wstr, len, NULL, 0, NULL, NULL);
  247. if (size_needed > 0) {
  248. mdst = c_malloc(size_needed + 1);
  249. if (mdst == NULL) {
  250. errno = ENOMEM;
  251. return NULL;
  252. }
  253. memset(mdst, 0, size_needed + 1);
  254. WideCharToMultiByte(CP_UTF8, 0, wstr, len, mdst, size_needed, NULL, NULL);
  255. dst = mdst;
  256. }
  257. #else
  258. #ifdef WITH_ICONV
  259. dst = c_iconv(wstr, iconv_from_native);
  260. #else
  261. dst = wstr;
  262. #endif
  263. #endif
  264. return dst;
  265. }
  266. /* Convert a an UTF8 string to multibyte */
  267. mbchar_t* c_utf8_to_locale(const char *str)
  268. {
  269. mbchar_t *dst = NULL;
  270. #ifdef _WIN32
  271. size_t len;
  272. int size_needed;
  273. #endif
  274. if (str == NULL ) {
  275. return NULL;
  276. }
  277. #ifdef _WIN32
  278. len = strlen(str);
  279. size_needed = MultiByteToWideChar(CP_UTF8, 0, str, len, NULL, 0);
  280. if (size_needed > 0) {
  281. int size_char = (size_needed + 1) * sizeof(mbchar_t);
  282. dst = c_malloc(size_char);
  283. if (dst == NULL) {
  284. errno = ENOMEM;
  285. return NULL;
  286. }
  287. memset((void*)dst, 0, size_char);
  288. MultiByteToWideChar(CP_UTF8, 0, str, -1, dst, size_needed);
  289. }
  290. #else
  291. #ifdef WITH_ICONV
  292. dst = c_iconv(str, iconv_to_native);
  293. #else
  294. dst = str;
  295. #endif
  296. #endif
  297. return dst;
  298. }