This example_config page provides an example of librnr support for configuration parsing and database access.
Example main():
This example provides a simple shell to create, modify, and delete configuration databases.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct
{
const char *m_sCmdName;
const char *m_sCmdHelpArgs;
const char *m_sCmdHelpDesc;
#define CHKARGS(argc, min) \
{ if((argc-1) < min) \
{ \
fprintf(stderr, "Error: %d arguments required\n", min); \
return; \
} \
}
#define CHKCFG() \
{ \
if(TheConfig == NULL) \
{ \
fprintf(stderr, "Error: configuration db does not exist\n"); \
return; \
} \
}
#define CHKNOCFG() \
{ \
if(TheConfig != NULL) \
{ \
fprintf(stderr, "Error: configuration %s exists - delete first\n", \
ConfigDbGetName(TheConfig)); \
return; \
} \
}
static void CmdHelp(
int,
char *[]);
static void CmdQuit(
int argc,
char *argv[])
{
exit(0);
}
static void CmdLog(
int argc,
char *argv[])
{
int n;
n = atoi(argv[1]);
}
{
}
static void CmdDbNew(
int argc,
char *argv[])
{
}
{
}
static void CmdDbRead(
int argc,
char *argv[])
{
}
static void CmdDelete(
int argc,
char *argv[])
{
{
printf("Error: failed to delete\n");
}
else
{
argv[1], argv[2]);
}
}
static void CmdFind(
int argc,
char *argv[])
{
char *sPat;
size_t lenPat;
const char *sSectionName;
const char *sKey;
const char *sVal;
sPat = argv[1];
lenPat = strlen(sPat);
{
{
if( !strncmp(sPat, sKey, lenPat) )
{
printf("%s: [%s]: %s=%s\n",
}
}
}
}
static void CmdGetd(
int argc,
char *argv[])
{
double fVal;
{
printf("Error: failed to get/convert\n");
}
else
{
argv[1], argv[2], fVal);
}
}
static void CmdGeti(
int argc,
char *argv[])
{
int iVal;
{
printf("Error: failed to get/convert\n");
}
else
{
argv[1], argv[2], iVal);
}
}
static void CmdGets(
int argc,
char *argv[])
{
const char *sVal;
if( (sVal =
ConfigGetStr(TheConfig, argv[1], argv[2])) == NULL )
{
printf("Error: failed to get\n");
}
else
{
argv[1], argv[2], sVal);
}
}
{
{
printf("Error: failed to delete section\n");
}
else
{
}
}
{
{
printf("Error: failed to add section\n");
}
else
{
}
}
static void CmdSetd(
int argc,
char *argv[])
{
double fVal;
fVal = atof(argv[3]);
{
printf("Error: failed to set\n");
}
else
{
argv[1], argv[2], fVal);
}
}
static void CmdSeti(
int argc,
char *argv[])
{
int iVal;
iVal = atoi(argv[3]);
{
printf("Error: failed to set\n");
}
else
{
argv[1], argv[2], iVal);
}
}
static void CmdSets(
int argc,
char *argv[])
{
{
printf("Error: failed to set\n");
}
else
{
argv[1], argv[2], argv[3]);
}
}
{
"delete existing configuration db"
},
"create new, empty configuration db"
},
"print out configuration db"
},
"read configuration db from file"
},
"delete entry from section"
},
"find all entries that partially match pattern"
},
{
"getd",
CmdGetd,
"<section> <key>",
"get key=<double> in section"
},
{
"geti",
CmdGeti,
"<section> <key>",
"get key=<int> in section"
},
{
"gets",
CmdGets,
"<section> <key>",
"get key=<string> in section"
},
"print list of commands"
},
"set logging level [0-4]"
},
"quit program"
},
"delete section in configuration db"
},
"create new, empty section in configuration db"
},
{
"setd",
CmdSetd,
"<section> <key> <double>",
"set key=<double> in section"
},
{
"seti",
CmdSeti,
"<section> <key> <int>",
"set key=<int> in section"
},
{
"sets",
CmdSets,
"<section> <key> <string>",
"set key=<string> in section"
},
};
static void CmdHelp(
int argc,
char *argv[])
{
int n;
{
}
}
static int Tokenize(
char *
string,
char *tokv[],
size_t tokmax)
{
int tokc = 0;
while( tokc < tokmax )
{
while(*string && isspace((int)*string))
{
string++;
}
if(!*string)
{
break;
}
tokv[tokc] = string;
while(*string && !isspace((int)*string))
{
string++;
}
tokc++;
if( *string == 0 )
{
break;
}
*string++ = 0;
}
return tokc;
}
{
size_t n;
int cnt;
n = strlen(sPattern);
{
{
pCmd = p;
break;
}
}
if( pCmd == NULL )
{
fprintf(stderr, "Error: %s: Command not found\n", sPattern);
}
{
if( (p != pCmd) && !strncmp(sPattern, p->
m_sCmdName, n) )
{
cnt++;
if( bIsUnique )
{
fprintf(stderr, "Error: %s: Command not unique: Matches %s %s ",
bIsUnique = false;
}
else if( cnt < 3 )
{
}
else
{
fprintf(stderr, "...");
break;
}
}
}
if( !bIsUnique )
{
fprintf(stderr, "\n");
}
return pCmd;
}
int main(
int argc,
char *argv[])
{
char input[256];
int tokc;
char *tokv[16];
printf(" Configuration Example\n");
printf("(enter 'help' for list of commands; "
"partial command matching supported)\n\n");
for(;;)
{
printf("config> ");
if( !fgets(input, (int)sizeof(input), stdin) )
{
break;
}
if( tokc <= 0 )
{
continue;
}
if( pCmd != NULL )
{
}
}
return 0;
}
Configuration input file:
#
# File: example.ini
# Desc: Example configuration file parseable by the librnr config module.
#
[global]
circus=Traveling Ring-Neck Broths # name of our circus
founded=1954
num_assets=4
asset1=clowns
asset2=dandylions
asset3=the_golden_fleas
asset4=von_trapeze_family
# list of our famous clowns
[clowns]
clownfish=I. Smelt
armed_and_silly=Herb Humorous
joe_cooler=Chester De Beers
# our dress-to-the-nines lions
[dandylions]
fopulous=3 yr. old male
fastidiously_eddie=9 year old male asian
spiffy_susan=9 yr. female
# wee bugs
[the_golden_fleas]
flea1=leaps
flea2=jumps
flea3=skips
flea4=bounds
# the hills are alive
[von_trapeze_family]
catcher=Captain Georg von Trapeze
rigger=Maria
board_biscuit=Friedrich
flyer1=Leisl
flyer2=Gretl
spotter=Kurt
Generated output:
$ ../loc/bin.x86_64/example_config
Configuration Example
(enter 'help' for list of commands; partial command matching supported)
config> help
dbdelete - delete existing configuration db
dbnew - create new, empty configuration db
dbprint - print out configuration db
dbread <filename> - read configuration db from file
delete <section> <key> - delete entry from section
find <pattern> - find all entries that partially match pattern
getd <section> <key> - get key=<double> in section
geti <section> <key> - get key=<int> in section
gets <section> <key> - get key=<string> in section
help - print list of commands
log <level> - set logging level [0-4]
quit - quit program
sectdelete <section> - delete section in configuration db
sectnew <section> - create new, empty section in configuration db
setd <section> <key> <double> - set key=<double> in section
seti <section> <key> <int> - set key=<int> in section
sets <section> <key> <string> - set key=<string> in section
config> dbread example.ini
config>
config> dbprint
# Configuration example.ini
[clowns]
joe_cooler=Chester De Beers
clownfish=I. Smelt
armed_and_silly=Herb Humorous
[the_golden_fleas]
flea4=bounds
flea3=skips
flea2=jumps
flea1=leaps
[von_trapeze_family]
spotter=Kurt
flyer1=Leisl
flyer2=Gretl
rigger=Maria
board_biscuit=Friedrich
catcher=Captain Georg von Trapeze
[dandylions]
spiffy_susan=9 yr. female
fastidiously_eddie=9 year old male asian
fopulous=3 yr. old male
[global]
asset4=von_trapeze_family
asset1=clowns
circus=Traveling Ring-Neck Broths
asset3=the_golden_fleas
founded=1954
asset2=dandylions
num_assets=4
config>
config> find flea
example.ini: [the_golden_fleas]: flea4=bounds
example.ini: [the_golden_fleas]: flea3=skips
example.ini: [the_golden_fleas]: flea2=jumps
example.ini: [the_golden_fleas]: flea1=leaps
config> geti global founded
example.ini: [global]: founded=1954
config> sets the_golden_fleas flea5 hops
example.ini: [the_golden_fleas]: flea5=hops
config> del global asset2
example.ini: [global]: asset2: deleted
config> sectdel danydlions
Error: failed to delete section
config> sectdel dandylions
example.ini: [dandylions] deleted
config> seti global num_assets 3
example.ini: [global]: num_assets=3
config>
config> dbprint
# Configuration example.ini
[clowns]
joe_cooler=Chester De Beers
clownfish=I. Smelt
armed_and_silly=Herb Humorous
[the_golden_fleas]
flea5=hops
flea4=bounds
flea3=skips
flea2=jumps
flea1=leaps
[von_trapeze_family]
spotter=Kurt
flyer1=Leisl
flyer2=Gretl
rigger=Maria
board_biscuit=Friedrich
catcher=Captain Georg von Trapeze
[global]
asset4=von_trapeze_family
asset1=clowns
circus=Traveling Ring-Neck Broths
asset3=the_golden_fleas
founded=1954
num_assets=3
config> dbdel
config> dbprint
Error: configuration db does not exist
config> quit