--- embedaddon/confuse/doc/html/simple_8c-example.html 2017/01/24 14:48:55 1.1.1.1 +++ embedaddon/confuse/doc/html/simple_8c-example.html 2021/03/17 00:49:17 1.1.1.2 @@ -1,98 +1,167 @@ - - + + - - confuse: simple.c - + + + +confuse: simple.c + + + + + + + +
+
+ + + + + + +
+
confuse +  3.3 +
+
+
+ + + + + + + +
+ +
+
-
- - - - - +
+
+
simple.c
+
+
+
#define _GNU_SOURCE
+
#include <string.h>
+
#include <stdlib.h>
+
#include <locale.h>
+
#include "confuse.h"
+
+
int main(void)
+
{
+
static cfg_bool_t verbose = cfg_false;
+
static char *server = NULL;
+
static double delay = 1.356e-32;
+
static char *username = NULL;
+
+
/* 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
+
* 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.
+
*
+
* 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
+
* cast.
+
*/
+
static long int debug = 1;
+
+
cfg_opt_t opts[] = {
+
CFG_SIMPLE_BOOL("verbose", &verbose),
+
CFG_SIMPLE_STR("server", &server),
+
CFG_SIMPLE_STR("user", &username),
+
CFG_SIMPLE_INT("debug", &debug),
+
CFG_SIMPLE_FLOAT("delay", &delay),
+ +
};
+
cfg_t *cfg;
+
+
/* Localize messages & types according to environment, since v2.9 */
+
#ifdef LC_MESSAGES
+
setlocale(LC_MESSAGES, "");
+
setlocale(LC_CTYPE, "");
+
#endif
+
+
/* set default value for the server option */
+
server = strdup("gazonk");
+
+
cfg = cfg_init(opts, 0);
+
cfg_parse(cfg, "simple.conf");
+
+
printf("verbose: %s\n", verbose ? "true" : "false");
+
printf("server: %s\n", server);
+
printf("username: %s\n", username);
+
printf("debug: %ld\n", debug);
+
printf("delay: %G\n", delay);
+
+
printf("setting username to 'foo'\n");
+
/* using cfg_setstr here is not necessary at all, the equivalent
+
* code is:
+
* free(username);
+
* username = strdup("foo");
+
*/
+
cfg_setstr(cfg, "user", "foo");
+
printf("username: %s\n", username);
+
+
/* print the parsed values to another file */
+
{
+
FILE *fp = fopen("simple.conf.out", "w");
+
+
cfg_print(cfg, fp);
+
fclose(fp);
+
}
+
+
cfg_free(cfg);
+
+
/* You are responsible for freeing string values. */
+
free(server);
+
free(username);
+
+
return 0;
+
}
+
+
DLLIMPORT cfg_t *__export cfg_init(cfg_opt_t *opts, cfg_flag_t flags)
Create and initialize a cfg_t structure.
Definition: confuse.c:1816
+
#define CFG_SIMPLE_BOOL(name, svalue)
Initialize a "simple" boolean option (see documentation for CFG_SIMPLE_STR for more information).
Definition: confuse.h:511
+
#define CFG_SIMPLE_FLOAT(name, svalue)
Initialize a "simple" floating point option (see documentation for CFG_SIMPLE_STR for more informatio...
Definition: confuse.h:478
+
DLLIMPORT int __export cfg_setstr(cfg_t *cfg, const char *name, const char *value)
Set the value of a string option given its name.
Definition: confuse.c:2211
+
#define CFG_END()
Terminate list of options.
Definition: confuse.h:574
+
A configuration file parser library.
+
#define CFG_SIMPLE_STR(name, svalue)
Initialize a "simple" string option.
Definition: confuse.h:410
+
DLLIMPORT int __export cfg_print(cfg_t *cfg, FILE *fp)
Print the options and values to a file.
Definition: confuse.c:2548
+
DLLIMPORT int __export cfg_parse(cfg_t *cfg, const char *filename)
Parse a configuration file.
Definition: confuse.c:1746
+
Data structure holding information about an option.
Definition: confuse.h:309
+
Data structure holding information about a "section".
Definition: confuse.h:252
+
DLLIMPORT int __export cfg_free(cfg_t *cfg)
Free a cfg_t context.
Definition: confuse.c:1962
+
#define CFG_SIMPLE_INT(name, svalue)
Initialize a "simple" integer option (see documentation for CFG_SIMPLE_STR for more information).
Definition: confuse.h:445
+
cfg_bool_t
Boolean values.
Definition: confuse.h:232
+ + +