aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-09-28 00:11:21 +0200
committerMattias Andrée <maandree@operamail.com>2014-09-28 00:11:21 +0200
commitf18dd8f8451f7be6a089c48b62228ad9516c1918 (patch)
tree18fa42fd826150a78326e0ac2af26ab3a3c9f5f6 /src
parentm + kbd layout compiler: read file (diff)
downloadmds-f18dd8f8451f7be6a089c48b62228ad9516c1918.tar.gz
mds-f18dd8f8451f7be6a089c48b62228ad9516c1918.tar.bz2
mds-f18dd8f8451f7be6a089c48b62228ad9516c1918.tar.xz
m + shrink kbd layout code
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
-rw-r--r--src/mds-kbdc.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/mds-kbdc.c b/src/mds-kbdc.c
index 0e33bc6..1969dc8 100644
--- a/src/mds-kbdc.c
+++ b/src/mds-kbdc.c
@@ -26,6 +26,7 @@
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
+#include <string.h>
@@ -100,6 +101,53 @@ static char* read_file(const char* restrict pathname, size_t* restrict size)
/**
+ * Remove non-functional code (comments and empty lines) from the content
+ *
+ * @param content The code to shrink
+ * @param size The size of `content`, in char:s
+ * @return The new size of `content`, in char:s; this function cannot fail
+ */
+static size_t shrink_file(char* restrict content, size_t size)
+{
+#define t content[n_ptr++] = c
+#define last content[n_ptr - 1]
+
+ size_t n_ptr = 0, o_ptr = 0;
+ int comment = 0, quote = 0, escape = 0;
+
+ while (o_ptr < size)
+ {
+ char c = content[o_ptr++];
+ if (comment)
+ {
+ if (c == '\n') comment = 0;
+ }
+ else if (escape) t, escape = 0;
+ else if (quote)
+ {
+ t;
+ if (c == '\\') escape = 1;
+ else if (c == '"') quote = 0;
+ }
+ else if (c == '#') comment = 1;
+ else if (c == '"') t, quote = 1;
+ else if (!strchr("\n ", c) || !n_ptr || (last != c))
+ {
+ /* Store data, but remove unnecessary new lines and unnecessary spaces. */
+ if ((c == '\n') && n_ptr && (last == ' ')) n_ptr--;
+ if ((c == ' ') && n_ptr && (last == '\n')) continue;
+ t;
+ }
+ }
+
+ return n_ptr;
+
+#undef last
+#undef t
+}
+
+
+/**
* Compile a keyboard layout file
*
* @param argc_ The number of elements in `argv_`
@@ -115,8 +163,12 @@ int main(int argc_, char** argv_)
argc = argc_;
argv = argv_;
+ /* Read the file. */
content = read_file(pathname, &content_size);
fail_if (content == NULL);
+ /* Remove comments and empty lines. */
+ content_size = shrink_file(content, content_size);
+ fail_if (xrealloc(content, content_size, char));
return 0;