summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2012-08-06 05:11:05 (GMT)
committerH. Peter Anvin <hpa@zytor.com>2012-08-06 05:11:05 (GMT)
commit8588a8c706818780f70577e0eefb3a8cbb996724 (patch)
tree306e2141d340692314c129bdc26aa7065af20174
parent280ff1cfa46da808d6b9f4c5994117e12b968cc6 (diff)
downloadabcdisk-8588a8c706818780f70577e0eefb3a8cbb996724.zip
abcdisk-8588a8c706818780f70577e0eefb3a8cbb996724.tar.gz
abcdisk-8588a8c706818780f70577e0eefb3a8cbb996724.tar.bz2
abcdisk-8588a8c706818780f70577e0eefb3a8cbb996724.tar.xz
abctext: utility to convert ABC text files saved in binary form
-rw-r--r--Makefile4
-rw-r--r--abctext.c59
2 files changed, 61 insertions, 2 deletions
diff --git a/Makefile b/Makefile
index 807858e..cfe1756 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CC = cc
-CFLAGS = -W -Wall -Wno-sign-compare -O0 -g
+CFLAGS = -W -Wall -Wno-sign-compare -O2 -g
LDFLAGS =
ifneq (,$(findstring _NT,$(shell uname -s)))
O = obj
@@ -9,7 +9,7 @@ O = o
X =
endif
-PROGS = abcwrite$(X) abcread$(X) deinter$(X)
+PROGS = abcwrite$(X) abcread$(X) deinter$(X) abctext$(X)
all : $(PROGS)
diff --git a/abctext.c b/abctext.c
new file mode 100644
index 0000000..09b16f1
--- /dev/null
+++ b/abctext.c
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <string.h>
+
+/*
+ * Convert an ABC text file in binary form to more normal
+ * form. This does not do character set conversions.
+ */
+
+int main(int argc, char *argv[])
+{
+ FILE *f;
+ char buf[253], *p;
+ int n, i;
+ int was_tab;
+
+ if (argc > 1)
+ f = fopen(argv[1], "rb");
+ else
+ f = stdin;
+
+ was_tab = 0;
+
+ while ((n = fread(buf, 1, 253, f)) > 0) {
+ if (!memcmp(buf, "\0\0\0\0\0\0", 6))
+ break; /* End of file */
+
+ p = buf;
+ while (n--) {
+ if (was_tab) {
+ /* TAB + count is used for compressed spaces */
+ i = *p;
+ while (i--)
+ putchar(' ');
+ was_tab = 0;
+ } else {
+ switch (*p) {
+ case '\t':
+ was_tab = 1;
+ break;
+ case '\3':
+ n = 0; /* ETX */
+ break;
+ case '\r':
+ putchar('\n');
+ break;
+ default:
+ putchar(*p);
+ break;
+ }
+ }
+ p++;
+ }
+ }
+
+ if (f != stdin)
+ fclose(f);
+
+ return 0;
+}