parser-helper: Make parser_helper_file_t private
authorTobias Brunner <tobias@strongswan.org>
Wed, 14 May 2014 15:21:04 +0000 (17:21 +0200)
committerTobias Brunner <tobias@strongswan.org>
Thu, 15 May 2014 09:28:10 +0000 (11:28 +0200)
src/libstrongswan/settings/settings_lexer.l
src/libstrongswan/utils/parser_helper.c
src/libstrongswan/utils/parser_helper.h

index 5074e70..a680e71 100644 (file)
@@ -163,7 +163,7 @@ static void include_files(parser_helper_t *ctx);
  */
 bool settings_parser_open_next_file(parser_helper_t *ctx)
 {
-       parser_helper_file_t *file;
+       FILE *file;
 
        file = ctx->file_next(ctx);
        if (!file)
@@ -171,9 +171,9 @@ bool settings_parser_open_next_file(parser_helper_t *ctx)
                return FALSE;
        }
 
-       settings_parser_set_in(file->file, ctx->scanner);
+       settings_parser_set_in(file, ctx->scanner);
        settings_parser_push_buffer_state(
-                       settings_parser__create_buffer(file->file, YY_BUF_SIZE,
+                       settings_parser__create_buffer(file, YY_BUF_SIZE,
                                                                                   ctx->scanner), ctx->scanner);
        return TRUE;
 }
index 403cca9..40c6cfe 100644 (file)
@@ -22,7 +22,7 @@
 #include <collections/array.h>
 
 typedef struct private_parser_helper_t private_parser_helper_t;
-typedef struct private_parser_helper_file_t private_parser_helper_file_t;
+typedef struct parser_helper_file_t parser_helper_file_t;
 
 struct private_parser_helper_t {
 
@@ -32,7 +32,7 @@ struct private_parser_helper_t {
        parser_helper_t public;
 
        /**
-        * Stack of included files, as private_parser_helper_file_t.
+        * Stack of included files, as parser_helper_file_t.
         */
        array_t *files;
 
@@ -42,12 +42,17 @@ struct private_parser_helper_t {
        bio_writer_t *writer;
 };
 
-struct private_parser_helper_file_t {
+struct parser_helper_file_t {
 
        /**
-        * File data.
+        * File name
         */
-       parser_helper_file_t public;
+       char *name;
+
+       /**
+        * File stream
+        */
+       FILE *file;
 
        /**
         * Enumerator of paths matching the most recent inclusion pattern.
@@ -58,34 +63,36 @@ struct private_parser_helper_file_t {
 /**
  * Destroy the given file data.
  */
-static void parser_helper_file_destroy(private_parser_helper_file_t *this)
+static void parser_helper_file_destroy(parser_helper_file_t *this)
 {
-       if (this->public.file)
+       if (this->file)
        {
-               fclose(this->public.file);
+               fclose(this->file);
        }
-       free(this->public.name);
+       free(this->name);
        DESTROY_IF(this->matches);
        free(this);
 }
 
-METHOD(parser_helper_t, file_current, parser_helper_file_t*,
-       private_parser_helper_t *this)
+/**
+ * Returns the current file, if any.
+ */
+static parser_helper_file_t *current_file(private_parser_helper_t *this)
 {
-       private_parser_helper_file_t *file;
+       parser_helper_file_t *file;
 
        array_get(this->files, ARRAY_TAIL, &file);
-       if (file->public.name)
+       if (file->name)
        {
-               return &file->public;
+               return file;
        }
        return NULL;
 }
 
-METHOD(parser_helper_t, file_next, parser_helper_file_t*,
+METHOD(parser_helper_t, file_next, FILE*,
        private_parser_helper_t *this)
 {
-       private_parser_helper_file_t *file, *next;
+       parser_helper_file_t *file, *next;
        char *name;
 
        array_get(this->files, ARRAY_TAIL, &file);
@@ -101,16 +108,14 @@ METHOD(parser_helper_t, file_next, parser_helper_file_t*,
                while (file->matches->enumerate(file->matches, &name, NULL))
                {
                        INIT(next,
-                               .public = {
-                                       .name = strdup(name),
-                                       .file = fopen(name, "r"),
-                               },
+                               .name = strdup(name),
+                               .file = fopen(name, "r"),
                        );
 
-                       if (next->public.file)
+                       if (next->file)
                        {
                                array_insert(this->files, ARRAY_TAIL, next);
-                               return &next->public;
+                               return next->file;
                        }
                        PARSER_DBG2(&this->public, "unable to open '%s'", name);
                        parser_helper_file_destroy(next);
@@ -124,7 +129,7 @@ METHOD(parser_helper_t, file_next, parser_helper_file_t*,
 METHOD(parser_helper_t, file_include, void,
        private_parser_helper_t *this, char *pattern)
 {
-       private_parser_helper_file_t *file;
+       parser_helper_file_t *file;
        char pat[PATH_MAX];
 
        array_get(this->files, ARRAY_TAIL, &file);
@@ -135,7 +140,7 @@ METHOD(parser_helper_t, file_include, void,
                return;
        }
 
-       if (!file->public.name || pattern[0] == '/')
+       if (!file->name || pattern[0] == '/')
        {       /* absolute path */
                if (snprintf(pat, sizeof(pat), "%s", pattern) >= sizeof(pat))
                {
@@ -146,7 +151,7 @@ METHOD(parser_helper_t, file_include, void,
        }
        else
        {       /* base relative paths to the directory of the current file */
-               char *dir = path_dirname(file->public.name);
+               char *dir = path_dirname(file->name);
                if (snprintf(pat, sizeof(pat), "%s/%s", dir, pattern) >= sizeof(pat))
                {
                        PARSER_DBG1(&this->public, "include pattern too long, ignored");
@@ -202,6 +207,7 @@ METHOD(parser_helper_t, destroy, void,
  */
 void parser_helper_log(int level, parser_helper_t *ctx, char *fmt, ...)
 {
+       private_parser_helper_t *this = (private_parser_helper_t*)ctx;
        parser_helper_file_t *file;
        char msg[8192];
        va_list args;
@@ -211,7 +217,7 @@ void parser_helper_log(int level, parser_helper_t *ctx, char *fmt, ...)
        vsnprintf(msg, sizeof(msg), fmt, args);
        va_end(args);
 
-       file = ctx->file_current(ctx);
+       file = current_file(this);
        line = ctx->get_lineno ? ctx->get_lineno(ctx->scanner) : 0;
        if (file)
        {
@@ -229,12 +235,11 @@ void parser_helper_log(int level, parser_helper_t *ctx, char *fmt, ...)
 parser_helper_t *parser_helper_create(void *context)
 {
        private_parser_helper_t *this;
-       private_parser_helper_file_t *sentinel;
+       parser_helper_file_t *sentinel;
 
        INIT(this,
                .public = {
                        .context = context,
-                       .file_current = _file_current,
                        .file_include = _file_include,
                        .file_next = _file_next,
                        .string_init = _string_init,
@@ -247,9 +252,7 @@ parser_helper_t *parser_helper_create(void *context)
        );
 
        INIT(sentinel,
-               .public = {
-                       .name = NULL,
-               },
+               .name = NULL,
        );
        array_insert(this->files, ARRAY_TAIL, sentinel);
 
index 6a2c26c..d860252 100644 (file)
@@ -25,7 +25,6 @@
 #include <bio/bio_writer.h>
 
 typedef struct parser_helper_t parser_helper_t;
-typedef struct parser_helper_file_t parser_helper_file_t;
 
 /**
  * Helper class for flex/bison based parsers.
@@ -59,13 +58,6 @@ struct parser_helper_t {
        int (*get_lineno)(void *scanner);
 
        /**
-        * Get the current file.
-        *
-        * @return                      current file, or NULL
-        */
-       parser_helper_file_t *(*file_current)(parser_helper_t *this);
-
-       /**
         * Resolves the given include pattern, relative to the location of the
         * current file.
         *
@@ -89,8 +81,8 @@ struct parser_helper_t {
         * then call this method to check if there are more files to include for
         * the most recent call to file_include(), if so, call
         * @code
-        * PREFIXset_in(file->file, helper->scanner);
-        * PREFIXpush_buffer_state(PREFIX_create_buffer(file->file, YY_BUF_SIZE,
+        * PREFIXset_in(file, helper->scanner);
+        * PREFIXpush_buffer_state(PREFIX_create_buffer(file, YY_BUF_SIZE,
         *                                              helper->scanner), helper->scanner);
         * @endcode
         *
@@ -99,7 +91,7 @@ struct parser_helper_t {
         *
         * @return                      next file to process, or NULL (see comment)
         */
-       parser_helper_file_t *(*file_next)(parser_helper_t *this);
+       FILE *(*file_next)(parser_helper_t *this);
 
        /**
         * Start parsing a string, discards any currently stored data.
@@ -127,19 +119,6 @@ struct parser_helper_t {
        void (*destroy)(parser_helper_t *this);
 };
 
-struct parser_helper_file_t {
-
-       /**
-        * File name
-        */
-       char *name;
-
-       /**
-        * File stream
-        */
-       FILE *file;
-};
-
 /**
  * Log the given message either as error or warning
  *