| 1 |
/* Glue functions for the minIni library, based on the C/C++ stdio library and |
| 2 |
* using BSD-style file locking to "serialize" concurrent accesses to an INI |
| 3 |
* file. It presumes GCC compiler extensions. |
| 4 |
* |
| 5 |
* By CompuPhase, 2020 |
| 6 |
* This "glue file" is in the public domain. It is distributed without |
| 7 |
* warranties or conditions of any kind, either express or implied. |
| 8 |
*/ |
| 9 |
|
| 10 |
/* map required file I/O types and functions to the standard C library */ |
| 11 |
#include <stdio.h> |
| 12 |
#include <unistd.h> |
| 13 |
#include <sys/file.h> |
| 14 |
|
| 15 |
#define INI_FILETYPE FILE* |
| 16 |
|
| 17 |
static inline int ini_openread(const char *filename, INI_FILETYPE *file) { |
| 18 |
if ((*file = fopen((filename),"r")) == NULL) |
| 19 |
return 0; |
| 20 |
return flock(fileno(*file), LOCK_SH) == 0; |
| 21 |
} |
| 22 |
|
| 23 |
static inline int ini_openwrite(const char *filename, INI_FILETYPE *file) { |
| 24 |
if ((*file = fopen((filename),"r+")) == NULL |
| 25 |
&& (*file = fopen((filename),"w")) == NULL) |
| 26 |
return 0; |
| 27 |
if (flock(fileno(*file), LOCK_EX) < 0) |
| 28 |
return 0; |
| 29 |
return ftruncate(fileno(*file), 0); |
| 30 |
} |
| 31 |
|
| 32 |
#define INI_OPENREWRITE |
| 33 |
static inline int ini_openrewrite(const char *filename, INI_FILETYPE *file) { |
| 34 |
if ((*file = fopen((filename),"r+")) == NULL) |
| 35 |
return 0; |
| 36 |
return flock(fileno(*file), LOCK_EX) == 0; |
| 37 |
} |
| 38 |
|
| 39 |
#define ini_close(file) (fclose(*(file)) == 0) |
| 40 |
#define ini_read(buffer,size,file) (fgets((buffer),(size),*(file)) != NULL) |
| 41 |
#define ini_write(buffer,file) (fputs((buffer),*(file)) >= 0) |
| 42 |
#define ini_rename(source,dest) (rename((source), (dest)) == 0) |
| 43 |
|
| 44 |
#define INI_FILEPOS long int |
| 45 |
#define ini_tell(file,pos) (*(pos) = ftell(*(file))) |
| 46 |
#define ini_seek(file,pos) (fseek(*(file), *(pos), SEEK_SET) == 0) |
| 47 |
|
| 48 |
/* for floating-point support, define additional types and functions */ |
| 49 |
#define INI_REAL float |
| 50 |
#define ini_ftoa(string,value) sprintf((string),"%f",(value)) |
| 51 |
#define ini_atof(string) (INI_REAL)strtod((string),NULL) |