static void mmapAndParse(char *path, int expected) { int fd, ret; struct toml_node *root; void *m; struct stat st; toml_init(&root); fd = open(path, O_RDONLY); CU_ASSERT_FATAL(fd != -1); ret = fstat(fd, &st); CU_ASSERT_FATAL(ret != -1); m = mmap(NULL, st.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0); CU_ASSERT_FATAL(m != NULL); ret = toml_parse(root, m, st.st_size); CU_ASSERT(ret == expected); munmap(m, st.st_size); close(fd); toml_free(root); }
toml_table_t *tomltk_parse (const char *conf, int len, struct tomltk_error *error) { char errbuf[200]; char *cpy; toml_table_t *tab; if (len < 0 || (!conf && len != 0)) { errprintf (error, NULL, -1, "invalid argument"); errno = EINVAL; return NULL; } if (!(cpy = calloc (1, len + 1))) { errprintf (error, NULL, -1, "out of memory"); errno = ENOMEM; return NULL; } memcpy (cpy, conf, len); tab = toml_parse (cpy, errbuf, sizeof (errbuf)); free (cpy); if (!tab) { errfromtoml (error, NULL, errbuf); errno = EINVAL; return NULL; } return tab; }
static void testTypes(void) { int ret; struct toml_node *root; char *types = "list = [ 1, \"string\" ]\n"; toml_init(&root); ret = toml_parse(root, types, strlen(types)); CU_ASSERT(ret); toml_free(root); }
static void testFruit(void) { int ret; struct toml_node *root; char *fruit = "[fruit]\ntype = \"apple\"\n\n[fruit.type]\napple = \"yes\"\n"; toml_init(&root); ret = toml_parse(root, fruit, strlen(fruit)); CU_ASSERT(ret); toml_free(root); }
static void testHex(void) { int ret; struct toml_node *node; struct toml_node *root; char *string_with_hex = "string_with_hex = \"\\x4bfoo\"\n"; toml_init(&root); ret = toml_parse(root, string_with_hex, strlen(string_with_hex)); CU_ASSERT(ret == 0); node = toml_get(root, "string_with_hex"); CU_ASSERT(node != NULL); CU_ASSERT(node->type == TOML_STRING); CU_ASSERT(strcmp(node->value.string, "Kfoo") == 0); toml_free(root); }