platform.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  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. #ifdef _WIN32
  73. int os_stat(const char *file, struct stat *st)
  74. {
  75. if (file) {
  76. wchar_t w_file[512];
  77. size_t size = os_utf8_to_wcs(file, 0, w_file, sizeof(w_file));
  78. if (size > 0) {
  79. struct _stat st_w32;
  80. int ret = _wstat(w_file, &st_w32);
  81. if (ret == 0) {
  82. st->st_dev = st_w32.st_dev;
  83. st->st_ino = st_w32.st_ino;
  84. st->st_mode = st_w32.st_mode;
  85. st->st_nlink = st_w32.st_nlink;
  86. st->st_uid = st_w32.st_uid;
  87. st->st_gid = st_w32.st_gid;
  88. st->st_rdev = st_w32.st_rdev;
  89. st->st_size = st_w32.st_size;
  90. st->st_atime = st_w32.st_atime;
  91. st->st_mtime = st_w32.st_mtime;
  92. st->st_ctime = st_w32.st_ctime;
  93. }
  94. return ret;
  95. }
  96. }
  97. return -1;
  98. }
  99. #endif
  100. int os_fseeki64(FILE *file, int64_t offset, int origin)
  101. {
  102. #ifdef _MSC_VER
  103. return _fseeki64(file, offset, origin);
  104. #else
  105. return fseeko(file, offset, origin);
  106. #endif
  107. }
  108. int64_t os_ftelli64(FILE *file)
  109. {
  110. #ifdef _MSC_VER
  111. return _ftelli64(file);
  112. #else
  113. return ftello(file);
  114. #endif
  115. }
  116. size_t os_fread_mbs(FILE *file, char **pstr)
  117. {
  118. size_t size = 0;
  119. size_t len = 0;
  120. fseek(file, 0, SEEK_END);
  121. size = (size_t)os_ftelli64(file);
  122. *pstr = NULL;
  123. if (size > 0) {
  124. char *mbstr = bmalloc(size+1);
  125. fseek(file, 0, SEEK_SET);
  126. size = fread(mbstr, 1, size, file);
  127. if (size == 0) {
  128. bfree(mbstr);
  129. return 0;
  130. }
  131. mbstr[size] = 0;
  132. len = os_mbs_to_utf8_ptr(mbstr, size, pstr);
  133. bfree(mbstr);
  134. }
  135. return len;
  136. }
  137. size_t os_fread_utf8(FILE *file, char **pstr)
  138. {
  139. size_t size = 0;
  140. size_t size_read;
  141. size_t len = 0;
  142. *pstr = NULL;
  143. fseek(file, 0, SEEK_END);
  144. size = (size_t)os_ftelli64(file);
  145. if (size > 0) {
  146. char bom[3];
  147. char *utf8str;
  148. off_t offset;
  149. /* remove the ghastly BOM if present */
  150. fseek(file, 0, SEEK_SET);
  151. size_read = fread(bom, 1, 3, file);
  152. if (size_read != 3)
  153. return 0;
  154. offset = (astrcmp_n(bom, "\xEF\xBB\xBF", 3) == 0) ? 3 : 0;
  155. size -= offset;
  156. if (size == 0)
  157. return 0;
  158. utf8str = bmalloc(size+1);
  159. fseek(file, offset, SEEK_SET);
  160. size = fread(utf8str, 1, size, file);
  161. if (size == 0) {
  162. bfree(utf8str);
  163. return 0;
  164. }
  165. utf8str[size] = 0;
  166. *pstr = utf8str;
  167. }
  168. return len;
  169. }
  170. char *os_quick_read_mbs_file(const char *path)
  171. {
  172. FILE *f = os_fopen(path, "rb");
  173. char *file_string = NULL;
  174. if (!f)
  175. return NULL;
  176. os_fread_mbs(f, &file_string);
  177. fclose(f);
  178. return file_string;
  179. }
  180. char *os_quick_read_utf8_file(const char *path)
  181. {
  182. FILE *f = os_fopen(path, "rb");
  183. char *file_string = NULL;
  184. if (!f)
  185. return NULL;
  186. os_fread_utf8(f, &file_string);
  187. fclose(f);
  188. return file_string;
  189. }
  190. bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
  191. {
  192. FILE *f = os_fopen(path, "wb");
  193. char *mbs = NULL;
  194. size_t mbs_len = 0;
  195. if (!f)
  196. return false;
  197. mbs_len = os_utf8_to_mbs_ptr(str, len, &mbs);
  198. if (mbs_len)
  199. fwrite(mbs, 1, mbs_len, f);
  200. bfree(mbs);
  201. fclose(f);
  202. return true;
  203. }
  204. bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
  205. bool marker)
  206. {
  207. FILE *f = os_fopen(path, "wb");
  208. if (!f)
  209. return false;
  210. if (marker)
  211. fwrite("\xEF\xBB\xBF", 1, 3, f);
  212. if (len)
  213. fwrite(str, 1, len, f);
  214. fclose(f);
  215. return true;
  216. }
  217. bool os_quick_write_utf8_file_safe(const char *path, const char *str,
  218. size_t len, bool marker, const char *temp_ext,
  219. const char *backup_ext)
  220. {
  221. struct dstr backup_path = {0};
  222. struct dstr temp_path = {0};
  223. bool success = false;
  224. if (!temp_ext || !*temp_ext) {
  225. blog(LOG_ERROR, "os_quick_write_utf8_file_safe: invalid "
  226. "temporary extension specified");
  227. return false;
  228. }
  229. dstr_copy(&temp_path, path);
  230. if (*temp_ext != '.')
  231. dstr_cat(&temp_path, ".");
  232. dstr_cat(&temp_path, temp_ext);
  233. if (!os_quick_write_utf8_file(temp_path.array, str, len, marker)) {
  234. goto cleanup;
  235. }
  236. if (backup_ext && *backup_ext) {
  237. dstr_copy(&backup_path, path);
  238. if (*backup_ext != '.')
  239. dstr_cat(&backup_path, ".");
  240. dstr_cat(&backup_path, backup_ext);
  241. os_unlink(backup_path.array);
  242. os_rename(path, backup_path.array);
  243. dstr_free(&backup_path);
  244. } else {
  245. os_unlink(path);
  246. }
  247. os_rename(temp_path.array, path);
  248. success = true;
  249. cleanup:
  250. dstr_free(&backup_path);
  251. dstr_free(&temp_path);
  252. return success;
  253. }
  254. int64_t os_get_file_size(const char *path)
  255. {
  256. FILE* f = os_fopen(path, "rb");
  257. if (!f)
  258. return -1;
  259. int64_t sz = os_fgetsize(f);
  260. fclose(f);
  261. return sz;
  262. }
  263. size_t os_mbs_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
  264. {
  265. size_t out_len;
  266. if (!str)
  267. return 0;
  268. out_len = dst ? (dst_size - 1) : mbstowcs(NULL, str, len);
  269. if (dst) {
  270. if (!dst_size)
  271. return 0;
  272. if (out_len)
  273. out_len = mbstowcs(dst, str, out_len + 1);
  274. dst[out_len] = 0;
  275. }
  276. return out_len;
  277. }
  278. size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
  279. size_t dst_size)
  280. {
  281. size_t in_len;
  282. size_t out_len;
  283. if (!str)
  284. return 0;
  285. in_len = len ? len : strlen(str);
  286. out_len = dst ? (dst_size - 1) : utf8_to_wchar(str, in_len, NULL, 0, 0);
  287. if (dst) {
  288. if (!dst_size)
  289. return 0;
  290. if (out_len)
  291. out_len = utf8_to_wchar(str, in_len,
  292. dst, out_len + 1, 0);
  293. dst[out_len] = 0;
  294. }
  295. return out_len;
  296. }
  297. size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size)
  298. {
  299. size_t out_len;
  300. if (!str)
  301. return 0;
  302. out_len = dst ? (dst_size - 1) : wcstombs(NULL, str, len);
  303. if (dst) {
  304. if (!dst_size)
  305. return 0;
  306. if (out_len)
  307. out_len = wcstombs(dst, str, out_len + 1);
  308. dst[out_len] = 0;
  309. }
  310. return out_len;
  311. }
  312. size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
  313. size_t dst_size)
  314. {
  315. size_t in_len;
  316. size_t out_len;
  317. if (!str)
  318. return 0;
  319. in_len = (len != 0) ? len : wcslen(str);
  320. out_len = dst ? (dst_size - 1) : wchar_to_utf8(str, in_len, NULL, 0, 0);
  321. if (dst) {
  322. if (!dst_size)
  323. return 0;
  324. if (out_len)
  325. out_len = wchar_to_utf8(str, in_len,
  326. dst, out_len + 1, 0);
  327. dst[out_len] = 0;
  328. }
  329. return out_len;
  330. }
  331. size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  332. {
  333. if (str) {
  334. size_t out_len = os_mbs_to_wcs(str, len, NULL, 0);
  335. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  336. return os_mbs_to_wcs(str, len, *pstr, out_len + 1);
  337. } else {
  338. *pstr = NULL;
  339. return 0;
  340. }
  341. }
  342. size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  343. {
  344. if (str) {
  345. size_t out_len = os_utf8_to_wcs(str, len, NULL, 0);
  346. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  347. return os_utf8_to_wcs(str, len, *pstr, out_len + 1);
  348. } else {
  349. *pstr = NULL;
  350. return 0;
  351. }
  352. }
  353. size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr)
  354. {
  355. if (str) {
  356. size_t out_len = os_wcs_to_mbs(str, len, NULL, 0);
  357. *pstr = bmalloc((out_len + 1) * sizeof(char));
  358. return os_wcs_to_mbs(str, len, *pstr, out_len + 1);
  359. } else {
  360. *pstr = NULL;
  361. return 0;
  362. }
  363. }
  364. size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
  365. {
  366. if (str) {
  367. size_t out_len = os_wcs_to_utf8(str, len, NULL, 0);
  368. *pstr = bmalloc((out_len + 1) * sizeof(char));
  369. return os_wcs_to_utf8(str, len, *pstr, out_len + 1);
  370. } else {
  371. *pstr = NULL;
  372. return 0;
  373. }
  374. }
  375. size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
  376. {
  377. char *dst = NULL;
  378. size_t out_len = 0;
  379. if (str) {
  380. wchar_t *wstr = NULL;
  381. size_t wlen = os_utf8_to_wcs_ptr(str, len, &wstr);
  382. out_len = os_wcs_to_mbs_ptr(wstr, wlen, &dst);
  383. bfree(wstr);
  384. }
  385. *pstr = dst;
  386. return out_len;
  387. }
  388. size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr)
  389. {
  390. char *dst = NULL;
  391. size_t out_len = 0;
  392. if (str) {
  393. wchar_t *wstr = NULL;
  394. size_t wlen = os_mbs_to_wcs_ptr(str, len, &wstr);
  395. out_len = os_wcs_to_utf8_ptr(wstr, wlen, &dst);
  396. bfree(wstr);
  397. }
  398. *pstr = dst;
  399. return out_len;
  400. }
  401. /* locale independent double conversion from jansson, credit goes to them */
  402. static inline void to_locale(char *str)
  403. {
  404. const char *point;
  405. char *pos;
  406. point = localeconv()->decimal_point;
  407. if(*point == '.') {
  408. /* No conversion needed */
  409. return;
  410. }
  411. pos = strchr(str, '.');
  412. if(pos)
  413. *pos = *point;
  414. }
  415. static inline void from_locale(char *buffer)
  416. {
  417. const char *point;
  418. char *pos;
  419. point = localeconv()->decimal_point;
  420. if(*point == '.') {
  421. /* No conversion needed */
  422. return;
  423. }
  424. pos = strchr(buffer, *point);
  425. if(pos)
  426. *pos = '.';
  427. }
  428. double os_strtod(const char *str)
  429. {
  430. char buf[64];
  431. snprintf(buf, 64, "%s", str);
  432. to_locale(buf);
  433. return strtod(buf, NULL);
  434. }
  435. int os_dtostr(double value, char *dst, size_t size)
  436. {
  437. int ret;
  438. char *start, *end;
  439. size_t length;
  440. ret = snprintf(dst, size, "%.17g", value);
  441. if(ret < 0)
  442. return -1;
  443. length = (size_t)ret;
  444. if(length >= size)
  445. return -1;
  446. from_locale(dst);
  447. /* Make sure there's a dot or 'e' in the output. Otherwise
  448. a real is converted to an integer when decoding */
  449. if(strchr(dst, '.') == NULL && strchr(dst, 'e') == NULL) {
  450. if(length + 3 >= size) {
  451. /* No space to append ".0" */
  452. return -1;
  453. }
  454. dst[length] = '.';
  455. dst[length + 1] = '0';
  456. dst[length + 2] = '\0';
  457. length += 2;
  458. }
  459. /* Remove leading '+' from positive exponent. Also remove leading
  460. zeros from exponents (added by some printf() implementations) */
  461. start = strchr(dst, 'e');
  462. if(start) {
  463. start++;
  464. end = start + 1;
  465. if(*start == '-')
  466. start++;
  467. while(*end == '0')
  468. end++;
  469. if(end != start) {
  470. memmove(start, end, length - (size_t)(end - dst));
  471. length -= (size_t)(end - start);
  472. }
  473. }
  474. return (int)length;
  475. }
  476. static int recursive_mkdir(char *path)
  477. {
  478. char *last_slash;
  479. int ret;
  480. ret = os_mkdir(path);
  481. if (ret != MKDIR_ERROR)
  482. return ret;
  483. last_slash = strrchr(path, '/');
  484. if (!last_slash)
  485. return MKDIR_ERROR;
  486. *last_slash = 0;
  487. ret = recursive_mkdir(path);
  488. *last_slash = '/';
  489. if (ret == MKDIR_ERROR)
  490. return MKDIR_ERROR;
  491. ret = os_mkdir(path);
  492. return ret;
  493. }
  494. int os_mkdirs(const char *dir)
  495. {
  496. struct dstr dir_str;
  497. int ret;
  498. dstr_init_copy(&dir_str, dir);
  499. dstr_replace(&dir_str, "\\", "/");
  500. ret = recursive_mkdir(dir_str.array);
  501. dstr_free(&dir_str);
  502. return ret;
  503. }