b2b0c0cfc62de30577f2e1ead356fab006a3938f
[rocksndiamonds.git] / src / libgame / misc.h
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2006 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * misc.h                                                   *
12 ***********************************************************/
13
14 #ifndef MISC_H
15 #define MISC_H
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <dirent.h>
20
21 #include "system.h"
22
23
24 /* values for InitCounter() and Counter() */
25 #define INIT_COUNTER                    0
26 #define READ_COUNTER                    1
27
28 /* values for InitRND() */
29 #define NEW_RANDOMIZE                   0
30
31 #define RANDOM_ENGINE                   0
32 #define RANDOM_SIMPLE                   1
33
34 #define InitEngineRandom(seed)          init_random_number(RANDOM_ENGINE, seed)
35 #define InitSimpleRandom(seed)          init_random_number(RANDOM_SIMPLE, seed)
36 #define GetEngineRandom(max)            get_random_number(RANDOM_ENGINE, max)
37 #define GetSimpleRandom(max)            get_random_number(RANDOM_SIMPLE, max)
38
39 /* values for Error() */
40 #define ERR_UNKNOWN                     0
41 #define ERR_DEBUG                       (1 << 0)
42 #define ERR_INFO                        (1 << 1)
43 #define ERR_INFO_LINE                   (1 << 2)
44 #define ERR_WARN                        (1 << 3)
45 #define ERR_EXIT                        (1 << 4)
46 #define ERR_HELP                        (1 << 5)
47 #define ERR_SOUND_SERVER                (1 << 6)
48 #define ERR_NETWORK_SERVER              (1 << 7)
49 #define ERR_NETWORK_CLIENT              (1 << 8)
50 #define ERR_FROM_SERVER                 (ERR_SOUND_SERVER | ERR_NETWORK_SERVER)
51 #define ERR_EXIT_HELP                   (ERR_EXIT | ERR_HELP)
52 #define ERR_EXIT_SOUND_SERVER           (ERR_EXIT | ERR_SOUND_SERVER)
53 #define ERR_EXIT_NETWORK_SERVER         (ERR_EXIT | ERR_NETWORK_SERVER)
54 #define ERR_EXIT_NETWORK_CLIENT         (ERR_EXIT | ERR_NETWORK_CLIENT)
55 #if 0
56 #define ERR_ERROR                       (ERR_UNKNOWN)
57 #define ERR_FATAL                       (ERR_EXIT)
58 #define ERR_VERBOSE                     (ERR_INFO)
59 #endif
60
61 /* values for getFile...() and putFile...() */
62 #define BYTE_ORDER_BIG_ENDIAN           0
63 #define BYTE_ORDER_LITTLE_ENDIAN        1
64
65 /* values for cursor bitmap creation */
66 #define BIT_ORDER_MSB                   0
67 #define BIT_ORDER_LSB                   1
68
69 /* values for createDirectory() */
70 #define PERMS_PRIVATE                   0
71 #define PERMS_PUBLIC                    1
72
73 /* values for general file handling stuff */
74 #define MAX_FILENAME_LEN                256
75 #define MAX_LINE_LEN                    1024
76
77 /* values for general username handling stuff */
78 #define MAX_USERNAME_LEN                1024
79
80 #if defined(PLATFORM_ANDROID)
81 /* values for Android asset handling */
82 #define ASSET_TOC_BASENAME              ".toc"
83 #endif
84
85
86 /* structure definitions */
87
88 typedef struct
89 {
90   char *filename;
91   FILE *file;
92   boolean end_of_file;
93
94 #if defined(PLATFORM_ANDROID)
95   boolean file_is_asset;
96   SDL_RWops *asset_file;
97 #endif
98 } File;
99
100 typedef struct
101 {
102   boolean is_directory;
103   char *basename;
104   char *filename;
105 } DirectoryEntry;
106
107 typedef struct
108 {
109   char *filename;
110   DIR *dir;
111   DirectoryEntry *dir_entry;
112
113 #if defined(PLATFORM_ANDROID)
114   boolean directory_is_asset;
115   SDL_RWops *asset_toc_file;
116   char *current_entry;
117 #endif
118 } Directory;
119
120
121 /* function definitions */
122
123 void fprintf_line(FILE *, char *, int);
124 void printf_line(char *, int);
125 void printf_line_with_prefix(char *, char *, int);
126 char *int2str(int, int);
127 char *i_to_a(unsigned int);
128 int log_2(unsigned int);
129
130 boolean getTokenValueFromString(char *, char **, char **);
131
132 void InitCounter(void);
133 unsigned int Counter(void);
134 void Delay(unsigned int);
135 boolean FrameReached(unsigned int *, unsigned int);
136 boolean DelayReached(unsigned int *, unsigned int);
137 void WaitUntilDelayReached(unsigned int *, unsigned int);
138
139 unsigned int init_random_number(int, int);
140 unsigned int get_random_number(int, int);
141
142 char *getLoginName(void);
143 char *getRealName(void);
144
145 time_t getFileTimestampEpochSeconds(char *);
146
147 char *getBasePath(char *);
148 char *getBaseName(char *);
149 char *getBaseNamePtr(char *);
150
151 char *getStringCat2WithSeparator(char *, char *, char *);
152 char *getStringCat3WithSeparator(char *, char *, char *, char *);
153 char *getStringCat2(char *, char *);
154 char *getStringCat3(char *, char *, char *);
155 char *getPath2(char *, char *);
156 char *getPath3(char *, char *, char*);
157 char *getStringCopy(const char *);
158 char *getStringCopyN(const char *, int);
159 char *getStringCopyNStatic(const char *, int);
160 char *getStringToLower(const char *);
161 void setString(char **, char *);
162 boolean strEqual(char *, char *);
163 boolean strEqualN(char *, char *, int);
164 boolean strPrefix(char *, char *);
165 boolean strSuffix(char *, char *);
166 boolean strPrefixLower(char *, char *);
167 boolean strSuffixLower(char *, char *);
168
169 void GetOptions(char **,
170                 void (*print_usage_function)(void),
171                 void (*print_version_function)(void));
172
173 void SetError(char *, ...);
174 char *GetError(void);
175 void Error(int, char *, ...);
176
177 void *checked_malloc(unsigned int);
178 void *checked_calloc(unsigned int);
179 void *checked_realloc(void *, unsigned int);
180 void checked_free(void *);
181 void clear_mem(void *, unsigned int);
182
183 void swap_numbers(int *, int *);
184 void swap_number_pairs(int *, int *, int *, int *);
185
186 #if 1
187
188 int getFile8BitInteger(File *);
189 int putFile8BitInteger(FILE *, int);
190 int getFile16BitInteger(File *, int);
191 int putFile16BitInteger(FILE *, int, int);
192 int getFile32BitInteger(File *, int);
193 int putFile32BitInteger(FILE *, int, int);
194
195 boolean getFileChunk(File *, char *, int *, int);
196 int putFileChunk(FILE *, char *, int, int);
197 int getFileVersion(File *);
198 int putFileVersion(FILE *, int);
199
200 void ReadBytesFromFile(File *, byte *, unsigned int);
201 void WriteBytesToFile(FILE *, byte *, unsigned int);
202
203 void ReadUnusedBytesFromFile(File *, unsigned int);
204 void WriteUnusedBytesToFile(FILE *, unsigned int);
205
206 #else
207
208 int getFile8BitInteger(FILE *);
209 int putFile8BitInteger(FILE *, int);
210 int getFile16BitInteger(FILE *, int);
211 int putFile16BitInteger(FILE *, int, int);
212 int getFile32BitInteger(FILE *, int);
213 int putFile32BitInteger(FILE *, int, int);
214
215 boolean getFileChunk(FILE *, char *, int *, int);
216 int putFileChunk(FILE *, char *, int, int);
217 int getFileVersion(FILE *);
218 int putFileVersion(FILE *, int);
219
220 void ReadBytesFromFile(FILE *, byte *, unsigned int);
221 void WriteBytesToFile(FILE *, byte *, unsigned int);
222
223 void ReadUnusedBytesFromFile(FILE *, unsigned int);
224 void WriteUnusedBytesToFile(FILE *, unsigned int);
225
226 #endif
227
228 #define getFile8Bit(f)        getFile8BitInteger(f)
229 #define putFile8Bit(f,x)      putFile8BitInteger(f,x)
230 #define getFile16BitBE(f)     getFile16BitInteger(f,BYTE_ORDER_BIG_ENDIAN)
231 #define getFile16BitLE(f)     getFile16BitInteger(f,BYTE_ORDER_LITTLE_ENDIAN)
232 #define putFile16BitBE(f,x)   putFile16BitInteger(f,x,BYTE_ORDER_BIG_ENDIAN)
233 #define putFile16BitLE(f,x)   putFile16BitInteger(f,x,BYTE_ORDER_LITTLE_ENDIAN)
234 #define getFile32BitBE(f)     getFile32BitInteger(f,BYTE_ORDER_BIG_ENDIAN)
235 #define getFile32BitLE(f)     getFile32BitInteger(f,BYTE_ORDER_LITTLE_ENDIAN)
236 #define putFile32BitBE(f,x)   putFile32BitInteger(f,x,BYTE_ORDER_BIG_ENDIAN)
237 #define putFile32BitLE(f,x)   putFile32BitInteger(f,x,BYTE_ORDER_LITTLE_ENDIAN)
238
239 #define getFileChunkBE(f,s,x) getFileChunk(f,s,x,BYTE_ORDER_BIG_ENDIAN)
240 #define getFileChunkLE(f,s,x) getFileChunk(f,s,x,BYTE_ORDER_LITTLE_ENDIAN)
241 #define putFileChunkBE(f,s,x) putFileChunk(f,s,x,BYTE_ORDER_BIG_ENDIAN)
242 #define putFileChunkLE(f,s,x) putFileChunk(f,s,x,BYTE_ORDER_LITTLE_ENDIAN)
243
244 char *getKeyNameFromKey(Key);
245 char *getX11KeyNameFromKey(Key);
246 Key getKeyFromKeyName(char *);
247 Key getKeyFromX11KeyName(char *);
248 char getCharFromKey(Key);
249 char getValidConfigValueChar(char);
250
251 int get_integer_from_string(char *);
252 boolean get_boolean_from_string(char *);
253 int get_switch3_from_string(char *);
254
255 ListNode *newListNode(void);
256 void addNodeToList(ListNode **, char *, void *);
257 void deleteNodeFromList(ListNode **, char *, void (*function)(void *));
258 ListNode *getNodeFromKey(ListNode *, char *);
259 int getNumNodes(ListNode *);
260
261 File *openFile(char *, char *);
262 int closeFile(File *);
263 int checkEndOfFile(File *);
264 size_t readFile(File *, void *, size_t, size_t);
265 int seekFile(File *, long, int);
266 int getByteFromFile(File *);
267 char *getStringFromFile(File *, char *, int);
268
269 Directory *openDirectory(char *);
270 int closeDirectory(Directory *);
271 DirectoryEntry *readDirectory(Directory *);
272 void freeDirectoryEntry(DirectoryEntry *);
273
274 boolean directoryExists(char *);
275 boolean fileExists(char *);
276 boolean FileIsGraphic(char *);
277 boolean FileIsSound(char *);
278 boolean FileIsMusic(char *);
279 boolean FileIsArtworkType(char *, int);
280
281 char *get_mapped_token(char *);
282
283 int get_parameter_value(char *, char *, int);
284
285 struct ScreenModeInfo *get_screen_mode_from_string(char *);
286 void get_aspect_ratio_from_screen_mode(struct ScreenModeInfo *, int *x, int *y);
287
288 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *,
289                                            struct ConfigTypeInfo *,
290                                            char **, int);
291 void LoadArtworkConfig(struct ArtworkListInfo *);
292 void ReloadCustomArtworkList(struct ArtworkListInfo *);
293 void FreeCustomArtworkLists(struct ArtworkListInfo *);
294
295 char *getErrorFilename(char *);
296 void openErrorFile();
297 void closeErrorFile();
298 void dumpErrorFile();
299 void NotifyUserAboutErrorFile();
300
301 #if DEBUG
302 void debug_print_timestamp(int, char *);
303 #endif
304 void print_timestamp_init(char *);
305 void print_timestamp_time(char *);
306 void print_timestamp_done(char *);
307
308
309 #endif /* MISC_H */