| 1 | |
| 2 | /** |
| 3 | * |
| 4 | * @file file.h |
| 5 | * |
| 6 | * Part of the OpenJazz project |
| 7 | * |
| 8 | * @par History: |
| 9 | * - 23rd August 2005: Created OpenJazz.h |
| 10 | * - 3rd February 2009: Created file.h from parts of OpenJazz.h |
| 11 | * |
| 12 | * @par Licence: |
| 13 | * Copyright (c) 2005-2010 AJ Thomson |
| 14 | * Copyright (c) 2015-2026 Carsten Teibes |
| 15 | * |
| 16 | * OpenJazz is distributed under the terms of |
| 17 | * the GNU General Public License, version 2.0 |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | |
| 22 | #ifndef _FILE_H |
| 23 | #define _FILE_H |
| 24 | |
| 25 | |
| 26 | #include "OpenJazz.h" |
| 27 | #include "io/gfx/video.h" |
| 28 | |
| 29 | #include <stdio.h> |
| 30 | #include <memory> |
| 31 | |
| 32 | // Classes |
| 33 | |
| 34 | struct SDL_Surface; |
| 35 | struct SDL_Color; |
| 36 | |
| 37 | /// File i/o |
| 38 | class File { |
| 39 | |
| 40 | private: |
| 41 | FILE* file; |
| 42 | char* filePath; |
| 43 | bool forWriting; |
| 44 | |
| 45 | bool open (const char* path, const char* name, bool write); |
| 46 | |
| 47 | public: |
| 48 | File (const char* name, int pathType, bool write = false); |
| 49 | ~File (); |
| 50 | |
| 51 | int getSize (); |
| 52 | void seek (int offset, bool reset = false); |
| 53 | int tell (); |
| 54 | unsigned char loadChar (); |
| 55 | void storeChar (unsigned char val); |
| 56 | unsigned short int loadShort (); |
| 57 | unsigned short int loadShort (unsigned short int max); |
| 58 | void storeShort (unsigned short int val); |
| 59 | signed int loadInt (); |
| 60 | void storeInt (signed int val); |
| 61 | void storeData (void* data, int length); |
| 62 | unsigned char* loadBlock (int length); |
| 63 | unsigned char* loadRLE (int length, bool checkSize = true); |
| 64 | void skipRLE (); |
| 65 | unsigned char* loadLZ (int compressedLength, int length); |
| 66 | char* loadTerminatedString (int maxSize = 0); |
| 67 | char* loadFileName(); |
| 68 | char* loadString (int length); |
| 69 | SDL_Surface* loadSurface (int width, int height, bool checkSize = true); |
| 70 | unsigned char* loadPixels (int length); |
| 71 | unsigned char* loadPixels (int length, int key); |
| 72 | void loadPalette (SDL_Color* palette, bool rle = true); |
| 73 | |
| 74 | }; |
| 75 | |
| 76 | using FilePtr = std::unique_ptr<File>; |
| 77 | |
| 78 | |
| 79 | /// Directory path |
| 80 | |
| 81 | enum path_type { |
| 82 | PATH_TYPE_INVALID = 0, ///< Invalid directory, do not use |
| 83 | PATH_TYPE_SYSTEM = 1 << 0, ///< Read-only system directory |
| 84 | PATH_TYPE_CONFIG = 1 << 1, ///< User writable configuration directory |
| 85 | PATH_TYPE_GAME = 1 << 2, ///< Directory containing game data |
| 86 | PATH_TYPE_TEMP = 1 << 3, ///< User writable temporary directory |
| 87 | PATH_TYPE_ANY = 1 << 4 ///< Special case: any type |
| 88 | }; |
| 89 | |
| 90 | class Path { |
| 91 | |
| 92 | public: |
| 93 | Path* next; ///< Next path to check |
| 94 | char* path; ///< Path |
| 95 | int pathType; ///< One or more of path_type enum |
| 96 | |
| 97 | Path (Path* newNext, char* newPath, int newPathType); |
| 98 | ~Path (); |
| 99 | |
| 100 | }; |
| 101 | |
| 102 | |
| 103 | class PathMgr { |
| 104 | |
| 105 | public: |
| 106 | PathMgr(); |
| 107 | ~PathMgr(); |
| 108 | |
| 109 | bool add(char* newPath, int newPathType = PATH_TYPE_ANY); |
| 110 | |
| 111 | Path* paths; |
| 112 | |
| 113 | bool has_config; |
| 114 | bool has_temp; |
| 115 | |
| 116 | }; |
| 117 | |
| 118 | |
| 119 | // Directory Seperator |
| 120 | |
| 121 | #ifdef _WIN32 |
| 122 | #define OJ_DIR_SEP '\\' |
| 123 | #define OJ_DIR_SEP_STR "\\" |
| 124 | #else |
| 125 | #define OJ_DIR_SEP '/' |
| 126 | #define OJ_DIR_SEP_STR "/" |
| 127 | #endif |
| 128 | |
| 129 | |
| 130 | // Variable |
| 131 | |
| 132 | EXTERN PathMgr gamePaths; ///< Paths to files |
| 133 | |
| 134 | #endif |
| 135 |