From 276f68ee4d55befa8922378199b4281c82d4fd93 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 18 Apr 2019 20:15:46 +0300 Subject: Add parse_manifest() and serialize_manifest() functions --- libbutl/manifest-parser.mxx | 47 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'libbutl/manifest-parser.mxx') diff --git a/libbutl/manifest-parser.mxx b/libbutl/manifest-parser.mxx index f39c9d2..a23d64f 100644 --- a/libbutl/manifest-parser.mxx +++ b/libbutl/manifest-parser.mxx @@ -10,6 +10,7 @@ #ifndef __cpp_lib_modules #include +#include #include #include // uint64_t #include // pair, move() @@ -25,9 +26,11 @@ export module butl.manifest_parser; import std.core; import std.io; #endif +import butl.optional; import butl.char_scanner; import butl.manifest_types; #else +#include #include #include #endif @@ -72,16 +75,15 @@ LIBBUTL_MODEXPORT namespace butl const std::string& name () const {return name_;} - // The first returned pair is special "start-of-manifest" with - // empty name and value being the format version: {"", ""}. - // After that we have a sequence of ordinary pairs which are - // the manifest. At the end of the manifest we have the special - // "end-of-manifest" pair with empty name and value: {"", ""}. - // After that we can either get another start-of-manifest pair - // (in which case the whole sequence repeats from the beginning) - // or we get another end-of-manifest pair which signals the end - // of stream (aka EOF). To put it another way, the parse sequence - // always has the following form: + // The first returned pair is special "start-of-manifest" with empty name + // and value being the format version: {"", ""}. After that we have a + // sequence of ordinary pairs which are the manifest. At the end of the + // manifest we have the special "end-of-manifest" pair with empty name and + // value: {"", ""}. After that we can either get another start-of-manifest + // pair (in which case the whole sequence repeats from the beginning) or + // we get another end-of-manifest-like pair which signals the end of + // stream (aka EOF) and which we will call the end-of-stream pair. To put + // it another way, the parse sequence always has the following form: // // ({"", ""} {"", ""}* {"", ""})* {"", ""} // @@ -120,6 +122,31 @@ LIBBUTL_MODEXPORT namespace butl enum {start, body, end} s_ = start; std::string version_; // Current format version. }; + + // Parse and return a single manifest. Throw manifest_parsing in case of an + // error. + // + // Note that the returned manifest doesn't contain the format version nor + // the end-of-manifest/stream pairs. + // + LIBBUTL_SYMEXPORT std::vector + parse_manifest (manifest_parser&); + + // As above but append the manifest values to an existing list. + // + LIBBUTL_SYMEXPORT void + parse_manifest (manifest_parser&, std::vector&); + + // As above but return nullopt if eos is reached before reading any values. + // + LIBBUTL_SYMEXPORT optional> + try_parse_manifest (manifest_parser&); + + // As above but append the manifest values to an existing list returning + // false if eos is reached before reading any values. + // + LIBBUTL_SYMEXPORT bool + try_parse_manifest (manifest_parser&, std::vector&); } #include -- cgit v1.1