platform.c 6.7 KB

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