platform.c 7.2 KB

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