version 1.1, 2017/01/24 14:48:56
|
version 1.1.1.2, 2021/03/17 00:49:17
|
Line 1
|
Line 1
|
|
#define _GNU_SOURCE |
#include <string.h> |
#include <string.h> |
#include <stdlib.h> |
#include <stdlib.h> |
|
#include <locale.h> |
#include "confuse.h" |
#include "confuse.h" |
|
|
int main(void) |
int main(void) |
{ |
{ |
cfg_bool_t verbose = cfg_false; | static cfg_bool_t verbose = cfg_false; |
char *server = NULL; | static char *server = NULL; |
double delay = 1.356e-32; | static double delay = 1.356e-32; |
char *username = NULL; | static char *username = NULL; |
|
|
/* Although the macro used to specify an integer option is called | /* Although the macro used to specify an integer option is called |
* CFG_SIMPLE_INT(), it actually expects a long int. On a 64 bit system | * CFG_SIMPLE_INT(), it actually expects a long int. On a 64 bit system |
* where ints are 32 bit and longs 64 bit (such as the x86-64 or amd64 | * where ints are 32 bit and longs 64 bit (such as the x86-64 or amd64 |
* architectures), you will get weird effects if you use an int here. | * architectures), you will get weird effects if you use an int here. |
* | * |
* If you use the regular (non-"simple") options, ie CFG_INT() and use | * If you use the regular (non-"simple") options, ie CFG_INT() and use |
* cfg_getint(), this is not a problem as the data types are implicitly | * cfg_getint(), this is not a problem as the data types are implicitly |
* cast. | * cast. |
*/ | */ |
long int debug = 1; | static long int debug = 1; |
|
|
cfg_opt_t opts[] = { | cfg_opt_t opts[] = { |
CFG_SIMPLE_BOOL("verbose", &verbose), | CFG_SIMPLE_BOOL("verbose", &verbose), |
CFG_SIMPLE_STR("server", &server), | CFG_SIMPLE_STR("server", &server), |
CFG_SIMPLE_STR("user", &username), | CFG_SIMPLE_STR("user", &username), |
CFG_SIMPLE_INT("debug", &debug), | CFG_SIMPLE_INT("debug", &debug), |
CFG_SIMPLE_FLOAT("delay", &delay), | CFG_SIMPLE_FLOAT("delay", &delay), |
CFG_END() | CFG_END() |
}; | }; |
cfg_t *cfg; | cfg_t *cfg; |
|
|
/* set default value for the server option */ | /* Localize messages & types according to environment, since v2.9 */ |
server = strdup("gazonk"); | #ifdef LC_MESSAGES |
| setlocale(LC_MESSAGES, ""); |
| setlocale(LC_CTYPE, ""); |
| #endif |
|
|
cfg = cfg_init(opts, 0); | /* set default value for the server option */ |
cfg_parse(cfg, "simple.conf"); | server = strdup("gazonk"); |
|
|
printf("verbose: %s\n", verbose ? "true" : "false"); | cfg = cfg_init(opts, 0); |
printf("server: %s\n", server); | cfg_parse(cfg, "simple.conf"); |
printf("username: %s\n", username); | |
printf("debug: %ld\n", debug); | |
printf("delay: %G\n", delay); | |
|
|
printf("setting username to 'foo'\n"); | printf("verbose: %s\n", verbose ? "true" : "false"); |
/* using cfg_setstr here is not necessary at all, the equivalent | printf("server: %s\n", server); |
* code is: | printf("username: %s\n", username); |
* free(username); | printf("debug: %ld\n", debug); |
* username = strdup("foo"); | printf("delay: %G\n", delay); |
*/ | |
cfg_setstr(cfg, "user", "foo"); | |
printf("username: %s\n", username); | |
|
|
/* print the parsed values to another file */ | printf("setting username to 'foo'\n"); |
{ | /* using cfg_setstr here is not necessary at all, the equivalent |
FILE *fp = fopen("simple.conf.out", "w"); | * code is: |
cfg_print(cfg, fp); | * free(username); |
fclose(fp); | * username = strdup("foo"); |
} | */ |
| cfg_setstr(cfg, "user", "foo"); |
| printf("username: %s\n", username); |
|
|
cfg_free(cfg); | /* print the parsed values to another file */ |
| { |
| FILE *fp = fopen("simple.conf.out", "w"); |
|
|
/* You are responsible for freeing string values. */ | cfg_print(cfg, fp); |
free(server); | fclose(fp); |
free(username); | } |
|
|
return 0; | cfg_free(cfg); |
| |
| /* You are responsible for freeing string values. */ |
| free(server); |
| free(username); |
| |
| return 0; |
} |
} |