platform.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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 "c99defs.h"
  20. #include "platform.h"
  21. #include "bmem.h"
  22. #include "utf8.h"
  23. #include "dstr.h"
  24. FILE *os_wfopen(const wchar_t *path, const char *mode)
  25. {
  26. FILE *file = NULL;
  27. if (path) {
  28. #ifdef _MSC_VER
  29. wchar_t *wcs_mode;
  30. os_utf8_to_wcs_ptr(mode, 0, &wcs_mode);
  31. file = _wfopen(path, wcs_mode);
  32. bfree(wcs_mode);
  33. #else
  34. char *mbs_path;
  35. os_wcs_to_utf8_ptr(path, 0, &mbs_path);
  36. file = fopen(mbs_path, mode);
  37. bfree(mbs_path);
  38. #endif
  39. }
  40. return file;
  41. }
  42. FILE *os_fopen(const char *path, const char *mode)
  43. {
  44. #ifdef _WIN32
  45. wchar_t *wpath = NULL;
  46. FILE *file = NULL;
  47. if (path) {
  48. os_utf8_to_wcs_ptr(path, 0, &wpath);
  49. file = os_wfopen(wpath, mode);
  50. bfree(wpath);
  51. }
  52. return file;
  53. #else
  54. return path ? fopen(path, mode) : NULL;
  55. #endif
  56. }
  57. int64_t os_fgetsize(FILE *file)
  58. {
  59. int64_t cur_offset = os_ftelli64(file);
  60. int64_t size;
  61. int errval = 0;
  62. if (fseek(file, 0, SEEK_END) == -1)
  63. return -1;
  64. size = os_ftelli64(file);
  65. if (size == -1)
  66. errval = errno;
  67. if (os_fseeki64(file, cur_offset, SEEK_SET) != 0 && errval != 0)
  68. errno = errval;
  69. return size;
  70. }
  71. int os_fseeki64(FILE *file, int64_t offset, int origin)
  72. {
  73. #ifdef _MSC_VER
  74. return _fseeki64(file, offset, origin);
  75. #else
  76. return fseeko(file, offset, origin);
  77. #endif
  78. }
  79. int64_t os_ftelli64(FILE *file)
  80. {
  81. #ifdef _MSC_VER
  82. return _ftelli64(file);
  83. #else
  84. return ftello(file);
  85. #endif
  86. }
  87. size_t os_fread_mbs(FILE *file, char **pstr)
  88. {
  89. size_t size = 0;
  90. size_t len = 0;
  91. fseek(file, 0, SEEK_END);
  92. size = (size_t)os_ftelli64(file);
  93. *pstr = NULL;
  94. if (size > 0) {
  95. char *mbstr = bmalloc(size+1);
  96. fseek(file, 0, SEEK_SET);
  97. size = fread(mbstr, 1, size, file);
  98. if (size == 0) {
  99. bfree(mbstr);
  100. return 0;
  101. }
  102. mbstr[size] = 0;
  103. len = os_mbs_to_utf8_ptr(mbstr, size, pstr);
  104. bfree(mbstr);
  105. }
  106. return len;
  107. }
  108. size_t os_fread_utf8(FILE *file, char **pstr)
  109. {
  110. size_t size = 0;
  111. size_t size_read;
  112. size_t len = 0;
  113. *pstr = NULL;
  114. fseek(file, 0, SEEK_END);
  115. size = (size_t)os_ftelli64(file);
  116. if (size > 0) {
  117. char bom[3];
  118. char *utf8str;
  119. off_t offset;
  120. /* remove the ghastly BOM if present */
  121. fseek(file, 0, SEEK_SET);
  122. size_read = fread(bom, 1, 3, file);
  123. if (size_read != 3)
  124. return 0;
  125. offset = (astrcmp_n(bom, "\xEF\xBB\xBF", 3) == 0) ? 3 : 0;
  126. size -= offset;
  127. if (size == 0)
  128. return 0;
  129. utf8str = bmalloc(size+1);
  130. fseek(file, offset, SEEK_SET);
  131. size = fread(utf8str, 1, size, file);
  132. if (size == 0) {
  133. bfree(utf8str);
  134. return 0;
  135. }
  136. utf8str[size] = 0;
  137. *pstr = utf8str;
  138. }
  139. return len;
  140. }
  141. char *os_quick_read_mbs_file(const char *path)
  142. {
  143. FILE *f = os_fopen(path, "rb");
  144. char *file_string = NULL;
  145. if (!f)
  146. return NULL;
  147. os_fread_mbs(f, &file_string);
  148. fclose(f);
  149. return file_string;
  150. }
  151. char *os_quick_read_utf8_file(const char *path)
  152. {
  153. FILE *f = os_fopen(path, "rb");
  154. char *file_string = NULL;
  155. if (!f)
  156. return NULL;
  157. os_fread_utf8(f, &file_string);
  158. fclose(f);
  159. return file_string;
  160. }
  161. bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
  162. {
  163. FILE *f = os_fopen(path, "wb");
  164. char *mbs = NULL;
  165. size_t mbs_len = 0;
  166. if (!f)
  167. return false;
  168. mbs_len = os_utf8_to_mbs_ptr(str, len, &mbs);
  169. if (mbs_len)
  170. fwrite(mbs, 1, mbs_len, f);
  171. bfree(mbs);
  172. fclose(f);
  173. return true;
  174. }
  175. bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
  176. bool marker)
  177. {
  178. FILE *f = os_fopen(path, "wb");
  179. if (!f)
  180. return false;
  181. if (marker)
  182. fwrite("\xEF\xBB\xBF", 1, 3, f);
  183. if (len)
  184. fwrite(str, 1, len, f);
  185. fclose(f);
  186. return true;
  187. }
  188. size_t os_mbs_to_wcs(const char *str, size_t len, wchar_t *dst, size_t dst_size)
  189. {
  190. size_t out_len;
  191. if (!str)
  192. return 0;
  193. out_len = dst ? (dst_size - 1) : mbstowcs(NULL, str, len);
  194. if (len && dst) {
  195. if (!dst_size)
  196. return 0;
  197. if (out_len)
  198. out_len = mbstowcs(dst, str, out_len + 1);
  199. dst[out_len] = 0;
  200. }
  201. return out_len;
  202. }
  203. size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst,
  204. size_t dst_size)
  205. {
  206. size_t in_len;
  207. size_t out_len;
  208. if (!str)
  209. return 0;
  210. in_len = len ? len : strlen(str);
  211. out_len = dst ? (dst_size - 1) : utf8_to_wchar(str, in_len, NULL, 0, 0);
  212. if (out_len && dst) {
  213. if (!dst_size)
  214. return 0;
  215. if (out_len)
  216. out_len = utf8_to_wchar(str, in_len,
  217. dst, out_len + 1, 0);
  218. dst[out_len] = 0;
  219. }
  220. return out_len;
  221. }
  222. size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst, size_t dst_size)
  223. {
  224. size_t out_len;
  225. if (!str)
  226. return 0;
  227. out_len = dst ? (dst_size - 1) : wcstombs(NULL, str, len);
  228. if (len && dst) {
  229. if (!dst_size)
  230. return 0;
  231. if (out_len)
  232. out_len = wcstombs(dst, str, out_len + 1);
  233. dst[out_len] = 0;
  234. }
  235. return out_len;
  236. }
  237. size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst,
  238. size_t dst_size)
  239. {
  240. size_t in_len;
  241. size_t out_len;
  242. if (!str)
  243. return 0;
  244. in_len = (len != 0) ? len : wcslen(str);
  245. out_len = dst ? (dst_size - 1) : wchar_to_utf8(str, in_len, NULL, 0, 0);
  246. if (out_len && dst) {
  247. if (!dst_size)
  248. return 0;
  249. if (out_len)
  250. out_len = wchar_to_utf8(str, in_len,
  251. dst, out_len + 1, 0);
  252. dst[out_len] = 0;
  253. }
  254. return out_len;
  255. }
  256. size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  257. {
  258. if (str) {
  259. size_t out_len = os_mbs_to_wcs(str, len, NULL, 0);
  260. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  261. return os_mbs_to_wcs(str, len, *pstr, out_len + 1);
  262. } else {
  263. *pstr = NULL;
  264. return 0;
  265. }
  266. }
  267. size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  268. {
  269. if (str) {
  270. size_t out_len = os_utf8_to_wcs(str, len, NULL, 0);
  271. *pstr = bmalloc((out_len + 1) * sizeof(wchar_t));
  272. return os_utf8_to_wcs(str, len, *pstr, out_len + 1);
  273. } else {
  274. *pstr = NULL;
  275. return 0;
  276. }
  277. }
  278. size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr)
  279. {
  280. if (str) {
  281. size_t out_len = os_wcs_to_mbs(str, len, NULL, 0);
  282. *pstr = bmalloc((out_len + 1) * sizeof(char));
  283. return os_wcs_to_mbs(str, len, *pstr, out_len + 1);
  284. } else {
  285. *pstr = NULL;
  286. return 0;
  287. }
  288. }
  289. size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
  290. {
  291. if (str) {
  292. size_t out_len = os_wcs_to_utf8(str, len, NULL, 0);
  293. *pstr = bmalloc((out_len + 1) * sizeof(char));
  294. return os_wcs_to_utf8(str, len, *pstr, out_len + 1);
  295. } else {
  296. *pstr = NULL;
  297. return 0;
  298. }
  299. }
  300. size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
  301. {
  302. char *dst = NULL;
  303. size_t out_len = 0;
  304. if (str) {
  305. wchar_t *wstr = NULL;
  306. size_t wlen = os_utf8_to_wcs_ptr(str, len, &wstr);
  307. out_len = os_wcs_to_mbs_ptr(wstr, wlen, &dst);
  308. bfree(wstr);
  309. }
  310. *pstr = dst;
  311. return out_len;
  312. }
  313. size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr)
  314. {
  315. char *dst = NULL;
  316. size_t out_len = 0;
  317. if (str) {
  318. wchar_t *wstr = NULL;
  319. size_t wlen = os_mbs_to_wcs_ptr(str, len, &wstr);
  320. out_len = os_wcs_to_utf8_ptr(wstr, wlen, &dst);
  321. bfree(wstr);
  322. }
  323. *pstr = dst;
  324. return out_len;
  325. }