diff options
author | Holden Rohrer <hr@hrhr.dev> | 2020-03-30 18:26:23 -0400 |
---|---|---|
committer | Holden Rohrer <hr@hrhr.dev> | 2020-03-30 18:26:23 -0400 |
commit | 969e678287771bab0a34ca44396a721ade715720 (patch) | |
tree | a74fe8b95ab908c0feb65d3d4ff6b60d5a548d4b | |
parent | 4b28d69cb526c7730c8e7cb0ab14e2a77e732211 (diff) |
general fn call interface
-rw-r--r-- | badroff.c | 16 |
1 files changed, 14 insertions, 2 deletions
@@ -51,10 +51,22 @@ char* center(char* text){ return str; } +char* cmds[] = {"CT "}; +char* (*call[])(char* txt) = {center}; + char* cmd(void){ char* dat = popstrbuf(inbuf, chrfill('\n')); - if (!strncmp(dat, "CT ", 3)){ - return center(dat+3); + size_t low = 0; size_t high = sizeof(cmds)/sizeof(*cmds); //len + while (high >= low){ + int mid = ((unsigned int)low + (unsigned int)high) >> 1; + char* mval = cmds[mid]; + + int cmp = strncmp(dat, mval, strlen(mval)); + if (cmp < 0) // dat < mval + low = mid + 1; + else if (cmp > 0) + high = mid - 1; + else return call[mid](dat+strlen(mval)); } return NULL; } |