platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. /*
  2. * Copyright (c) 2013-2014 Hugh Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #define _FILE_OFFSET_BITS 64
  17. #include <errno.h>
  18. #include <stdlib.h>
  19. #include <locale.h>
  20. #include "c99defs.h"
  21. #include "platform.h"
  22. #include "bmem.h"
  23. #include "utf8.h"
  24. #include "dstr.h"
  25. FILE *os_wfopen(const wchar_t *path, const char *mode)
  26. {
  27. FILE *file = NULL;
  28. if (path) {
  29. #ifdef _MSC_VER
  30. wchar_t *wcs_mode;
  31. os_utf8_to_wcs_ptr(mode, 0, &wcs_mode);
  32. file = _wfopen(path, wcs_mode);
  33. bfree(wcs_mode);
  34. #else
  35. char *mbs_path;
  36. os_wcs_to_utf8_ptr(path, 0, &mbs_path);
  37. file = fopen(mbs_path, mode);
  38. bfree(mbs_path);
  39. #endif
  40. }
  41. return file;
  42. }
  43. FILE *os_fopen(const char *path, const char *mode)
  44. {
  45. #ifdef _WIN32
  46. wchar_t *wpath = NULL;
  47. FILE *file = NULL;
  48. if (path) {
  49. os_utf8_to_wcs_ptr(path, 0, &wpath);
  50. file = os_wfopen(wpath, mode);
  51. bfree(wpath);
  52. }
  53. return file;
  54. #else
  55. return path ? fopen(path, mode) : NULL;
  56. #endif
  57. }
  58. int64_t os_fgetsize(FILE *file)
  59. {
  60. int64_t cur_offset = os_ftelli64(file);
  61. int64_t size;
  62. int errval = 0;
  63. if (fseek(file, 0, SEEK_END) == -1)
  64. return -1;
  65. size = os_ftelli64(file);
  66. if (size == -1)
  67. errval = errno;
  68. if (os_fseeki64(file, cur_offset, SEEK_SET) != 0 && errval != 0)
  69. errno = errval;
  70. return size;
  71. }
  72. int os_fseeki64(FILE *file, int64_t offset, int origin)
  73. {
  74. #ifdef _MSC_VER
  75. return _fseeki64(file, offset, origin);
  76. #else
  77. return fseeko(file, offset, origin);
  78. #endif
  79. }
  80. int64_t os_ftelli64(FILE *file)
  81. {
  82. #ifdef _MSC_VER
  83. return _ftelli64(file);
  84. #else
  85. return ftello(file);
  86. #endif
  87. }
  88. size_t os_fread_mbs(FILE *file, char **pstr)
  89. {
  90. size_t size = 0;
  91. size_t len = 0;
  92. fseek(file, 0, SEEK_END);
  93. size = (size_t)os_ftelli64(file);
  94. *pstr = NULL;
  95. if (size > 0) {
  96. char *mbstr = bmalloc(size+1);
  97. fseek(file, 0, SEEK_SET);
  98. size = fread(mbstr, 1, size, file);
  99. if (size == 0) {
  100. bfree(mbstr);
  101. return 0;
  102. }
  103. mbstr[size] = 0;
  104. len = os_mbs_to_utf8_ptr(mbstr, size, pstr);
  105. bfree(mbstr);
  106. }
  107. return len;
  108. }
  109. size_t os_fread_utf8(FILE *file, char **pstr)
  110. {
  111. size_t size = 0;
  112. size_t size_read;
  113. size_t len = 0;
  114. *pstr = NULL;
  115. fseek(file, 0, SEEK_END);
  116. size = (size_t)os_ftelli64(file);
  117. if (size > 0) {
  118. char bom[3];
  119. char *utf8str;
  120. off_t offset;
  121. /* remove the ghastly BOM if present */
  122. fseek(file, 0, SEEK_SET);
  123. size_read = fread(bom, 1, 3, file);
  124. if (size_read != 3)
  125. return 0;
  126. offset = (astrcmp_n(bom, "\xEF\xBB\xBF", 3) == 0) ? 3 : 0;
  127. size -= offset;
  128. if (size == 0)
  129. return 0;
  130. utf8str = bmalloc(size+1);
  131. fseek(file, offset, SEEK_SET);
  132. size = fread(utf8str, 1, size, file);
  133. if (size == 0) {
  134. bfree(utf8str);
  135. return 0;
  136. }
  137. utf8str[size] = 0;
  138. *pstr = utf8str;
  139. }
  140. return len;
  141. }
  142. char *os_quick_read_mbs_file(const char *path)
  143. {
  144. FILE *f = os_fopen(path, "rb");
  145. char *file_string = NULL;
  146. if (!f)
  147. return NULL;
  148. os_fread_mbs(f, &file_string);
  149. fclose(f);
  150. return file_string;
  151. }
  152. char *os_quick_read_utf8_file(const char *path)
  153. {
  154. FILE *f = os_fopen(path, "rb");
  155. char *file_string = NULL;
  156. if (!f)
  157. return NULL;
  158. os_fread_utf8(f, &file_string);
  159. fclose(f);
  160. return file_string;
  161. }
  162. bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
  163. {
  164. FILE *f = os_fopen(path, "wb");
  165. char *mbs = NULL;
  166. size_t mbs_len = 0;
  167. if (!f)
  168. return false;
  169. mbs_len = os_utf8_to_mbs_ptr(str, len, &mbs);
  170. if (mbs_len)
  171. fwrite(mbs, 1, mbs_len, f);
  172. bfree(mbs);
  173. fclose(f);
  174. return true;
  175. }
  176. bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
  177. bool marker)
  178. {
  179. FILE *f = os_fopen(path, "wb");
  180. if (!f)
  181. return false;
  182. if (marker)
  183. fwrite("\xEF\xBB\xBF", 1, 3, f);
  184. if (len)
  185. fwrite(str, 1, len, f);
  186. fclose(f);
  187. return true;
  188. }
  189. bool os_quick_write_utf8_file_safe(const char *path, const char *str,
  190. size_t len, bool marker, const char *temp_ext,
  191. const char *backup_ext)
  192. {
  193. struct dstr backup_path = {0};
  194. struct dstr temp_path = {0};
  195. bool success = false;
  196. if (!temp_ext || !*temp_ext) {
  197. blog(LOG_ERROR, "os_quick_write_utf8_file_safe: invalid "
  198. "temporary extension specified");
  199. return false;
  200. }
  201. dstr_copy(&temp_path, path);
  202. if (*temp_ext != '.')
  203. dstr_cat(&temp_path, ".");
  204. dstr_cat(&temp_path, temp_ext);
  205. if (!os_quick_write_utf8_file(temp_path.array, str, len, marker)) {
  206. goto cleanup;
  207. }
  208. if (backup_ext && *backup_ext) {
  209. dstr_copy(&backup_path, path);
  210. if (*backup_ext != '.')
  211. dstr_cat(&backup_path, ".");
  212. dstr_cat(&backup_path, backup_ext);
  213. os_unlink(backup_path.array);
  214. os_rename(path, backup_path.array);
  215. dstr_free(&backup_path);
  216. } else {
  217. os_unlink(path);
  218. }
  219. os_rename(temp_path.array, path);
  220. success = true;
  221. cleanup:
  222. dstr_free(&backup_path);
  223. dstr_free(&temp_path);
  224. return success;
  225. }
  226. int64_t os_get_file_size(const char *path)
  227. {
  228. FILE* f = os_fopen(path, "rb");
  229. if (!f)
  230. return -1;
  231. int64_t sz = os_fgetsize(f);
  232. fclose(f);
  233. return sz;
  234. }
  235. size_t os_mbs_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
  236. {
  237. size_t out_len;
  238. if (!str)
  239. return 0;
  240. out_len = dst ? (dst_size - 1) : mbstowcs(NULL, str, len);
  241. if (dst) {
  242. if (!dst_size)
  243. return 0;
  244. if (out_len)
  245. out_len = mbstowcs(dst, str, out_len + 1);
  246. dst[out_len] = 0;
  247. }
  248. return out_len;
  249. }
  250. size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
  251. size_t dst_size)
  252. {
  253. size_t in_len;
  254. size_t out_len;
  255. if (!str)
  256. return 0;
  257. in_len = len ? len : strlen(str);
  258. out_len = dst ? (dst_size - 1) : utf8_to_wchar(str, in_len, NULL, 0, 0);
  259. if (dst) {
  260. if (!dst_size)
  261. return 0;
  262. if (out_len)
  263. out_len = utf8_to_wchar(str, in_len,
  264. dst, out_len + 1, 0);
  265. dst[out_len] = 0;
  266. }
  267. return out_len;
  268. }
  269. size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size)
  270. {
  271. size_t out_len;
  272. if (!str)
  273. return 0;
  274. out_len = dst ? (dst_size - 1) : wcstombs(NULL, str, len);
  275. if (dst) {
  276. if (!dst_size)
  277. return 0;
  278. if (out_len)
  279. out_len = wcstombs(dst, str, out_len + 1);
  280. dst[out_len] = 0;
  281. }
  282. return out_len;
  283. }
  284. size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
  285. size_t dst_size)
  286. {
  287. size_t in_len;
  288. size_t out_len;
  289. if (!str)
  290. return 0;
  291. in_len = (len != 0) ? len : wcslen(str);
  292. out_len = dst ? (dst_size - 1) : wchar_to_utf8(str, in_len, NULL, 0, 0);
  293. if (dst) {
  294. if (!dst_size)
  295. return 0;
  296. if (out_len)
  297. out_len = wchar_to_utf8(str, in_len,
  298. dst, out_len + 1, 0);
  299. dst[out_len] = 0;
  300. }
  301. return out_len;
  302. }
  303. size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  304. {
  305. if (str) {
  306. size_t out_len = os_mbs_to_wcs(str, len, NULL, 0);
  307. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  308. return os_mbs_to_wcs(str, len, *pstr, out_len + 1);
  309. } else {
  310. *pstr = NULL;
  311. return 0;
  312. }
  313. }
  314. size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  315. {
  316. if (str) {
  317. size_t out_len = os_utf8_to_wcs(str, len, NULL, 0);
  318. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  319. return os_utf8_to_wcs(str, len, *pstr, out_len + 1);
  320. } else {
  321. *pstr = NULL;
  322. return 0;
  323. }
  324. }
  325. size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr)
  326. {
  327. if (str) {
  328. size_t out_len = os_wcs_to_mbs(str, len, NULL, 0);
  329. *pstr = bmalloc((out_len + 1) * sizeof(char));
  330. return os_wcs_to_mbs(str, len, *pstr, out_len + 1);
  331. } else {
  332. *pstr = NULL;
  333. return 0;
  334. }
  335. }
  336. size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
  337. {
  338. if (str) {
  339. size_t out_len = os_wcs_to_utf8(str, len, NULL, 0);
  340. *pstr = bmalloc((out_len + 1) * sizeof(char));
  341. return os_wcs_to_utf8(str, len, *pstr, out_len + 1);
  342. } else {
  343. *pstr = NULL;
  344. return 0;
  345. }
  346. }
  347. size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
  348. {
  349. char *dst = NULL;
  350. size_t out_len = 0;
  351. if (str) {
  352. wchar_t *wstr = NULL;
  353. size_t wlen = os_utf8_to_wcs_ptr(str, len, &wstr);
  354. out_len = os_wcs_to_mbs_ptr(wstr, wlen, &dst);
  355. bfree(wstr);
  356. }
  357. *pstr = dst;
  358. return out_len;
  359. }
  360. size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr)
  361. {
  362. char *dst = NULL;
  363. size_t out_len = 0;
  364. if (str) {
  365. wchar_t *wstr = NULL;
  366. size_t wlen = os_mbs_to_wcs_ptr(str, len, &wstr);
  367. out_len = os_wcs_to_utf8_ptr(wstr, wlen, &dst);
  368. bfree(wstr);
  369. }
  370. *pstr = dst;
  371. return out_len;
  372. }
  373. /* locale independent double conversion from jansson, credit goes to them */
  374. static inline void to_locale(char *str)
  375. {
  376. const char *point;
  377. char *pos;
  378. point = localeconv()->decimal_point;
  379. if(*point == '.') {
  380. /* No conversion needed */
  381. return;
  382. }
  383. pos = strchr(str, '.');
  384. if(pos)
  385. *pos = *point;
  386. }
  387. static inline void from_locale(char *buffer)
  388. {
  389. const char *point;
  390. char *pos;
  391. point = localeconv()->decimal_point;
  392. if(*point == '.') {
  393. /* No conversion needed */
  394. return;
  395. }
  396. pos = strchr(buffer, *point);
  397. if(pos)
  398. *pos = '.';
  399. }
  400. double os_strtod(const char *str)
  401. {
  402. char buf[64];
  403. snprintf(buf, 64, "%s", str);
  404. to_locale(buf);
  405. return strtod(buf, NULL);
  406. }
  407. int os_dtostr(double value, char *dst, size_t size)
  408. {
  409. int ret;
  410. char *start, *end;
  411. size_t length;
  412. ret = snprintf(dst, size, "%.17g", value);
  413. if(ret < 0)
  414. return -1;
  415. length = (size_t)ret;
  416. if(length >= size)
  417. return -1;
  418. from_locale(dst);
  419. /* Make sure there's a dot or 'e' in the output. Otherwise
  420. a real is converted to an integer when decoding */
  421. if(strchr(dst, '.') == NULL && strchr(dst, 'e') == NULL) {
  422. if(length + 3 >= size) {
  423. /* No space to append ".0" */
  424. return -1;
  425. }
  426. dst[length] = '.';
  427. dst[length + 1] = '0';
  428. dst[length + 2] = '\0';
  429. length += 2;
  430. }
  431. /* Remove leading '+' from positive exponent. Also remove leading
  432. zeros from exponents (added by some printf() implementations) */
  433. start = strchr(dst, 'e');
  434. if(start) {
  435. start++;
  436. end = start + 1;
  437. if(*start == '-')
  438. start++;
  439. while(*end == '0')
  440. end++;
  441. if(end != start) {
  442. memmove(start, end, length - (size_t)(end - dst));
  443. length -= (size_t)(end - start);
  444. }
  445. }
  446. return (int)length;
  447. }
  448. static int recursive_mkdir(char *path)
  449. {
  450. char *last_slash;
  451. int ret;
  452. ret = os_mkdir(path);
  453. if (ret != MKDIR_ERROR)
  454. return ret;
  455. last_slash = strrchr(path, '/');
  456. if (!last_slash)
  457. return MKDIR_ERROR;
  458. *last_slash = 0;
  459. ret = recursive_mkdir(path);
  460. *last_slash = '/';
  461. if (ret == MKDIR_ERROR)
  462. return MKDIR_ERROR;
  463. ret = os_mkdir(path);
  464. return ret;
  465. }
  466. int os_mkdirs(const char *dir)
  467. {
  468. struct dstr dir_str;
  469. int ret;
  470. dstr_init_copy(&dir_str, dir);
  471. dstr_replace(&dir_str, "\\", "/");
  472. ret = recursive_mkdir(dir_str.array);
  473. dstr_free(&dir_str);
  474. return ret;
  475. }