From f18dd8f8451f7be6a089c48b62228ad9516c1918 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 28 Sep 2014 00:11:21 +0200 Subject: m + shrink kbd layout code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- res/keyboard/compose/dead | 12 +++++------ src/mds-kbdc.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/res/keyboard/compose/dead b/res/keyboard/compose/dead index 2276275..609ec89 100644 --- a/res/keyboard/compose/dead +++ b/res/keyboard/compose/dead @@ -1148,21 +1148,21 @@ composite("⇧" "^" "⇮") # Enclosed Alphanumerics: for "1" to "9" as \1 - "(" "\1" ")" : "\add(\u2460 \sub(\1 "1"))" + "(" "\1" ")" : "\add(\u2460 \sub(\1 "1"))" "(" "\1" [" " .] ")" : "\add(\u2474 \sub(\1 "1"))" "(" "\1" ")" : "\add(\u2474 \sub(\1 "1"))" - "\1" "." : "\add(\u2488 \sub(\1 "1"))" + "\1" "." : "\add(\u2488 \sub(\1 "1"))" end for for "0" to "9" as \1 - "(" "1" "\1" ")" : "\add(\u2469 \sub(\1 "0"))" + "(" "1" "\1" ")" : "\add(\u2469 \sub(\1 "0"))" "(" "1" "\1" [" " .] ")" : "\add(\u247D \sub(\1 "0"))" "(" "1" "\1" ")" : "\add(\u247D \sub(\1 "0"))" - "1" "\1" "." : "\add(\u2491 \sub(\1 "0"))" + "1" "\1" "." : "\add(\u2491 \sub(\1 "0"))" end for - "(" "2" "0" ")" : "\u2473" + "(" "2" "0" ")" : "\u2473" "(" "2" "0" [" " .] ")" : "\u2487" "(" "2" "0" ")" : "\u2473" - "2" "0" "." : "\u2498" + "2" "0" "." : "\u2498" for "a" to "z" as \1 "(" "\1" [" " .] ")" : "\add(\u249C \sub(\1 "a"))" "(" "\1" ")" : "\add(\u249C \sub(\1 "a"))" 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 #include #include +#include @@ -99,6 +100,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 * @@ -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; -- cgit v1.2.3-70-g09d2