From ee3ec6bd3456c7be65c214da8145510e0df74d89 Mon Sep 17 00:00:00 2001 From: Albert Graef Date: Tue, 30 Jun 2015 13:15:53 +0200 Subject: [PATCH] allow sharing the left-hand side of rules This allows to abbreviate a collection of rules where the same OSC message is mapped to different MIDI messages. E.g., /multi/{i} ff, touch,x,y : controlchange( channel, touch, x*127); /multi/{i} ff, touch,x,y : controlchange( channel, touch+10, y*127); may now be written as: /multi/{i} ff, touch,x,y : controlchange( channel, touch, x*127); : controlchange( channel, touch+10, y*127); Note that the latter simply gets expanded to the former. There's no change in semantics (except that an OSC message path can't start with a colon any longer, but that's rather unlikely anyway). --- src/main.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 05e252e..53014fb 100644 --- a/src/main.c +++ b/src/main.c @@ -54,7 +54,7 @@ void init_regs(float ***regs, int n) int load_map(CONVERTER* conv, char* file) { int i; - char path[200],line[400],*home; + char path[200],line[400],prefix[400],*home; FILE* map = NULL; FILE* tmp = NULL; PAIRHANDLE *p; @@ -156,12 +156,39 @@ int load_map(CONVERTER* conv, char* file) int nkeys = 0; rewind(map); i=0; + *prefix = 0; while(!feof(map)) { + char rule[800]; if (!fgets(line,400,map)) break; if(!is_empty(line)) { - p[i] = alloc_pair(line, conv->tab, conv->registers, &nkeys); + // This provides a quick and dirty way to left-factor rules, where + // the same osc message is mapped to different midi messages. -ag + char *s = line; + while (*s && isspace(*s)) ++s; + if (*s == ':') + { + // Line starts with ':' delimiter, use prefix from previous + // rule. + strcat(strcpy(rule, prefix), s); + } + else + { + // Skip over the OSC path. + while (*s && !isspace(*s)) ++s; + // Skip over the rest of the lhs of the rule. + while (*s && *s != ':') ++s; + if (*s == ':') + { + // Complete rule, store prefix for subsequent rules. + int n = s-line; + strncpy(prefix, line, n); + prefix[n] = 0; + } + strcpy(rule, line); + } + p[i] = alloc_pair(rule, conv->tab, conv->registers, &nkeys); if(p[i++]) { if(conv->verbose)