From e3558f2df4712a308cff8b79bd838cc3db43473a Mon Sep 17 00:00:00 2001 From: danij Date: Fri, 1 Mar 2013 07:34:18 +0000 Subject: [PATCH] Uri: Added Uri::fromUserInput() Defined according to common functionality extracted from the material and texture resource collections (in libdeng1). --- doomsday/client/include/uri.hh | 25 ++++++++++++++++++-- doomsday/client/src/uri.cpp | 43 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/doomsday/client/include/uri.hh b/doomsday/client/include/uri.hh index 810787d7b2..927fc6db37 100644 --- a/doomsday/client/include/uri.hh +++ b/doomsday/client/include/uri.hh @@ -107,7 +107,7 @@ public: * never contain a scheme as a prefix, so @a resClass is mandatory. * * @param resClass Scheme for the URI. @c RC_UNKNOWN: resource locator - * guesses an appropriate scheme for this type of file. + * guesses an appropriate scheme for this. * * @param path Path of the URI. */ @@ -127,7 +127,7 @@ public: * @param nullTerminatedCStr String to be parsed. Assumed to be in * percent-encoded representation. */ - Uri(char const* nullTerminatedCStr); + Uri(char const *nullTerminatedCStr); /** * Construct a Uri instance by duplicating @a other. @@ -180,6 +180,27 @@ public: static Uri fromNativeDirPath(NativePath const &nativeDirPath, resourceclassid_t defaultResourceClass = RC_NULL); + /** + * Construct a Uri instance from a user supplied, variable-length list of UTF-8 + * C-style text string arguments. + * + * @param argv The arguments to be interpreted. All of which are assumed to + * be in a @em non-percent-encoded representation. + * + * Supported forms (where <> denote keyword component names): + * - [0: ":"] + * - [0: ""] (if @a knownScheme set) + * - [0: ""] + * - [0: "", 1: ""] + * + * @param argc The number of elements in @a argv. + * + * @param knownScheme Callback function used to identify known scheme names when + * attempting to resolve ambiguous cases (only the one argument + * is provided. + */ + static Uri fromUserInput(char **argv, int argc, bool (*knownScheme) (String name) = 0); + /** * Convert this URI to a text string. * diff --git a/doomsday/client/src/uri.cpp b/doomsday/client/src/uri.cpp index 2201cbb424..0e30171d78 100644 --- a/doomsday/client/src/uri.cpp +++ b/doomsday/client/src/uri.cpp @@ -256,6 +256,49 @@ Uri::Uri(char const *nullTerminatedCStr) : d(new Instance) setUri(nullTerminatedCStr); } +Uri Uri::fromUserInput(char **argv, int argc, bool (*knownScheme) (String name)) +{ + Uri output; + if(argv) + { + // [0: :] or [0: ] or [0: ]. + switch(argc) + { + case 1: { + // Try to extract the scheme and encode the rest of the path. + String rawUri(argv[0]); + int pos = rawUri.indexOf(':'); + if(pos >= 0) + { + output.setScheme(rawUri.left(pos)); + rawUri.remove(0, pos + 1); + output.setPath(Path(QString(QByteArray(rawUri.toUtf8()).toPercentEncoding()))); + } + // Just a scheme name? + else if(knownScheme && knownScheme(rawUri)) + { + output.setScheme(rawUri); + } + else + { + // Just a path. + output.setPath(Path(QString(QByteArray(rawUri.toUtf8()).toPercentEncoding()))); + } + break; } + + // [0: , 1: ] + case 2: + // Assign the scheme and encode the path. + output.setScheme(argv[0]); + output.setPath(Path(QString(QByteArray(argv[1]).toPercentEncoding()))); + break; + + default: break; + } + } + return output; +} + Uri::Uri(Uri const &other) : LogEntry::Arg::Base(), d(new Instance(*other.d)) {}