summaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authorPaul Gardiner <paul@glidos.net>2012-07-12 19:21:02 +0100
committerPaul Gardiner <paul@glidos.net>2012-07-12 19:21:02 +0100
commiteb8029809dfb642bdae2532aa2d959ef7f2f09c1 (patch)
treea0abf44a1127040c75c7c059c84c05bcea91bcb7 /scripts
parentb3a52c4786ab4d9b119dec48ebcc66751e573f5e (diff)
downloadmupdf-eb8029809dfb642bdae2532aa2d959ef7f2f09c1.tar.xz
Separate out the Javascript utility functions and autogenerate C string
Diffstat (limited to 'scripts')
-rw-r--r--scripts/cquote.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/scripts/cquote.c b/scripts/cquote.c
new file mode 100644
index 00000000..0363574d
--- /dev/null
+++ b/scripts/cquote.c
@@ -0,0 +1,126 @@
+/* cquote.c -- Turn the contents of a file into a quoted string */
+
+#include <stdio.h>
+#include <string.h>
+
+/* We never want to build memento versions of the cquote util */
+#undef MEMENTO
+
+static void
+clean(char *p)
+{
+ while (*p)
+ {
+ if ((*p == '/') || (*p == '.') || (*p == '\\') || (*p == '-'))
+ *p = '_';
+ p ++;
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ FILE *fi, *fo;
+ char name[256];
+ char *realname;
+ int i, c;
+
+ if (argc < 3)
+ {
+ fprintf(stderr, "usage: cquote output.c lots of text files\n");
+ return 1;
+ }
+
+ fo = fopen(argv[1], "wb");
+ if (!fo)
+ {
+ fprintf(stderr, "cquote: could not open output file '%s'\n", argv[1]);
+ return 1;
+ }
+
+ fprintf(fo, "/* This is an automatically generated file. Do not edit. */\n");
+
+ for (i = 2; i < argc; i++)
+ {
+ realname = strrchr(argv[i], '/');
+ if (!realname)
+ realname = strrchr(argv[i], '\\');
+ if (realname)
+ realname ++;
+ else
+ realname = argv[i];
+
+ if (strlen(realname) > (sizeof name - 1))
+ {
+ fprintf(stderr, "cquote: file name too long\n");
+ if (fclose(fo))
+ {
+ fprintf(stderr, "cquote: could not close output file '%s'\n", argv[1]);
+ return 1;
+ }
+ return 1;
+ }
+
+ strcpy(name, realname);
+ clean(name);
+
+ fi = fopen(argv[i], "rb");
+
+ fprintf(fo, "\n/* %s */\n\n", name);
+
+ fputc('\"', fo);
+ c = fgetc(fi);
+ while (c != EOF)
+ {
+ int eol = 0;
+
+ switch (c)
+ {
+ case '\"':
+ fprintf(fo, "\\\"");
+ break;
+
+ case '\\':
+ fprintf(fo, "\\\\");
+ break;
+
+ case '\r':
+ case '\n':
+ eol = 1;
+ break;
+
+ default:
+ fputc(c, fo);
+ break;
+ }
+
+ if (eol)
+ {
+ fprintf(fo, "\\n\"\n\"");
+ while ((c = fgetc(fi)) == '\r' || c == '\n')
+ ;
+ }
+ else
+ {
+ c = fgetc(fi);
+ }
+ }
+
+ fprintf(fo, "\\n\"\n");
+
+ if (fclose(fo))
+ {
+ fprintf(stderr, "cmapdump: could not close output file '%s'\n", argv[1]);
+ return 1;
+ }
+
+ }
+
+ if (fclose(fo))
+ {
+ fprintf(stderr, "cmapdump: could not close output file '%s'\n", argv[1]);
+ return 1;
+ }
+
+ return 0;
+}