Navigation Menu

Skip to content

Commit

Permalink
Uri: Improved API for compositing textual URIs
Browse files Browse the repository at this point in the history
Folded logic for Uri::debugPrint() into Uri::compose() and then
removed the former.
  • Loading branch information
danij-deng committed Feb 26, 2013
1 parent 379ad41 commit f9fc1d4
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 106 deletions.
33 changes: 12 additions & 21 deletions doomsday/api/api_uri.h
Expand Up @@ -50,14 +50,17 @@
///@}

/**
* @defgroup printUriFlags Print Uri Flags
* @defgroup uriComposeAsTextFlags Uri Compose As Text Flags
* @ingroup base
*
* Flags which determine how a textual URI representation is composited.
*/
///@{
#define UPF_OUTPUT_RESOLVED 0x1 ///< Include the resolved path in the output.
#define UPF_TRANSFORM_PATH_MAKEPRETTY 0x2 ///< Transform paths making them "pretty".
#define UCTF_OMITSCHEME 0x1 ///< Exclude the scheme.
#define UCTF_OMITPATH 0x2 ///< Exclude the path.
#define UCTF_DECODEPATH 0x4 ///< Decode percent-endcoded characters in the path.

#define DEFAULT_PRINTURIFLAGS (UPF_OUTPUT_RESOLVED|UPF_TRANSFORM_PATH_MAKEPRETTY)
#define DEFAULT_URI_COMPOSE_AS_TEXT_FLAGS 0
///@}

struct uri_s; // The uri instance (opaque).
Expand Down Expand Up @@ -188,8 +191,12 @@ DENG_API_TYPEDEF(Uri) // v1
* the resultant string.
*
* @param uri Uri instance.
* @param flags @ref uriComposeAsTextFlags.
*
* @return Plain-text String representation.
*/
AutoStr* (*Compose2)(Uri const* uri, int flags);

AutoStr* (*Compose)(Uri const* uri);

/**
Expand Down Expand Up @@ -243,20 +250,6 @@ DENG_API_TYPEDEF(Uri) // v1
*/
void (*ReadWithDefaultScheme)(Uri* uri, Reader* reader, char const* defaultScheme);

/**
* Print debug output for @a uri.
*
* @param uri Uri instance.
* @param indent Number of characters to indent the print output.
* @param flags @ref printUriFlags
* @param unresolvedText Text string to be printed if @a uri is not resolvable.
*/
void (*DebugPrint3)(Uri const* uri, int indent, int flags, char const* unresolvedText);

void (*DebugPrint2)(Uri const* uri, int indent, int flags/*, use the default unresolved text */);

void (*DebugPrint)(Uri const* uri, int indent/*, flags = DEFAULT_PRINTURIFLAGS */);

} DENG_API_T(Uri);

// Macros for accessing exported functions.
Expand All @@ -279,15 +272,13 @@ DENG_API_TYPEDEF(Uri) // v1
#define Uri_SetUri _api_Uri.SetUri
#define Uri_SetUriStr _api_Uri.SetUriStr
#define Uri_Compose _api_Uri.Compose
#define Uri_Compose2 _api_Uri.Compose2
#define Uri_ToString _api_Uri.ToString
#define Uri_Equality _api_Uri.Equality
#define Uri_Write2 _api_Uri.Write2
#define Uri_Write _api_Uri.Write
#define Uri_Read _api_Uri.Read
#define Uri_ReadWithDefaultScheme _api_Uri.ReadWithDefaultScheme
#define Uri_DebugPrint3 _api_Uri.DebugPrint3
#define Uri_DebugPrint2 _api_Uri.DebugPrint2
#define Uri_DebugPrint _api_Uri.DebugPrint
#endif

// Internal access.
Expand Down
40 changes: 16 additions & 24 deletions doomsday/client/include/uri.hh
Expand Up @@ -60,16 +60,17 @@ public:
DENG2_SUB_ERROR(ResolveError, ResolveSymbolError);

/**
* Flags for printing URIs.
* Flags determining the composition of textual representation:
*/
enum PrintFlag
enum ComposeAsTextFlag
{
OutputResolved = 0x1, ///< Include the resolved path in the output.
PrettifyPath = 0x2, ///< Transform paths making them "pretty".
OmitScheme = 0x1, ///< Exclude the scheme.
OmitPath = 0x2, ///< Exclude the path.
DecodePath = 0x4, ///< Decode percent-endcoded characters in the path.

DefaultPrintFlags = OutputResolved | PrettifyPath
DefaultComposeAsTextFlags = 0
};
Q_DECLARE_FLAGS(PrintFlags, PrintFlag)
Q_DECLARE_FLAGS(ComposeAsTextFlags, ComposeAsTextFlag)

public:
/**
Expand Down Expand Up @@ -275,36 +276,27 @@ public:
Uri &setUri(String newUri, resourceclassid_t defaultResourceClass = RC_UNKNOWN, QChar sep = '/');

/**
* Compose from this URI a plain-text representation. Any internal encoding
* method or symbolic identifiers will be left unchanged in the resultant
* string (not decoded, not resolved).
* Compose from this URI a plain-text representation. Any symbolic identifiers
* will be left unchanged in the resultant string (not resolved).
*
* @param compositionFlags Flags determining how the textual representation
* is composited.
* @param sep Character to use to replace path segment separators.
*
* @return Plain-text String representation.
*
* @todo Should define a set of flags to determine which components of the
* URI to include in the string, like @ref debugPrint() -ds
*/
String compose(QChar sep = '/') const;
String compose(ComposeAsTextFlags compositionFlags = DefaultComposeAsTextFlags,
QChar sep = '/') const;

/**
* Transform the URI into a human-friendly representation. Percent-encoded
* symbols are decoded.
*
* @return Human-friendly representation of the URI.
*/
String asText() const;

/**
* Print debug ouput for the URI.
*
* @param indent Number of characters to indent the print output.
* @param flags @ref printUriFlags
* @param unresolvedText Text string to be printed if not resolvable.
* Same as @code compose(DefaultComposeAsTextFlags | DecodePath); @endcode
*/
void debugPrint(int indent, PrintFlags flags = DefaultPrintFlags,
String unresolvedText = "") const;
String asText() const;

// Implements LogEntry::Arg::Base.
LogEntry::Arg::Type logEntryArgType() const { return LogEntry::Arg::STRING; }
Expand All @@ -317,7 +309,7 @@ private:
DENG2_PRIVATE(d)
};

Q_DECLARE_OPERATORS_FOR_FLAGS(Uri::PrintFlags)
Q_DECLARE_OPERATORS_FOR_FLAGS(Uri::ComposeAsTextFlags)

} // namespace de

Expand Down
72 changes: 42 additions & 30 deletions doomsday/client/src/api_uri.cpp
Expand Up @@ -55,18 +55,21 @@ static void writeUri(const Uri* uri, Writer* writer, int omitComponents = 0)
Str_Write(de::DualString(self->path()).toStrUtf8(), writer);
}

#undef Uri_Clear
Uri* Uri_Clear(Uri* uri)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->clear());
}

#undef Uri_SetScheme
Uri* Uri_SetScheme(Uri* uri, char const* scheme)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->setScheme(scheme));
}

#undef Uri_SetPath
Uri* Uri_SetPath(Uri* uri, char const* path)
{
SELF(uri);
Expand Down Expand Up @@ -94,27 +97,32 @@ static void readUri(Uri* uri, Reader* reader, de::String defaultScheme = "")
Uri_SetPath (uri, Str_Text(&path ));
}

#undef Uri_NewWithPath2
Uri* Uri_NewWithPath2(char const* path, resourceclassid_t defaultResourceClass)
{
return reinterpret_cast<Uri*>( new de::Uri(path, defaultResourceClass) );
}

#undef Uri_NewWithPath
Uri* Uri_NewWithPath(char const* path)
{
return reinterpret_cast<Uri*>( new de::Uri(path) );
}

#undef Uri_New
Uri* Uri_New(void)
{
return reinterpret_cast<Uri*>( new de::Uri() );
}

#undef Uri_Dup
Uri* Uri_Dup(Uri const* other)
{
DENG_ASSERT(other);
return reinterpret_cast<Uri*>( new de::Uri(*(TOINTERNAL_CONST(other))) );
}

#undef Uri_FromReader
Uri* Uri_FromReader(Reader* reader)
{
DENG_ASSERT(reader);
Expand All @@ -127,6 +135,7 @@ Uri* Uri_FromReader(Reader* reader)
return uri;
}

#undef Uri_Delete
void Uri_Delete(Uri* uri)
{
if(uri)
Expand All @@ -136,6 +145,7 @@ void Uri_Delete(Uri* uri)
}
}

#undef Uri_Copy
Uri* Uri_Copy(Uri* uri, Uri const* other)
{
SELF(uri);
Expand All @@ -144,19 +154,22 @@ Uri* Uri_Copy(Uri* uri, Uri const* other)
return reinterpret_cast<Uri*>(self);
}

#undef Uri_Equality
boolean Uri_Equality(Uri const* uri, Uri const* other)
{
SELF_CONST(uri);
DENG_ASSERT(other);
return *self == (*(TOINTERNAL_CONST(other)));
}

#undef Uri_IsEmpty
boolean Uri_IsEmpty(Uri const* uri)
{
SELF_CONST(uri);
return self->isEmpty();
}

#undef Uri_Resolved
AutoStr* Uri_Resolved(Uri const* uri)
{
SELF_CONST(uri);
Expand All @@ -171,62 +184,88 @@ AutoStr* Uri_Resolved(Uri const* uri)
return AutoStr_NewStd();
}

#undef Uri_Scheme
const Str* Uri_Scheme(Uri const* uri)
{
SELF_CONST(uri);
return self->schemeStr();
}

#undef Uri_Path
const Str* Uri_Path(Uri const* uri)
{
SELF_CONST(uri);
return self->pathStr();
}

#undef Uri_SetUri2
Uri* Uri_SetUri2(Uri* uri, char const* path, resourceclassid_t defaultResourceClass)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->setUri(path, defaultResourceClass));
}

#undef Uri_SetUri
Uri* Uri_SetUri(Uri* uri, char const* path)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->setUri(path));
}

#undef Uri_SetUriStr
Uri* Uri_SetUriStr(Uri* uri, ddstring_t const* path)
{
SELF(uri);
return reinterpret_cast<Uri*>(&self->setUri(Str_Text(path)));
}

static de::Uri::ComposeAsTextFlags translateFlags(int flags)
{
de::Uri::ComposeAsTextFlags catf;
if(flags & UCTF_OMITSCHEME) catf |= de::Uri::OmitScheme;
if(flags & UCTF_OMITPATH) catf |= de::Uri::OmitPath;
if(flags & UCTF_DECODEPATH) catf |= de::Uri::DecodePath;
return catf;
}

#undef Uri_Compose2
AutoStr* Uri_Compose2(Uri const* uri, int flags)
{
SELF_CONST(uri);
return AutoStr_FromTextStd(self->compose(translateFlags(flags)).toUtf8().constData());
}

#undef Uri_Compose
AutoStr* Uri_Compose(Uri const* uri)
{
SELF_CONST(uri);
return AutoStr_FromTextStd(self->compose().toUtf8().constData());
}

#undef Uri_ToString
AutoStr* Uri_ToString(Uri const* uri)
{
SELF_CONST(uri);
return AutoStr_FromTextStd(self->asText().toUtf8().constData());
}

#undef Uri_Write2
void Uri_Write2(Uri const* uri, struct writer_s* writer, int omitComponents)
{
DENG_ASSERT(uri);
DENG_ASSERT(writer);
writeUri(uri, writer, omitComponents);
}

#undef Uri_Write
void Uri_Write(Uri const* uri, struct writer_s* writer)
{
DENG_ASSERT(uri);
DENG_ASSERT(writer);
writeUri(uri, writer);
}

#undef Uri_Read
Uri* Uri_Read(Uri* uri, struct reader_s* reader)
{
DENG_ASSERT(uri);
Expand All @@ -235,39 +274,14 @@ Uri* Uri_Read(Uri* uri, struct reader_s* reader)
return uri;
}

#undef Uri_ReadWithDefaultScheme
void Uri_ReadWithDefaultScheme(Uri* uri, struct reader_s* reader, char const* defaultScheme)
{
DENG_ASSERT(uri);
DENG_ASSERT(reader);
readUri(uri, reader, defaultScheme);
}

static de::Uri::PrintFlags translateFlags(int flags)
{
de::Uri::PrintFlags pf;
if(flags & UPF_OUTPUT_RESOLVED) pf |= de::Uri::OutputResolved;
if(flags & UPF_TRANSFORM_PATH_MAKEPRETTY) pf |= de::Uri::PrettifyPath;
return pf;
}

void Uri_DebugPrint3(Uri const* uri, int indent, int flags, char const* unresolvedText)
{
SELF_CONST(uri);
self->debugPrint(indent, translateFlags(flags), unresolvedText);
}

void Uri_DebugPrint2(Uri const* uri, int indent, int flags)
{
SELF_CONST(uri);
self->debugPrint(indent, translateFlags(flags));
}

void Uri_DebugPrint(Uri const* uri, int indent)
{
SELF_CONST(uri);
self->debugPrint(indent);
}

DENG_DECLARE_API(Uri) =
{
{ DE_API_URI },
Expand All @@ -288,14 +302,12 @@ DENG_DECLARE_API(Uri) =
Uri_SetUri2,
Uri_SetUri,
Uri_SetUriStr,
Uri_Compose2,
Uri_Compose,
Uri_ToString,
Uri_Equality,
Uri_Write2,
Uri_Write,
Uri_Read,
Uri_ReadWithDefaultScheme,
Uri_DebugPrint3,
Uri_DebugPrint2,
Uri_DebugPrint
Uri_ReadWithDefaultScheme
};

0 comments on commit f9fc1d4

Please sign in to comment.