return r;
}
+int pakfire_config_parse(struct pakfire_config* config, const char* s, ssize_t length) {
+ FILE* f = NULL;
+ int r;
+
+ // Check inputs
+ if (!s)
+ return -EINVAL;
+
+ // Automatically determine the length
+ if (length < 0)
+ length = strlen(s);
+
+ // Open a memory stream
+ f = fmemopen((char*)s, length, "r");
+ if (!f)
+ return -errno;
+
+ // Read the buffer
+ r = pakfire_config_read(config, f);
+
+ // Close the buffer
+ fclose(f);
+
+ return r;
+}
+
static int pakfire_config_dump_multiline_entry(struct pakfire_config_entry* entry, FILE* f) {
char* value = NULL;
char* p = NULL;
int pakfire_config_has_section(struct pakfire_config* config, const char* section);
int pakfire_config_read(struct pakfire_config* config, FILE* f);
+int pakfire_config_parse(struct pakfire_config* config, const char* s, ssize_t length);
int pakfire_config_dump(struct pakfire_config* config, FILE* f);
FILE* pakfire_config_fdump(struct pakfire_config* config);
}
static int test_parse(const struct test* t) {
+ struct pakfire_config* config = NULL;
int r = EXIT_FAILURE;
- char* TEST_INPUT =
+
+ const char* TEST_INPUT =
"key1 = value1\n"
"\n"
"# This is a comment\n"
"key1k = 1k\n"
"key1X = 1X\n";
- FILE* f = fmemopen(TEST_INPUT, strlen(TEST_INPUT), "r");
- ASSERT(f);
-
- struct pakfire_config* config = NULL;
-
ASSERT_SUCCESS(pakfire_config_create(&config));
- ASSERT_SUCCESS(pakfire_config_read(config, f));
+ ASSERT_SUCCESS(pakfire_config_parse(config, TEST_INPUT, -1));
// Dump the configuration file
ASSERT_SUCCESS(pakfire_config_dump(config, stdout));
r = EXIT_SUCCESS;
FAIL:
- if (f)
- fclose(f);
if (config)
pakfire_config_unref(config);
}
static int test_parse_multiline(const struct test* t) {
+ struct pakfire_config* config = NULL;
int r = EXIT_FAILURE;
- char* TEST_INPUT =
+ const char* TEST_INPUT =
"key1 =\n"
" value1\n"
" value2\n"
"\n"
"key2 = value1\n";
- FILE* f = fmemopen(TEST_INPUT, strlen(TEST_INPUT), "r");
- ASSERT(f);
-
- struct pakfire_config* config = NULL;
-
ASSERT_SUCCESS(pakfire_config_create(&config));
- ASSERT_SUCCESS(pakfire_config_read(config, f));
+ ASSERT_SUCCESS(pakfire_config_parse(config, TEST_INPUT, -1));
// Dump the configuration file
ASSERT_SUCCESS(pakfire_config_dump(config, stdout));
r = EXIT_SUCCESS;
FAIL:
- if (f)
- fclose(f);
if (config)
pakfire_config_unref(config);