Audio Processing Framework (APF) version 0.5.0
Functions
apf::str Namespace Reference

Helper functions for string manipulation. More...

Functions

template<typename T >
std::string A2S (const T &input)
 Converter "Anything to String". More...
 
template<typename out_T >
bool convert (std::istream &input, out_T &output)
 Convert a stream to a given type. More...
 
bool convert (std::istream &input, bool &output)
 This is a overloaded function for boolean output. More...
 
template<typename out_T >
bool S2A (const std::string &input, out_T &output)
 Converter "String to Anything". More...
 
bool S2A (const std::string &input, std::string &output)
 Overloaded function with a std::string as output. More...
 
template<typename out_T >
out_T S2RV (const std::string &input, out_T def)
 Converter "String to Return Value". More...
 
std::string S2RV (const std::string &input, const char *def)
 Overloaded function with C-string as default value. More...
 
template<typename out_T , typename in_T >
out_T S2RV (const in_T &input)
 Throwing version of S2RV(). More...
 
template<typename char_T , typename traits >
std::basic_ios< char_T, traits > & clear_iostate_except_eof (std::basic_ios< char_T, traits > &stream)
 Clear the state of a stream but leave eofbit as is. More...
 
template<int digits, typename char_T , typename traits , typename out_T >
std::basic_istream< char_T, traits > & convert_chars (std::basic_istream< char_T, traits > &input, out_T &output)
 Remove a specified number of characters from a stream and convert them to a numeric type. More...
 
template<typename char_T , typename traits >
std::basic_istream< char_T, traits > & remove_char (std::basic_istream< char_T, traits > &input, const char_T character)
 Remove a character from a stream and check if it is the one given as parameter. More...
 
template<typename char_T , typename traits >
std::basic_istream< char_T, traits > & remove_colon (std::basic_istream< char_T, traits > &input)
 Remove a colon from an input stream. More...
 
template<template< typename, typename, typename > class in_T, typename char_T , typename traits , typename Allocator , typename out_T >
bool string2time (const in_T< char_T, traits, Allocator > &input, out_T &output)
 Convert time string to numeric value in seconds. More...
 
template<typename char_T , typename out_T >
bool string2time (const char_T *input, out_T &output)
 Overloaded function for a character array. More...
 

Detailed Description

Helper functions for string manipulation.

Function Documentation

◆ A2S()

template<typename T >
std::string apf::str::A2S ( const T &  input)

Converter "Anything to String".

Converts a numeric value to a std::string. A boolean value is converted to either "true" or "false".

Template Parameters
Tgiven input type
Parameters
inputanything you want to convert to a std::string
Returns
If std::stringstream could handle it, a std::string representation of input, if something went wrong, an empty string.
Examples
jack_simpleprocessor.cpp, and simpleprocessor.h.

Definition at line 52 of file stringtools.h.

Referenced by apf::mex::convert(), apf::load_sndfile(), and apf::parameter_map::set().

◆ convert() [1/2]

template<typename out_T >
bool apf::str::convert ( std::istream &  input,
out_T &  output 
)
inline

Convert a stream to a given type.

If the item of interest is followed by anything non-whitespace, the conversion is invalid and false is returned.

Template Parameters
out_Tdesired output type
Parameters
[in,out]inputinput stream
[out]outputoutput
Returns
true on success
Note
If false is returned, output is unchanged.

Definition at line 69 of file stringtools.h.

Referenced by S2A().

◆ convert() [2/2]

bool apf::str::convert ( std::istream &  input,
bool &  output 
)
inline

This is a overloaded function for boolean output.

See also
convert()
Parameters
[in,out]inputinput stream
[out]outputoutput (boolean)
Returns
true on success
Note
If false is returned, output is unchanged.

Definition at line 90 of file stringtools.h.

◆ S2A() [1/2]

template<typename out_T >
bool apf::str::S2A ( const std::string &  input,
out_T &  output 
)

Converter "String to Anything".

Convert a std::string or C-string to a given numeric type. Also, convert "1", "true", "0" and "false" to the respective boolean values.

Template Parameters
out_Tdesired output type (must have an input stream operator!)
Parameters
inputa std::string or char*
[out]outputresult converted to the desired type.
Returns
true on success
Note
If false is returned, output is unchanged.
Examples
jack_simpleprocessor.cpp.

Definition at line 122 of file stringtools.h.

References convert().

Referenced by S2RV().

◆ S2A() [2/2]

bool apf::str::S2A ( const std::string &  input,
std::string &  output 
)
inline

Overloaded function with a std::string as output.

The input is simply assigned to the output.

Returns
always true

Definition at line 132 of file stringtools.h.

◆ S2RV() [1/3]

template<typename out_T >
out_T apf::str::S2RV ( const std::string &  input,
out_T  def 
)

Converter "String to Return Value".

Convert a string (either a zero-terminated char* or a std::string) to a given numeric type and return the result.

Template Parameters
out_Tdesired output type (must have an input stream operator!)
Parameters
inputstring to be converted.
defdefault value.
Returns
input converted to the desired type. If conversion failed, the default value def is returned.
Warning
If the result is equal to the default value def, there is no way to know if the conversion was successful, sorry! You have to use S2A() if you want to make sure.
See also
S2A()

Definition at line 152 of file stringtools.h.

References S2A().

Referenced by S2RV().

◆ S2RV() [2/3]

std::string apf::str::S2RV ( const std::string &  input,
const char *  def 
)
inline

Overloaded function with C-string as default value.

Returns
a std::string
See also
S2RV()

Definition at line 162 of file stringtools.h.

References S2A().

◆ S2RV() [3/3]

template<typename out_T , typename in_T >
out_T apf::str::S2RV ( const in_T &  input)

Throwing version of S2RV().

Template Parameters
int_Tstring type
out_Tdesired output type (must have an input stream operator!)
Parameters
inputstring to be converted.
Returns
input converted to the desired type. If conversion failed, an exception is thrown.
Exceptions
std::invalid_argumentif input cannot be converted to out_T.

Definition at line 178 of file stringtools.h.

References S2A(), and S2RV().

◆ clear_iostate_except_eof()

template<typename char_T , typename traits >
std::basic_ios< char_T, traits > & apf::str::clear_iostate_except_eof ( std::basic_ios< char_T, traits > &  stream)

Clear the state of a stream but leave eofbit as is.

It can be used as stream modifier to ignore failbit and badbit but still respect eofbit:

my_stream >> std::ws >> clear_iostate_except_eof;
std::basic_ios< char_T, traits > & clear_iostate_except_eof(std::basic_ios< char_T, traits > &stream)
Clear the state of a stream but leave eofbit as is.
Definition: stringtools.h:206

This may be useful because some implementations (e.g. libc++) set failbit if std::ws is used to extract whitespace from the end of a stream but no whitespace is present.

See also
http://stackoverflow.com/q/13423514/500098
Template Parameters
char_Tcharacter type of the stream (also used for the output)
traitstraits class for std::basic_ios
Parameters
[in,out]streamthe stream to manipulate
Returns
a reference to the modified stream.

Definition at line 206 of file stringtools.h.

Referenced by string2time().

◆ convert_chars()

template<int digits, typename char_T , typename traits , typename out_T >
std::basic_istream< char_T, traits > & apf::str::convert_chars ( std::basic_istream< char_T, traits > &  input,
out_T &  output 
)

Remove a specified number of characters from a stream and convert them to a numeric type.

If the stream flag skipws is set, leading whitespace is removed first.

Template Parameters
digitsnumber of digits to read. The rest of the template parameters are determined automatically.
char_Tcharacter type of the stream
traitstraits class for std::basic_istream
out_Tdesired (numerical) output type
Parameters
[in,out]inputinput stream
[out]outputresulting number
Returns
a reference to the modified stream.
Attention
You have to check the returned stream for std::ios_base::failbit to know if the conversion was successful.

Definition at line 228 of file stringtools.h.

◆ remove_char()

template<typename char_T , typename traits >
std::basic_istream< char_T, traits > & apf::str::remove_char ( std::basic_istream< char_T, traits > &  input,
const char_T  character 
)

Remove a character from a stream and check if it is the one given as parameter.

If the stream flag skipws is set, leading whitespace is removed first.

Template Parameters
char_Tcharacter type of the stream (also used for the output)
traitstraits class for std::basic_istream
Parameters
[in,out]inputinput stream
charactercharacter to remove
Returns
a reference to the modified stream.
Attention
You have to check the returned stream for std::ios_base::failbit to know if the conversion was successful.

Definition at line 270 of file stringtools.h.

Referenced by remove_colon().

◆ remove_colon()

template<typename char_T , typename traits >
std::basic_istream< char_T, traits > & apf::str::remove_colon ( std::basic_istream< char_T, traits > &  input)

Remove a colon from an input stream.

This function is just a convenient shortcut for remove_char(stream, ':'). Contrary to remove_char(), this can be used as a stream modifier like this:

int i; float f;
my_stream >> i >> remove_colon >> f;
std::basic_istream< char_T, traits > & remove_colon(std::basic_istream< char_T, traits > &input)
Remove a colon from an input stream.
Definition: stringtools.h:304

If the stream flag skipws is set, leading whitespace is removed first.

Template Parameters
char_Tcharacter type of the stream
traitstraits class for std::basic_istream
Parameters
[in,out]inputinput stream
Returns
a reference to the modified stream.
Attention
You have to check the returned stream for std::ios_base::failbit to know if there actually was a colon and that is was successfully removed.

Definition at line 304 of file stringtools.h.

References remove_char().

Referenced by string2time().

◆ string2time() [1/2]

template<template< typename, typename, typename > class in_T, typename char_T , typename traits , typename Allocator , typename out_T >
bool apf::str::string2time ( const in_T< char_T, traits, Allocator > &  input,
out_T &  output 
)

Convert time string to numeric value in seconds.

input can be in format "h:mm:ss.x" or "xx.x h|min|s|ms" or just in seconds. Decimals and hours are optional. Time can also be negative. Multiple whitespace is allowed before and after. See http://www.w3.org/TR/SMIL2/smil-timing.html#Timing-ClockValueSyntax for the similar SMIL time syntax.

Template Parameters
in_Tinput string type (e.g. std::string)
char_Tcharacter type of the input string (e.g. char)
traitstraits class for the string type in_T
Allocatorallocator for the string type in_T
out_Tdesired output type
Parameters
inputtime string (similar to SMIL format)
[out]outputnumeric result in seconds. This can be either of an integer or a floating point type. Conversion to an integer only works if the resulting value in seconds is a whole number.
Returns
true if conversion was successful.
Since
r404

Definition at line 330 of file stringtools.h.

References clear_iostate_except_eof(), and remove_colon().

Referenced by string2time().

◆ string2time() [2/2]

template<typename char_T , typename out_T >
bool apf::str::string2time ( const char_T *  input,
out_T &  output 
)

Overloaded function for a character array.

See also
string2time()
Template Parameters
char_Tcharacter type of the input array
out_Tdesired output type
Parameters
inputcharacter array holding time string
[out]outputsee above
Returns
true if conversion was successful.

Definition at line 458 of file stringtools.h.

References string2time().