Commit 2e4dbafd authored by Daniel Salzman's avatar Daniel Salzman

yparser: replace yp_create/yp_free with yp_init/yp_deinit

parent d94abbe5
......@@ -25,9 +25,13 @@
#include "libknot/internal/yparser/ypbody.h"
#include "libknot/errcode.h"
static void init_parser(
void yp_init(
yp_parser_t *parser)
{
if (parser == NULL) {
return;
}
memset(parser, 0, sizeof(*parser));
parser->cs = _start_state();
......@@ -35,10 +39,13 @@ static void init_parser(
parser->line_count = 1;
}
static void purge_parser(
void yp_deinit(
yp_parser_t *parser)
{
// Cleanup the current file context if any.
if (parser == NULL) {
return;
}
if (parser->file.descriptor != -1) {
munmap((void *)parser->input.start,
parser->input.end - parser->input.start);
......@@ -47,30 +54,6 @@ static void purge_parser(
}
}
yp_parser_t* yp_create(
void)
{
yp_parser_t *parser = malloc(sizeof(yp_parser_t));
if (parser == NULL) {
return NULL;
}
init_parser(parser);
return parser;
}
void yp_free(
yp_parser_t *parser)
{
if (parser == NULL) {
return;
}
purge_parser(parser);
free(parser);
}
int yp_set_input_string(
yp_parser_t *parser,
const char *input,
......@@ -80,9 +63,9 @@ int yp_set_input_string(
return KNOT_EINVAL;
}
// Initialize the parser.
purge_parser(parser);
init_parser(parser);
// Reinitialize the parser.
yp_deinit(parser);
yp_init(parser);
// Set the parser input limits.
parser->input.start = input;
......@@ -101,9 +84,9 @@ int yp_set_input_file(
return KNOT_EINVAL;
}
// Initialize the parser.
purge_parser(parser);
init_parser(parser);
// Reinitialize the parser.
yp_deinit(parser);
yp_init(parser);
// Try to open the file.
parser->file.descriptor = open(file_name, O_RDONLY);
......
......@@ -82,27 +82,27 @@ typedef struct {
} yp_parser_t;
/*!
* Allocates new parser.
* Initializes the parser.
*
* \return NULL if error.
* \param[in] parser Parser context.
*/
yp_parser_t* yp_create(
void
void yp_init(
yp_parser_t *parser
);
/*!
* Deallocates the parser.
* Deinitializes the parser.
*
* \param[in] parser Parser returned by #yp_create().
* \param[in] parser Parser context.
*/
void yp_free(
void yp_deinit(
yp_parser_t *parser
);
/*!
* Sets the parser to parse given string.
*
* \param[in] parser Parser returned by #yp_create().
* \param[in] parser Parser context.
* \param[in] input The string to parse.
* \param[in] size Length of the string.
*
......@@ -117,7 +117,7 @@ int yp_set_input_string(
/*!
* Sets the parser to parse given file.
*
* \param[in] parser Parser returned by #yp_create().
* \param[in] parser Parser context.
* \param[in] file_name The filename to parse.
*
* \return Error code, KNOT_EOK if success.
......@@ -133,7 +133,7 @@ int yp_set_input_file(
* If the item have more values, this function returns for each value. The item
* can also have no value.
*
* \param[in] parser Parser returned by #yp_create().
* \param[in] parser Parser context.
*
* \return Error code, KNOT_EOK if success, KNOT_EOF if end of data.
*/
......
......@@ -37,7 +37,7 @@ int _start_state(
/*!
* Executes the parser on the current input block.
*
* \param[in] parser A parser returned by #yp_create().
* \param[in] parser Parser context.
*/
int _parse(
yp_parser_t *parser
......
......@@ -242,7 +242,7 @@ yp_check_ctx_t* yp_scheme_check_init(
* If the item is correct, context also contains binary value of the item.
*
* \param[in,out] ctx New copy of the scheme.
* \param[in] parser Parser returned by #yp_create().
* \param[in] parser Parser context.
*
* \return Error code, KNOT_EOK if success.
*/
......
......@@ -77,8 +77,9 @@ int main(int argc, char *argv[])
int ret;
size_t line;
yp_parser_t *yp = yp_create();
ok(yp != NULL, "create yparser");
yp_parser_t yparser;
yp_parser_t *yp = &yparser;
yp_init(yp);
ret = yp_set_input_string(yp, syntax, strlen(syntax));
ok(ret == KNOT_EOK, "set input string");
......@@ -150,7 +151,7 @@ int main(int argc, char *argv[])
ret = yp_parse(yp);
ok(ret == KNOT_EOF, "parse EOF");
yp_free(yp);
yp_deinit(yp);
return 0;
}
......@@ -73,8 +73,9 @@ int main(int argc, char *argv[])
ret = yp_scheme_copy(&scheme, static_scheme);
ok(ret == KNOT_EOK, "scheme copy");
yp_parser_t *yp = yp_create();
ok(yp != NULL, "create yparser");
yp_parser_t yparser;
yp_parser_t *yp = &yparser;
yp_init(yp);
yp_check_ctx_t *ctx = yp_scheme_check_init(scheme);
ok(ctx != NULL, "create check ctx");
......@@ -175,7 +176,7 @@ int main(int argc, char *argv[])
ok(strcmp(i->name + 1, "string") == 0, "name check");
yp_scheme_check_deinit(ctx);
yp_free(yp);
yp_deinit(yp);
yp_scheme_free(scheme);
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment