nlib
nn::nlib::UriTemplate Class Referencefinal

The class that supports URI Template (RFC 6570, Level 3). More...

#include "nn/nlib/UriTemplate.h"

Public Member Functions

errno_t SetTemplate (const char *first, const char *last) noexcept
 Sets a template. More...
 
errno_t SetTemplate (const char *str) noexcept
 A parameter omitted version of the above function which receives a null terminated string.
 
errno_t SetParameter (const char *param, const char *value) noexcept
 Sets parameters and their values. More...
 
void ResetParameter () noexcept
 Clears all settings for parameters and their values.
 
errno_t Resolve (size_t *written, char *str, size_t n) const noexcept
 Takes the string with assigned values in the URI template and writes it to str. More...
 
template<size_t N>
errno_t Resolve (size_t *written, char(&str)[N]) const noexcept
 A template overload of the above function.
 
std::pair< errno_t, size_t > Resolve (char *str, size_t n) const noexcept
 Takes the string with assigned values in the URI template and writes it to str. More...
 
template<size_t N>
std::pair< errno_t, size_t > Resolve (char(&str)[N]) const noexcept
 A template overload of the above function.
 
Constructor, Destructor, and Initialization
constexpr UriTemplate () noexcept
 Instantiates the object with default parameters (default constructor).
 
 ~UriTemplate () noexcept
 Destructor.
 
 UriTemplate (UriTemplate &&rhs)
 Instantiates the object (move constructor).
 
UriTemplateoperator= (UriTemplate &&rhs)
 Move assignment operator.
 
void Reset () noexcept
 Resets this object to the state immediately after the default constructor was executed.
 

Static Public Member Functions

static bool IsUriTemplate (const char *first, const char *last) noexcept
 Determines whether the string is a URI Template with a level up to Level 3. More...
 
static bool IsUriTemplate (const char *str) noexcept
 A parameter omitted version of the above function which receives a null terminated string.
 

Detailed Description

The class that supports URI Template (RFC 6570, Level 3).

Description
A URI template refers to text that consists of a URI string, as defined in RFC 6570, and variables each enclosed in braces ( { } ). Creating the actual URI is a simple task of instantiating the variables.
The UriTemplate class supports RFC 6570 Level 1, Level 2, and Level 3.
Sample code is provided below.
errno_t e = templ.SetTemplate("http://www.example.com/foo{?query,number}");
SUCCEED_IF(e == 0);
e = templ.SetParameter("query", "mycelium");
SUCCEED_IF(e == 0);
e = templ.SetParameter("number", "100");
SUCCEED_IF(e == 0);
char str[256];
auto rval = templ.Resolve(str);
SUCCEED_IF(rval.first == 0);
nlib_printf("Uri: %s\n", str);
/*
Output:
Uri: http://www.example.com/foo?query=mycelium&number=100
*/
For more information about URI Template, see the RFC 6570 documentation.
See also
https://tools.ietf.org/html/rfc6570 (RFC6570)
Examples:
misc/uri/uri.cpp.

Definition at line 29 of file UriTemplate.h.

Member Function Documentation

◆ IsUriTemplate()

nn::nlib::UriTemplate::IsUriTemplate ( const char *  first,
const char *  last 
)
staticnoexcept

Determines whether the string is a URI Template with a level up to Level 3.

Parameters
[in]firstThe first character of the string you want to inspect.
[in]lastThe last character of the string you want to inspect.
Return values
Returnstrue if the string is a URI Template, otherwise returns false.

◆ Resolve() [1/2]

nn::nlib::UriTemplate::Resolve ( size_t *  written,
char *  str,
size_t  n 
) const
noexcept

Takes the string with assigned values in the URI template and writes it to str.

Parameters
[out]writtenThe number of written characters (not including the null character).
[out]strThe buffer to which the string is stored.
[in]nThe size of the buffer.
Returns
Returns 0 on success.

◆ Resolve() [2/2]

nn::nlib::UriTemplate::Resolve ( char *  str,
size_t  n 
) const
inlinenoexcept

Takes the string with assigned values in the URI template and writes it to str.

Parameters
[out]strThe buffer to which the string is stored.
[in]nThe size of the buffer.
Returns
A pair of the error value and the number of written characters (not including the null characters).

Definition at line 50 of file UriTemplate.h.

◆ SetParameter()

nn::nlib::UriTemplate::SetParameter ( const char *  param,
const char *  value 
)
noexcept

Sets parameters and their values.

Parameters
[in]paramA variable name.
[in]valueA value.
Return values
0No error occurred.
EINVALparam is NULL or an empty string.
Description
An empty string entered for the value results in different behavior than NULL. If NULL, the value is undefined.

◆ SetTemplate()

nn::nlib::UriTemplate::SetTemplate ( const char *  first,
const char *  last 
)
noexcept

Sets a template.

Parameters
[in]firstThe first character of the template string.
[in]lastThe last character of the template string.
Return values
0No error occurred.
EINVALIndicates that str is NULL.
EILSEQA variable list inside braces ( { } ) is invalid.
ERANGEThe template string is too long.
ENOMEMIndicates that internal memory allocation failed.
Description
Sets a template. Characters that require percent-encoding are percent-encoded as appropriate.

The documentation for this class was generated from the following files: