filelib.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #ifdef SYSTEM_WIN32
  2. #pragma warning(disable:4996)
  3. #endif
  4. #ifdef HAVE_CONFIG_H
  5. #include "config.h"
  6. #endif
  7. #ifdef SYSTEM_WIN32
  8. #include <sys/stat.h>
  9. #include <io.h>
  10. #include <fcntl.h>
  11. #endif
  12. #ifdef SYSTEM_POSIX
  13. #ifdef HAVE_SYS_STAT_H
  14. #include <sys/stat.h>
  15. #endif
  16. #ifdef HAVE_FCNTL_H
  17. #include <fcntl.h>
  18. #endif
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #endif
  23. #include "cmdlib.h"
  24. #include "messages.h"
  25. #include "log.h"
  26. #include "mathtypes.h"
  27. #include "mathlib.h"
  28. #include "blockmem.h"
  29. /*
  30. * ==============
  31. * getfiletime
  32. * ==============
  33. */
  34. time_t getfiletime(const char* const filename)
  35. {
  36. time_t filetime = 0;
  37. struct stat filestat;
  38. if (stat(filename, &filestat) == 0)
  39. filetime = max(filestat.st_mtime, filestat.st_ctime);
  40. return filetime;
  41. }
  42. /*
  43. * ==============
  44. * getfilesize
  45. * ==============
  46. */
  47. long getfilesize(const char* const filename)
  48. {
  49. long size = 0;
  50. struct stat filestat;
  51. if (stat(filename, &filestat) == 0)
  52. size = filestat.st_size;
  53. return size;
  54. }
  55. /*
  56. * ==============
  57. * getfiledata
  58. * ==============
  59. */
  60. long getfiledata(const char* const filename, char* buffer, const int buffersize)
  61. {
  62. long size = 0;
  63. int handle;
  64. time_t start, end;
  65. time(&start);
  66. if ((handle = (int)fopen(filename, O_RDONLY)) != -1)
  67. {
  68. int bytesread;
  69. Log("%-20s Restoring [%-13s - ", "BuildVisMatrix:", filename);
  70. while ((bytesread = _read(handle, buffer, min(32 * 1024, buffersize - size))) > 0)
  71. {
  72. size += bytesread;
  73. buffer += bytesread;
  74. }
  75. _close(handle);
  76. time(&end);
  77. Log("%10.3fMB] (%d)\n", size / (1024.0 * 1024.0), end - start);
  78. }
  79. if (buffersize != size)
  80. {
  81. Warning("Invalid file [%s] found. File will be rebuilt!\n", filename);
  82. _unlink(filename);
  83. }
  84. return size;
  85. }
  86. /*
  87. * ================
  88. * filelength
  89. * ================
  90. */
  91. int q_filelength(FILE* f)
  92. {
  93. int pos;
  94. int end;
  95. pos = ftell(f);
  96. fseek(f, 0, SEEK_END);
  97. end = ftell(f);
  98. fseek(f, pos, SEEK_SET);
  99. return end;
  100. }
  101. /*
  102. * ================
  103. * exists
  104. * ================
  105. */
  106. bool q_exists(const char* const filename)
  107. {
  108. FILE* f;
  109. f = fopen(filename, "rb");
  110. if (!f)
  111. {
  112. IfDebug(Developer(DEVELOPER_LEVEL_SPAM, "Checking for existance of file %s (failed)\n", filename));
  113. return false;
  114. }
  115. else
  116. {
  117. fclose(f);
  118. IfDebug(Developer(DEVELOPER_LEVEL_SPAM, "Checking for existance of file %s (success)\n", filename));
  119. return true;
  120. }
  121. }
  122. FILE* SafeOpenWrite(const char* const filename)
  123. {
  124. FILE* f;
  125. f = fopen(filename, "wb");
  126. if (!f)
  127. Error("Error opening %s: %s", filename, strerror(errno));
  128. return f;
  129. }
  130. FILE* SafeOpenRead(const char* const filename)
  131. {
  132. FILE* f;
  133. f = fopen(filename, "rb");
  134. if (!f)
  135. Error("Error opening %s: %s", filename, strerror(errno));
  136. return f;
  137. }
  138. void SafeRead(FILE* f, void* buffer, int count)
  139. {
  140. if (fread(buffer, 1, count, f) != (size_t) count) {
  141. Log("\n");
  142. Error("File read failure.\n");
  143. }
  144. }
  145. void SafeWrite(FILE* f, const void* const buffer, int count)
  146. {
  147. if (fwrite(buffer, 1, count, f) != (size_t) count) {
  148. Log("\n");
  149. Error("File write failure.\n"
  150. " Maybe you exceeded a critical map limit? (E.g.: Leafs)\n"
  151. " Check above. (Some objects, as a result of exceeding a\n"
  152. " limit, may be negative bogus)\n");
  153. }
  154. }
  155. /*
  156. * ==============
  157. * LoadFile
  158. * ==============
  159. */
  160. int LoadFile(const char* const filename, char** bufferptr)
  161. {
  162. FILE* f;
  163. int length;
  164. char* buffer;
  165. f = SafeOpenRead(filename);
  166. length = q_filelength(f);
  167. buffer = (char*)Alloc(length + 1);
  168. SafeRead(f, buffer, length);
  169. fclose(f);
  170. *bufferptr = buffer;
  171. return length;
  172. }
  173. /*
  174. * ==============
  175. * SaveFile
  176. * ==============
  177. */
  178. void SaveFile(const char* const filename, const void* const buffer, int count)
  179. {
  180. FILE* f;
  181. f = SafeOpenWrite(filename);
  182. SafeWrite(f, buffer, count);
  183. fclose(f);
  184. }