From c0ae9fee7e30cdd4898df22aecf5412b2d8f8ee6 Mon Sep 17 00:00:00 2001 From: Alexander Karatarakis Date: Mon, 27 Feb 2017 15:45:56 -0800 Subject: Move some functions from vcpkglib.h to Paragraphs.h --- toolsrc/src/Paragraphs.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 3 deletions(-) (limited to 'toolsrc/src/Paragraphs.cpp') diff --git a/toolsrc/src/Paragraphs.cpp b/toolsrc/src/Paragraphs.cpp index 0c41e0b5c..fdb583bcc 100644 --- a/toolsrc/src/Paragraphs.cpp +++ b/toolsrc/src/Paragraphs.cpp @@ -6,9 +6,7 @@ namespace vcpkg::Paragraphs { struct Parser { - Parser(const char* c, const char* e) : cur(c), end(e) - { - } + Parser(const char* c, const char* e) : cur(c), end(e) { } private: const char* cur; @@ -167,4 +165,56 @@ namespace vcpkg::Paragraphs { return Parser(str.c_str(), str.c_str() + str.size()).get_paragraphs(); } + + expected try_load_port(const fs::path& path) + { + try + { + auto pghs = get_paragraphs(path / "CONTROL"); + Checks::check_exit(pghs.size() == 1, "Invalid control file at %s\\CONTROL", path.string()); + return SourceParagraph(pghs[0]); + } + catch (std::runtime_error const&) {} + + return std::errc::no_such_file_or_directory; + } + + expected try_load_cached_package(const vcpkg_paths& paths, const package_spec& spec) + { + const fs::path path = paths.package_dir(spec) / "CONTROL"; + + auto control_contents_maybe = Files::read_contents(path); + if (auto control_contents = control_contents_maybe.get()) + { + std::vector> pghs; + try + { + pghs = parse_paragraphs(*control_contents); + } + catch (std::runtime_error) {} + Checks::check_exit(pghs.size() == 1, "Invalid control file at %s", path.string()); + return BinaryParagraph(pghs[0]); + } + return control_contents_maybe.error_code(); + } + + std::vector load_all_ports(const fs::path& ports_dir) + { + std::vector output; + for (auto it = fs::directory_iterator(ports_dir); it != fs::directory_iterator(); ++it) + { + const fs::path& path = it->path(); + expected source_paragraph = try_load_port(path); + if (auto srcpgh = source_paragraph.get()) + { + output.emplace_back(std::move(*srcpgh)); + } + else + { + Checks::exit_with_message("Error loading port from %s: %s", path.generic_string(), source_paragraph.error_code().message()); + } + } + + return output; + } } -- cgit v1.2.3