#include <pakfire/private.h>
#include <pakfire/util.h>
-#define NUM_DECLARATIONS 1024
#define VARIABLE_PATTERN "%\\{([A-Za-z0-9_\\-]+)\\}"
struct _PakfireParser {
char* namespace;
- struct pakfire_parser_declaration* declarations[NUM_DECLARATIONS];
- size_t next_declaration;
+ struct pakfire_parser_declaration** declarations;
size_t num_declarations;
};
// Make namespace
parser->namespace = pakfire_parser_make_namespace(parent, namespace);
- parser->num_declarations =
- sizeof(parser->declarations) / sizeof(*parser->declarations);
- parser->next_declaration = 0;
-
DEBUG(pakfire, "Allocated new parser at %p (%s, %p)\n",
parser, parser->namespace, parser->parent);
}
}
static void pakfire_parser_free_declarations(PakfireParser parser) {
+ if (!parser->declarations)
+ return;
+
for (unsigned int i = 0; i < parser->num_declarations; i++) {
struct pakfire_parser_declaration* d = parser->declarations[i];
- // If we hit NULL, this is the end
- if (!d)
- break;
-
// Free everything
if (d->name)
free(d->name);
free(d->value);
free(d);
}
+
+ free(parser->declarations);
}
static void pakfire_parser_free(PakfireParser parser) {
return 0;
}
- // Check if we have any space left
- if (parser->next_declaration >= parser->num_declarations) {
- ERROR(parser->pakfire, "No free declarations left\n");
- return -1;
- }
-
// Allocate a new declaration
d = calloc(1, sizeof(*d));
if (!d)
DEBUG(parser->pakfire, "New declaration: %s = %s\n", d->name, d->value);
// Assign new declaration to array
- parser->declarations[parser->next_declaration++] = d;
+ parser->declarations = reallocarray(parser->declarations,
+ sizeof(*parser->declarations), parser->num_declarations + 1);
+ parser->declarations[parser->num_declarations++] = d;
return 0;
}