From 8b0fe501e77a1f4032a396b97ad317f09e160ee3 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Mon, 8 Apr 2019 16:36:39 +0300 Subject: Add support for manifest serializing long lines mode --- libbutl/manifest-serializer.bash.in | 26 ++++++++++++++++++++------ libbutl/manifest.cxx | 27 ++++++++++++++++++++++----- 2 files changed, 42 insertions(+), 11 deletions(-) (limited to 'libbutl') diff --git a/libbutl/manifest-serializer.bash.in b/libbutl/manifest-serializer.bash.in index b1220e1..06770ba 100644 --- a/libbutl/manifest-serializer.bash.in +++ b/libbutl/manifest-serializer.bash.in @@ -11,13 +11,13 @@ fi @import libbutl/utility@ # Serialize the manifest reading the binary representation from stdin and -# writing to stdout. +# writing to stdout. Unless --long-lines is specified, break lines in values. # # Normally you would use the start/finish functions below. # -function butl_serialize_manifest () +function butl_serialize_manifest () # [--long-lines] { - "$(butl_path)/manifest" serialize + "$(butl_path)/manifest" "$@" serialize } # Start the manifest serialization co-process setting the following "return" @@ -27,7 +27,8 @@ function butl_serialize_manifest () # butl_manifest_serializer_ofd # butl_manifest_serializer_pid # -# If is not specified, then write to stdout. +# If is not specified, then write to stdout. Unless --long-lines is +# specified, break lines in values. # # The typical usage: # @@ -41,8 +42,21 @@ function butl_serialize_manifest () # # butl_manifest_serializer_finish # -function butl_manifest_serializer_start () # [] +function butl_manifest_serializer_start () # [--long-lines] [] { + local ops=() + while [ $# -gt 0 ]; do + case "$1" in + --long-lines) + ops+=("$1") + shift + ;; + *) + break + ;; + esac + done + if [ "$#" -gt 0 ]; then exec {butl_manifest_serializer_ofd}>"$1" else @@ -51,7 +65,7 @@ function butl_manifest_serializer_start () # [] # See notes in butl_manifest_parser_start() on bash co-process issues. # - coproc { butl_serialize_manifest; } >&"$butl_manifest_serializer_ofd" + coproc { butl_serialize_manifest "${ops[@]}"; } >&"$butl_manifest_serializer_ofd" butl_manifest_serializer_ifd="${COPROC[1]}" butl_manifest_serializer_pid="$COPROC_PID" } diff --git a/libbutl/manifest.cxx b/libbutl/manifest.cxx index d095c45..b3811a5 100644 --- a/libbutl/manifest.cxx +++ b/libbutl/manifest.cxx @@ -81,7 +81,7 @@ cmd_parse () } static int -cmd_serialize () +cmd_serialize (bool long_lines) { using serializer = manifest_serializer; using serialization = manifest_serialization; @@ -97,7 +97,7 @@ cmd_serialize () cout.exceptions (ios::badbit | ios::failbit); - serializer s (cout, "stdout"); + serializer s (cout, "stdout", long_lines); for (string l; !eof (getline (cin, l, '\0')); ) { @@ -132,21 +132,38 @@ cmd_serialize () return 0; } +// Usages: +// +// argv[0] parse +// argv[0] [--long-lines] serialize +// int main (int argc, char* argv[]) { // We should switch to CLI if we need anything more elaborate. // - if (argc < 2) + int i (1); + bool long_lines (false); + for (; i != argc; ++i) + { + string op (argv[i]); + + if (op == "--long-lines") + long_lines = true; + else + break; + } + + if (i == argc) { cerr << "error: missing command" << endl; return 1; } - string c (argv[1]); + string c (argv[i]); if (c == "parse") return cmd_parse (); - if (c == "serialize") return cmd_serialize (); + if (c == "serialize") return cmd_serialize (long_lines); cerr << "error: unknown command '" << c << "'" << endl; return 1; -- cgit v1.1