static int textPackageListRecursive(const char *input, const char *filePath, DFError **error, int indent) { TextPackage *package = TextPackageNewWithString(input,filePath,error); if (package == NULL) return 0; if (package->nkeys == 1) { TextPackageRelease(package); return 1; } for (size_t ki = 0; ki < package->nkeys; ki++) { const char *key = package->keys[ki]; if (strlen(key) == 0) continue; for (int i = 0; i < indent; i++) printf(" "); printf("%s\n",key); const char *value = DFHashTableLookup(package->items,key); if (!textPackageListRecursive(value,filePath,error,indent+1)) { TextPackageRelease(package); return 0; } } TextPackageRelease(package); return 1; }
TextPackage *TextPackageNewWithString(const char *string, const char *path, DFError **error) { TextPackage *package = TextPackageNew(); if (!parsePackage(package,string,path,error)) { TextPackageRelease(package); return NULL; } else return package; }
int textPackageGet(const char *filename, const char *itemPath, DFError **error) { char *value = DFStringReadFromFile(filename,error); if (value == NULL) { DFErrorFormat(error,"%s: %s",filename,DFErrorMessage(error)); return 0; } const char **components = DFStringSplit(itemPath,"/",0); for (size_t i = 0; components[i]; i++) { const char *name = components[i]; char *filePath = DFPathDirName(filename); TextPackage *package = TextPackageNewWithString(value,filePath,error); free(filePath); if (package == NULL) { free(value); free(components); return 0; } free(value); value = xstrdup(DFHashTableLookup(package->items,name)); if (value == NULL) { DFErrorFormat(error,"%s: Item %s not found",filename,itemPath); TextPackageRelease(package); free(value); free(components); return 0; } TextPackageRelease(package); } free(components); printf("%s",value); free(value); return 1; }