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 * Prototypes
*/ */
char *format_int2str(enum format_enum showas);
enum format_enum format_str2int(char *str);
/* Debug dump config options */ /* Debug dump config options */
int clicon_option_dump(clixon_handle h, int dblevel); int clicon_option_dump(clixon_handle h, int dblevel);

View file

@ -56,8 +56,6 @@ struct clicon_msg {
/* /*
* Prototypes * 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); 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))); struct clicon_msg *clicon_msg_encode(uint32_t id, const char *format, ...) __attribute__ ((format (printf, 2, 3)));

View file

@ -849,7 +849,8 @@ xmldb_dump(clixon_handle h,
int retval = -1; int retval = -1;
cxobj *xm; cxobj *xm;
cxobj *xmodst = NULL; cxobj *xmodst = NULL;
char *format; char *formatstr;
enum format_enum format = FORMAT_XML;
int pretty; int pretty;
/* Add modstate first */ /* Add modstate first */
@ -860,17 +861,27 @@ xmldb_dump(clixon_handle h,
goto done; goto done;
xml_parent_set(xmodst, xt); xml_parent_set(xmodst, xt);
} }
if ((format = clicon_option_str(h, "CLICON_XMLDB_FORMAT")) == NULL){ pretty = clicon_option_bool(h, "CLICON_XMLDB_PRETTY");
clixon_err(OE_CFG, ENOENT, "No CLICON_XMLDB_FORMAT"); 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; goto done;
} }
pretty = clicon_option_bool(h, "CLICON_XMLDB_PRETTY"); }
if (strcmp(format,"json")==0){ switch (format){
case FORMAT_JSON:
if (clixon_json2file(f, xt, pretty, fprintf, 0, 0) < 0) if (clixon_json2file(f, xt, pretty, fprintf, 0, 0) < 0)
goto done; goto done;
} break;
else if (clixon_xml2file1(f, xt, 0, pretty, NULL, fprintf, 0, 0, wdef) < 0) case FORMAT_XML:
if (clixon_xml2file1(f, xt, 0, pretty, NULL, fprintf, 0, 0, wdef) < 0)
goto done; 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 */ /* Remove modules state after writing to file */
if (xmodst && xml_purge(xmodst) < 0) if (xmodst && xml_purge(xmodst) < 0)
goto done; goto done;
@ -912,10 +923,6 @@ xmldb_write_cache2file(clixon_handle h,
} }
if (xmldb_dump(h, f, xt, WITHDEFAULTS_EXPLICIT) < 0) if (xmldb_dump(h, f, xt, WITHDEFAULTS_EXPLICIT) < 0)
goto done; goto done;
if (f) {
fclose(f);
f = NULL;
}
retval = 0; retval = 0;
done: done:
if (dbfile) if (dbfile)
@ -924,4 +931,3 @@ xmldb_write_cache2file(clixon_handle h,
fclose(f); fclose(f);
return retval; return retval;
} }

View file

@ -92,8 +92,7 @@ clicon_files_recursive1(const char *dir,
struct dirent *dent = NULL; struct dirent *dent = NULL;
char path[MAXPATHLEN]; char path[MAXPATHLEN];
DIR *dirp = NULL; DIR *dirp = NULL;
int res = 0; struct stat st = {0,};
struct stat st;
if (dir == NULL){ if (dir == NULL){
clixon_err(OE_UNIX, EINVAL, "Requires 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) regexec(re, dent->d_name, (size_t)0, NULL, 0) != 0)
continue; continue;
snprintf(path, MAXPATHLEN-1, "%s/%s", dir, dent->d_name); 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"); clixon_err(OE_UNIX, errno, "lstat");
goto done; goto done;
} }

View file

@ -125,6 +125,42 @@ static const map_str2int yang_regexp_map[] = {
{NULL, -1} {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 /*! Debug dump config options
* *
* @param[in] h Clixon handle * @param[in] h Clixon handle

View file

@ -84,42 +84,6 @@
static int _atomicio_sig = 0; 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 /*! Given family, addr str, port, return sockaddr and length
* *
* @param[in] addrtype Address family: inet:ipv4-address or inet:ipv6-address * @param[in] addrtype Address family: inet:ipv4-address or inet:ipv6-address