Move format code to options and cleanup

This commit is contained in:
Olof hagsand 2024-04-16 12:05:36 +02:00
parent 1be158b7be
commit aba5c68fe2
6 changed files with 67 additions and 62 deletions

View file

@ -97,6 +97,8 @@ enum regexp_mode{
/*
* Prototypes
*/
char *format_int2str(enum format_enum showas);
enum format_enum format_str2int(char *str);
/* Debug dump config options */
int clicon_option_dump(clixon_handle h, int dblevel);

View file

@ -56,8 +56,6 @@ struct clicon_msg {
/*
* Prototypes
*/
char *format_int2str(enum format_enum showas);
enum format_enum format_str2int(char *str);
int clixon_inet2sin(const char *addrtype, const char *addrstr, uint16_t port, struct sockaddr *sa, size_t *sa_len);
struct clicon_msg *clicon_msg_encode(uint32_t id, const char *format, ...) __attribute__ ((format (printf, 2, 3)));

View file

@ -846,11 +846,12 @@ xmldb_dump(clixon_handle h,
cxobj *xt,
withdefaults_type wdef)
{
int retval = -1;
cxobj *xm;
cxobj *xmodst = NULL;
char *format;
int pretty;
int retval = -1;
cxobj *xm;
cxobj *xmodst = NULL;
char *formatstr;
enum format_enum format = FORMAT_XML;
int pretty;
/* Add modstate first */
if ((xm = clicon_modst_cache_get(h, 1)) != NULL){
@ -860,17 +861,27 @@ xmldb_dump(clixon_handle h,
goto done;
xml_parent_set(xmodst, xt);
}
if ((format = clicon_option_str(h, "CLICON_XMLDB_FORMAT")) == NULL){
clixon_err(OE_CFG, ENOENT, "No CLICON_XMLDB_FORMAT");
goto done;
}
pretty = clicon_option_bool(h, "CLICON_XMLDB_PRETTY");
if (strcmp(format,"json")==0){
if ((formatstr = clicon_option_str(h, "CLICON_XMLDB_FORMAT")) != NULL){
if ((format = format_str2int(formatstr)) < 0){
clixon_err(OE_XML, 0, "Format %s invalid", formatstr);
goto done;
}
}
switch (format){
case FORMAT_JSON:
if (clixon_json2file(f, xt, pretty, fprintf, 0, 0) < 0)
goto done;
}
else if (clixon_xml2file1(f, xt, 0, pretty, NULL, fprintf, 0, 0, wdef) < 0)
break;
case FORMAT_XML:
if (clixon_xml2file1(f, xt, 0, pretty, NULL, fprintf, 0, 0, wdef) < 0)
goto done;
break;
default:
clixon_err(OE_XML, 0, "Format %s not supported", format_int2str(format));
goto done;
break;
}
/* Remove modules state after writing to file */
if (xmodst && xml_purge(xmodst) < 0)
goto done;
@ -912,10 +923,6 @@ xmldb_write_cache2file(clixon_handle h,
}
if (xmldb_dump(h, f, xt, WITHDEFAULTS_EXPLICIT) < 0)
goto done;
if (f) {
fclose(f);
f = NULL;
}
retval = 0;
done:
if (dbfile)
@ -924,4 +931,3 @@ xmldb_write_cache2file(clixon_handle h,
fclose(f);
return retval;
}

View file

@ -88,12 +88,11 @@ clicon_files_recursive1(const char *dir,
regex_t *re,
cvec *cvv)
{
int retval = -1;
struct dirent *dent = NULL;
char path[MAXPATHLEN];
DIR *dirp = NULL;
int res = 0;
struct stat st;
int retval = -1;
struct dirent *dent = NULL;
char path[MAXPATHLEN];
DIR *dirp = NULL;
struct stat st = {0,};
if (dir == NULL){
clixon_err(OE_UNIX, EINVAL, "Requires dir != NULL");
@ -119,7 +118,7 @@ clicon_files_recursive1(const char *dir,
regexec(re, dent->d_name, (size_t)0, NULL, 0) != 0)
continue;
snprintf(path, MAXPATHLEN-1, "%s/%s", dir, dent->d_name);
if ((res = lstat(path, &st)) != 0){
if (lstat(path, &st) < 0){
clixon_err(OE_UNIX, errno, "lstat");
goto done;
}

View file

@ -125,6 +125,42 @@ static const map_str2int yang_regexp_map[] = {
{NULL, -1}
};
/*! Translate between int and string of tree formats
*
* @see enum format_enum
*/
static const map_str2int _FORMATS[] = {
{"xml", FORMAT_XML},
{"text", FORMAT_TEXT},
{"json", FORMAT_JSON},
{"cli", FORMAT_CLI},
{"netconf", FORMAT_NETCONF},
{"default", FORMAT_DEFAULT},
{NULL, -1}
};
/*! Translate from numeric format to string representation
*
* @param[in] showas Format value (see enum format_enum)
* @retval str String value
*/
char *
format_int2str(enum format_enum showas)
{
return (char*)clicon_int2str(_FORMATS, showas);
}
/*! Translate from string to numeric format representation
*
* @param[in] str String value
* @retval enum Format value (see enum format_enum)
*/
enum format_enum
format_str2int(char *str)
{
return clicon_str2int(_FORMATS, str);
}
/*! Debug dump config options
*
* @param[in] h Clixon handle

View file

@ -84,42 +84,6 @@
static int _atomicio_sig = 0;
/*! Translate between int and string of tree formats
*
* @see enum format_enum
*/
static const map_str2int _FORMATS[] = {
{"xml", FORMAT_XML},
{"text", FORMAT_TEXT},
{"json", FORMAT_JSON},
{"cli", FORMAT_CLI},
{"netconf", FORMAT_NETCONF},
{"default", FORMAT_DEFAULT},
{NULL, -1}
};
/*! Translate from numeric format to string representation
*
* @param[in] showas Format value (see enum format_enum)
* @retval str String value
*/
char *
format_int2str(enum format_enum showas)
{
return (char*)clicon_int2str(_FORMATS, showas);
}
/*! Translate from string to numeric format representation
*
* @param[in] str String value
* @retval enum Format value (see enum format_enum)
*/
enum format_enum
format_str2int(char *str)
{
return clicon_str2int(_FORMATS, str);
}
/*! Given family, addr str, port, return sockaddr and length
*
* @param[in] addrtype Address family: inet:ipv4-address or inet:ipv6-address