From a18a54a3ae97e1e6ff5aae196449587a8492b4ff Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Sat, 18 Dec 2021 00:20:45 -0500 Subject: [PATCH] Refactor parse vars into state struct This'll allow closeblock to mutate the state, which will be necessary with nested lists! --- blag.c | 237 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------------------------------------------------- 1 file changed, 125 insertions(+), 112 deletions(-) diff --git a/blag.c b/blag.c index 79de3a4..bcc761d 100644 --- a/blag.c +++ b/blag.c @@ -76,39 +76,50 @@ void putesc(int c) { } } -int closeblock(int in, int hlvl) { - if (in == HEADER) { - in = NONE; - printf("", hlvl); - } else if (in == PARAGRAPH) { - in = PARAGRAPH_BREAK; - } else if (in == PARAGRAPH_BREAK) { - in = NONE; +typedef struct State { + enum Block in; + enum Block in_link; + int hlvl; + int in_code; + int in_ital; + int in_bold; + int escape; + char lnkbuf[2048]; + int lnkidx; +} state; + +void closeblock(state *s) { + if (s->in == HEADER) { + s->in = NONE; + printf("", s->hlvl); + } else if (s->in == PARAGRAPH) { + s->in = PARAGRAPH_BREAK; + } else if (s->in == PARAGRAPH_BREAK) { + s->in = NONE; printf("

\n"); - } else if (in == CODE) { - in = CODE_BREAK; - } else if (in == CODE_BREAK) { - in = NONE; + } else if (s->in == CODE) { + s->in = CODE_BREAK; + } else if (s->in == CODE_BREAK) { + s->in = NONE; printf("\n"); - } else if (in == QUOTE) { - in = QUOTE_BREAK; - } else if (in == QUOTE_BREAK) { - in = NONE; + } else if (s->in == QUOTE) { + s->in = QUOTE_BREAK; + } else if (s->in == QUOTE_BREAK) { + s->in = NONE; printf("\n"); - } else if (in == ULIST) { - in = ULIST_BREAK; - } else if (in == ULIST_BREAK) { - in = NONE; + } else if (s->in == ULIST) { + s->in = ULIST_BREAK; + } else if (s->in == ULIST_BREAK) { + s->in = NONE; printf("\n\n\n"); - } else if (in == OLIST) { - in = OLIST_BREAK; - } else if (in == OLIST_BREAK) { - in = NONE; + } else if (s->in == OLIST) { + s->in = OLIST_BREAK; + } else if (s->in == OLIST_BREAK) { + s->in = NONE; printf("\n\n\n"); } else { /* keep in as is */ } - return in; } int parse() { @@ -123,172 +134,174 @@ int parse() { * action is taken until the parsing is done (usually on ' ') */ int c; - enum Block in = NONE; - enum Block in_link = NONE; - int hlvl = 0; - int in_code = 0; - int in_ital = 0; - int in_bold = 0; - int escape = 0; - char lnkbuf[2048] = {0}; - int lnkidx = 0; + state s = { + .in = NONE, + .in_link = NONE, + .hlvl = 0, + .in_code = 0, + .in_ital = 0, + .in_bold = 0, + .escape = 0, + .lnkbuf = {0}, + .lnkidx = 0 + }; while ((c = getchar()) != EOF) { /* Handle Escapes before all else */ - if (escape) { - if (in == NONE) { - in = PARAGRAPH; + if (s.escape) { + if (s.in == NONE) { + s.in = PARAGRAPH; printf("

\n"); } putesc(c); - escape = 0; + s.escape = 0; continue; } /* Store links as we go */ - if (in_link == LINK_URL_PARSE && c != ']') { - lnkbuf[lnkidx++] = c; + if (s.in_link == LINK_URL_PARSE && c != ']') { + s.lnkbuf[s.lnkidx++] = c; } /* Handle unique state changes by char */ switch (c) { case '\\': - escape = 1; + s.escape = 1; break; case '#': - if (in == NONE) { - in = HEADER_PARSE; - hlvl = 1; - } else if (in == HEADER_PARSE) { - hlvl++; + if (s.in == NONE) { + s.in = HEADER_PARSE; + s.hlvl = 1; + } else if (s.in == HEADER_PARSE) { + s.hlvl++; } else { /* not a special # */ putesc(c); } break; case ' ': - if (in == HEADER_PARSE) { - printf("", hlvl); - in = HEADER; - } else if (in == ULIST_START) { + if (s.in == HEADER_PARSE) { + printf("", s.hlvl); + s.in = HEADER; + } else if (s.in == ULIST_START) { printf("