From 2538e24a17975a957f5332b9f993be1be3496d2b Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Fri, 6 Jul 2018 15:18:58 +1200 Subject: [PATCH] Set up --buffer and --keep --- README.md | 44 +++++++++++++++--------------- src/main/c/main.c | 53 ++++++++++++++++++++++++++++++------ src/main/c/util/fstreamout.c | 15 ++++++++-- 3 files changed, 78 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 7cd9b0e..a01d701 100644 --- a/README.md +++ b/README.md @@ -100,18 +100,18 @@ Path to a file to write to; it will be created if it doesn't exist already. If o #### `--keep` -Don't automatically delete the output file if an error occurred. This option does nothing if the output is `stdout`, and cannot be used with `--buffer`. +Don't automatically delete the output file if an error occurred. If the output is `stdout`, or the output is a file but `--buffer` is provided, this option does nothing. #### `--buffer` -Buffer all output until the process is complete and successful. This can prevent many writes to storage (and won't cause any writes on error), but will use a non-constant amount of memory. -This applies even when the output is `stdout`, and cannot be used with `--keep`. +Buffer all output until the process is complete and successful. This won't truncate or write anything to the output until the build process is done, but will use a non-constant amount of memory. +This applies even when the output is `stdout`. -#### `--errorEx` +#### `--suppress` Suppress errors specified by this option. hyperbuild will quitely ignore and continue processing when otherwise one of the provided errors would occur. -Separate the error names by a comma. Suppressible errors are marked with a `⌫` in the [Errors](#errors) section. +Separate the error names with commas. Suppressible errors are marked with a `⌫` in the [Errors](#errors) section. ## Processing @@ -238,7 +238,7 @@ For brevity, hyperbuild has built-in sets of tags that can be used in place of d |`$void`|`area`, `base`, `br`, `col`, `embed`, `hr`, `img`, `input`, `keygen`, `link`, `meta`, `param`, `source`, `track`, `wbr`|[voidtags.c](src/main/c/rule/tag/voidtags.c)| |`$wss`|`pre`, `code`|[wsstags.c](src/main/c/rule/tag/wsstags.c)| -#### `--collapseWhitespaceEx $wss` +#### `--MXcollapseWhitespace $wss` Reduce a sequence of whitespace characters in text nodes to a single space (U+0020), unless they are a child of the tags specified by this option. @@ -260,7 +260,7 @@ Reduce a sequence of whitespace characters in text nodes to a single space (U+00 -#### `--destroyWholeWhitespaceEx $wss,$content,$formatting` +#### `--MXdestroyWholeWhitespace $wss,$content,$formatting` Remove any text nodes that only consist of whitespace characters, unless they are a child of the tags specified by this option. @@ -286,7 +286,7 @@ Especially useful when using `display: inline-block` so that whitespace between -#### `--trimWhitespaceEx $wss,$formatting` +#### `--MXtrimWhitespace $wss,$formatting` Remove any whitespace from the start and end of a tag, if the first and/or last node is a text node, unless the tag is one of the tags specified by this option. @@ -315,9 +315,9 @@ Basically, a tag should only either contain text and [inline text semantics](htt -#### `--trimClassAttribute` +#### `--MXtrimClassAttribute` -Trim and collapse whitespace in `class` attribute values. +Don't trim and collapse whitespace in `class` attribute values.
BeforeAfter
@@ -339,29 +339,29 @@ Trim and collapse whitespace in `class` attribute values.
-#### `--decodeEntities` +#### `--MXdecodeEntities` -Decode any valid entities into their UTF-8 values. +Don't decode any valid entities into their UTF-8 values. -#### `--processConditionalComments` +#### `--MXprocessConditionalComments` -Process the contents of conditional comments, including downlevel-revealed conditional comments. +Don't process the contents of conditional comments, including downlevel-revealed conditional comments. -#### `--removeAttributeQuotes` +#### `--MXremoveAttributeQuotes` -Remove quotes around attribute values when possible. +Don't remove quotes around attribute values when possible. -#### `--removeComments` +#### `--MXremoveComments` -Remove any comments, except conditional comments. +Don't remove any comments, except conditional comments. -#### `--removeOptionalTags` +#### `--MXremoveOptionalTags` -Remove optional starting or ending tags. +Don't remove optional starting or ending tags. -#### `--removeTagWhitespace` +#### `--MXremoveTagWhitespace` -Remove spaces between attributes when possible. +Don't remove spaces between attributes when possible. ### Non-options diff --git a/src/main/c/main.c b/src/main/c/main.c index 3aede53..053349b 100644 --- a/src/main/c/main.c +++ b/src/main/c/main.c @@ -20,17 +20,21 @@ int main(int argc, char **argv) { // Prepare config char *input_path = NULL; char *output_path = NULL; + int config_keep = 0; + int config_buffer = 0; // Parse arguments while (1) { static struct option long_options[] = { + {"keep", no_argument, NULL, 'k'}, + {"buffer", no_argument, NULL, 'b'}, {"input", required_argument, NULL, 'i'}, {"output", required_argument, NULL, 'o'}, {0, 0, 0, 0} }; int option_index = 0; - int c = getopt_long(argc, argv, "i:o:", long_options, &option_index); + int c = getopt_long(argc, argv, "kbi:o:", long_options, &option_index); if (c == -1) { break; @@ -44,26 +48,57 @@ int main(int argc, char **argv) { case 'o': output_path = optarg; break; + + case 'k': + config_keep = 1; + break; + + case 'b': + config_buffer = 1; + break; } } hbe_debug("Input: %s", input_path); hbe_debug("Output: %s", output_path); - - hbu_fstreamin_t input = hbu_fstreamin_create(input_path); - hbu_fstreamout_t output = hbu_fstreamout_create(output_path); - - if (output != NULL) { - // Set after opening output stream (file is created then) - hbe_fatal_set_autodelete(output_path); + if (config_buffer) { + hbe_debug("Buffer: %d", config_buffer); + } + if (config_keep) { + hbe_debug("Keep: %d", config_keep); } hbu_pipe_t pipe = hbu_pipe_create_blank(); + + hbu_fstreamin_t input = hbu_fstreamin_create(input_path); hbu_pipe_blank_set_input(pipe, input); - hbu_pipe_blank_set_output_fstreamout(pipe, output); + + hbu_fstreamout_t output; + hbu_buffer_t output_buffer; + + if (config_buffer) { + output = NULL; + output_buffer = hbu_buffer_create(); + hbu_pipe_blank_set_output_buffer(pipe, output_buffer); + } else { + output = hbu_fstreamout_create(output_path); + output_buffer = NULL; + hbu_pipe_blank_set_output_fstreamout(pipe, output); + } + + if (output != NULL && !config_keep && !config_buffer) { + // Set after opening output stream (file is created then) + // Don't need to set if $config_buffer, as it won't write anything anyway + hbe_fatal_set_autodelete(output_path); + } hbs_content(pipe); + if (config_buffer) { + output = hbu_fstreamout_create(output_path); + hbu_fstreamout_write_buffer(output, output_buffer); + } + hbe_debug("All done!"); exit(EXIT_SUCCESS); } diff --git a/src/main/c/util/fstreamout.c b/src/main/c/util/fstreamout.c index 8cf61ab..ee05846 100644 --- a/src/main/c/util/fstreamout.c +++ b/src/main/c/util/fstreamout.c @@ -4,14 +4,23 @@ #include #include #include "../error/error.c" -#include "fstream.h" +#include "./buffer.c" +#include "./fstream.h" HBU_FSTREAM_BUILD_INFRA(out, "w", "write", "writing", stdout) -void hbu_fstreamout_write(hbu_fstreamout_t fstreamout, hb_char_t c) { - if (fwrite(&c, SIZEOF_CHAR, 1, fstreamout->fd) != SIZEOF_CHAR) { +static void _hbu_fstreamout_fwrite(hbu_fstreamout_t fstreamout, hb_char_t *source, size_t length) { + if (fwrite(source, SIZEOF_CHAR, length, fstreamout->fd) != SIZEOF_CHAR * length) { hbe_fatal(HBE_IO_FWRITE_FAIL, "Failed to write to output file %s", fstreamout->name); } } +void hbu_fstreamout_write_buffer(hbu_fstreamout_t fstreamout, hbu_buffer_t buffer) { + _hbu_fstreamout_fwrite(fstreamout, hbu_buffer_underlying(buffer), buffer->length); +} + +void hbu_fstreamout_write(hbu_fstreamout_t fstreamout, hb_char_t c) { + _hbu_fstreamout_fwrite(fstreamout, &c, 1); +} + #endif // _HDR_HYPERBUILD_UTIL_FSTREAMOUT