chiark / gitweb /
Compile fixes for amd64 according to Ian
[ypp-sc-tools.main.git] / pctb / common.c
index 7fa5b44bc6b393a922ee8ea8db120b7fec377205..bc1cb8df749cb63ec2ef392300551c37262c6e16 100644 (file)
 
 #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);
+}