-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsparrowFile.h
565 lines (525 loc) · 17.6 KB
/
sparrowFile.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* This file is part of sparrow3d.
* Sparrow3d is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* Sparrow3d is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Foobar. If not, see <http://www.gnu.org/licenses/>
*
* For feedback and questions about my Files and Projects please mail me,
* Alexander Matthes (Ziz) , zizsdl_at_googlemail.com */
/* File: sparrowFile
*
* SparrowFile is for file handling. Most basic tasks (opening, reading a
* amount of bytes, closing) are already done by SDL. sparrowFile extends this
* to checking whether a file exists, reading until specific signs, searching
* files, creating folders, etc. Use only /! not \ ... For more information
* about file functions see http://www.libsdl.org/cgi/docwiki.cgi/SDL_API
* and scroll down to "Files (RWops)". Keep in mind: spFilePointer and
* SDL_RWops* are EXACTLY the same. ;-) */
#ifndef _SPARROW_FILE_H
#define _SPARROW_FILE_H
#include "sparrow3d.h"
#include <SDL.h>
/* enum: spFileError
*
* Enumeration for errors while working with files
*
* SP_FILE_EVERYTHING_OK - no error, everything ok
* SP_FILE_ACCESS_ERROR - error while accessing the file
* SP_FILE_NOT_FOUND_ERROR - the file coudln't be found
* SP_FILE_ALREADY_EXISTS_ERROR - the file already exists
* SP_FILE_INVALID_PARAMETER_ERROR - some invalid parameter like a file as
* parameter if a path is needed
* SP_FILE_UNKNOWN_ERROR - unknown error */
typedef enum
{
SP_FILE_EVERYTHING_OK = 0,
SP_FILE_ACCESS_ERROR = 1,
SP_FILE_NOT_FOUND_ERROR = 2,
SP_FILE_ALREADY_EXISTS_ERROR = 3,
SP_FILE_INVALID_PARAMETER_ERROR = 4,
SP_FILE_UNKNOWN_ERROR = 5
} spFileError;
/* enum: spFileType
*
* Enumeration for file types
*
* SP_FILE_FILE - a normal file
* SP_FILE_DIRECTORY - a directory
* SP_FILE_LINK - a link to a file or a directory */
typedef enum
{
SP_FILE_FILE = 0,
SP_FILE_DIRECTORY = 1,
SP_FILE_LINK = 2,
} spFileType;
/* enum: spFileSortType
*
* This enumeration is needed for sorting files when searching in a folder
*
* SP_FILE_SORT_BY_NAME - Sorting by name
* SP_FILE_SORT_BY_TYPE - Sorting by type
* SP_FILE_SORT_BY_TYPE_AND_NAME - Sort by both
* SP_FILE_SORT_BACKWARDS - Combine this with the above to sort backwards */
typedef enum
{
SP_FILE_SORT_BY_NAME = 0,
SP_FILE_SORT_BY_TYPE = 1,
SP_FILE_SORT_BY_TYPE_AND_NAME = 2,
SP_FILE_SORT_BACKWARDS = 4
} spFileSortType;
//type: spFilePointer
//Same lik SDL_RWops*, but looks better ;)
typedef SDL_RWops *spFilePointer;
/* type: spFileList
*
* Linked list type for searching results, which contains found files.
*
* Variables:
* name (char*) - filename
* type (spFileType) - filetype, see <spFileType>
* last_mod - time of the last modification in seconds since 1.1.1970 00:00:00
* last_acc - time of the last access in seconds since 1.1.1970 00:00:00
* prev (spFileList*) - previous found
* next (spFileList*) - next found
* count (int) - only valid for the first element, describes the number of found files*/
typedef struct spFileListStruct *spFileListPointer;
typedef struct spFileListStruct {
char name[256];
spFileType type;
Sint64 last_mod;
Sint64 last_acc;
spFileListPointer prev;
spFileListPointer next;
int count; //only valid for the first element!
} spFileList;
/* Function: spFileExists
*
* Tests, whether the file "filename" exists ;-)
*
* Parameters:
* filename - the filename to check
*
* Returns:
* int - 1 if the file exists, 0 if not*/
PREFIX int spFileExists( const char* filename );
/* Function: spReadOneLine
*
* Reads one line from a SDL_RWops file.
*
* Parameters:
* file - the file to read
* buffer - where the file content is written to
* buffer_len - the length of buffer (+ zero byte!).
*
* Returns:
* int - If the end of file is reached, 1 is returned, else 0*/
PREFIX int spReadOneLine( spFilePointer file , char* buffer, int buffer_len);
/* Function: spWriteOneLine
*
* Writes one line from a SDL_RWops file.
*
* Parameters:
* file - the file to read
* buffer - content to be written
*
* Returns:
* int - 1 at error, 0 else*/
PREFIX int spWriteOneLine( spFilePointer file , char* buffer);
/* Function: spReadUntil
*
* Reads signs from the file "file" until the buffer is full
* (buffer_len) or the sign "end_sign" is reached. The sign before
* "end_sign" is the last sign of the string! If you read more signs
* from the file, the sign AFTER "end_sign" is the next you will read.
* It is useful for parsing simple XML files.
*
* Parameters:
* file - the file to be read
* buffer - the buffer for reading signs
* buffer_len - the length of the buffer
* end_sign - the sign to which it will be read
* ignore_windows_return - Windows uses 2 signs for line breaks (VERY smart...),
* so if you set ignore_windows_return to 1, \r will be ignored. Necessary for
* Windows text files, but for binary files it doesn't make sense at all.
*
* Returns:
* int - Like spReadOneLine it returns 1 if eof is reached, else 0.*/
PREFIX int spReadUntil( spFilePointer file , char* buffer, int buffer_len, char end_sign,char ignore_windows_return);
/* Function: spCreateDirectoryChain
*
* Creates a directory chain (like /home/user/.config/pinball/settings) if
* it not exists already. That means if you create e.g. rainbow/dash, the folder
* rainbow will be created if it doesn't exist.
*
* Parameters:
* directories - the path of the directory to create
*
* Returns:
* spFileError - <SP_FILE_EVERYTHING_OK> if no error, else see
* <spFileError>*/
PREFIX spFileError spCreateDirectoryChain( const char* directories );
/* Function: spRemoveFile
*
* Removes/deletes a file
*
* Parameters:
* filename - the name of the file
*
* Returns:
* spFileError - <SP_FILE_EVERYTHING_OK> if no error, else see
* <spFileError>
*
* See Also:
* <spRemoveDirectory>*/
PREFIX spFileError spRemoveFile( const char* filename );
/* Function: spRemoveDirectory
*
* Removes an EMPTY directory
*
* Parameters:
* dirname - the name of the directory
*
* Returns:
* spFileError - <SP_FILE_EVERYTHING_OK> if no error, else see
* <spFileError>
*
* See Also:
* <spRemoveFile>*/
PREFIX spFileError spRemoveDirectory( const char* dirname );
/* Function: spRenameFile
*
* Renames a file
*
* Parameters:
* filename - the old filename
* newname - the new filename
*
* Returns:
* spFileError - <SP_FILE_EVERYTHING_OK> if no error, else see
* <spFileError>
*
* See Also:
* <spRenameDirectory>*/
PREFIX spFileError spRenameFile( const char* filename , const char* newname);
/* Function: spRenameDirectory
*
* Renames a directory, works exactly like spRenameFile. In fact it is just a
* redefinement.
*
* See Also:
* <spRenameFile>*/
#define spRenameDirectory spRenameFile
/* Function: spFileGetDirectory
*
* Searches for files
* Puts a double linked list of found files in directory to pointer, found files
* are directly extracted from the system and may very well not be ordered.
* Call <spFileSortList> on the list afterwards.
* Finally call <spFileDeleteList> to dispose the list.
* Returned filepaths include the passed directory string
*
* Parameters:
* pointer - pointer to an existing <spFileListPointer> (not a struct), the result
* will be placed in here
* directory - path of the directory to be searched. Use forward slashes (even
* on Windows). Use "." for working directory, ".." for parent dir.
* DON'T include a trailing slash
* recursive - if 0 only _directory_ will be searched, if 1 the subdirectories,
* too. Be carefull with infinite directory loops!
* no_hidden_files - if 1, hidden files like .git are ignored.
*
* Returns:
* spFileError - <SP_FILE_EVERYTHING_OK> if no error, else see
* <spFileError>*/
PREFIX spFileError spFileGetDirectory(spFileListPointer* pointer, char* directory,int recursive,int no_hidden_files);
/* Function: spFileDeleteList
*
* Deletes a list created by spFileGetDirectory.
*
* Parameters:
* list - the list to be deleted*/
PREFIX void spFileDeleteList(spFileListPointer list);
/* Function: spFileSortList
*
* Sorts the file list like you want.
*
* Parameters:
* list - the list to be sorted
* sortBy - the sorting pattern. The possible sort pattern are
* (self-explanatory): <SP_FILE_SORT_BY_NAME>, <SP_FILE_SORT_BY_TYPE>,
* <SP_FILE_SORT_BY_TYPE_AND_NAME>. Add <SP_FILE_SORT_BACKWARDS> to sort
* backwards */
PREFIX void spFileSortList(spFileListPointer* list,spFileSortType sortBy);
/* Function: spConfigGetPath
*
* Returns the path to config files, which may be everywhere on different
* targets, e.g. in appdata for the pandora, home-folder for the gcw, etc.
*
* Parameters:
* buffer - buffer to fill with the path
* subfolder - name of the program used to create subfolders e.g. in ~/.config.
* Not used by every target. In fact only by x86 linux and gcw.
* file - name of the config file to give the path to
*
* Returns:
* char* - filled pointer to buffer*/
PREFIX char* spConfigGetPath(char* buffer,char* subfolder,char* file);
/* type: spConfigEntry
*
* List type for config entries.
*
* Variables:
* key (char[64]) - name of the config setting, e.g. "resolution"
* value (char[512]) - value of the config setting, e.g. "800x480"
* next (pointer) - next element in list*/
typedef struct spConfigEntryStruct *spConfigEntryPointer;
typedef struct spConfigEntryStruct
{
char key[64];
char value[512];
spConfigEntryPointer next;
} spConfigEntry;
/* type: spConfig
*
* Config type
*
* Variables:
* filename (char*) - full path of the config
* firstEntry (pointer) - pointer to the first entry in the config file*/
typedef struct spConfigStruct *spConfigPointer;
typedef struct spConfigStruct
{
char* filename;
spConfigEntryPointer firstEntry;
spConfigEntryPointer lastEntry;
} spConfig;
/* Function: spConfigRead
*
* Reads a config and saves it to a <spConfig> struct
*
* Parameters:
* filename - filename of the config. It will be saved to a specific folder on
* every target
* subfolder - needed to "calculate" this specific folder. Should just be your
* program name
*
* Returns:
* spConfigPointer - Pointer to a <spConfig> Struct*/
PREFIX spConfigPointer spConfigRead(char* filename,char* subfolder);
/* Function: spConfigWrite
*
* Writes the config to the file.
*
* Parameters:
* config - config, that shall be written */
PREFIX void spConfigWrite(spConfigPointer config);
/* Function: spConfigFree
*
* Frees the struct if you don't need it anymore
*
* Parameters:
* config - config, that shall be freed */
PREFIX void spConfigFree(spConfigPointer config);
/* Function: spConfigGetString
*
* Returns the string value to a specific key. If the key is not found, it will
* be added with the default_value. There is no Setter function, because you
* can edit the returned pointer directly.
*
* Parameters:
* config - config to read from
* key - key of the item
* default_value - value to add/return if the key is not available yet
*
* Returns:
* char* - the value*/
PREFIX char* spConfigGetString(spConfigPointer config,char* key,char* default_value);
/* Function: spConfigGetStringWithComment
*
* Returns the string value to a specific key. If the key is not found, it will
* be added with the default_value. There is no Setter function, because you
* can edit the returned pointer directly.
* If the key is not available in the config yet, the comment will be added
* before the setting
*
* Parameters:
* config - config to read from
* key - key of the item
* default_value - value to add/return if the key is not available yet
* comment - a comment, which will be set before the key-value-pair in the
* config if it doesn't exist yet
*
* Returns:
* char* - the value*/
PREFIX char* spConfigGetStringWithComment(spConfigPointer config,char* key,char* default_value,char* comment);
/* Function: spConfigGetBool
*
* Returns the bool value to a specific key. If the key is not found, it will
* be added with the default_value.
*
* Parameters:
* config - config to read from
* key - key of the item
* default_value - value to add/return if the key is not available yet
*
* Returns:
* int - the value. 1 means true, 0 false*/
PREFIX int spConfigGetBool(spConfigPointer config,char* key,int default_value);
/* Function: spConfigGetBoolWithComment
*
* Returns the bool value to a specific key. If the key is not found, it will
* be added with the default_value.
* If the key is not available in the config yet, the comment will be added
* before the setting
*
* Parameters:
* config - config to read from
* key - key of the item
* default_value - value to add/return if the key is not available yet
* comment - a comment, which will be set before the key-value-pair in the
* config if it doesn't exist yet
*
* Returns:
* int - the value. 1 means true, 0 false*/
PREFIX int spConfigGetBoolWithComment(spConfigPointer config,char* key,int default_value,char* comment);
/* Function: spConfigSetBool
*
* Sets the bool value to a specific key. If the key is not found, it will
* be added.
*
* Parameters:
* config - config to write to
* key - key of the item
* value - value to set*/
PREFIX void spConfigSetBool(spConfigPointer config,char* key,int value);
/* Function: spConfigSetBoolWithComment
*
* Sets the bool value to a specific key. If the key is not found, it will
* be added.
* If the key is not available in the config yet, the comment will be added
* before the setting
*
* Parameters:
* config - config to write to
* key - key of the item
* value - value to set
* comment - a comment, which will be set before the key-value-pair in the
* config if it doesn't exist yet*/
PREFIX void spConfigSetBoolWithComment(spConfigPointer config,char* key,int value,char* comment);
/* Function: spConfigGetInt
*
* Returns the int value to a specific key. If the key is not found, it will
* be added with the default_value.
*
* Parameters:
* config - config to read from
* key - key of the item
* default_value - value to add/return if the key is not available yet
*
* Returns:
* int - the value*/
PREFIX int spConfigGetInt(spConfigPointer config,char* key,int default_value);
/* Function: spConfigGetIntWithComment
*
* Returns the int value to a specific key. If the key is not found, it will
* be added with the default_value.
* If the key is not available in the config yet, the comment will be added
* before the setting
*
* Parameters:
* config - config to read from
* key - key of the item
* default_value - value to add/return if the key is not available yet
* comment - a comment, which will be set before the key-value-pair in the
* config if it doesn't exist yet
*
* Returns:
* int - the value*/
PREFIX int spConfigGetIntWithComment(spConfigPointer config,char* key,int default_value,char* comment);
/* Function: spConfigSetInt
*
* Sets the int value to a specific key. If the key is not found, it will
* be added.
*
* Parameters:
* config - config to write to
* key - key of the item
* value - value to set*/
PREFIX void spConfigSetInt(spConfigPointer config,char* key,int value);
/* Function: spConfigSetIntWithComment
*
* Sets the int value to a specific key. If the key is not found, it will
* be added.
* If the key is not available in the config yet, the comment will be added
* before the setting
*
* Parameters:
* config - config to write to
* key - key of the item
* value - value to set
* comment - a comment, which will be set before the key-value-pair in the
* config if it doesn't exist yet*/
PREFIX void spConfigSetIntWithComment(spConfigPointer config,char* key,int value,char* comment);
/* Function: spConfigGetFloat
*
* Returns the float value to a specific key. If the key is not found, it will
* be added with the default_value.
*
* Parameters:
* config - config to read from
* key - key of the item
* default_value - value to add/return if the key is not available yet
*
* Returns:
* float - the value*/
PREFIX float spConfigGetFloat(spConfigPointer config,char* key,float default_value);
/* Function: spConfigGetFloatWithComment
*
* Returns the float value to a specific key. If the key is not found, it will
* be added with the default_value.
* If the key is not available in the config yet, the comment will be added
* before the setting
*
* Parameters:
* config - config to read from
* key - key of the item
* default_value - value to add/return if the key is not available yet
* comment - a comment, which will be set before the key-value-pair in the
* config if it doesn't exist yet
*
* Returns:
* float - the value*/
PREFIX float spConfigGetFloatWithComment(spConfigPointer config,char* key,float default_value,char* comment);
/* Function: spConfigSetFloat
*
* Sets the float value to a specific key. If the key is not found, it will
* be added.
*
* Parameters:
* config - config to write to
* key - key of the item
* value - value to set*/
PREFIX void spConfigSetFloat(spConfigPointer config,char* key,float value);
/* Function: spConfigSetFloatWithComment
*
* Sets the float value to a specific key. If the key is not found, it will
* be added.
* If the key is not available in the config yet, the comment will be added
* before the setting
*
* Parameters:
* config - config to write to
* key - key of the item
* value - value to set
* comment - a comment, which will be set before the key-value-pair in the
* config if it doesn't exist yet*/
PREFIX void spConfigSetFloatWithComment(spConfigPointer config,char* key,float value,char* comment);
#endif