renamed 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 //                  https://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 #define RANDOM_BETTER                   2
32
33 #define InitEngineRandom(seed)          init_random_number(RANDOM_ENGINE, seed)
34 #define InitSimpleRandom(seed)          init_random_number(RANDOM_SIMPLE, seed)
35 #define InitBetterRandom(seed)          init_random_number(RANDOM_BETTER, seed)
36 #define GetEngineRandom(max)            get_random_number(RANDOM_ENGINE, max)
37 #define GetSimpleRandom(max)            get_random_number(RANDOM_SIMPLE, max)
38 #define GetBetterRandom(max)            get_random_number(RANDOM_BETTER, max)
39
40 // values for getFile...() and putFile...()
41 #define BYTE_ORDER_BIG_ENDIAN           0
42 #define BYTE_ORDER_LITTLE_ENDIAN        1
43
44 // values for cursor bitmap creation
45 #define BIT_ORDER_MSB                   0
46 #define BIT_ORDER_LSB                   1
47
48 // values for character encoding
49 #define TEXT_ENCODING_UNKNOWN           0
50 #define TEXT_ENCODING_ASCII             1
51 #define TEXT_ENCODING_UTF_8             2
52
53 // values for createDirectory()
54 #define PERMS_PRIVATE                   0
55 #define PERMS_PUBLIC                    1
56
57 // values for general file handling stuff
58 #define MAX_FILENAME_LEN                256
59 #define MAX_LINE_LEN                    1024
60
61 // values for general username handling stuff
62 #define MAX_USERNAME_LEN                1024
63
64 #if defined(PLATFORM_ANDROID)
65 // values for Android asset handling
66 #define ASSET_TOC_BASENAME              ".toc"
67 #endif
68
69
70 // structure definitions
71
72 typedef struct
73 {
74   char *filename;
75   FILE *file;
76   boolean end_of_file;
77
78 #if defined(PLATFORM_ANDROID)
79   boolean file_is_asset;
80   SDL_RWops *asset_file;
81 #endif
82 } File;
83
84 typedef struct
85 {
86   boolean is_directory;
87   char *basename;
88   char *filename;
89 } DirectoryEntry;
90
91 typedef struct
92 {
93   char *filename;
94   DIR *dir;
95   DirectoryEntry *dir_entry;
96
97 #if defined(PLATFORM_ANDROID)
98   boolean directory_is_asset;
99   SDL_RWops *asset_toc_file;
100   char *current_entry;
101 #endif
102 } Directory;
103
104
105 // function definitions
106
107 void fprintf_line(FILE *, char *, int);
108 void fprintf_line_with_prefix(FILE *, char *, char *, int);
109 void printf_line(char *, int);
110 void printf_line_with_prefix(char *, char *, int);
111
112 void Print(char *, ...);
113 void PrintNoLog(char *, ...);
114 void PrintLine(char *, int);
115 void PrintLineWithPrefix(char *, char *, int);
116
117 void DebugContinued(char *, char *, ...);
118 void Debug(char *, char *, ...);
119 void Info(char *, ...);
120 void Warn(char *, ...);
121 void Error(char *, ...);
122 void Fail(char *, ...);
123 void FailWithHelp(char *, ...);
124
125 char *int2str(int, int);
126 char *i_to_a(unsigned int);
127 int log_2(unsigned int);
128
129 boolean getTokenValueFromString(char *, char **, char **);
130
131 char *getUUIDExt(unsigned int (*function)(int));
132 char *getUUID(void);
133
134 void InitCounter(void);
135 unsigned int Counter(void);
136 void Delay(unsigned int);
137 boolean DelayReachedExt2(unsigned int *, unsigned int, unsigned int);
138 boolean DelayReachedExt(DelayCounter *, unsigned int);
139 boolean FrameReached(DelayCounter *);
140 boolean DelayReached(DelayCounter *);
141 void ResetDelayCounterExt(DelayCounter *, unsigned int);
142 void ResetFrameCounter(DelayCounter *);
143 void ResetDelayCounter(DelayCounter *);
144 int WaitUntilDelayReached(DelayCounter *);
145 void SkipUntilDelayReached(DelayCounter *, int *, int);
146
147 unsigned int init_random_number(int, int);
148 unsigned int get_random_number(int, int);
149
150 #if defined(PLATFORM_UNIX)
151 char *getUnixLoginName(void);
152 char *getUnixRealName(void);
153 char *getUnixHomeDir(void);
154 #endif
155
156 char *getLoginName(void);
157 char *getRealName(void);
158 char *getFixedUserName(char *);
159 char *getDefaultUserName(int);
160
161 char *getTimestampFromEpoch(time_t);
162 char *getCurrentTimestamp(void);
163
164 time_t getFileTimestampEpochSeconds(char *);
165
166 char *getBasePath(char *);
167 char *getBaseName(char *);
168 char *getBaseNamePtr(char *);
169 char *getBaseNameNoSuffix(char *);
170
171 char *getStringCat2WithSeparator(const char *, const char *, const char *);
172 char *getStringCat3WithSeparator(const char *, const char *, const char *, const char *);
173 char *getStringCat2(const char *, const char *);
174 char *getStringCat3(const char *, const char *, const char *);
175 char *getPath2(const char *, const char *);
176 char *getPath3(const char *, const char *, const char *);
177 char *getImg2(const char *, const char *);
178 char *getImg3(const char *, const char *, const char *);
179 char *getStringCopy(const char *);
180 char *getStringCopyN(const char *, int);
181 char *getStringCopyNStatic(const char *, int);
182 char *getStringToLower(const char *);
183 void setString(char **, const char *);
184 char **getSplitStringArray(const char *s, const char *, int);
185 int getStringArrayLength(char **);
186 void freeStringArray(char **);
187 char *getUnescapedString(const char *);
188 boolean strEqual(const char *, const char *);
189 boolean strEqualN(const char *, const char *, int);
190 boolean strEqualCase(const char *, const char *);
191 boolean strEqualCaseN(const char *, const char *, int);
192 boolean strPrefix(const char *, const char *);
193 boolean strSuffix(const char *, const char *);
194 boolean strPrefixLower(const char *, const char *);
195 boolean strSuffixLower(const char *, const char *);
196 boolean isURL(const char *);
197
198 void GetOptions(int, char **,
199                 void (*print_usage_function)(void),
200                 void (*print_version_function)(void));
201
202 void *checked_malloc(unsigned int);
203 void *checked_calloc(unsigned int);
204 void *checked_realloc(void *, unsigned int);
205 void checked_free(void *);
206 void clear_mem(void *, unsigned int);
207
208 void swap_numbers(int *, int *);
209 void swap_number_pairs(int *, int *, int *, int *);
210 int get_number_of_bits(int);
211
212 int getFile8BitInteger(File *);
213 int putFile8BitInteger(FILE *, int);
214 int getFile16BitInteger(File *, int);
215 int putFile16BitInteger(FILE *, int, int);
216 int getFile32BitInteger(File *, int);
217 int putFile32BitInteger(FILE *, int, int);
218
219 boolean getFileChunk(File *, char *, int *, int);
220 int putFileChunk(FILE *, char *, int, int);
221 int getFileVersion(File *);
222 int putFileVersion(FILE *, int);
223
224 void ReadBytesFromFile(File *, byte *, unsigned int);
225 void WriteBytesToFile(FILE *, byte *, unsigned int);
226
227 void ReadUnusedBytesFromFile(File *, unsigned int);
228 void WriteUnusedBytesToFile(FILE *, unsigned int);
229
230 #define getFile8Bit(f)        getFile8BitInteger(f)
231 #define putFile8Bit(f,x)      putFile8BitInteger(f,x)
232 #define getFile16BitBE(f)     getFile16BitInteger(f,BYTE_ORDER_BIG_ENDIAN)
233 #define getFile16BitLE(f)     getFile16BitInteger(f,BYTE_ORDER_LITTLE_ENDIAN)
234 #define putFile16BitBE(f,x)   putFile16BitInteger(f,x,BYTE_ORDER_BIG_ENDIAN)
235 #define putFile16BitLE(f,x)   putFile16BitInteger(f,x,BYTE_ORDER_LITTLE_ENDIAN)
236 #define getFile32BitBE(f)     getFile32BitInteger(f,BYTE_ORDER_BIG_ENDIAN)
237 #define getFile32BitLE(f)     getFile32BitInteger(f,BYTE_ORDER_LITTLE_ENDIAN)
238 #define putFile32BitBE(f,x)   putFile32BitInteger(f,x,BYTE_ORDER_BIG_ENDIAN)
239 #define putFile32BitLE(f,x)   putFile32BitInteger(f,x,BYTE_ORDER_LITTLE_ENDIAN)
240
241 #define getFileChunkBE(f,s,x) getFileChunk(f,s,x,BYTE_ORDER_BIG_ENDIAN)
242 #define getFileChunkLE(f,s,x) getFileChunk(f,s,x,BYTE_ORDER_LITTLE_ENDIAN)
243 #define putFileChunkBE(f,s,x) putFileChunk(f,s,x,BYTE_ORDER_BIG_ENDIAN)
244 #define putFileChunkLE(f,s,x) putFileChunk(f,s,x,BYTE_ORDER_LITTLE_ENDIAN)
245
246 char *getUTF8FromLatin1(char *);
247 char *getLatin1FromUTF8(char *);
248 int getTextEncoding(char *);
249
250 char *getEscapedJSON(char *);
251
252 char *getKeyNameFromKey(Key);
253 char *getX11KeyNameFromKey(Key);
254 Key getKeyFromKeyName(char *);
255 Key getKeyFromX11KeyName(char *);
256 char getCharFromKey(Key);
257 char getValidConfigValueChar(char);
258
259 int get_integer_from_string(char *);
260 boolean get_boolean_from_string(char *);
261 int get_switch3_from_string(char *);
262 int get_player_nr_from_string(char *);
263
264 ListNode *newListNode(void);
265 void addNodeToList(ListNode **, char *, void *);
266 void deleteNodeFromList(ListNode **, char *, void (*function)(void *));
267 ListNode *getNodeFromKey(ListNode *, char *);
268 int getNumNodes(ListNode *);
269
270 File *openFile(char *, char *);
271 int closeFile(File *);
272 int checkEndOfFile(File *);
273 size_t readFile(File *, void *, size_t, size_t);
274 size_t writeFile(File *, void *, size_t, size_t);
275 int seekFile(File *, long, int);
276 int getByteFromFile(File *);
277 char *getStringFromFile(File *, char *, int);
278 int copyFile(char *, char *);
279 boolean touchFile(char *);
280
281 Directory *openDirectory(char *);
282 int closeDirectory(Directory *);
283 DirectoryEntry *readDirectory(Directory *);
284 void freeDirectoryEntry(DirectoryEntry *);
285
286 boolean directoryExists(const char *);
287 boolean fileExists(const char *);
288
289 boolean FileIsGraphic(char *);
290 boolean FileIsSound(char *);
291 boolean FileIsMusic(char *);
292 boolean FileIsArtworkType(char *, int);
293
294 char *get_mapped_token(char *);
295
296 struct FileInfo *getFileListFromConfigList(struct ConfigInfo *,
297                                            struct ConfigTypeInfo *,
298                                            char **, int);
299 void LoadArtworkConfig(struct ArtworkListInfo *);
300 void ReloadCustomArtworkList(struct ArtworkListInfo *);
301 void FreeCustomArtworkLists(struct ArtworkListInfo *);
302
303 char *getLogBasename(char *);
304 char *getLogFilename(char *);
305 void OpenLogFile(void);
306 void CloseLogFile(void);
307 void DumpLogFile(void);
308
309 void NotifyUserAboutErrorFile(void);
310
311 #if DEBUG
312 void debug_print_timestamp(int, char *);
313 #endif
314 void print_timestamp_init(char *);
315 void print_timestamp_time(char *);
316 void print_timestamp_done(char *);
317
318
319 #endif // MISC_H