int main(void) { ini_section sec; stringptr result; stringptr* ini = stringptr_copy(SPLITERAL("[main]\nfoo=bar\nbar=baz\n[section1]\nbaz=blah\nblah=foo\n")); // we need stringptr_copy since stringptr_linesplit modifies the buffer. which would segfault using a literal. stringptrlist* lines = stringptr_linesplit(ini); sec = iniparser_get_section(lines, SPLITERAL("main")); iniparser_getvalue(lines, &sec, SPLITERAL("bar"), &result); printf("[main] bar=%s, size=%d\n", result.ptr, (int) result.size); sec = iniparser_get_section(lines, SPLITERAL("section1")); iniparser_getvalue(lines, &sec, SPLITERAL("blah"), &result); printf("blah=%s, size=%d\n", result.ptr, (int) result.size); stringptr_free(ini); free(lines); return 0; }
// contract: out is already zeroed and contains only name void get_package_contents(pkgconfig* cfg, stringptr* packagename, pkgdata* out) { ini_section sec; char buf[256]; ulz_snprintf(buf, sizeof(buf), "%s/pkg/%s", cfg->pkgroot.ptr, packagename->ptr); stringptr* fc = stringptr_fromfile(buf); stringptr val; if(!fc) goto err; stringptrlist* ini = stringptr_linesplit(fc); size_t start = 0; stringptr* tmp; sec = iniparser_get_section(ini, SPL("mirrors")); out->mirrors = stringptrlist_new(sec.linecount); for(start = sec.startline; start < sec.startline + sec.linecount; start++) { tmp = stringptrlist_get(ini, start); if(tmp->size) stringptrlist_add(out->mirrors, stringptr_strdup(tmp), tmp->size); } sec = iniparser_get_section(ini, SPL("main")); iniparser_getvalue(ini, &sec, SPL("tardir"), &val); if(val.size) out->tardir = stringptr_copy(&val); else { // must run after mirrors! stringptr fe; if(get_tarball_filename(out, buf, sizeof(buf))) { stringptr_fromchar(buf, &fe); strip_fileext(&fe); } else { fe.size = 0; fe.ptr = NULL; } out->tardir = stringptr_copy(&fe); } iniparser_getvalue(ini, &sec, SPL("sha512"), &val); if(val.size) out->sha512 = stringptr_copy(&val); else out->sha512 = NULL; iniparser_getvalue(ini, &sec, SPL("filesize"), &val); if(val.size) out->filesize = strtoint64(val.ptr, val.size); else out->filesize = 0; sec = iniparser_get_section(ini, SPL("deps")); out->deps = stringptrlist_new(sec.linecount); for(start = sec.startline; start < sec.startline + sec.linecount; start++) { tmp = stringptrlist_get(ini, start); if(tmp->size) stringptrlist_add(out->deps, stringptr_strdup(tmp), tmp->size); } sec = iniparser_get_section(ini, SPL("build")); // the build section has always to come last if(sec.startline || sec.linecount) { start = sec.startline; sec = iniparser_file_as_section(ini); // iniparser may disinterpret lines starting with [ // so be sure to use the entire rest of the file sec.startline = start; sec.linecount -= start; out->buildscript = stringptrlist_new(sec.linecount); for(start = sec.startline; start < sec.startline + sec.linecount; start++) { tmp = stringptrlist_get(ini, start); stringptrlist_add(out->buildscript, stringptr_strdup(tmp), tmp->size); } } else out->buildscript = stringptrlist_new(1); stringptrlist_free(ini); stringptr_free(fc); return; err: log_perror(packagename->ptr); die(SPL("package not existing")); }