Skip to content

Commit

Permalink
Uri: Added Uri::fromUserInput()
Browse files Browse the repository at this point in the history
Defined according to common functionality extracted from the material
and texture resource collections (in libdeng1).
  • Loading branch information
danij-deng committed Mar 1, 2013
1 parent dfebb8d commit e3558f2
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
25 changes: 23 additions & 2 deletions doomsday/client/include/uri.hh
Expand Up @@ -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.
*/
Expand All @@ -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.
Expand Down Expand Up @@ -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: "<scheme>:<path>"]
* - [0: "<scheme>"] (if @a knownScheme set)
* - [0: "<path>"]
* - [0: "<scheme>", 1: "<path>"]
*
* @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.
*
Expand Down
43 changes: 43 additions & 0 deletions doomsday/client/src/uri.cpp
Expand Up @@ -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: <scheme>:<path>] or [0: <scheme>] or [0: <path>].
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: <scheme>, 1: <path>]
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))
{}

Expand Down

0 comments on commit e3558f2

Please sign in to comment.