X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-test.git;a=blobdiff_plain;f=pctb%2Fcommon.c;h=bc1cb8df749cb63ec2ef392300551c37262c6e16;hp=7fa5b44bc6b393a922ee8ea8db120b7fec377205;hb=fdb2c7e1f2211fe4328ee6390f40a55446ecd078;hpb=74e4e249f2c3e848592984cb193aded6a77a341d diff --git a/pctb/common.c b/pctb/common.c index 7fa5b44..bc1cb8d 100644 --- a/pctb/common.c +++ b/pctb/common.c @@ -27,22 +27,6 @@ #include "common.h" -#define dbassert(x) \ - ((x) ? (void)0 : \ - fatal("Error in database.\n" \ - " Requirement not met: %s:%d: %s", __FILE__,__LINE__, #x)) - -void fgetsline(FILE *f, char *lbuf, size_t lbufsz) { - errno=0; - char *s= fgets(lbuf,lbufsz,f); - sysassert(!ferror(f)); - dbassert(!feof(f)); - assert(s); - int l= strlen(lbuf); - dbassert(l>0); dbassert(lbuf[--l]='\n'); - lbuf[l]= 0; -} - void *mmalloc(size_t sz) { void *r; if (!sz) return 0; @@ -55,3 +39,63 @@ void *mrealloc(void *p, size_t sz) { sysassert( r= realloc(p,sz) ); return r; } + + +FILE *dbfile; +static const char *path; + +int dbfile_open(const char *tpath) { + assert(!dbfile); + path= tpath; + dbfile= fopen(path,"r"); + if (dbfile) return 1; + sysassert(errno==ENOENT); + return 0; +} + +#define dbassertgl(x) ((x) ? (void)0 : dbfile_assertfail(file,line,#x)) + +void dbfile_getsline(char *lbuf, size_t lbufsz, const char *file, int line) { + errno=0; + char *s= fgets(lbuf,lbufsz,dbfile); + sysassert(!ferror(dbfile)); + dbassertgl(!feof(dbfile)); + assert(s); + int l= strlen(lbuf); + dbassertgl(l>0); dbassertgl(lbuf[--l]=='\n'); + lbuf[l]= 0; +} + +void dbfile_close(void) { + if (!dbfile) return; + sysassert(!ferror(dbfile)); + sysassert(!fclose(dbfile)); + dbfile= 0; +} + +int dbfile_vscanf(const char *fmt, va_list al) { + int r= vfscanf(dbfile,fmt,al); + sysassert(!ferror(dbfile)); + return r; +} + +int dbfile_scanf(const char *fmt, ...) { + va_list al; + va_start(al,fmt); + int r= dbfile_vscanf(fmt,al); + va_end(al); + return r; +} + +void dbfile_assertfail(const char *file, int line, const char *m) { + if (dbfile) + fatal("Error in dictionary file %s at byte %ld:\n" + " Requirement not met at %s:%d:\n" + " %s", + path,(long)ftell(dbfile), file,line, m); + else + fatal("Semantic error in dictionaries:\n" + " Requirement not met at %s:%d:\n" + " %s", + file,line, m); +}