| 1 | /* |
| 2 | * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | gameinfo.h - extract and parse tads 2/3 game file information |
| 10 | Function |
| 11 | Searches a compiled tads 2 game file or a compiled tads 3 image file |
| 12 | for a game information resource. If the resource is found, extracts and |
| 13 | parses the resource. |
| 14 | |
| 15 | A game information resource is a set of name/value pairs giving |
| 16 | information on the game, such as its name, author, and description. Each |
| 17 | value is simply a text string. The game information is supplied by the |
| 18 | author, and is intended to help archives and users by providing a way to |
| 19 | obtain specific information about the game in a structured format directly |
| 20 | from the game file itself. |
| 21 | |
| 22 | This implementation is independent of any tads 2 or tads 3 subsystem |
| 23 | except osifc, which it uses for portable file I/O and portable byte format |
| 24 | conversions. We also depend upon the 'resfind' module, which extracts |
| 25 | multimedia resources from tads game files. |
| 26 | Notes |
| 27 | |
| 28 | Modified |
| 29 | 09/24/01 MJRoberts - Creation |
| 30 | */ |
| 31 | |
| 32 | #ifndef GAMEINFO_H |
| 33 | #define GAMEINFO_H |
| 34 | |
| 35 | #include "os.h" |
| 36 | |
| 37 | /* |
| 38 | * Base game information parser. This base version, when used directly, |
| 39 | * uses UTF-8 encoding for all value strings. If values are desired in a |
| 40 | * different character set, use the CTadsGameInfoLocal subclass. |
| 41 | */ |
| 42 | class CTadsGameInfo |
| 43 | { |
| 44 | public: |
| 45 | /* create */ |
| 46 | CTadsGameInfo(); |
| 47 | |
| 48 | /* delete the object */ |
| 49 | virtual ~CTadsGameInfo(); |
| 50 | |
| 51 | /* |
| 52 | * Parse a game file and extract the game information. Returns FALSE |
| 53 | * if there is no game information in the file, the file is not a |
| 54 | * valid game file, or any other error occurs. Returns TRUE on |
| 55 | * success. |
| 56 | */ |
| 57 | int read_from_file(const char *fname); |
| 58 | |
| 59 | /* |
| 60 | * Parse a file that's already open and positioned to the start of the |
| 61 | * game file data. This can be used to parse a game file data stream |
| 62 | * that is embedded in a larger file. |
| 63 | */ |
| 64 | int read_from_fp(osfildef *fp); |
| 65 | |
| 66 | /* |
| 67 | * Find a value given the value name. Returns a pointer to a |
| 68 | * null-terminated value string, which uses the character set |
| 69 | * appropriate to the game information reader; this is UTF-8 for this |
| 70 | * base class. (The pointer returned refers to memory we manage, so |
| 71 | * the pointer is valid as long as 'this' is valid - i.e., as long as |
| 72 | * 'this' isn't deleted.) Returns null if there is no value with the |
| 73 | * given name. |
| 74 | * |
| 75 | * The name is given as an ASCII string and is insensitive to case. |
| 76 | */ |
| 77 | const char *get_val(const char *name) const; |
| 78 | |
| 79 | /* |
| 80 | * Enumerate all of the named values. Invokes the callback interface |
| 81 | * (cb->tads_enum_game_info()) for each value. |
| 82 | */ |
| 83 | void enum_values(class CTadsGameInfo_enum *cb); |
| 84 | |
| 85 | protected: |
| 86 | /* |
| 87 | * Store a value string. The value is given as UTF-8; the result |
| 88 | * should be in the appropriate character set for the subclass. This |
| 89 | * is virtualized to allow subclasses to translate the value's |
| 90 | * character set before storing the value. |
| 91 | * |
| 92 | * The given value string is null-terminated, and is in memory managed |
| 93 | * by the class and retained as long as the class is valid. |
| 94 | * Implementations can thus return the given value string unchanged, |
| 95 | * if desired. |
| 96 | */ |
| 97 | virtual const char *store_value(const char *val, size_t len) |
| 98 | { |
| 99 | /* |
| 100 | * The base version can simply return a reference to the original |
| 101 | * string, since we want to store the values in UTF-8 as given. |
| 102 | */ |
| 103 | return val; |
| 104 | } |
| 105 | |
| 106 | /* free a value string previously stored with store_value() */ |
| 107 | virtual void free_value(const char *) |
| 108 | { |
| 109 | /* |
| 110 | * The base version doesn't allocate the value strings separately |
| 111 | * (it just uses the original pool of strings), so we don't have |
| 112 | * to free the strings separately. We thus simply do nothing. |
| 113 | */ |
| 114 | } |
| 115 | |
| 116 | /* free the value list */ |
| 117 | void free_value_list(); |
| 118 | |
| 119 | /* parse the given file data; returns true on success, false on error */ |
| 120 | int parse_file(osfildef *fp, unsigned long res_seek_pos, |
| 121 | unsigned long res_size); |
| 122 | |
| 123 | /* buffer containing the contents of the game information */ |
| 124 | char *buf_; |
| 125 | |
| 126 | /* our list of name/value pairs */ |
| 127 | struct tads_valinfo *first_val_; |
| 128 | }; |
| 129 | |
| 130 | /* |
| 131 | * Game information extensions for the local character set. This subclass |
| 132 | * has some extra functions that allow retrieving the game information in |
| 133 | * the local character set, rather than in utf-8. By default, we use the |
| 134 | * local display character set as specified by os_get_charmap(), but the |
| 135 | * caller can specify a character set explicitly by name, or by giving us |
| 136 | * a character mapper object. |
| 137 | */ |
| 138 | class CTadsGameInfoLocal: public CTadsGameInfo |
| 139 | { |
| 140 | public: |
| 141 | /* initialize with the default character mapper */ |
| 142 | CTadsGameInfoLocal(const char *argv0); |
| 143 | |
| 144 | /* initialize with the named character mapper */ |
| 145 | CTadsGameInfoLocal(const char *argv0, const char *charset_name); |
| 146 | |
| 147 | /* initialize with the given character mapper */ |
| 148 | CTadsGameInfoLocal(class CCharmapToLocal *charmap); |
| 149 | |
| 150 | /* delete */ |
| 151 | ~CTadsGameInfoLocal(); |
| 152 | |
| 153 | protected: |
| 154 | /* store a value in the local character set */ |
| 155 | virtual const char *store_value(const char *val, size_t len); |
| 156 | |
| 157 | /* free a value string previously stored with store_value() */ |
| 158 | virtual void free_value(const char *); |
| 159 | |
| 160 | /* initialize from a named character mapping */ |
| 161 | void init(const char *argv0, const char *charset_name); |
| 162 | |
| 163 | /* |
| 164 | * our unicode-to-local character mapper, for the local character set |
| 165 | * methods; this is null if we haven't had a need for it yet |
| 166 | */ |
| 167 | class CCharmapToLocal *local_mapper_; |
| 168 | }; |
| 169 | |
| 170 | |
| 171 | |
| 172 | /* |
| 173 | * Enumerator interface. This interface must be implemented in order to |
| 174 | * call CTadsGameInfo::enum_values(). |
| 175 | */ |
| 176 | class CTadsGameInfo_enum |
| 177 | { |
| 178 | public: |
| 179 | /* |
| 180 | * Receive one name/value pair. 'name' points to a null-terminated |
| 181 | * ASCII string giving the value's name, and 'val' points to a |
| 182 | * null-terminated string giving the value text. The value string |
| 183 | * will be encoded in the character set which the GameInfo reader is |
| 184 | * using. |
| 185 | * |
| 186 | * The name and value pointers refer to memory managed as part of the |
| 187 | * CTadsGameInfo object doing the enumerating. The memory to which |
| 188 | * these pointers refer is valid as long as the CTadsGameInfo object |
| 189 | * is valid (i.e., until it's deleted). |
| 190 | */ |
| 191 | virtual void tads_enum_game_info(const char *name, const char *val) = 0; |
| 192 | }; |
| 193 | |
| 194 | #endif /* GAMEINFO_H */ |