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