From 9e0dafbd93ad2961a8343d977a27e44fef18bbdc Mon Sep 17 00:00:00 2001 From: Norman Feske Date: Mon, 18 Dec 2017 16:05:45 +0100 Subject: [PATCH] depot_query: move 'Archive' utils to include/depot This enables other depot tools to use the same utilities. --- repos/gems/include/depot/archive.h | 95 ++++++++++++++++++++++++++ repos/gems/src/app/depot_query/main.cc | 74 +------------------- 2 files changed, 97 insertions(+), 72 deletions(-) create mode 100644 repos/gems/include/depot/archive.h diff --git a/repos/gems/include/depot/archive.h b/repos/gems/include/depot/archive.h new file mode 100644 index 0000000000..8278beb5fe --- /dev/null +++ b/repos/gems/include/depot/archive.h @@ -0,0 +1,95 @@ +/* + * \brief Utilities to handle depot-archive paths + * \author Norman Feske + * \date 2017-12-18 + */ + +/* + * Copyright (C) 2017 Genode Labs GmbH + * + * This file is part of the Genode OS framework, which is distributed + * under the terms of the GNU Affero General Public License version 3. + */ + +#ifndef _INCLUDE__DEPOT__ARCHIVE_H_ +#define _INCLUDE__DEPOT__ARCHIVE_H_ + +/* Genode includes */ +#include + +namespace Depot { + using namespace Genode; + struct Archive; +} + + +struct Depot::Archive +{ + typedef String<100> Path; + typedef String<64> User; + typedef String<80> Name; + typedef String<40> Version; + + enum Type { PKG, RAW, SRC }; + + struct Unknown_archive_type : Exception { }; + + /** + * Return Nth path element + * + * The first path element corresponds to n == 0. + */ + template + static STRING _path_element(Path const &path, unsigned n) + { + char const *s = path.string(); + + /* skip 'n' path elements */ + for (; n > 0; n--) { + + /* search '/' */ + while (*s && *s != '/') + s++; + + if (*s == 0) + return STRING(); + + /* skip '/' */ + s++; + } + + /* find '/' marking the end of the path element */ + unsigned i = 0; + while (s[i] != 0 && s[i] != '/') + i++; + + return STRING(Cstring(s, i)); + } + + /** + * Return archive user of depot-local path + */ + static User user(Path const &path) { return _path_element(path, 0); } + + /** + * Return archive type of depot-local path + * + * \throw Unknown_archive_type + */ + static Type type(Path const &path) + { + typedef String<8> Name; + Name const name = _path_element(path, 1); + + if (name == "src") return SRC; + if (name == "pkg") return PKG; + if (name == "raw") return RAW; + + throw Unknown_archive_type(); + } + + static Name name (Path const &path) { return _path_element(path, 2); } + static Version version(Path const &path) { return _path_element(path, 3); } +}; + +#endif /* _INCLUDE__DEPOT__ARCHIVE_H_ */ diff --git a/repos/gems/src/app/depot_query/main.cc b/repos/gems/src/app/depot_query/main.cc index 1c10611b30..af84736aa0 100644 --- a/repos/gems/src/app/depot_query/main.cc +++ b/repos/gems/src/app/depot_query/main.cc @@ -17,11 +17,11 @@ #include #include #include +#include namespace Depot_query { - using namespace Genode; + using namespace Depot; struct Recursion_limit; - struct Archive; struct Dependencies; struct Main; } @@ -62,76 +62,6 @@ class Depot_query::Recursion_limit : Noncopyable }; -struct Depot_query::Archive -{ - typedef String<100> Path; - typedef String<64> User; - typedef String<80> Name; - typedef String<40> Version; - - enum Type { PKG, RAW, SRC }; - - struct Unknown_archive_type : Exception { }; - - /** - * Return Nth path element - * - * The first path element corresponds to n == 0. - */ - template - static STRING _path_element(Path const &path, unsigned n) - { - char const *s = path.string(); - - /* skip 'n' path elements */ - for (; n > 0; n--) { - - /* search '/' */ - while (*s && *s != '/') - s++; - - if (*s == 0) - return STRING(); - - /* skip '/' */ - s++; - } - - /* find '/' marking the end of the path element */ - unsigned i = 0; - while (s[i] != 0 && s[i] != '/') - i++; - - return STRING(Cstring(s, i)); - } - - /** - * Return archive user of depot-local path - */ - static User user(Path const &path) { return _path_element(path, 0); } - - /** - * Return archive type of depot-local path - * - * \throw Unknown_archive_type - */ - static Type type(Path const &path) - { - typedef String<8> Name; - Name const name = _path_element(path, 1); - - if (name == "src") return SRC; - if (name == "pkg") return PKG; - if (name == "raw") return RAW; - - throw Unknown_archive_type(); - } - - static Name name (Path const &path) { return _path_element(path, 2); } - static Version version(Path const &path) { return _path_element(path, 3); } -}; - - /** * Collection of dependencies *