* Changed first parameter from int fd to FILE *f in the following functions:
* clixon_xml_parse_file(), clixon_json_parse_file(), yang_parse_file() * See [Bytewise read() of files is slow #146](https://github.com/clicon/clixon/issues/146)
This commit is contained in:
parent
7a0838da3a
commit
c31b1c471b
21 changed files with 113 additions and 99 deletions
|
|
@ -250,14 +250,14 @@ main(int argc, char **argv)
|
|||
usage(argv0);
|
||||
}
|
||||
if (argc == 2){
|
||||
int fd;
|
||||
if ((fd = open(xmlfilename, O_RDONLY)) < 0){
|
||||
clicon_err(OE_UNIX, errno, "open(%s)", xmlfilename);
|
||||
FILE *fp;
|
||||
if ((fp = fopen(xmlfilename, "r")) < 0){
|
||||
clicon_err(OE_UNIX, errno, "fopen(%s)", xmlfilename);
|
||||
goto done;
|
||||
}
|
||||
if (clixon_xml_parse_file(fd, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
|
||||
if (clixon_xml_parse_file(fp, YB_MODULE, yspec, NULL, &xt, NULL) < 0)
|
||||
goto done;
|
||||
close(fd);
|
||||
fclose(fp);
|
||||
}
|
||||
else
|
||||
if (clixon_xml_parse_string(argv[2], YB_MODULE, yspec, &xt, NULL) < 0)
|
||||
|
|
|
|||
|
|
@ -137,7 +137,7 @@ main(int argc,
|
|||
return -1;
|
||||
}
|
||||
}
|
||||
if ((ret = clixon_json_parse_file(0, yspec?YB_MODULE:YB_NONE, yspec, &xt, &xerr)) < 0)
|
||||
if ((ret = clixon_json_parse_file(stdin, yspec?YB_MODULE:YB_NONE, yspec, &xt, &xerr)) < 0)
|
||||
goto done;
|
||||
if (ret == 0){
|
||||
xml_print(stderr, xerr);
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ main(int argc,
|
|||
int len;
|
||||
char *buf = NULL;
|
||||
int ret;
|
||||
int fd = 0; /* unless overriden by argv[1] */
|
||||
FILE *fp = stdin; /* unless overriden by argv[1] */
|
||||
char *yang_file_dir = NULL;
|
||||
yang_stmt *yspec = NULL;
|
||||
char *path = NULL;
|
||||
|
|
@ -135,8 +135,8 @@ main(int argc,
|
|||
break;
|
||||
case 'f': /* XML file */
|
||||
filename = optarg;
|
||||
if ((fd = open(filename, O_RDONLY)) < 0){
|
||||
clicon_err(OE_UNIX, errno, "open(%s)", optarg);
|
||||
if ((fp = fopen(filename, "r")) < 0){
|
||||
clicon_err(OE_UNIX, errno, "fopen(%s)", optarg);
|
||||
goto done;
|
||||
}
|
||||
break;
|
||||
|
|
@ -212,10 +212,10 @@ main(int argc,
|
|||
}
|
||||
|
||||
/*
|
||||
* If fd=0, then continue reading from stdin (after CR)
|
||||
* If fd>0, reading from file opened as argv[1]
|
||||
* If fp=stdin, then continue reading from stdin (after CR)
|
||||
* XXX Note 0 above, stdin here
|
||||
*/
|
||||
if (clixon_xml_parse_file(fd, YB_NONE, NULL, NULL, &x, NULL) < 0){
|
||||
if (clixon_xml_parse_file(fp, YB_NONE, NULL, NULL, &x, NULL) < 0){
|
||||
fprintf(stderr, "Error: parsing: %s\n", clicon_err_reason);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -297,8 +297,8 @@ main(int argc,
|
|||
xml_free(x);
|
||||
if (xcfg)
|
||||
xml_free(xcfg);
|
||||
if (fd > 0)
|
||||
close(fd);
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
if (h)
|
||||
clicon_handle_exit(h);
|
||||
return retval;
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ main(int argc,
|
|||
char *retdata = NULL;
|
||||
int jsonin = 0;
|
||||
char *input_filename = NULL;
|
||||
int fd = 0; /* stdin */
|
||||
FILE *fp = stdin;
|
||||
cxobj *xt = NULL;
|
||||
cxobj *xc;
|
||||
cxobj *xerr = NULL;
|
||||
|
|
@ -138,14 +138,14 @@ main(int argc,
|
|||
usage(argv[0]);
|
||||
}
|
||||
if (input_filename){
|
||||
if ((fd = open(input_filename, O_RDONLY)) < 0){
|
||||
if ((fp = fopen(input_filename, "r")) < 0){
|
||||
clicon_err(OE_YANG, errno, "open(%s)", input_filename);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
/* 2. Parse data (xml/json) */
|
||||
if (jsonin){
|
||||
if ((ret = clixon_json_parse_file(fd, YB_NONE, NULL, &xt, &xerr)) < 0)
|
||||
if ((ret = clixon_json_parse_file(fp, YB_NONE, NULL, &xt, &xerr)) < 0)
|
||||
goto done;
|
||||
if (ret == 0){
|
||||
fprintf(stderr, "Invalid JSON\n");
|
||||
|
|
@ -153,7 +153,7 @@ main(int argc,
|
|||
}
|
||||
}
|
||||
else{
|
||||
if (clixon_xml_parse_file(fd, YB_NONE, NULL, NULL, &xt, NULL) < 0){
|
||||
if (clixon_xml_parse_file(fp, YB_NONE, NULL, NULL, &xt, NULL) < 0){
|
||||
fprintf(stderr, "xml parse error: %s\n", clicon_err_reason);
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -176,6 +176,8 @@ main(int argc,
|
|||
fprintf(stdout, "%s\n", retdata);
|
||||
retval = 0;
|
||||
done:
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
if (xerr)
|
||||
xml_free(xerr);
|
||||
if (xt)
|
||||
|
|
|
|||
|
|
@ -154,8 +154,8 @@ main(int argc,
|
|||
int output = 0;
|
||||
clicon_handle h;
|
||||
struct stat st;
|
||||
int fd = 0; /* base file, stdin */
|
||||
int tfd = -1; /* top file */
|
||||
FILE *fp = stdin; /* base file, stdin */
|
||||
FILE *tfp = NULL; /* top file */
|
||||
cxobj *xcfg = NULL;
|
||||
cbuf *cbret = NULL;
|
||||
cxobj *xtop = NULL; /* Top tree if any */
|
||||
|
|
@ -263,11 +263,11 @@ main(int argc,
|
|||
* Always validated
|
||||
*/
|
||||
if (top_input_filename){
|
||||
if ((tfd = open(top_input_filename, O_RDONLY)) < 0){
|
||||
clicon_err(OE_YANG, errno, "open(%s)", top_input_filename);
|
||||
if ((tfp = fopen(top_input_filename, "r")) < 0){
|
||||
clicon_err(OE_YANG, errno, "fopen(%s)", top_input_filename);
|
||||
goto done;
|
||||
}
|
||||
if ((ret = clixon_xml_parse_file(tfd, YB_MODULE, yspec, NULL, &xtop, &xerr)) < 0){
|
||||
if ((ret = clixon_xml_parse_file(tfp, YB_MODULE, yspec, NULL, &xtop, &xerr)) < 0){
|
||||
fprintf(stderr, "xml parse error: %s\n", clicon_err_reason);
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -288,14 +288,14 @@ main(int argc,
|
|||
xt = xbot;
|
||||
}
|
||||
if (input_filename){
|
||||
if ((fd = open(input_filename, O_RDONLY)) < 0){
|
||||
if ((fp = fopen(input_filename, "r")) < 0){
|
||||
clicon_err(OE_YANG, errno, "open(%s)", input_filename);
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
/* 2. Parse data (xml/json) */
|
||||
if (jsonin){
|
||||
if ((ret = clixon_json_parse_file(fd, top_input_filename?YB_PARENT:YB_MODULE, yspec, &xt, &xerr)) < 0)
|
||||
if ((ret = clixon_json_parse_file(fp, top_input_filename?YB_PARENT:YB_MODULE, yspec, &xt, &xerr)) < 0)
|
||||
goto done;
|
||||
if (ret == 0){
|
||||
clixon_netconf_error(xerr, "util_xml", NULL);
|
||||
|
|
@ -309,7 +309,7 @@ main(int argc,
|
|||
yb = YB_MODULE;
|
||||
else
|
||||
yb = YB_PARENT;
|
||||
if ((ret = clixon_xml_parse_file(fd, yb, yspec, NULL, &xt, &xerr)) < 0){
|
||||
if ((ret = clixon_xml_parse_file(fp, yb, yspec, NULL, &xt, &xerr)) < 0){
|
||||
fprintf(stderr, "xml parse error: %s\n", clicon_err_reason);
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -345,6 +345,10 @@ main(int argc,
|
|||
}
|
||||
retval = 0;
|
||||
done:
|
||||
if (tfp)
|
||||
fclose(tfp);
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
if (nsc)
|
||||
cvec_free(nsc);
|
||||
if (cbret)
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ main(int argc,
|
|||
int len;
|
||||
char *buf = NULL;
|
||||
int ret;
|
||||
int fd = 0; /* unless overriden by argv[1] */
|
||||
FILE *fp = stdin; /* unless overriden by argv[1] */
|
||||
char *yang_file_dir = NULL;
|
||||
yang_stmt *yspec = NULL;
|
||||
char *xpath = NULL;
|
||||
|
|
@ -168,7 +168,7 @@ main(int argc,
|
|||
break;
|
||||
case 'f': /* XML file */
|
||||
filename = optarg;
|
||||
if ((fd = open(filename, O_RDONLY)) < 0){
|
||||
if ((fp = fopen(filename, "r")) < 0){
|
||||
clicon_err(OE_UNIX, errno, "open(%s)", argv[1]);
|
||||
goto done;
|
||||
}
|
||||
|
|
@ -295,10 +295,10 @@ main(int argc,
|
|||
}
|
||||
|
||||
/*
|
||||
* If fd=0, then continue reading from stdin (after CR)
|
||||
* If fd>0, reading from file opened as argv[1]
|
||||
* If fp=stdin, then continue reading from stdin (after CR)
|
||||
* XXX Note 0 above, stdin here
|
||||
*/
|
||||
if (clixon_xml_parse_file(fd, YB_NONE, NULL, NULL, &x0, NULL) < 0){
|
||||
if (clixon_xml_parse_file(fp, YB_NONE, NULL, NULL, &x0, NULL) < 0){
|
||||
fprintf(stderr, "Error: parsing: %s\n", clicon_err_reason);
|
||||
return -1;
|
||||
}
|
||||
|
|
@ -375,7 +375,7 @@ main(int argc,
|
|||
free(buf);
|
||||
if (x0)
|
||||
xml_free(x0);
|
||||
if (fd > 0)
|
||||
close(fd);
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
return retval;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ main(int argc, char **argv)
|
|||
clicon_debug_init(dbg, NULL);
|
||||
if ((yspec = yspec_new()) == NULL)
|
||||
goto done;
|
||||
if (yang_parse_file(0, "yang test", yspec) == NULL){
|
||||
if (yang_parse_file(stdin, "yang test", yspec) == NULL){
|
||||
fprintf(stderr, "yang parse error %s\n", clicon_err_reason);
|
||||
return -1;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue