2
0

file_access_windows.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*************************************************************************/
  2. /* file_access_windows.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifdef WINDOWS_ENABLED
  30. #include "file_access_windows.h"
  31. #include <sys/types.h>
  32. #include <sys/stat.h>
  33. #include <wchar.h>
  34. #include <tchar.h>
  35. #include "print_string.h"
  36. #ifdef _MSC_VER
  37. #define S_ISREG(m) ((m)&_S_IFREG)
  38. #endif
  39. void FileAccessWindows::check_errors() const {
  40. ERR_FAIL_COND(!f);
  41. if (feof(f)) {
  42. last_error=ERR_FILE_EOF;;
  43. }
  44. }
  45. Error FileAccessWindows::_open(const String& p_filename, int p_mode_flags) {
  46. String filename=fix_path(p_filename);
  47. if (f)
  48. close();
  49. const wchar_t* mode_string;
  50. if (p_mode_flags==READ)
  51. mode_string=L"rb";
  52. else if (p_mode_flags==WRITE)
  53. mode_string=L"wb";
  54. else if (p_mode_flags==READ_WRITE)
  55. mode_string=L"wb+";
  56. else
  57. return ERR_INVALID_PARAMETER;
  58. /* pretty much every implementation that uses fopen as primary
  59. backend supports utf8 encoding */
  60. struct _stat st;
  61. if (_wstat(filename.c_str(), &st) == 0) {
  62. if (!S_ISREG(st.st_mode))
  63. return ERR_FILE_CANT_OPEN;
  64. };
  65. if (is_backup_save_enabled() && p_mode_flags&WRITE && !(p_mode_flags&READ)) {
  66. save_path=filename;
  67. filename=filename+".tmp";
  68. //print_line("saving instead to "+path);
  69. }
  70. f=_wfopen(filename.c_str(), mode_string);
  71. if (f==NULL) {
  72. last_error=ERR_FILE_CANT_OPEN;
  73. return ERR_FILE_CANT_OPEN;
  74. } else {
  75. last_error=OK;
  76. flags=p_mode_flags;
  77. return OK;
  78. }
  79. }
  80. void FileAccessWindows::close() {
  81. if (!f)
  82. return;
  83. fclose(f);
  84. f = NULL;
  85. if (save_path!="") {
  86. //unlink(save_path.utf8().get_data());
  87. //print_line("renaming..");
  88. _wunlink(save_path.c_str()); //unlink if exists
  89. int rename_error = _wrename((save_path+".tmp").c_str(),save_path.c_str());
  90. save_path="";
  91. ERR_FAIL_COND( rename_error != 0);
  92. }
  93. }
  94. bool FileAccessWindows::is_open() const {
  95. return (f!=NULL);
  96. }
  97. void FileAccessWindows::seek(size_t p_position) {
  98. ERR_FAIL_COND(!f);
  99. last_error=OK;
  100. if ( fseek(f,p_position,SEEK_SET) )
  101. check_errors();
  102. }
  103. void FileAccessWindows::seek_end(int64_t p_position) {
  104. ERR_FAIL_COND(!f);
  105. if ( fseek(f,p_position,SEEK_END) )
  106. check_errors();
  107. }
  108. size_t FileAccessWindows::get_pos() const {
  109. size_t aux_position=0;
  110. if ( !(aux_position = ftell(f)) ) {
  111. check_errors();
  112. };
  113. return aux_position;
  114. }
  115. size_t FileAccessWindows::get_len() const {
  116. ERR_FAIL_COND_V(!f,0);
  117. size_t pos = get_pos();
  118. fseek(f,0,SEEK_END);
  119. int size = get_pos();
  120. fseek(f, pos, SEEK_SET);
  121. return size;
  122. }
  123. bool FileAccessWindows::eof_reached() const {
  124. check_errors();
  125. return last_error==ERR_FILE_EOF;
  126. }
  127. uint8_t FileAccessWindows::get_8() const {
  128. ERR_FAIL_COND_V(!f,0);
  129. uint8_t b;
  130. if (fread(&b,1,1,f) == 0) {
  131. check_errors();
  132. };
  133. return b;
  134. }
  135. int FileAccessWindows::get_buffer(uint8_t *p_dst, int p_length) const {
  136. ERR_FAIL_COND_V(!f,-1);
  137. int read = fread(p_dst, 1,p_length, f);
  138. check_errors();
  139. return read;
  140. };
  141. Error FileAccessWindows::get_error() const {
  142. return last_error;
  143. }
  144. void FileAccessWindows::store_8(uint8_t p_dest) {
  145. ERR_FAIL_COND(!f);
  146. fwrite(&p_dest,1,1,f);
  147. }
  148. bool FileAccessWindows::file_exists(const String& p_name) {
  149. FILE *g;
  150. //printf("opening file %s\n", p_fname.c_str());
  151. String filename=fix_path(p_name);
  152. g=_wfopen(filename.c_str(),L"rb");
  153. if (g==NULL) {
  154. return false;
  155. } else {
  156. fclose(g);
  157. return true;
  158. }
  159. }
  160. uint64_t FileAccessWindows::_get_modified_time(const String& p_file) {
  161. String file=fix_path(p_file);
  162. if (file.ends_with("/") && file!="/")
  163. file=file.substr(0,file.length()-1);
  164. struct _stat st;
  165. int rv = _wstat(file.c_str(), &st);
  166. if (rv == 0) {
  167. return st.st_mtime;
  168. } else {
  169. print_line("no access to "+file );
  170. }
  171. ERR_FAIL_V(0);
  172. };
  173. FileAccessWindows::FileAccessWindows() {
  174. f=NULL;
  175. flags=0;
  176. last_error=OK;
  177. }
  178. FileAccessWindows::~FileAccessWindows() {
  179. close();
  180. }
  181. #endif