summaryrefslogtreecommitdiff
path: root/common/repfile.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/repfile.c')
-rw-r--r--common/repfile.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/common/repfile.c b/common/repfile.c
new file mode 100644
index 0000000..eec6ca2
--- /dev/null
+++ b/common/repfile.c
@@ -0,0 +1,113 @@
+/*
+ * AUTHOR
+ * N. Nielsen
+ *
+ * LICENSE
+ * This software is in the public domain.
+ *
+ * The software is provided "as is", without warranty of any kind,
+ * express or implied, including but not limited to the warranties
+ * of merchantability, fitness for a particular purpose, and
+ * noninfringement. In no event shall the author(s) be liable for any
+ * claim, damages, or other liability, whether in an action of
+ * contract, tort, or otherwise, arising from, out of, or in connection
+ * with the software or the use or other dealings in the software.
+ *
+ * SUPPORT
+ * Send bug reports to: <nielsen@memberwebs.com>
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include "common/usuals.h"
+#include "common/compat.h"
+#include "common/binfile.h"
+#include "common/repfile.h"
+
+/* errmsg: -----------------------------------------------------------
+ * Display an rlib error on the console
+ */
+int errmsg(int ret, r_script* script)
+{
+ const char* msg;
+
+ switch(ret)
+ {
+ case R_NOMEM:
+ msg = "out of memory";
+ break;
+ case R_SYNTAX:
+ msg = "syntax error:";
+ break;
+ case R_REGEXP:
+ msg = "reg expression error:";
+ break;
+ case R_LOOP:
+ msg = "an endless loop was encountered ";
+ break;
+ case R_USER:
+ msg = "stop: ";
+ break;
+ case R_IOERR:
+ msg = "read or write error";
+ break;
+ case R_INVARG:
+ ASSERT(0 && "This error should be taken care of by programmer");
+ msg = "internal programmer error";
+ break;
+ default:
+ msg = "unknown error";
+ break;
+ }
+
+ if(script && script->error)
+ {
+ if(script->errline > 0)
+ warnx("%s %s (at line %d)", msg, script->error, script->errline);
+ else
+ warnx("%s %s", msg, script->error);
+ }
+ else
+ {
+ warnx(msg);
+ }
+
+ return ret;
+}
+
+/* repscriptheader: -------------------------------------------------
+ * The top of every compiled rep file has this signature
+ */
+typedef struct _repscriptheader
+{
+ uint sig; /* 'rep0' */
+ uint version; /* Note this is the file version */
+}
+repscriptheader;
+
+const uint kReplSig = '0per';
+const uint kReplVer = 0x00023100;
+
+/* repfWriteHeader: -------------------------------------------------
+ * Write out a valid header
+ */
+bool repfWriteHeader(BFILE h)
+{
+ repscriptheader head;
+ head.sig = kReplSig;
+ head.version = kReplVer;
+
+ bfWriteRaw(h, &head, sizeof(head));
+ return !bfError(h);
+}
+
+/* repfReadHeader: --------------------------------------------------
+ * Read and validate rep file header
+ */
+bool repfReadHeader(BFILE h)
+{
+ repscriptheader head;
+ memset(&head, 0, sizeof(head));
+ bfReadRaw(h, &head, sizeof(head));
+ return head.sig == kReplSig && head.version == kReplVer;
+}