platform.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 <time.h>
  18. #include <errno.h>
  19. #include <stdlib.h>
  20. #include <locale.h>
  21. #include "c99defs.h"
  22. #include "platform.h"
  23. #include "bmem.h"
  24. #include "utf8.h"
  25. #include "dstr.h"
  26. FILE *os_wfopen(const wchar_t *path, const char *mode)
  27. {
  28. FILE *file = NULL;
  29. if (path) {
  30. #ifdef _MSC_VER
  31. wchar_t *wcs_mode;
  32. os_utf8_to_wcs_ptr(mode, 0, &wcs_mode);
  33. file = _wfopen(path, wcs_mode);
  34. bfree(wcs_mode);
  35. #else
  36. char *mbs_path;
  37. os_wcs_to_utf8_ptr(path, 0, &mbs_path);
  38. file = fopen(mbs_path, mode);
  39. bfree(mbs_path);
  40. #endif
  41. }
  42. return file;
  43. }
  44. FILE *os_fopen(const char *path, const char *mode)
  45. {
  46. #ifdef _WIN32
  47. wchar_t *wpath = NULL;
  48. FILE *file = NULL;
  49. if (path) {
  50. os_utf8_to_wcs_ptr(path, 0, &wpath);
  51. file = os_wfopen(wpath, mode);
  52. bfree(wpath);
  53. }
  54. return file;
  55. #else
  56. return path ? fopen(path, mode) : NULL;
  57. #endif
  58. }
  59. int64_t os_fgetsize(FILE *file)
  60. {
  61. int64_t cur_offset = os_ftelli64(file);
  62. int64_t size;
  63. int errval = 0;
  64. if (fseek(file, 0, SEEK_END) == -1)
  65. return -1;
  66. size = os_ftelli64(file);
  67. if (size == -1)
  68. errval = errno;
  69. if (os_fseeki64(file, cur_offset, SEEK_SET) != 0 && errval != 0)
  70. errno = errval;
  71. return size;
  72. }
  73. #ifdef _WIN32
  74. int os_stat(const char *file, struct stat *st)
  75. {
  76. if (file) {
  77. wchar_t w_file[512];
  78. size_t size = os_utf8_to_wcs(file, 0, w_file, sizeof(w_file));
  79. if (size > 0) {
  80. struct _stat st_w32;
  81. int ret = _wstat(w_file, &st_w32);
  82. if (ret == 0) {
  83. st->st_dev = st_w32.st_dev;
  84. st->st_ino = st_w32.st_ino;
  85. st->st_mode = st_w32.st_mode;
  86. st->st_nlink = st_w32.st_nlink;
  87. st->st_uid = st_w32.st_uid;
  88. st->st_gid = st_w32.st_gid;
  89. st->st_rdev = st_w32.st_rdev;
  90. st->st_size = st_w32.st_size;
  91. st->st_atime = st_w32.st_atime;
  92. st->st_mtime = st_w32.st_mtime;
  93. st->st_ctime = st_w32.st_ctime;
  94. }
  95. return ret;
  96. }
  97. }
  98. return -1;
  99. }
  100. #endif
  101. int os_fseeki64(FILE *file, int64_t offset, int origin)
  102. {
  103. #ifdef _MSC_VER
  104. return _fseeki64(file, offset, origin);
  105. #else
  106. return fseeko(file, offset, origin);
  107. #endif
  108. }
  109. int64_t os_ftelli64(FILE *file)
  110. {
  111. #ifdef _MSC_VER
  112. return _ftelli64(file);
  113. #else
  114. return ftello(file);
  115. #endif
  116. }
  117. size_t os_fread_mbs(FILE *file, char **pstr)
  118. {
  119. size_t size = 0;
  120. size_t len = 0;
  121. fseek(file, 0, SEEK_END);
  122. size = (size_t)os_ftelli64(file);
  123. *pstr = NULL;
  124. if (size > 0) {
  125. char *mbstr = bmalloc(size+1);
  126. fseek(file, 0, SEEK_SET);
  127. size = fread(mbstr, 1, size, file);
  128. if (size == 0) {
  129. bfree(mbstr);
  130. return 0;
  131. }
  132. mbstr[size] = 0;
  133. len = os_mbs_to_utf8_ptr(mbstr, size, pstr);
  134. bfree(mbstr);
  135. }
  136. return len;
  137. }
  138. size_t os_fread_utf8(FILE *file, char **pstr)
  139. {
  140. size_t size = 0;
  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. bom[0] = 0;
  150. bom[1] = 0;
  151. bom[2] = 0;
  152. /* remove the ghastly BOM if present */
  153. fseek(file, 0, SEEK_SET);
  154. fread(bom, 1, 3, file);
  155. offset = (astrcmp_n(bom, "\xEF\xBB\xBF", 3) == 0) ? 3 : 0;
  156. size -= offset;
  157. if (size == 0)
  158. return 0;
  159. utf8str = bmalloc(size+1);
  160. fseek(file, offset, SEEK_SET);
  161. size = fread(utf8str, 1, size, file);
  162. if (size == 0) {
  163. bfree(utf8str);
  164. return 0;
  165. }
  166. utf8str[size] = 0;
  167. *pstr = utf8str;
  168. }
  169. return len;
  170. }
  171. char *os_quick_read_mbs_file(const char *path)
  172. {
  173. FILE *f = os_fopen(path, "rb");
  174. char *file_string = NULL;
  175. if (!f)
  176. return NULL;
  177. os_fread_mbs(f, &file_string);
  178. fclose(f);
  179. return file_string;
  180. }
  181. char *os_quick_read_utf8_file(const char *path)
  182. {
  183. FILE *f = os_fopen(path, "rb");
  184. char *file_string = NULL;
  185. if (!f)
  186. return NULL;
  187. os_fread_utf8(f, &file_string);
  188. fclose(f);
  189. return file_string;
  190. }
  191. bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
  192. {
  193. FILE *f = os_fopen(path, "wb");
  194. char *mbs = NULL;
  195. size_t mbs_len = 0;
  196. if (!f)
  197. return false;
  198. mbs_len = os_utf8_to_mbs_ptr(str, len, &mbs);
  199. if (mbs_len)
  200. fwrite(mbs, 1, mbs_len, f);
  201. bfree(mbs);
  202. fflush(f);
  203. fclose(f);
  204. return true;
  205. }
  206. bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
  207. bool marker)
  208. {
  209. FILE *f = os_fopen(path, "wb");
  210. if (!f)
  211. return false;
  212. if (marker)
  213. fwrite("\xEF\xBB\xBF", 1, 3, f);
  214. if (len)
  215. fwrite(str, 1, len, f);
  216. fflush(f);
  217. fclose(f);
  218. return true;
  219. }
  220. bool os_quick_write_utf8_file_safe(const char *path, const char *str,
  221. size_t len, bool marker, const char *temp_ext,
  222. const char *backup_ext)
  223. {
  224. struct dstr backup_path = {0};
  225. struct dstr temp_path = {0};
  226. bool success = false;
  227. if (!temp_ext || !*temp_ext) {
  228. blog(LOG_ERROR, "os_quick_write_utf8_file_safe: invalid "
  229. "temporary extension specified");
  230. return false;
  231. }
  232. dstr_copy(&temp_path, path);
  233. if (*temp_ext != '.')
  234. dstr_cat(&temp_path, ".");
  235. dstr_cat(&temp_path, temp_ext);
  236. if (!os_quick_write_utf8_file(temp_path.array, str, len, marker)) {
  237. goto cleanup;
  238. }
  239. if (backup_ext && *backup_ext) {
  240. dstr_copy(&backup_path, path);
  241. if (*backup_ext != '.')
  242. dstr_cat(&backup_path, ".");
  243. dstr_cat(&backup_path, backup_ext);
  244. }
  245. if (os_safe_replace(path, temp_path.array, backup_path.array) == 0)
  246. success = true;
  247. cleanup:
  248. dstr_free(&backup_path);
  249. dstr_free(&temp_path);
  250. return success;
  251. }
  252. int64_t os_get_file_size(const char *path)
  253. {
  254. FILE* f = os_fopen(path, "rb");
  255. if (!f)
  256. return -1;
  257. int64_t sz = os_fgetsize(f);
  258. fclose(f);
  259. return sz;
  260. }
  261. size_t os_mbs_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
  262. {
  263. size_t out_len;
  264. if (!str)
  265. return 0;
  266. out_len = dst ? (dst_size - 1) : mbstowcs(NULL, str, len);
  267. if (dst) {
  268. if (!dst_size)
  269. return 0;
  270. if (out_len)
  271. out_len = mbstowcs(dst, str, out_len + 1);
  272. dst[out_len] = 0;
  273. }
  274. return out_len;
  275. }
  276. size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
  277. size_t dst_size)
  278. {
  279. size_t in_len;
  280. size_t out_len;
  281. if (!str)
  282. return 0;
  283. in_len = len ? len : strlen(str);
  284. out_len = dst ? (dst_size - 1) : utf8_to_wchar(str, in_len, NULL, 0, 0);
  285. if (dst) {
  286. if (!dst_size)
  287. return 0;
  288. if (out_len)
  289. out_len = utf8_to_wchar(str, in_len,
  290. dst, out_len + 1, 0);
  291. dst[out_len] = 0;
  292. }
  293. return out_len;
  294. }
  295. size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size)
  296. {
  297. size_t out_len;
  298. if (!str)
  299. return 0;
  300. out_len = dst ? (dst_size - 1) : wcstombs(NULL, str, len);
  301. if (dst) {
  302. if (!dst_size)
  303. return 0;
  304. if (out_len)
  305. out_len = wcstombs(dst, str, out_len + 1);
  306. dst[out_len] = 0;
  307. }
  308. return out_len;
  309. }
  310. size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
  311. size_t dst_size)
  312. {
  313. size_t in_len;
  314. size_t out_len;
  315. if (!str)
  316. return 0;
  317. in_len = (len != 0) ? len : wcslen(str);
  318. out_len = dst ? (dst_size - 1) : wchar_to_utf8(str, in_len, NULL, 0, 0);
  319. if (dst) {
  320. if (!dst_size)
  321. return 0;
  322. if (out_len)
  323. out_len = wchar_to_utf8(str, in_len,
  324. dst, out_len + 1, 0);
  325. dst[out_len] = 0;
  326. }
  327. return out_len;
  328. }
  329. size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  330. {
  331. if (str) {
  332. size_t out_len = os_mbs_to_wcs(str, len, NULL, 0);
  333. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  334. return os_mbs_to_wcs(str, len, *pstr, out_len + 1);
  335. } else {
  336. *pstr = NULL;
  337. return 0;
  338. }
  339. }
  340. size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  341. {
  342. if (str) {
  343. size_t out_len = os_utf8_to_wcs(str, len, NULL, 0);
  344. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  345. return os_utf8_to_wcs(str, len, *pstr, out_len + 1);
  346. } else {
  347. *pstr = NULL;
  348. return 0;
  349. }
  350. }
  351. size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr)
  352. {
  353. if (str) {
  354. size_t out_len = os_wcs_to_mbs(str, len, NULL, 0);
  355. *pstr = bmalloc((out_len + 1) * sizeof(char));
  356. return os_wcs_to_mbs(str, len, *pstr, out_len + 1);
  357. } else {
  358. *pstr = NULL;
  359. return 0;
  360. }
  361. }
  362. size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
  363. {
  364. if (str) {
  365. size_t out_len = os_wcs_to_utf8(str, len, NULL, 0);
  366. *pstr = bmalloc((out_len + 1) * sizeof(char));
  367. return os_wcs_to_utf8(str, len, *pstr, out_len + 1);
  368. } else {
  369. *pstr = NULL;
  370. return 0;
  371. }
  372. }
  373. size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
  374. {
  375. char *dst = NULL;
  376. size_t out_len = 0;
  377. if (str) {
  378. wchar_t *wstr = NULL;
  379. size_t wlen = os_utf8_to_wcs_ptr(str, len, &wstr);
  380. out_len = os_wcs_to_mbs_ptr(wstr, wlen, &dst);
  381. bfree(wstr);
  382. }
  383. *pstr = dst;
  384. return out_len;
  385. }
  386. size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr)
  387. {
  388. char *dst = NULL;
  389. size_t out_len = 0;
  390. if (str) {
  391. wchar_t *wstr = NULL;
  392. size_t wlen = os_mbs_to_wcs_ptr(str, len, &wstr);
  393. out_len = os_wcs_to_utf8_ptr(wstr, wlen, &dst);
  394. bfree(wstr);
  395. }
  396. *pstr = dst;
  397. return out_len;
  398. }
  399. /* locale independent double conversion from jansson, credit goes to them */
  400. static inline void to_locale(char *str)
  401. {
  402. const char *point;
  403. char *pos;
  404. point = localeconv()->decimal_point;
  405. if(*point == '.') {
  406. /* No conversion needed */
  407. return;
  408. }
  409. pos = strchr(str, '.');
  410. if(pos)
  411. *pos = *point;
  412. }
  413. static inline void from_locale(char *buffer)
  414. {
  415. const char *point;
  416. char *pos;
  417. point = localeconv()->decimal_point;
  418. if(*point == '.') {
  419. /* No conversion needed */
  420. return;
  421. }
  422. pos = strchr(buffer, *point);
  423. if(pos)
  424. *pos = '.';
  425. }
  426. double os_strtod(const char *str)
  427. {
  428. char buf[64];
  429. snprintf(buf, 64, "%s", str);
  430. to_locale(buf);
  431. return strtod(buf, NULL);
  432. }
  433. int os_dtostr(double value, char *dst, size_t size)
  434. {
  435. int ret;
  436. char *start, *end;
  437. size_t length;
  438. ret = snprintf(dst, size, "%.17g", value);
  439. if(ret < 0)
  440. return -1;
  441. length = (size_t)ret;
  442. if(length >= size)
  443. return -1;
  444. from_locale(dst);
  445. /* Make sure there's a dot or 'e' in the output. Otherwise
  446. a real is converted to an integer when decoding */
  447. if(strchr(dst, '.') == NULL && strchr(dst, 'e') == NULL) {
  448. if(length + 3 >= size) {
  449. /* No space to append ".0" */
  450. return -1;
  451. }
  452. dst[length] = '.';
  453. dst[length + 1] = '0';
  454. dst[length + 2] = '\0';
  455. length += 2;
  456. }
  457. /* Remove leading '+' from positive exponent. Also remove leading
  458. zeros from exponents (added by some printf() implementations) */
  459. start = strchr(dst, 'e');
  460. if(start) {
  461. start++;
  462. end = start + 1;
  463. if(*start == '-')
  464. start++;
  465. while(*end == '0')
  466. end++;
  467. if(end != start) {
  468. memmove(start, end, length - (size_t)(end - dst));
  469. length -= (size_t)(end - start);
  470. }
  471. }
  472. return (int)length;
  473. }
  474. static int recursive_mkdir(char *path)
  475. {
  476. char *last_slash;
  477. int ret;
  478. ret = os_mkdir(path);
  479. if (ret != MKDIR_ERROR)
  480. return ret;
  481. last_slash = strrchr(path, '/');
  482. if (!last_slash)
  483. return MKDIR_ERROR;
  484. *last_slash = 0;
  485. ret = recursive_mkdir(path);
  486. *last_slash = '/';
  487. if (ret == MKDIR_ERROR)
  488. return MKDIR_ERROR;
  489. ret = os_mkdir(path);
  490. return ret;
  491. }
  492. int os_mkdirs(const char *dir)
  493. {
  494. struct dstr dir_str;
  495. int ret;
  496. dstr_init_copy(&dir_str, dir);
  497. dstr_replace(&dir_str, "\\", "/");
  498. ret = recursive_mkdir(dir_str.array);
  499. dstr_free(&dir_str);
  500. return ret;
  501. }
  502. const char *os_get_path_extension(const char *path)
  503. {
  504. struct dstr temp;
  505. size_t pos = 0;
  506. char *period;
  507. char *slash;
  508. dstr_init_copy(&temp, path);
  509. dstr_replace(&temp, "\\", "/");
  510. slash = strrchr(temp.array, '/');
  511. period = strrchr(temp.array, '.');
  512. if (period)
  513. pos = (size_t)(period - temp.array);
  514. dstr_free(&temp);
  515. if (!period || slash > period)
  516. return NULL;
  517. return path + pos;
  518. }
  519. static inline bool valid_string(const char *str)
  520. {
  521. while (str && *str) {
  522. if (*(str++) != ' ')
  523. return true;
  524. }
  525. return false;
  526. }
  527. static void replace_text(struct dstr *str, size_t pos, size_t len,
  528. const char *new_text)
  529. {
  530. struct dstr front = {0};
  531. struct dstr back = {0};
  532. dstr_left(&front, str, pos);
  533. dstr_right(&back, str, pos + len);
  534. dstr_copy_dstr(str, &front);
  535. dstr_cat(str, new_text);
  536. dstr_cat_dstr(str, &back);
  537. dstr_free(&front);
  538. dstr_free(&back);
  539. }
  540. static void erase_ch(struct dstr *str, size_t pos)
  541. {
  542. struct dstr new_str = {0};
  543. dstr_left(&new_str, str, pos);
  544. dstr_cat(&new_str, str->array + pos + 1);
  545. dstr_free(str);
  546. *str = new_str;
  547. }
  548. char *os_generate_formatted_filename(const char *extension, bool space,
  549. const char *format)
  550. {
  551. time_t now = time(0);
  552. struct tm *cur_time;
  553. cur_time = localtime(&now);
  554. const size_t spec_count = 23;
  555. static const char *spec[][2] = {
  556. {"%CCYY", "%Y"},
  557. {"%YY", "%y"},
  558. {"%MM", "%m"},
  559. {"%DD", "%d"},
  560. {"%hh", "%H"},
  561. {"%mm", "%M"},
  562. {"%ss", "%S"},
  563. {"%%", "%%"},
  564. {"%a", ""},
  565. {"%A", ""},
  566. {"%b", ""},
  567. {"%B", ""},
  568. {"%d", ""},
  569. {"%H", ""},
  570. {"%I", ""},
  571. {"%m", ""},
  572. {"%M", ""},
  573. {"%p", ""},
  574. {"%S", ""},
  575. {"%y", ""},
  576. {"%Y", ""},
  577. {"%z", ""},
  578. {"%Z", ""},
  579. };
  580. char convert[128] = {0};
  581. struct dstr sf;
  582. struct dstr c = {0};
  583. size_t pos = 0;
  584. dstr_init_copy(&sf, format);
  585. while (pos < sf.len) {
  586. for (size_t i = 0; i < spec_count && !convert[0]; i++) {
  587. size_t len = strlen(spec[i][0]);
  588. const char *cmp = sf.array + pos;
  589. if (astrcmp_n(cmp, spec[i][0], len) == 0) {
  590. if (strlen(spec[i][1]))
  591. strftime(convert, sizeof(convert),
  592. spec[i][1], cur_time);
  593. else
  594. strftime(convert, sizeof(convert),
  595. spec[i][0], cur_time);
  596. dstr_copy(&c, convert);
  597. if (c.len && valid_string(c.array))
  598. replace_text(&sf, pos, len, convert);
  599. }
  600. }
  601. if (convert[0]) {
  602. pos += strlen(convert);
  603. convert[0] = 0;
  604. } else if (!convert[0] && sf.array[pos] == '%') {
  605. erase_ch(&sf, pos);
  606. } else {
  607. pos++;
  608. }
  609. }
  610. if (!space)
  611. dstr_replace(&sf, " ", "_");
  612. dstr_cat_ch(&sf, '.');
  613. dstr_cat(&sf, extension);
  614. dstr_free(&c);
  615. if (sf.len > 255)
  616. dstr_mid(&sf, &sf, 0, 255);
  617. return sf.array;
  618. }