stringptr normalize(stringptr* id, char* workbuf, ssize_t buflen) { size_t i; size_t os = buflen; char hexbuf[5]; stringptr res = {workbuf, 0}; for(i = 0; i < id->size; i++) { switch(id->ptr[i]) { case STRSWITCH_ALPHA: buflen--; if(buflen <= 0) goto err; *workbuf++ = id->ptr[i]; break; default: buflen -= 4; if(buflen <= 0) goto err; ulz_snprintf(hexbuf, 5, "0x%.2X", id->ptr[i]); memcpy(workbuf, hexbuf, 4); workbuf += 4; break; } } if(buflen > 0) *workbuf = 0; res.size = os - buflen; out: return res; err: res.ptr = NULL; goto out; }
void write_enum(int outfd, sectionrec* sref) { hashlist* h; hashlist_iterator iter2; hashrec *href; stringptrlist* newlist; size_t i; stringptr *entry, normalized; char norm_buf[1024]; char buf[1024]; log_put(outfd, VARISL("typedef enum {\n\tstringswitch_enumerator_default_member_name("), VARIS(sref->id), VARISL("),"), NULL); h = sref->h; hashlist_iterator_init(&iter2); while((href = hashlist_next(h, &iter2))) { newlist = href->list; for(i = 0; i < stringptrlist_getsize(newlist); i++) { entry = stringptrlist_get(newlist, i); log_put(1, VARISL("id "), VARIS(sref->id), VARISL(", entry: "), VARII((int) entry->size), VARISL(", "), VARIS(entry), NULL); normalized = normalize(entry, norm_buf, sizeof(norm_buf)); ulz_snprintf(buf, sizeof(buf), "\tstringswitch_enumerator_member_name(%s, %s),\n", sref->id->ptr, normalized.ptr); log_putc(outfd, buf); } } log_put(outfd, VARISL("} stringswitch_enumerator_name("), VARIS(sref->id), VARISL(");\n"), NULL); }
void get_installed_packages(pkgconfig* cfg, stringptrlist* packages) { fileparser f; char buf[256]; stringptr line; ulz_snprintf(buf, sizeof(buf), "%s/pkg/installed.dat", cfg->pkgroot.ptr); if(fileparser_open(&f, buf)) goto err; while(!fileparser_readline(&f) && !fileparser_getline(&f, &line) && line.size) { stringptrlist_add(packages, stringptr_strdup(&line), line.size); } fileparser_close(&f); return; err: log_perror("failed to open installed.dat!"); }
// 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")); }
int get_tarball_filename_with_path(pkgconfig* cfg, pkgdata* package, char* buf, size_t bufsize) { if(stringptrlist_getsize(package->mirrors) == 0) return 0; ulz_snprintf(buf, bufsize, "%s/%s", cfg->filecache.ptr, getfilename(stringptrlist_get(package->mirrors, 0))); return 1; }
int get_tarball_filename(pkgdata* package, char* buf, size_t bufsize) { if(stringptrlist_getsize(package->mirrors) == 0) return 0; ulz_snprintf(buf, bufsize, "%s", getfilename(stringptrlist_get(package->mirrors, 0))); return 1; }
size_t timestamp(char* buffer, size_t bufsize) { time_t secs = time(NULL); struct tm tim; localtime_r(&secs, &tim); return ulz_snprintf(buffer, bufsize, "%.4d-%.2d-%.2d %.2d:%.2d:%.2d", 1900 + tim.tm_year, tim.tm_mon + 1, tim.tm_mday, tim.tm_hour, tim.tm_min, tim.tm_sec); }