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 *str) noexcept
 Sets a template. More...
 
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 template and writes it to str. More...
 
template<size_t N>
errno_t Resolve (size_t *written, char(&str)[N]) const noexcept
 Calls Resolve(written, str, N).
 

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 usage is shown below.
UriTemplate tmpl;
errno_t e = templ.SetTemplate("http://www.example.com/foo{?query,number}");
if (nlib_is_error(e)) { ERROR; }
e = templ.SetParameter("query", "mycelium");
if (nlib_is_error(e)) { ERROR; }
e = templ.SetParameter("number", "100");
if (nlib_is_error(e)) { ERROR; }
char str[256];
e = templ.Resolve(str);
if (nlib_is_error(e)) { ERROR; }
// "http://www.example.com/foo?query=mycelium&number=100"gets written to 'str.'
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 13 of file UriTemplate.h.

Member Function Documentation

§ Resolve()

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

Takes the string with assigned values in the 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.

§ 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 *  str)
noexcept

Sets a template.

Parameters
[in]strThe template string.
Return values
0No error occurred.
EINVALIndicates that str is NULL.
EILSEQA variable list inside braces ( { } ) is invalid.
EIOThe 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: