rnd-20131211-1-src
[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_INFO                        0
41 #define ERR_INFO_LINE                   (1 << 0)
42 #define ERR_WARN                        (1 << 1)
43 #define ERR_EXIT                        (1 << 2)
44 #define ERR_HELP                        (1 << 3)
45 #define ERR_SOUND_SERVER                (1 << 4)
46 #define ERR_NETWORK_SERVER              (1 << 5)
47 #define ERR_NETWORK_CLIENT              (1 << 6)
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 printf_line(char *, int);
118 void printf_line_with_prefix(char *, char *, int);
119 char *int2str(int, int);
120 char *i_to_a(unsigned int);
121 int log_2(unsigned int);
122
123 boolean getTokenValueFromString(char *, char **, char **);
124
125 void InitCounter(void);
126 unsigned int Counter(void);
127 void Delay(unsigned int);
128 boolean FrameReached(unsigned int *, unsigned int);
129 boolean DelayReached(unsigned int *, unsigned int);
130 void WaitUntilDelayReached(unsigned int *, unsigned int);
131
132 unsigned int init_random_number(int, int);
133 unsigned int get_random_number(int, int);
134
135 char *getLoginName(void);
136 char *getRealName(void);
137
138 time_t getFileTimestampEpochSeconds(char *);
139
140 char *getBasePath(char *);
141 char *getBaseName(char *);
142 char *getBaseNamePtr(char *);
143
144 char *getStringCat2WithSeparator(char *, char *, char *);
145 char *getStringCat3WithSeparator(char *, char *, char *, char *);
146 char *getStringCat2(char *, char *);
147 char *getStringCat3(char *, char *, char *);
148 char *getPath2(char *, char *);
149 char *getPath3(char *, char *, char*);
150 char *getStringCopy(char *);
151 char *getStringCopyN(char *, int);
152 char *getStringToLower(char *);
153 void setString(char **, char *);
154 boolean strEqual(char *, char *);
155 boolean strEqualN(char *, char *, int);
156 boolean strPrefix(char *, char *);
157 boolean strSuffix(char *, char *);
158 boolean strPrefixLower(char *, char *);
159 boolean strSuffixLower(char *, char *);
160
161 void GetOptions(char **, void (*print_usage_function)(void));
162
163 void SetError(char *, ...);
164 char *GetError(void);
165 void Error(int, char *, ...);
166
167 void *checked_malloc(unsigned int);
168 void *checked_calloc(unsigned int);
169 void *checked_realloc(void *, unsigned int);
170 void checked_free(void *);
171 void clear_mem(void *, unsigned int);
172
173 void swap_numbers(int *, int *);
174 void swap_number_pairs(int *, int *, int *, int *);
175
176 int getFile8BitInteger(FILE *);
177 int putFile8BitInteger(FILE *, int);
178 int getFile16BitInteger(FILE *, int);
179 int putFile16BitInteger(FILE *, int, int);
180 int getFile32BitInteger(FILE *, int);
181 int putFile32BitInteger(FILE *, int, int);
182
183 boolean getFileChunk(FILE *, char *, int *, int);
184 int putFileChunk(FILE *, char *, int, int);
185 int getFileVersion(FILE *);
186 int putFileVersion(FILE *, int);
187
188 void ReadBytesFromFile(FILE *, byte *, unsigned int);
189 void WriteBytesToFile(FILE *, byte *, unsigned int);
190
191 void ReadUnusedBytesFromFile(FILE *, unsigned int);
192 void WriteUnusedBytesToFile(FILE *, unsigned int);
193
194 #define getFile8Bit(f)        getFile8BitInteger(f)
195 #define putFile8Bit(f,x)      putFile8BitInteger(f,x)
196 #define getFile16BitBE(f)     getFile16BitInteger(f,BYTE_ORDER_BIG_ENDIAN)
197 #define getFile16BitLE(f)     getFile16BitInteger(f,BYTE_ORDER_LITTLE_ENDIAN)
198 #define putFile16BitBE(f,x)   putFile16BitInteger(f,x,BYTE_ORDER_BIG_ENDIAN)
199 #define putFile16BitLE(f,x)   putFile16BitInteger(f,x,BYTE_ORDER_LITTLE_ENDIAN)
200 #define getFile32BitBE(f)     getFile32BitInteger(f,BYTE_ORDER_BIG_ENDIAN)
201 #define getFile32BitLE(f)     getFile32BitInteger(f,BYTE_ORDER_LITTLE_ENDIAN)
202 #define putFile32BitBE(f,x)   putFile32BitInteger(f,x,BYTE_ORDER_BIG_ENDIAN)
203 #define putFile32BitLE(f,x)   putFile32BitInteger(f,x,BYTE_ORDER_LITTLE_ENDIAN)
204
205 #define getFileChunkBE(f,s,x) getFileChunk(f,s,x,BYTE_ORDER_BIG_ENDIAN)
206 #define getFileChunkLE(f,s,x) getFileChunk(f,s,x,BYTE_ORDER_LITTLE_ENDIAN)
207 #define putFileChunkBE(f,s,x) putFileChunk(f,s,x,BYTE_ORDER_BIG_ENDIAN)
208 #define putFileChunkLE(f,s,x) putFileChunk(f,s,x,BYTE_ORDER_LITTLE_ENDIAN)
209
210 char *getKeyNameFromKey(Key);
211 char *getX11KeyNameFromKey(Key);
212 Key getKeyFromKeyName(char *);
213 Key getKeyFromX11KeyName(char *);
214 char getCharFromKey(Key);
215 char getValidConfigValueChar(char);
216
217 int get_integer_from_string(char *);
218 boolean get_boolean_from_string(char *);
219 int get_switch3_from_string(char *);
220
221 ListNode *newListNode(void);
222 void addNodeToList(ListNode **, char *, void *);
223 void deleteNodeFromList(ListNode **, char *, void (*function)(void *));
224 ListNode *getNodeFromKey(ListNode *, char *);
225 int getNumNodes(ListNode *);
226
227 File *openFile(char *, char *);
228 int closeFile(File *);
229 int checkEndOfFile(File *);
230 char *getStringFromFile(File *, char *, int);
231
232 Directory *openDirectory(char *);
233 int closeDirectory(Directory *);
234 DirectoryEntry *readDirectory(Directory *);
235 void freeDirectoryEntry(DirectoryEntry *);
236
237 boolean fileExists(char *);
238 boolean FileIsGraphic(char *);
239 boolean FileIsSound(char *);
240 boolean FileIsMusic(char *);
241 boolean FileIsArtworkType(char *, int);
242
243 char *get_mapped_token(char *);
244
245 int get_parameter_value(char *, char *, int);
246
247 struct ScreenModeInfo *get_screen_mode_from_string(char *);
248 void get_aspect_ratio_from_screen_mode(struct ScreenModeInfo *, int *x, int *y);
249
250 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *,
251                                            struct ConfigTypeInfo *,
252                                            char **, int);
253 void LoadArtworkConfig(struct ArtworkListInfo *);
254 void ReloadCustomArtworkList(struct ArtworkListInfo *);
255 void FreeCustomArtworkLists(struct ArtworkListInfo *);
256
257 char *getErrorFilename(char *);
258 void openErrorFile();
259 void closeErrorFile();
260 void dumpErrorFile();
261 void NotifyUserAboutErrorFile();
262
263 #if DEBUG
264 void debug_print_timestamp(int, char *);
265 void print_timestamp_init(char *);
266 void print_timestamp_time(char *);
267 void print_timestamp_done(char *);
268 #endif  /* DEBUG */
269
270
271
272 #endif /* MISC_H */