return 0;
}
-static void pakfire_parser_free_declarations(struct pakfire_parser* parser) {
- if (!parser->declarations)
- return;
+static void pakfire_parser_free_declaration(struct pakfire_parser_declaration* d) {
+ if (d->value)
+ free(d->value);
+ free(d);
+}
- for (unsigned int i = 0; i < parser->num_declarations; i++) {
- struct pakfire_parser_declaration* d = parser->declarations[i];
+static void pakfire_parser_free_declarations(struct pakfire_parser* parser) {
+ if (parser->declarations) {
+ for (unsigned int i = 0; i < parser->num_declarations; i++)
+ pakfire_parser_free_declaration(parser->declarations[i]);
- // Free everything
- if (d->value)
- free(d->value);
- free(d);
+ free(parser->declarations);
+ parser->declarations = NULL;
}
-
- free(parser->declarations);
}
static void pakfire_parser_free(struct pakfire_parser* parser) {
int pakfire_parser_set(struct pakfire_parser* parser,
const char* namespace, const char* name, const char* value, int flags) {
+ struct pakfire_parser_declaration* d = NULL;
char* buffer = NULL;
+ int r;
+ // Check inputs
if (!name)
return -EINVAL;
namespace = "";
// Handle when name already exists
- struct pakfire_parser_declaration* d = pakfire_parser_get_declaration(parser, namespace, name);
+ d = pakfire_parser_get_declaration(parser, namespace, name);
if (d) {
+ // Append?
if (flags & PAKFIRE_PARSER_DECLARATION_APPEND) {
buffer = pakfire_parser_join(" ", d->value, value);
if (!buffer)
- return 1;
+ return -errno;
// Reset the append flag
flags &= ~PAKFIRE_PARSER_DECLARATION_APPEND;
+
+ // Copy?
} else {
buffer = strdup(value);
+ if (!buffer)
+ return -errno;
}
// Replace value
if (d->value)
free(d->value);
-
d->value = buffer;
// Update flags
if (flags)
d->flags = flags;
+#ifdef PAKFIRE_DEBUG_PARSER
DEBUG(parser->ctx, "%p: Updated declaration: %s.%s = %s\n",
parser, d->namespace, d->name, d->value);
+#endif /* PAKFIRE_DEBUG_PARSER */
// All done
return 0;
// Allocate a new declaration
d = calloc(1, sizeof(*d));
if (!d)
- return -1;
+ return -errno;
// Store namespace
- pakfire_string_set(d->namespace, namespace);
+ r = pakfire_string_set(d->namespace, namespace);
+ if (r < 0)
+ goto ERROR;
- // Import name & value
- pakfire_string_set(d->name, name);
- if (value)
+ // Import name
+ r = pakfire_string_set(d->name, name);
+ if (r < 0)
+ goto ERROR;
+
+ // Import value
+ if (value) {
d->value = strdup(value);
+ if (!d->value) {
+ r = -errno;
+ goto ERROR;
+ }
+ }
// Import flags
d->flags = flags;
+#ifdef PAKFIRE_DEBUG_PARSER
DEBUG(parser->ctx, "%p: New declaration (%u): %s.%s %s= %s\n",
parser,
d->flags,
d->name,
(d->flags & PAKFIRE_PARSER_DECLARATION_APPEND) ? "+" : "",
d->value);
+#endif /* PAKFIRE_DEBUG_PARSER */
- // Assign new declaration to array
+ // Grow the array
parser->declarations = reallocarray(parser->declarations,
- parser->num_declarations + 1, sizeof(*parser->declarations));
+ parser->num_declarations + 1, sizeof(*parser->declarations));
+ if (!parser->declarations) {
+ r = -errno;
+ goto ERROR;
+ }
+
+ // Store the declaration
parser->declarations[parser->num_declarations++] = d;
return 0;
+
+ERROR:
+ if (d)
+ pakfire_parser_free_declaration(d);
+
+ return r;
}
int pakfire_parser_apply_declaration(struct pakfire_parser* parser,