chiark / gitweb /
Much better error handling.
[ypp-sc-tools.db-test.git] / pctb / common.h
diff --git a/pctb/common.h b/pctb/common.h
new file mode 100644 (file)
index 0000000..9afb083
--- /dev/null
@@ -0,0 +1,92 @@
+
+#ifndef COMMON_H
+#define COMMON_H
+
+#define _GNU_SOURCE
+
+#include <stdarg.h>
+
+#include <sys/types.h>
+
+typedef struct {
+  int w,h;
+  char d[];
+} CanonImage;
+
+
+/*----- debugging arrangements, rather contingent -----*/
+
+typedef struct {
+  int x, y;
+} Point;
+
+typedef struct { /* both inclusive */
+  Point tl;
+  Point br;
+} Rect;
+
+
+
+#define DEBUG_FLAG_LIST                                \
+   DF(findypp)                                 \
+   DF(pages)                                   \
+   DF(rect)                                    \
+   DF(ocr)                                     \
+   DF(callout)
+
+enum {
+#define DF(f) dbg__shift_##f,
+  DEBUG_FLAG_LIST
+#undef DF
+};
+enum {
+#define DF(f) dbg_##f = 1 << dbg__shift_##f,
+  DEBUG_FLAG_LIST
+#undef DF
+};
+
+unsigned debug_flags;
+
+#define DEBUGP(f) (!!(debug_flags & dbg_##f))
+
+void debug_flush(void);
+#define debug stderr
+
+const char *get_vardir(void);
+
+#define FMT(f,a) __attribute__((format(printf,f,a)))
+#define NORET __attribute__((noreturn))
+
+#define DEFINE_VWRAPPERF(decls, funcf, otherattribs)                   \
+  decls void funcf(const char *fmt, ...) FMT(1,2) otherattribs;         \
+  decls void funcf(const char *fmt, ...) {                             \
+    va_list al;  va_start(al,fmt);  v##funcf(fmt,al);  va_end(al);     \
+  }
+
+#define DEBUG_DEFINE_SOME_DEBUGF(fl,funcf)                             \
+  static void v##funcf(const char *fmt, va_list al) {                  \
+    if (DEBUGP(fl))                                                    \
+      vfprintf(debug,fmt,al);                                          \
+  }                                                                    \
+  DEFINE_VWRAPPERF(static, funcf, )
+
+#define DEBUG_DEFINE_DEBUGF(fl) DEBUG_DEFINE_SOME_DEBUGF(fl,debugf)
+
+
+/*---------- error handling ----------*/
+
+void vfatal(const char *fmt, va_list)  FMT(1,0) NORET;
+void fatal(const char *fmt, ...)       FMT(1,2) NORET;
+
+#define sysassert(what) \
+  ((what) ? (void)0 : sysassert_fail(__FILE__, __LINE__, #what))
+
+void sysassert_fail(const char *file, int line, const char *what)
+   __attribute__((noreturn));
+
+
+void *mmalloc(size_t sz);
+void *mrealloc(void *p, size_t sz);
+
+
+#endif /*COMMON_H*/