1 // Crimson Fields -- a game of tactical warfare 2 // Copyright (C) 2000-2007 Jens Granseuer 3 // 4 // This program is free software; you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation; either version 2 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with this program; if not, write to the Free Software 16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 // 18 19 //////////////////////////////////////////////////////////////////////// 20 // fileio.h 21 //////////////////////////////////////////////////////////////////////// 22 23 #ifndef _INCLUDE_FILEIO_H 24 #define _INCLUDE_FILEIO_H 25 26 #if defined WIN32 27 # include <windows.h> 28 #elif HAVE_DIRENT_H 29 # include <dirent.h> 30 #elif HAVE_SYS_NDIR_H 31 # include <sys/ndir.h> 32 #elif HAVE_SYS_DIR_H 33 # include <sys/dir.h> 34 #elif HAVE_NDIR_H 35 # include <ndir.h> 36 #endif 37 38 #include <string> 39 using namespace std; 40 41 #include "SDL.h" 42 #include "SDL_endian.h" 43 44 #ifdef WIN32 45 #define PATHDELIM '\\' 46 #define CRIMSONRC "crimson.ini" 47 #else 48 #define PATHDELIM '/' 49 #define CRIMSONRC "crimsonrc" 50 #endif 51 52 #define CURRENTDIR "." 53 54 class Directory { 55 public: 56 Directory( const char *dir ); 57 ~Directory( void ); 58 IsValid(void)59 bool IsValid( void ) const 60 #ifdef WIN32 61 { return m_Dir != INVALID_HANDLE_VALUE; } 62 #else 63 { return m_Dir != NULL; } 64 #endif 65 66 const char *GetFileName( void ) const; 67 size_t GetFileNameLen() const; 68 bool IsFileHidden( void ) const; 69 bool NextFile( void ); 70 71 private: 72 #ifdef WIN32 73 HANDLE m_Dir; 74 WIN32_FIND_DATA m_Entry; 75 # ifdef UNICODE 76 char m_AsciiDir[MAX_PATH]; 77 # endif 78 #elif HAVE_DIRENT_H 79 DIR *m_Dir; 80 dirent *m_Entry; 81 #else 82 DIR *m_Dir; 83 direct *m_Entry; 84 #endif 85 }; 86 87 // generic buffer access 88 class MemBuffer { 89 public: MemBuffer(void)90 MemBuffer( void ) {} ~MemBuffer(void)91 virtual ~MemBuffer( void ) {} 92 93 virtual int Read( void *buffer, int size ) = 0; 94 virtual unsigned char Read8( void ) = 0; 95 virtual unsigned short Read16( void ) = 0; 96 virtual unsigned long Read32( void ) = 0; 97 string ReadS( int size ); 98 99 virtual int Write( const void *values, int size ) = 0; 100 virtual int Write8( unsigned char value ) = 0; 101 virtual int Write16( unsigned short value ) = 0; 102 virtual int Write32( unsigned long value ) = 0; 103 int WriteS( string value, int len = 0 ); 104 }; 105 106 // file abstraction to encapsulate SDL file access layer 107 class File : public MemBuffer { 108 public: File(const string & path)109 File( const string &path ) : fh(0), name(path) {} ~File(void)110 ~File( void ) { if (fh) Close(); } 111 Read(void * buffer,int size)112 int Read( void *buffer, int size ) 113 { return SDL_RWread(fh, buffer, 1, size); } 114 unsigned char Read8( void ); Read16(void)115 unsigned short Read16( void ) { return SDL_ReadLE16(fh); } Read32(void)116 unsigned long Read32( void ) { return SDL_ReadLE32(fh); } 117 Write(const void * values,int size)118 int Write( const void *values, int size ) 119 { return (SDL_RWwrite(fh, values, size, 1) == 1) ? 0 : -1; } Write8(unsigned char value)120 int Write8( unsigned char value ) 121 { return (SDL_RWwrite(fh, &value, 1, 1) == 1) ? 0 : -1; } Write16(unsigned short value)122 int Write16( unsigned short value ) 123 { return (SDL_WriteLE16(fh, value) == 1) ? 0 : -1; } Write32(unsigned long value)124 int Write32( unsigned long value ) 125 { return (SDL_WriteLE32(fh, value) == 1) ? 0 : -1; } 126 127 bool Open( const char *mode, bool compressed = true ); 128 bool OpenData( const char *mode, const string &subdir = "", bool compressed = true ); 129 void Close( void ); 130 static bool Exists( const string &name ); Name(void)131 const string &Name( void ) const { return name; } 132 133 private: 134 SDL_RWops *fh; 135 string name; 136 }; 137 138 string get_config_dir( void ); 139 string get_home_dir( void ); 140 string get_save_dir( void ); 141 string get_data_dir( void ); 142 string get_data_subdir( const string &sub ); 143 string get_sfx_dir( void ); 144 string get_music_dir( void ); 145 string get_levels_dir( void ); 146 string get_locale_dir( void ); 147 string get_home_levels_dir( void ); 148 void create_config_dir( void ); 149 150 string file_part( const string &path ); 151 void append_path( string &path, const string &sub ); 152 void append_path_delim( string &path ); 153 void make_dir( const char *dir ); 154 155 #endif /* _INCLUDE_FILEIO_H */ 156 157