77 #define CHKARGS(argc, min) \ 78 { if((argc-1) < min) \ 80 fprintf(stderr, "Error: %d arguments required\n", min); \ 90 if(TheConfig == NULL) \ 92 fprintf(stderr, "Error: configuration db does not exist\n"); \ 102 if(TheConfig != NULL) \ 104 fprintf(stderr, "Error: configuration %s exists - delete first\n", \ 105 ConfigDbGetName(TheConfig)); \ 111 static void CmdHelp(
int,
char *[]);
130 static void CmdLog(
int argc,
char *argv[])
208 printf(
"Error: failed to delete\n");
229 const char *sSectionName;
237 lenPat = strlen(sPat);
247 if( !strncmp(sPat, sKey, lenPat) )
250 printf(
"%s: [%s]: %s=%s\n",
276 printf(
"Error: failed to get/convert\n");
281 argv[1], argv[2], fVal);
300 printf(
"Error: failed to get/convert\n");
305 argv[1], argv[2], iVal);
324 printf(
"Error: failed to get\n");
329 argv[1], argv[2], sVal);
348 printf(
"Error: failed to delete section\n");
370 printf(
"Error: failed to add section\n");
391 fVal = atof(argv[3]);
395 printf(
"Error: failed to set\n");
400 argv[1], argv[2], fVal);
417 iVal = atoi(argv[3]);
421 printf(
"Error: failed to set\n");
426 argv[1], argv[2], iVal);
443 printf(
"Error: failed to set\n");
448 argv[1], argv[2], argv[3]);
458 "delete existing configuration db" 461 "create new, empty configuration db" 464 "print out configuration db" 467 "read configuration db from file" 470 {
"delete",
CmdDelete,
"<section> <key>",
471 "delete entry from section" 474 {
"find",
CmdFind,
"<pattern>",
475 "find all entries that partially match pattern" 478 {
"getd",
CmdGetd,
"<section> <key>",
479 "get key=<double> in section" 481 {
"geti",
CmdGeti,
"<section> <key>",
482 "get key=<int> in section" 484 {
"gets",
CmdGets,
"<section> <key>",
485 "get key=<string> in section" 489 "print list of commands" 491 {
"log",
CmdLog,
"<level>",
492 "set logging level [0-4]" 499 "delete section in configuration db" 502 "create new, empty section in configuration db" 505 {
"setd",
CmdSetd,
"<section> <key> <double>",
506 "set key=<double> in section" 508 {
"seti",
CmdSeti,
"<section> <key> <int>",
509 "set key=<int> in section" 511 {
"sets",
CmdSets,
"<section> <key> <string>",
512 "set key=<string> in section" 545 static int Tokenize(
char *
string,
char *tokv[],
size_t tokmax)
549 while( tokc < tokmax )
551 while(*
string && isspace((
int)*
string))
560 while(*
string && !isspace((
int)*
string))
589 n = strlen(sPattern);
604 fprintf(stderr,
"Error: %s: Command not found\n", sPattern);
611 if( (p != pCmd) && !strncmp(sPattern, p->
m_sCmdName, n) )
618 fprintf(stderr,
"Error: %s: Command not unique: Matches %s %s ",
630 fprintf(stderr,
"...");
638 fprintf(stderr,
"\n");
654 int main(
int argc,
char *argv[])
661 printf(
" Configuration Example\n");
662 printf(
"(enter 'help' for list of commands; " 663 "partial command matching supported)\n\n");
671 if( !fgets(input, (
int)
sizeof(input), stdin) )
void ConfigIterDelete(ConfigIter_T *pIter)
Delete a configuration iterator.
int main(int argc, char *argv[])
Example main.
const char * m_sCmdHelpArgs
command help arguments
static void CmdSetd(int argc, char *argv[])
Set or create key=<double> entry in section.
int ConfigDelete(Config_T *pConfig, const char *sSectionName, const char *sKey)
Delete entry from the configuration database.
static void CmdGetd(int argc, char *argv[])
Get configuration value string converting to double.
static void CmdSectDelete(int argc, char *argv[])
Delete entier configuration db section.
static void CmdFind(int argc, char *argv[])
Find configuration key=values matching substring.
int ConfigSetStr(Config_T *pConfig, const char *sSectionName, const char *sKey, const char *sVal)
Set or update a string value in the configuration database.
Configuration parser declarations.
static void CmdDbNew(int argc, char *argv[])
Create new, empty configuration db.
int LOG_SET_THRESHOLD(int nLevel)
Set new logging threshold level.
const char * ConfigGetStr(Config_T *pConfig, const char *sSectionName, const char *sKey)
Get a string value from the configuration database.
void ConfigDbPrint(Config_T *pConfig, FILE *fp)
Print configuration database to the output file stream.
ConfigIter_T * ConfigDbIterNew(Config_T *pConfig)
Create a new configuration database iterator.
static void CmdDbRead(int argc, char *argv[])
Create new configuration db from file.
#define arraysize(array)
array size, i.e. number of array entries
Config_T * ConfigDbNew(const char *sMainName)
Create and initialize a new empty configuration database.
static int Tokenize(char *string, char *tokv[], size_t tokmax)
Tokenize input.
CmdExec_T m_fnCmdExec
command execution function
static Cmd_T * MatchCmd(const char *sPattern, Cmd_T tblCmds[])
Match command.
static void CmdHelp(int, char *[])
Print command help.
static void CmdDbPrint(int argc, char *argv[])
Print out entire configuration db.
int ConfigSectionDelete(Config_T *pConfig, ConfigSection_T *pSect)
Delete a section from the configuration database.
static void CmdDbDelete(int argc, char *argv[])
Delete configuration db.
ConfigSection_T * ConfigSectionNew(Config_T *pConfig, const char *sSectionName)
Add a new empty section to the configuration database.
const char * ConfigIterNext(ConfigIter_T *pIter)
Get the next value in iteration.
static Config_T * TheConfig
the configuration db
const char * m_sCmdName
command name
static void CmdSeti(int argc, char *argv[])
Set or create key=<int> entry in section.
static void CmdSets(int argc, char *argv[])
Set or create key=<string> entry in section.
ConfigSection_T * ConfigSectionGet(Config_T *pConfig, const char *sSectionName)
Get a section from the configuration database.
RoadNarrows Robotics common configuration file.
static void CmdGets(int argc, char *argv[])
Get configuration value string.
static void CmdQuit(int argc, char *argv[])
Quit this example program.
static void CmdSectNew(int argc, char *argv[])
Create new, empty configuration db section.
static int ConfigGetInt(Config_T *pConfig, const char *sSectionName, const char *sKey, int *pVal)
Get and convert an int value from the configuration database.
#define CHKNOCFG()
Check that the configuration db does not exist.
void(* CmdExec_T)(int, char *[])
command execution function type
static void CmdGeti(int argc, char *argv[])
Get configuration value string converting to int.
void ConfigDbDelete(Config_T *pConfig)
Delete the configuration database.
int ConfigSetDouble(Config_T *pConfig, const char *sSectionName, const char *sKey, double fVal)
Set or update a double value in the configuration database.
static void CmdLog(int argc, char *argv[])
Set diagnotics logging level.
static Cmd_T CmdTbl[]
The command set.
static void CmdDelete(int argc, char *argv[])
Delete configuration key.
static int ConfigSetInt(Config_T *pConfig, const char *sSectionName, const char *sKey, int iVal)
Set or update an int value in the configuration database.
ConfigIter_T * ConfigSectionIterNew(Config_T *pConfig, const char *sSectionName)
Create a new configuration section iterator.
#define CHKARGS(argc, min)
Check argument count against minimum required.
int ConfigGetDouble(Config_T *pConfig, const char *sSectionName, const char *sKey, double *pVal)
Get and convert a double value from the configuration database.
const char * ConfigDbGetName(Config_T *pConfig)
Get the current name assigned to the configuration database.
const char * m_sCmdHelpDesc
command help description
Config_T * ConfigDbRead(const char *sFileName)
Create a new configuration database from a configuration file.
#define CHKCFG()
Check that the configuration db exists.