Skip to content

Commit

Permalink
Tweak layout.
Browse files Browse the repository at this point in the history
Reformated comments and use bstrcasecmp.
  • Loading branch information
Marco van Wieringen committed Feb 17, 2015
1 parent 9b49454 commit 0bf5735
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 48 deletions.
85 changes: 57 additions & 28 deletions src/lib/ini.c
Expand Up @@ -154,7 +154,9 @@ static void s_warn(const char *file, int line, LEX *lc, const char *msg, ...)
}
}

/* Reset free items */
/*
* Reset free items
*/
void ConfigFile::clear_items()
{
if (!items) {
Expand All @@ -163,7 +165,9 @@ void ConfigFile::clear_items()

for (int i = 0; items[i].name; i++) {
if (items[i].found) {
/* special members require delete or free */
/*
* Special members require delete or free
*/
if (items[i].handler == ini_store_str) {
free(items[i].val.strval);
items[i].val.strval = NULL;
Expand All @@ -190,28 +194,31 @@ void ConfigFile::free_items()
items_allocated = false;
}

/* Get a particular item from the items list */
/*
* Get a particular item from the items list
*/
int ConfigFile::get_item(const char *name)
{
if (!items) {
return -1;
}

for (int i = 0; i < MAX_INI_ITEMS && items[i].name; i++) {
if (strcasecmp(name, items[i].name) == 0) {
if (bstrcasecmp(name, items[i].name)) {
return i;
}
}
return -1;
}

/* Dump a buffer to a file in the working directory
/*
* Dump a buffer to a file in the working directory
* Needed to unserialise() a config
*/
bool ConfigFile::dump_string(const char *buf, int32_t len)
{
FILE *fp;
bool ret=false;
bool ret = false;

if (!out_fname) {
out_fname = get_pool_memory(PM_FNAME);
Expand All @@ -231,7 +238,9 @@ bool ConfigFile::dump_string(const char *buf, int32_t len)
return ret;
}

/* Dump the item table format to a text file (used by plugin) */
/*
* Dump the item table format to a text file (used by plugin)
*/
bool ConfigFile::serialize(const char *fname)
{
FILE *fp;
Expand Down Expand Up @@ -259,7 +268,9 @@ bool ConfigFile::serialize(const char *fname)
return ret;
}

/* Dump the item table format to a text file (used by plugin) */
/*
* Dump the item table format to a text file (used by plugin)
*/
int ConfigFile::serialize(POOLMEM **buf)
{
int len;
Expand Down Expand Up @@ -297,7 +308,9 @@ int ConfigFile::serialize(POOLMEM **buf)
return len ;
}

/* Dump the item table content to a text file (used by director) */
/*
* Dump the item table content to a text file (used by director)
*/
int ConfigFile::dump_results(POOLMEM **buf)
{
int len;
Expand Down Expand Up @@ -326,11 +339,13 @@ int ConfigFile::dump_results(POOLMEM **buf)
return len ;
}

/* Parse a config file used by Plugin/Director */
/*
* Parse a config file used by Plugin/Director
*/
bool ConfigFile::parse(const char *fname)
{
int token, i;
bool ret=false;
bool ret = false;

if (!items) {
return false;
Expand All @@ -351,15 +366,17 @@ bool ConfigFile::parse(const char *fname)
continue;
}
for (i = 0; items[i].name; i++) {
if (strcasecmp(items[i].name, lc->str) == 0) {
if (bstrcasecmp(items[i].name, lc->str)) {
if ((token = lex_get_token(lc, T_EQUALS)) == T_ERROR) {
Dmsg1(dbglevel, "in T_IDENT got token=%s\n",
lex_tok_to_str(token));
break;
}

Dmsg1(dbglevel, "calling handler for %s\n", items[i].name);
/* Call item handler */
/*
* Call item handler
*/
ret = items[i].found = items[i].handler(lc, this, &items[i]);
i = -1;
break;
Expand All @@ -368,7 +385,9 @@ bool ConfigFile::parse(const char *fname)
if (i >= 0) {
Dmsg1(dbglevel, "Keyword = %s\n", lc->str);
scan_err1(lc, "Keyword %s not found", lc->str);
/* We can raise an error here */
/*
* We can raise an error here
*/
break;
}
if (!ret) {
Expand All @@ -388,7 +407,8 @@ bool ConfigFile::parse(const char *fname)
return ret;
}

/* Analyse the content of a ini file to build the item list
/*
* Analyse the content of a ini file to build the item list
* It uses special syntax for datatype. Used by Director on Restore object
*
* OptPrompt = "Variable1"
Expand All @@ -400,17 +420,21 @@ bool ConfigFile::parse(const char *fname)
bool ConfigFile::unserialize(const char *fname)
{
int token, i, nb = 0;
bool ret=false;
bool ret = false;
const char **assign;

/* At this time, we allow only 32 different items */
/*
* At this time, we allow only 32 different items
*/
int s = MAX_INI_ITEMS * sizeof (struct ini_items);

items = (struct ini_items *) malloc (s);
memset(items, 0, s);
items_allocated = true;

/* parse the file and generate the items structure on the fly */
/*
* Parse the file and generate the items structure on the fly
*/
if ((lc = lex_open_file(lc, fname, s_err, s_warn)) == NULL) {
berrno be;
Emsg2(M_ERROR, 0, _("Cannot open config file %s: %s\n"),
Expand All @@ -434,13 +458,13 @@ bool ConfigFile::unserialize(const char *fname)
break;
}

if (strcasecmp("optprompt", lc->str) == 0) {
if (bstrcasecmp("optprompt", lc->str)) {
assign = &(items[nb].comment);

} else if (strcasecmp("optdefault", lc->str) == 0) {
} else if (bstrcasecmp("optdefault", lc->str)) {
assign = &(items[nb].default_value);

} else if (strcasecmp("optrequired", lc->str) == 0) {
} else if (bstrcasecmp("optrequired", lc->str)) {
items[nb].required = true; /* Don't use argument */
scan_to_eol(lc);
continue;
Expand All @@ -457,7 +481,9 @@ bool ConfigFile::unserialize(const char *fname)
break;
}

/* We may allow blank variable */
/*
* We may allow blank variable
*/
if (lex_get_token(lc, T_STRING) == T_ERROR) {
break;
}
Expand Down Expand Up @@ -490,9 +516,8 @@ bool ConfigFile::unserialize(const char *fname)
return ret;
}

/* ----------------------------------------------------------------
/*
* Handle data type. Import/Export
* ----------------------------------------------------------------
*/
bool ini_store_str(LEX *lc, ConfigFile *inifile, ini_items *item)
{
Expand Down Expand Up @@ -530,7 +555,9 @@ bool ini_store_alist_str(LEX *lc, ConfigFile *inifile, ini_items *item)
{
alist *list;
if (!lc) {
/* TODO, write back the alist to edit buffer */
/*
* TODO, write back the alist to edit buffer
*/
return true;
}
if (lex_get_token(lc, T_STRING) == T_ERROR) {
Expand Down Expand Up @@ -617,12 +644,14 @@ bool ini_store_bool(LEX *lc, ConfigFile *inifile, ini_items *item)
if (lex_get_token(lc, T_NAME) == T_ERROR) {
return false;
}
if (strcasecmp(lc->str, "yes") == 0 || strcasecmp(lc->str, "true") == 0) {
if (bstrcasecmp(lc->str, "yes") || bstrcasecmp(lc->str, "true")) {
item->val.boolval = true;
} else if (strcasecmp(lc->str, "no") == 0 || strcasecmp(lc->str, "false") == 0) {
} else if (bstrcasecmp(lc->str, "no") || bstrcasecmp(lc->str, "false")) {
item->val.boolval = false;
} else {
/* YES and NO must not be translated */
/*
* YES and NO must not be translated
*/
scan_err2(lc, _("Expect %s, got: %s"), "YES, NO, TRUE, or FALSE", lc->str);
return false;
}
Expand Down
73 changes: 53 additions & 20 deletions src/lib/ini.h
Expand Up @@ -48,7 +48,9 @@
class ConfigFile;
struct ini_items;

/* Used to store result */
/*
* Used to store result
*/
typedef union {
char *strval;
char nameval[MAX_NAME_LENGTH];
Expand All @@ -58,12 +60,15 @@ typedef union {
bool boolval;
} item_value;

/* These functions are used to convert a string to the appropriate value */
/*
* These functions are used to convert a string to the appropriate value
*/
typedef
bool (INI_ITEM_HANDLER)(LEX *lc, ConfigFile *inifile,
struct ini_items *item);

/* If no items are registred at the scan time, we detect this list from
/*
* If no items are registred at the scan time, we detect this list from
* the file itself
*/
struct ini_items {
Expand All @@ -80,15 +85,18 @@ struct ini_items {
item_value val; /* val contains the value */
};

/* When reading a ini file, we limit the number of items that we
* can create
/*
* When reading a ini file, we limit the number of items that we can create
*/
#define MAX_INI_ITEMS 32

/* Special RestoreObject name used to get user input at restore time */
/*
* Special RestoreObject name used to get user input at restore time
*/
#define INI_RESTORE_OBJECT_NAME "RestoreOptions"

/* Can be used to set re_value, in_value, default_value, found and val to 0
/*
* Can be used to set re_value, in_value, default_value, found and val to 0
* G++ looks to allow partial declaration, let see with an other compiler
*/
#define ITEMS_DEFAULT NULL,NULL,NULL,0,{0}
Expand Down Expand Up @@ -142,35 +150,52 @@ class ConfigFile
free_items();
}

/* Dump a config string to out_fname */
/*
* Dump a config string to out_fname
*/
bool dump_string(const char *buf, int32_t len);

/* JCR needed for Jmsg */
/*
* JCR needed for Jmsg
*/
void set_jcr(JCR *ajcr) {
jcr = ajcr;
}

/* Free malloced items such as char* or alist or items */
/*
* Free malloced items such as char* or alist or items
*/
void free_items();

/* Clear items member */
/*
* Clear items member
*/
void clear_items();

/* Dump the item table to a file (used on plugin side) */
/*
* Dump the item table to a file (used on plugin side)
*/
bool serialize(const char *fname);

/* Dump the item table format to a buffer (used on plugin side)
/*
* Dump the item table format to a buffer (used on plugin side)
* returns the length of the buffer, -1 if error
*/
int serialize(POOLMEM **buf);

/* Dump the item table content to a buffer */
/*
* Dump the item table content to a buffer
*/
int dump_results(POOLMEM **buf);

/* Get item position in items list (useful when dynamic) */
/*
* Get item position in items list (useful when dynamic)
*/
int get_item(const char *name);

/* Register config file structure, if size doesn't match */
/*
* Register config file structure, if size doesn't match
*/
bool register_items(struct ini_items *aitems, int size) {
int i;
if (sizeof_ini_items == size) {
Expand All @@ -183,10 +208,14 @@ class ConfigFile
return false;
}

/* Parse a ini file with a item list previously registred (plugin side) */
/*
* Parse a ini file with a item list previously registred (plugin side)
*/
bool parse(const char *filename);

/* Create a item list from a ini file (director side) */
/*
* Create a item list from a ini file (director side)
*/
bool unserialize(const char *filename);
};

Expand All @@ -204,10 +233,14 @@ bool ini_store_pint32(LEX *lc, ConfigFile *inifile, ini_items *item);
bool ini_store_int32(LEX *lc, ConfigFile *inifile, ini_items *item);
bool ini_store_bool(LEX *lc, ConfigFile *inifile, ini_items *item);

/* Get handler code from handler @ */
/*
* Get handler code from handler @
*/
const char *ini_get_store_code(INI_ITEM_HANDLER *handler);

/* Get handler function from handler name */
/*
* Get handler function from handler name
*/
INI_ITEM_HANDLER *ini_get_store_handler(const char *);

#endif

0 comments on commit 0bf5735

Please sign in to comment.