gcsx_file.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* GCSx
  2. ** FILE.H
  3. **
  4. ** Basic file access with exceptions
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #ifndef __GCSx_FILE_H_
  24. #define __GCSx_FILE_H_
  25. // Traits of the current file system
  26. // Case sensitive?
  27. // Uses drives? and separator
  28. // Absolute path prefix
  29. // Valid filename separators
  30. // Preferred filename separator
  31. // Extension separator
  32. #ifdef WIN32
  33. // Windows
  34. #define FILESYSTEM_CASE_SENSITIVE 0
  35. #define FILESYSTEM_DRIVE_SEP ':'
  36. #define FILESYSTEM_ABSOLUTEPREFIX ""
  37. #define FILESYSTEM_SEPARATORS "\\/"
  38. #define FILESYSTEM_PREFERREDSEP "\\"
  39. #define FILESYSTEM_EXTENSIONSEP "."
  40. #else
  41. // For now, assume *nix
  42. #define FILESYSTEM_CASE_SENSITIVE 1
  43. #define FILESYSTEM_DRIVE_SEP 0
  44. #define FILESYSTEM_ABSOLUTEPREFIX "/"
  45. #define FILESYSTEM_SEPARATORS "/"
  46. #define FILESYSTEM_PREFERREDSEP "/"
  47. #define FILESYSTEM_EXTENSIONSEP "."
  48. #endif
  49. // Even game module uses read and write, so there's no need
  50. // for an object that doesn't support writing at all.
  51. class File {
  52. public:
  53. enum {
  54. FILE_MODE_READ = 0,
  55. FILE_MODE_READWRITE,
  56. FILE_MODE_OVERWRITE, // Read/Write
  57. };
  58. private:
  59. FILE* filePtr;
  60. const char* filename;
  61. int mode;
  62. int offset;
  63. enum {
  64. OPER_READ,
  65. OPER_WRITE,
  66. OPER_SEEK,
  67. } lastOperation;
  68. void reseek();
  69. enum {
  70. COPY_BUFFER_SIZE = 8192,
  71. };
  72. static char copyBuffer[COPY_BUFFER_SIZE + 1];
  73. public:
  74. // Pointer must remain valid
  75. File(const char* fFilename, int fMode) throw_File;
  76. ~File();
  77. void read(void* ptr, int size) throw_File;
  78. Uint32 readInt() throw_File;
  79. /* not used at this time
  80. Uint16 readInt16() throw_File;
  81. // (returns total bytes read- not the same as string length)
  82. // NON-REENTRANT
  83. int readStr(std::string& str) throw_File;
  84. */
  85. void write(const void* ptr, int size) throw_File;
  86. void writeInt(Uint32 value) throw_File;
  87. /* not used at this time
  88. void writeInt16(Uint16 value) throw_File;
  89. // (returns total bytes written- not the same as string length)
  90. int writeStr(const std::string& str) throw_File;
  91. */
  92. void writeBlanks(int size) throw_File;
  93. void flush() throw_File;
  94. // NON-REENTRANT
  95. void copy(File* copyFrom, int size) throw_File;
  96. void skip(int size) throw_File;
  97. void seek(int position) throw_File;
  98. int tell() const;
  99. };
  100. // Generic filename and directory functions, to keep things cross-compatible
  101. // Top-level directory to start all searching at
  102. const char* topDir();
  103. // Adds to a vector all subdirectories (NULL match) OR matching files (.ext) in a given directory
  104. // Doesn't return full pathnames- just filenames within the dir
  105. // Returns number of matches found
  106. int findFiles(const char* rootdir, const char* match, std::vector<std::string>& result);
  107. // Like findFiles, but merely tells us whether at least one match exists
  108. int existFiles(const char* rootdir, const char* match);
  109. // Pass a rootdir and a subdir name from findFiles to create a new rootdir
  110. // The existence of the directories is not checked
  111. void createDirname(const char* rootdir, const char* subdir, std::string& newRootdir);
  112. // Pass a rootdir and a matching file name from findFiles to create a
  113. // A manually entered filename (relative or absolute path) can also be passed
  114. // filename ready to pass to a File object
  115. // The existence of the directories or file is not checked
  116. void createFilename(const char* rootdir, const char* filename, std::string& fullFilename);
  117. // Pass a filename from createFilename or a filename entered by the user to break it up
  118. // into zero or more rootdirs (subdirectories) and a file name; rootdirs are added in
  119. // order to a vector; the last one is going to either be a file name or a rootdir
  120. // The existence of the directories or file is not checked
  121. void splitFilename(const char* filename, std::vector<std::string>& result);
  122. // Pass a path/filename and it removes the file, returning the pathname
  123. void getPathname(const char* filename, std::string& result);
  124. // Our main directory and resource directory
  125. extern std::string* mainDir;
  126. extern std::string* resourceDir;
  127. #endif