improved logging functions
[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
54 /* values for getFile...() and putFile...() */
55 #define BYTE_ORDER_BIG_ENDIAN           0
56 #define BYTE_ORDER_LITTLE_ENDIAN        1
57
58 /* values for cursor bitmap creation */
59 #define BIT_ORDER_MSB                   0
60 #define BIT_ORDER_LSB                   1
61
62 /* values for createDirectory() */
63 #define PERMS_PRIVATE                   0
64 #define PERMS_PUBLIC                    1
65
66 /* values for general file handling stuff */
67 #define MAX_FILENAME_LEN                256
68 #define MAX_LINE_LEN                    1024
69
70 /* values for general username handling stuff */
71 #define MAX_USERNAME_LEN                1024
72
73 #if defined(PLATFORM_ANDROID)
74 /* values for Android asset handling */
75 #define ASSET_TOC_BASENAME              ".toc"
76 #endif
77
78
79 /* structure definitions */
80
81 typedef struct
82 {
83   char *filename;
84   FILE *file;
85   boolean end_of_file;
86
87 #if defined(PLATFORM_ANDROID)
88   boolean file_is_asset;
89   SDL_RWops *asset_file;
90 #endif
91 } File;
92
93 typedef struct
94 {
95   boolean is_directory;
96   char *basename;
97   char *filename;
98 } DirectoryEntry;
99
100 typedef struct
101 {
102   char *filename;
103   DIR *dir;
104   DirectoryEntry *dir_entry;
105
106 #if defined(PLATFORM_ANDROID)
107   boolean directory_is_asset;
108   SDL_RWops *asset_toc_file;
109   char *current_entry;
110 #endif
111 } Directory;
112
113
114 /* function definitions */
115
116 void fprintf_line(FILE *, char *, int);
117 void fprintf_line_with_prefix(FILE *, char *, char *, int);
118 void printf_line(char *, int);
119 void printf_line_with_prefix(char *, char *, int);
120
121 char *int2str(int, int);
122 char *i_to_a(unsigned int);
123 int log_2(unsigned int);
124
125 boolean getTokenValueFromString(char *, char **, char **);
126
127 void InitCounter(void);
128 unsigned int Counter(void);
129 void Delay(unsigned int);
130 boolean FrameReached(unsigned int *, unsigned int);
131 boolean DelayReached(unsigned int *, unsigned int);
132 void WaitUntilDelayReached(unsigned int *, unsigned int);
133
134 unsigned int init_random_number(int, int);
135 unsigned int get_random_number(int, int);
136
137 char *getLoginName(void);
138 char *getRealName(void);
139
140 time_t getFileTimestampEpochSeconds(char *);
141
142 char *getBasePath(char *);
143 char *getBaseName(char *);
144 char *getBaseNamePtr(char *);
145
146 char *getStringCat2WithSeparator(char *, char *, char *);
147 char *getStringCat3WithSeparator(char *, char *, char *, char *);
148 char *getStringCat2(char *, char *);
149 char *getStringCat3(char *, char *, char *);
150 char *getPath2(char *, char *);
151 char *getPath3(char *, char *, char*);
152 char *getImg2(char *, char *);
153 char *getImg3(char *, char *, char*);
154 char *getStringCopy(const char *);
155 char *getStringCopyN(const char *, int);
156 char *getStringCopyNStatic(const char *, int);
157 char *getStringToLower(const char *);
158 void setString(char **, char *);
159 boolean strEqual(char *, char *);
160 boolean strEqualN(char *, char *, int);
161 boolean strPrefix(char *, char *);
162 boolean strSuffix(char *, char *);
163 boolean strPrefixLower(char *, char *);
164 boolean strSuffixLower(char *, char *);
165
166 void GetOptions(char **,
167                 void (*print_usage_function)(void),
168                 void (*print_version_function)(void));
169
170 void SetError(char *, ...);
171 char *GetError(void);
172 void Error(int, char *, ...);
173
174 void *checked_malloc(unsigned int);
175 void *checked_calloc(unsigned int);
176 void *checked_realloc(void *, unsigned int);
177 void checked_free(void *);
178 void clear_mem(void *, unsigned int);
179
180 void swap_numbers(int *, int *);
181 void swap_number_pairs(int *, int *, int *, int *);
182
183 int getFile8BitInteger(File *);
184 int putFile8BitInteger(FILE *, int);
185 int getFile16BitInteger(File *, int);
186 int putFile16BitInteger(FILE *, int, int);
187 int getFile32BitInteger(File *, int);
188 int putFile32BitInteger(FILE *, int, int);
189
190 boolean getFileChunk(File *, char *, int *, int);
191 int putFileChunk(FILE *, char *, int, int);
192 int getFileVersion(File *);
193 int putFileVersion(FILE *, int);
194
195 void ReadBytesFromFile(File *, byte *, unsigned int);
196 void WriteBytesToFile(FILE *, byte *, unsigned int);
197
198 void ReadUnusedBytesFromFile(File *, unsigned int);
199 void WriteUnusedBytesToFile(FILE *, unsigned int);
200
201 #define getFile8Bit(f)        getFile8BitInteger(f)
202 #define putFile8Bit(f,x)      putFile8BitInteger(f,x)
203 #define getFile16BitBE(f)     getFile16BitInteger(f,BYTE_ORDER_BIG_ENDIAN)
204 #define getFile16BitLE(f)     getFile16BitInteger(f,BYTE_ORDER_LITTLE_ENDIAN)
205 #define putFile16BitBE(f,x)   putFile16BitInteger(f,x,BYTE_ORDER_BIG_ENDIAN)
206 #define putFile16BitLE(f,x)   putFile16BitInteger(f,x,BYTE_ORDER_LITTLE_ENDIAN)
207 #define getFile32BitBE(f)     getFile32BitInteger(f,BYTE_ORDER_BIG_ENDIAN)
208 #define getFile32BitLE(f)     getFile32BitInteger(f,BYTE_ORDER_LITTLE_ENDIAN)
209 #define putFile32BitBE(f,x)   putFile32BitInteger(f,x,BYTE_ORDER_BIG_ENDIAN)
210 #define putFile32BitLE(f,x)   putFile32BitInteger(f,x,BYTE_ORDER_LITTLE_ENDIAN)
211
212 #define getFileChunkBE(f,s,x) getFileChunk(f,s,x,BYTE_ORDER_BIG_ENDIAN)
213 #define getFileChunkLE(f,s,x) getFileChunk(f,s,x,BYTE_ORDER_LITTLE_ENDIAN)
214 #define putFileChunkBE(f,s,x) putFileChunk(f,s,x,BYTE_ORDER_BIG_ENDIAN)
215 #define putFileChunkLE(f,s,x) putFileChunk(f,s,x,BYTE_ORDER_LITTLE_ENDIAN)
216
217 char *getKeyNameFromKey(Key);
218 char *getX11KeyNameFromKey(Key);
219 Key getKeyFromKeyName(char *);
220 Key getKeyFromX11KeyName(char *);
221 char getCharFromKey(Key);
222 char getValidConfigValueChar(char);
223
224 int get_integer_from_string(char *);
225 boolean get_boolean_from_string(char *);
226 int get_switch3_from_string(char *);
227
228 ListNode *newListNode(void);
229 void addNodeToList(ListNode **, char *, void *);
230 void deleteNodeFromList(ListNode **, char *, void (*function)(void *));
231 ListNode *getNodeFromKey(ListNode *, char *);
232 int getNumNodes(ListNode *);
233
234 File *openFile(char *, char *);
235 int closeFile(File *);
236 int checkEndOfFile(File *);
237 size_t readFile(File *, void *, size_t, size_t);
238 int seekFile(File *, long, int);
239 int getByteFromFile(File *);
240 char *getStringFromFile(File *, char *, int);
241
242 Directory *openDirectory(char *);
243 int closeDirectory(Directory *);
244 DirectoryEntry *readDirectory(Directory *);
245 void freeDirectoryEntry(DirectoryEntry *);
246
247 boolean directoryExists(char *);
248 boolean fileExists(char *);
249 boolean FileIsGraphic(char *);
250 boolean FileIsSound(char *);
251 boolean FileIsMusic(char *);
252 boolean FileIsArtworkType(char *, int);
253
254 char *get_mapped_token(char *);
255
256 int get_parameter_value(char *, char *, int);
257
258 struct ScreenModeInfo *get_screen_mode_from_string(char *);
259 void get_aspect_ratio_from_screen_mode(struct ScreenModeInfo *, int *x, int *y);
260
261 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *,
262                                            struct ConfigTypeInfo *,
263                                            char **, int);
264 void LoadArtworkConfig(struct ArtworkListInfo *);
265 void ReloadCustomArtworkList(struct ArtworkListInfo *);
266 void FreeCustomArtworkLists(struct ArtworkListInfo *);
267
268 char *getErrorFilename(char *);
269 void openErrorFile();
270 void closeErrorFile();
271 void dumpErrorFile();
272 void NotifyUserAboutErrorFile();
273
274 #if DEBUG
275 void debug_print_timestamp(int, char *);
276 #endif
277 void print_timestamp_init(char *);
278 void print_timestamp_time(char *);
279 void print_timestamp_done(char *);
280
281
282 #endif /* MISC_H */