diff options
author | Thomas White <taw@physics.org> | 2017-10-19 21:42:48 +0200 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2017-10-19 22:33:48 +0200 |
commit | 7f2d0abd61d9d25c7c07ad1de1de2e6067c22b3e (patch) | |
tree | 1109d2a96d55d156eb70989686e39895980d96e7 /src/sc_parse.c | |
parent | 74be5482424aef26ea081aca0ccd0e5b6a5bcdcb (diff) |
Track the newlines at ends of paragraphs directly
No more messing around searching for \n characters
Diffstat (limited to 'src/sc_parse.c')
-rw-r--r-- | src/sc_parse.c | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/sc_parse.c b/src/sc_parse.c index 56ebcaf..79e59a0 100644 --- a/src/sc_parse.c +++ b/src/sc_parse.c @@ -296,6 +296,9 @@ char *serialise_sc_block(const SCBlock *bl) if ( bl->name == NULL ) { strcat(a, bl->contents); + } else if ( strcmp(bl->name, "newpara") == 0 ) { + strcat(a, "\n"); + } else { strcat(a, "\\"); @@ -503,6 +506,41 @@ static size_t read_block(const char *sc, char **pname, char **options, } +static void separate_newlines(SCBlock *bl) +{ + while ( bl != NULL ) { + + char *npos; + const char *contents = sc_block_contents(bl); + + if ( contents != NULL ) { + npos = strchr(contents, '\n'); + if ( npos != NULL ) { + SCBlock *nb = NULL; + if ( npos == contents ) { + bl->name = strdup("newpara"); + bl->contents = NULL; + nb = bl; + } else { + sc_block_append(bl, strdup("newpara"), NULL, NULL, &nb); + } + if ( strlen(npos+1) > 0 ) { + sc_block_append(nb, NULL, NULL, strdup(npos+1), &nb); + } + npos[0] = '\0'; + } + } + + if ( sc_block_child(bl) != NULL ) { + separate_newlines(sc_block_child(bl)); + } + + bl = sc_block_next(bl); + + } +} + + SCBlock *sc_parse(const char *sc) { SCBlock *bl; @@ -601,10 +639,19 @@ SCBlock *sc_parse(const char *sc) j = 0; } + separate_newlines(blf); + return blf; } +void sc_block_set_name(SCBlock *bl, char *nam) +{ + free(bl->name); + bl->name = nam; +} + + void sc_block_set_options(SCBlock *bl, char *opt) { free(bl->options); |