void element_print(scew_element const* element, FILE* out, unsigned int indent) { unsigned int closed = 0; XML_Char const* contents; scew_element* child = NULL; scew_attribute* attribute = NULL; if (element == NULL) { return; } indent_print(out, indent); scew_fprintf(out, _XT("<%s"), scew_element_name(element)); attribute = NULL; while ((attribute = scew_attribute_next(element, attribute)) != NULL) { attribute_print(attribute, out); } contents = scew_element_contents(element); if ((contents == NULL) && (element->child == NULL) && (element->parent != NULL)) { scew_fprintf(out, _XT("/>\n")); closed = 1; } else { scew_fprintf(out, _XT(">")); if (contents == NULL) { scew_fprintf(out, _XT("\n")); } } child = NULL; while ((child = scew_element_next(element, child)) != NULL) { element_print(child, out, indent + 1); } if (contents != NULL) { scew_fprintf(out, _XT("%s"), contents); } else if (!closed) { indent_print(out, indent); } if (!closed) { scew_fprintf(out, _XT("</%s>\n"), scew_element_name(element)); } }
int main(int argc,char *argv[]) { FILE *f=fopen(argv[1],"r"); // Parse XML file struct foo *b=(struct foo *)xml_parse(f,"root",&myschema,metafind(&myschema, "foo"),1); struct item *i; // Access values in foo printf("root->val = %d\n",b->val); printf("root->val_name = %s\n",b->val_name); printf("root->items->val = %d\n",b->items->val); printf("root->items->next->val = %d\n",b->items->next->val); printf("root->items->next->next->val = %d\n",b->items->next->next->val); // Print in various formats xml_print(stdout,"root",0,(struct base *)b); lisp_print(stdout,"root",0,(struct base *)b); lisp_print_untagged(stdout,"root",0,(struct base *)b); indent_print(stdout,"root",0,(struct base *)b); indent_print_untagged(stdout,"root",0,(struct base *)b); json_print(stdout,NULL,0,(struct base *)b,0); // Create a database within C b=mk(&myschema, "foo"); b->val=10; b->val_name=strdup("Hello"); // Build list i=b->items=mk(&myschema, "item"); i->val=7; i=i->next=mk(&myschema, "item"); i->val=8; i=i->next=mk(&myschema, "item"); i->val=9; // Print it xml_print(stdout,"root",0,(struct base *)b); return 0; }
/* Line is assumed to have been strtrim'ed */ static int _parse_pmoption(char *line) { static int rootdir_com, rootdir; static int dbpath_com, dbpath; static int cachedir_com, cachedir; int len; if (line[0] == '#') { /* Look for default settings first. */ ++line; line = strtrim(line); len = strlen(line); if (len == 0) { return 0; } if (!strncmp(line, comstrs.rootdir, 7)) { parse_pmoption_keyval(&rootdir, &rootdir_com, line, comstrs.rootdir, &pacman_rootdir, 1); } else if (!strncmp(line, comstrs.dbpath, 6)) { parse_pmoption_keyval(&dbpath, &dbpath_com, line, comstrs.dbpath, &pacman_dbpath, 1); } else if (!strncmp(line, comstrs.cachedir, 8)) { parse_pmoption_repeat(&cachedir, &cachedir_com, line, comstrs.cachedir, setrepeat_cachedir, free_cachedir, (void **) &pacman_cachedirs, 1); pw_printf(PW_LOG_DEBUG, "%s%sCachedir default:\n", comstrs.tab, comstrs.tab); indent_print(PW_LOG_DEBUG, pacman_cachedirs, 12); } } else { /* Non-commented, ie. official stuff */ if (!strncmp(line, comstrs.rootdir, 7)) { if (rootdir) { pw_printf(PW_LOG_ERROR, "%s%sRepeated %s\n", comstrs.tab, comstrs.tab, comstrs.rootdir); return -1; } parse_pmoption_keyval(&rootdir, &rootdir_com, line, comstrs.rootdir, &pacman_rootdir, 0); } else if (!strncmp(line, comstrs.dbpath, 6)) { if (dbpath) { pw_printf(PW_LOG_ERROR, "%s%sRepeated %s\n", comstrs.tab, comstrs.tab, comstrs.dbpath); return -1; } parse_pmoption_keyval(&dbpath, &dbpath_com, line, comstrs.dbpath, &pacman_dbpath, 0); } else if (!strncmp(line, comstrs.cachedir, 8)) { if (cachedir) { pw_printf(PW_LOG_ERROR, "%s%sRepeated %s\n", comstrs.tab, comstrs.tab, comstrs.cachedir); return -1; } parse_pmoption_repeat(&cachedir, &cachedir_com, line, comstrs.cachedir, setrepeat_cachedir, free_cachedir, (void **) &pacman_cachedirs, 0); pw_printf(PW_LOG_DEBUG, "%s%sCachedir final:\n", comstrs.tab, comstrs.tab); indent_print(PW_LOG_DEBUG, pacman_cachedirs, 12); } } return 0; }