-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminGlue.h
51 lines (43 loc) · 2 KB
/
minGlue.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
/* Glue functions for the minIni library, based on the C/C++ stdio library and
* using BSD-style file locking to "serialize" concurrent accesses to an INI
* file. It presumes GCC compiler extensions.
*
* By CompuPhase, 2020
* This "glue file" is in the public domain. It is distributed without
* warranties or conditions of any kind, either express or implied.
*/
/* map required file I/O types and functions to the standard C library */
#include <stdio.h>
#include <unistd.h>
#include <sys/file.h>
#define INI_FILETYPE FILE*
static inline int ini_openread(const char *filename, INI_FILETYPE *file) {
if ((*file = fopen((filename), "r")) == NULL)
return 0;
return flock(fileno(*file), LOCK_SH) == 0;
}
static inline int ini_openwrite(const char *filename, INI_FILETYPE *file) {
if ((*file = fopen((filename), "r+")) == NULL
&& (*file = fopen((filename), "w")) == NULL)
return 0;
if (flock(fileno(*file), LOCK_EX) < 0)
return 0;
return ftruncate(fileno(*file), 0) == 0;
}
#define INI_OPENREWRITE
static inline int ini_openrewrite(const char *filename, INI_FILETYPE *file) {
if ((*file = fopen((filename), "r+")) == NULL)
return 0;
return flock(fileno(*file), LOCK_EX) == 0;
}
#define ini_close(file) (fclose(*(file)) == 0)
#define ini_read(buffer,size,file) (fgets((buffer),(size),*(file)) != NULL)
#define ini_write(buffer,file) (fputs((buffer),*(file)) >= 0)
#define ini_rename(source,dest) (rename((source), (dest)) == 0)
#define INI_FILEPOS long int
#define ini_tell(file,pos) (*(pos) = ftell(*(file)))
#define ini_seek(file,pos) (fseek(*(file), *(pos), SEEK_SET) == 0)
/* for floating-point support, define additional types and functions */
#define INI_REAL float
#define ini_ftoa(string,value) sprintf((string),"%g",(value))
#define ini_atof(string) (INI_REAL)strtod((string),NULL)