platform.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. fseeko(file, 0, SEEK_END);
  93. size = (size_t)ftello(file);
  94. if (size > 0) {
  95. char bom[3];
  96. char *utf8str;
  97. off_t offset;
  98. /* remove the ghastly BOM if present */
  99. fseeko(file, 0, SEEK_SET);
  100. size_read = fread(bom, 1, 3, file);
  101. if (size_read != 3)
  102. return 0;
  103. offset = (astrcmp_n(bom, "\xEF\xBB\xBF", 3) == 0) ? 3 : 0;
  104. size -= offset;
  105. utf8str = bmalloc(size+1);
  106. fseeko(file, offset, SEEK_SET);
  107. size = fread(utf8str, 1, size, file);
  108. if (size == 0) {
  109. bfree(utf8str);
  110. return 0;
  111. }
  112. utf8str[size] = 0;
  113. *pstr = utf8str;
  114. } else {
  115. *pstr = NULL;
  116. }
  117. return len;
  118. }
  119. char *os_quick_read_mbs_file(const char *path)
  120. {
  121. FILE *f = os_fopen(path, "rb");
  122. char *file_string = NULL;
  123. if (!f)
  124. return NULL;
  125. os_fread_mbs(f, &file_string);
  126. fclose(f);
  127. return file_string;
  128. }
  129. char *os_quick_read_utf8_file(const char *path)
  130. {
  131. FILE *f = os_fopen(path, "rb");
  132. char *file_string = NULL;
  133. if (!f)
  134. return NULL;
  135. os_fread_utf8(f, &file_string);
  136. fclose(f);
  137. return file_string;
  138. }
  139. bool os_quick_write_mbs_file(const char *path, const char *str, size_t len)
  140. {
  141. FILE *f = os_fopen(path, "wb");
  142. char *mbs = NULL;
  143. size_t mbs_len = 0;
  144. if (!f)
  145. return false;
  146. mbs_len = os_utf8_to_mbs_ptr(str, len, &mbs);
  147. if (mbs_len)
  148. fwrite(mbs, 1, mbs_len, f);
  149. bfree(mbs);
  150. fclose(f);
  151. return true;
  152. }
  153. bool os_quick_write_utf8_file(const char *path, const char *str, size_t len,
  154. bool marker)
  155. {
  156. FILE *f = os_fopen(path, "wb");
  157. if (!f)
  158. return false;
  159. if (marker)
  160. fwrite("\xEF\xBB\xBF", 1, 3, f);
  161. if (len)
  162. fwrite(str, 1, len, f);
  163. fclose(f);
  164. return true;
  165. }
  166. size_t os_mbs_to_wcs(const char *str, size_t len, wchar_t *dst)
  167. {
  168. size_t out_len = dst ? len : mbstowcs(NULL, str, len);
  169. if (len && dst) {
  170. mbstowcs(dst, str, out_len+1);
  171. dst[out_len] = 0;
  172. }
  173. return out_len;
  174. }
  175. size_t os_utf8_to_wcs(const char *str, size_t len, wchar_t *dst)
  176. {
  177. size_t in_len = len ? len : strlen(str);
  178. size_t out_len = dst ? len : utf8_to_wchar(str, in_len, NULL, 0, 0);
  179. if (out_len && dst) {
  180. utf8_to_wchar(str, in_len, dst, out_len+1, 0);
  181. dst[out_len] = 0;
  182. }
  183. return out_len;
  184. }
  185. size_t os_wcs_to_mbs(const wchar_t *str, size_t len, char *dst)
  186. {
  187. size_t out_len = dst ? len : wcstombs(NULL, str, len);
  188. if (len && dst) {
  189. wcstombs(dst, str, out_len+1);
  190. dst[out_len] = 0;
  191. }
  192. return out_len;
  193. }
  194. size_t os_wcs_to_utf8(const wchar_t *str, size_t len, char *dst)
  195. {
  196. size_t in_len = (len != 0) ? len : wcslen(str);
  197. size_t out_len = dst ? len : wchar_to_utf8(str, in_len, NULL, 0, 0);
  198. if (out_len && dst) {
  199. wchar_to_utf8(str, in_len, dst, out_len+1, 0);
  200. dst[out_len] = 0;
  201. }
  202. return out_len;
  203. }
  204. size_t os_mbs_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  205. {
  206. size_t out_len = os_mbs_to_wcs(str, len, NULL);
  207. *pstr = bmalloc((out_len+1) * sizeof(wchar_t));
  208. return os_mbs_to_wcs(str, out_len, *pstr);
  209. }
  210. size_t os_utf8_to_wcs_ptr(const char *str, size_t len, wchar_t **pstr)
  211. {
  212. size_t out_len = os_utf8_to_wcs(str, len, NULL);
  213. *pstr = bmalloc((out_len+1) * sizeof(wchar_t));
  214. return os_utf8_to_wcs(str, out_len, *pstr);
  215. }
  216. size_t os_wcs_to_mbs_ptr(const wchar_t *str, size_t len, char **pstr)
  217. {
  218. size_t out_len = os_wcs_to_mbs(str, len, NULL);
  219. *pstr = bmalloc((out_len+1) * sizeof(char));
  220. return os_wcs_to_mbs(str, out_len, *pstr);
  221. }
  222. size_t os_wcs_to_utf8_ptr(const wchar_t *str, size_t len, char **pstr)
  223. {
  224. size_t out_len = os_wcs_to_utf8(str, len, NULL);
  225. *pstr = bmalloc((out_len+1) * sizeof(char));
  226. return os_wcs_to_utf8(str, out_len, *pstr);
  227. }
  228. size_t os_utf8_to_mbs_ptr(const char *str, size_t len, char **pstr)
  229. {
  230. wchar_t *wstr = NULL;
  231. char *dst = NULL;
  232. size_t wlen = os_utf8_to_wcs_ptr(str, len, &wstr);
  233. size_t out_len = os_wcs_to_mbs_ptr(wstr, wlen, &dst);
  234. bfree(wstr);
  235. *pstr = dst;
  236. return out_len;
  237. }
  238. size_t os_mbs_to_utf8_ptr(const char *str, size_t len, char **pstr)
  239. {
  240. wchar_t *wstr = NULL;
  241. char *dst = NULL;
  242. size_t wlen = os_mbs_to_wcs_ptr(str, len, &wstr);
  243. size_t out_len = os_wcs_to_utf8_ptr(wstr, wlen, &dst);
  244. bfree(wstr);
  245. *pstr = dst;
  246. return out_len;
  247. }
  248. #ifdef _MSC_VER
  249. int fseeko(FILE *stream, off_t offset, int whence)
  250. {
  251. #if _FILE_OFFSET_BITS == 64
  252. return _fseeki64(stream, offset, whence);
  253. #else
  254. return fseek(stream, offset, whence);
  255. #endif /* _FILE_OFFSET_BITS == 64 */
  256. }
  257. off_t ftello(FILE *stream)
  258. {
  259. #if _FILE_OFFSET_BITS == 64
  260. return _ftelli64(stream);
  261. #else
  262. return ftell(stream);
  263. #endif /* _FILE_OFFSET_BITS == 64 */
  264. }
  265. #endif /* _MSC_VER */