file.hh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /********************************************************************** <BR>
  2. This file is part of Crack dot Com's free source code release of
  3. Golgotha. <a href="http://www.crack.com/golgotha_release"> <BR> for
  4. information about compiling & licensing issues visit this URL</a>
  5. <PRE> If that doesn't help, contact Jonathan Clark at
  6. golgotha_source@usa.net (Subject should have "GOLG" in it)
  7. ***********************************************************************/
  8. #ifndef __I4FILE_HPP
  9. #define __I4FILE_HPP
  10. #include "arch.hh"
  11. #include "isllist.hh"
  12. enum { I4_FILE_STATUS_DIRECTORY=1};
  13. struct i4_file_status_struct
  14. {
  15. w32 last_modified;
  16. w32 last_accessed;
  17. w32 created;
  18. w32 flags;
  19. } ;
  20. // Summary
  21. // A file (i4_file_class) is a purely virtual file
  22. // which should allow much flexability.
  23. // A i4_file_class is opened through the i4_file_manager_class::open().
  24. // A file manager defaults going through the operating system's open(), but
  25. // like the unix model other file managers can be mounted on top of directories.
  26. // When a file is accessed in this directory, the open call will be transfered
  27. // to this file manager.
  28. // This allows for redirection, tar-like-files, memory-files, and other goodies.
  29. // One particular instance this is useful, might be running game off of a cdrom.
  30. // the data directory can be linked to the cdrom and the savegame data can
  31. // be linked to somewhere on the hard-drive.
  32. // i.e.
  33. // i4_file_man.mount_dir("/cdrom",new file_redirector("d:\\"));
  34. // i4_file_class *fp=i4_file_man.open("/cdrom/art/picture.pcx");
  35. // TODO : test mounting, now that all string are i4_const_str instead of char *
  36. class i4_const_str;
  37. class i4_str;
  38. class i4_file_class
  39. {
  40. public:
  41. virtual w32 read (void *buffer, w32 size) = 0;
  42. virtual w32 write(const void *buffer, w32 size) = 0;
  43. virtual w32 seek (w32 offset) = 0;
  44. virtual w32 size () = 0;
  45. virtual w32 tell () = 0;
  46. i4_bool eof() { return tell()==size(); }
  47. typedef void (*async_callback)(w32 count, void *context);
  48. // returns i4_F if an immediate error occured
  49. virtual i4_bool async_read (void *buffer, w32 size,
  50. async_callback call,
  51. void *context=0);
  52. virtual i4_bool async_write(const void *buffer, w32 size,
  53. async_callback call,
  54. void *context=0);
  55. // abort current operation
  56. virtual void abort() {}
  57. // Nice Read Methods
  58. w8 read_8 ()
  59. {
  60. w8 buf;
  61. if (read(&buf,1)!=1)
  62. return 0xff;
  63. else return buf;
  64. }
  65. w16 read_16 () // loads and converts to LSB (intel-format)
  66. {
  67. w16 buf;
  68. if (read(&buf,2)!=2)
  69. return 0xffff;
  70. else return s_to_lsb(buf);
  71. }
  72. w32 read_32 () // loads and converts to LSB (intel-format)
  73. {
  74. w32 buf;
  75. if (read(&buf,4)!=4)
  76. return 0xffffffff;
  77. else return l_to_lsb(buf);
  78. }
  79. float read_float() // loads and converts to LSB (intel-format)
  80. {
  81. w32 buf;
  82. if (read(&buf,4)!=4)
  83. return (float)0xffff;
  84. buf = l_to_lsb(buf);
  85. return *((float*)&buf);
  86. }
  87. i4_str* read_str(w32 len);
  88. i4_str* read_counted_str();
  89. // C++ stream operators
  90. i4_file_class& operator>>(w32& v) { v = (w32) read_32(); return *this; }
  91. i4_file_class& operator>>(sw32& v) { v = (sw32)read_32(); return *this; }
  92. i4_file_class& operator>>(w16& v) { v = (w16) read_16(); return *this; }
  93. i4_file_class& operator>>(sw16& v) { v = (sw16)read_16(); return *this; }
  94. i4_file_class& operator>>(w8& v) { v = (w8) read_8(); return *this; }
  95. i4_file_class& operator>>(sw8& v) { v = (sw8) read_8(); return *this; }
  96. i4_file_class& operator>>(float& v) { v = read_float(); return *this; }
  97. // Nice Write Methods
  98. w32 write_8 (w8 num)
  99. {
  100. return (write(&num,1));
  101. }
  102. w32 write_16 (w16 num) // loads and converts to LSB (intel-format)
  103. {
  104. num = s_to_lsb(num);
  105. return (write(&num,2));
  106. }
  107. w32 write_32 (w32 num) // loads and converts to LSB (intel-format)
  108. {
  109. num = l_to_lsb(num);
  110. return (write(&num,4));
  111. }
  112. w32 write_float(float num) // loads and converts to LSB (intel-format)
  113. {
  114. w32 tmp = l_to_lsb( *((w32*)&num) );
  115. return (write(&tmp,4));
  116. }
  117. w32 write_str(const i4_const_str &str);
  118. w32 write_counted_str(const i4_const_str &str);
  119. // same as fprintf, but with the addition %S is a i4_const_str *
  120. int printf(char *format, ...);
  121. // write_format takes a different set of % symbols than the typical printf to elimante
  122. // confusion with sizes and be easily usuable with readf_binary
  123. // writef_binary("124fS", &a_w8_var, &a_w16_var, &a_w32_var,
  124. // &a_float_var, &a_i4_const_str_pointer);
  125. int write_format(char *format, ...);
  126. // format is same as write_format
  127. int read_format(char *format, ...);
  128. // C++ stream operators
  129. i4_file_class& operator<<(w32 v) { write_32(v); return *this; }
  130. i4_file_class& operator<<(sw32 v) { write_32((w32)v); return *this; }
  131. i4_file_class& operator<<(w16 v) { write_16(v); return *this; }
  132. i4_file_class& operator<<(sw16 v) { write_16((w16)v); return *this; }
  133. i4_file_class& operator<<(w8 v) { write_8(v); return *this; }
  134. i4_file_class& operator<<(sw8 v) { write_8((w8)v); return *this; }
  135. i4_file_class& operator<<(float v) { write_float(v); return *this; }
  136. virtual ~i4_file_class() {}
  137. };
  138. // open flags
  139. enum { I4_READ=1,
  140. I4_WRITE=2,
  141. I4_APPEND=4,
  142. I4_NO_BUFFER=8,
  143. I4_SUPPORT_ASYNC=16 // this flag is needed if you intend to call async_read/write
  144. };
  145. // returns NULL if unable to open file
  146. i4_file_class *i4_open(const i4_const_str &name, w32 flags=I4_READ);
  147. // return i4_F on failure
  148. i4_bool i4_unlink(const i4_const_str &name);
  149. // returns i4_F if file does not exsist
  150. i4_bool i4_get_status(const i4_const_str &filename,
  151. i4_file_status_struct &return_stat);
  152. // return i4_F on failure
  153. i4_bool i4_mkdir(const i4_const_str &path);
  154. i4_bool i4_rmdir(const i4_const_str &path);
  155. i4_bool i4_chdir(const i4_const_str &path);
  156. struct i4_directory_struct
  157. {
  158. i4_str **files;
  159. w32 tfiles;
  160. i4_str **dirs;
  161. w32 tdirs;
  162. i4_file_status_struct *file_status; // array of file stats coresponding to above files
  163. i4_directory_struct() { tfiles=tdirs=0; files=dirs=0; file_status=0; }
  164. ~i4_directory_struct();
  165. };
  166. // returns i4_F if path is bad (tfiles and tdirs will be 0 as well)
  167. // you are responsible for deleting both the array of strings and each string in the array
  168. // file_status is a pointer to an array of file_status's that will be created, you
  169. // must free these as well. file_status may be 0 (default), in which case no array is created
  170. class i4_status_class;
  171. i4_bool i4_get_directory(const i4_const_str &path,
  172. i4_directory_struct &dir_struct,
  173. i4_bool get_status=i4_F,
  174. i4_status_class *status=0);
  175. struct i4_filename_struct
  176. {
  177. char path[256];
  178. char filename[256];
  179. char extension[256];
  180. i4_filename_struct() { path[0]=0; filename[0]=0; extension[0]=0; }
  181. };
  182. // returns i4_F if path cannot be split
  183. i4_bool i4_split_path(const i4_const_str &name, i4_filename_struct &fname_struct);
  184. // return 0 if full path cannot be determined
  185. i4_str *i4_full_path(const i4_const_str &relative_name);
  186. // returns the shortest relative path
  187. i4_str *i4_relative_path(const i4_const_str &path);
  188. #endif