Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
format_utils.hxx File Reference
#include <string>
#include <string.h>
#include <stdint.h>

Go to the source code of this file.

Functions

char * integer_to_buffer (int value, char *buffer)
 Renders an integer to string, left-justified.
 
char * unsigned_integer_to_buffer (int value, char *buffer)
 Renders an unsigned integer to string, left-justified.
 
char * uint64_integer_to_buffer (uint64_t value, char *buffer)
 Renders an uint64_t to string, left-justified.
 
char * int64_integer_to_buffer (int64_t value, char *buffer)
 Renders an int64_t to string, left-justified.
 
char * unsigned_integer_to_buffer_hex (unsigned int value, char *buffer)
 Renders an unsigned integer to string, left-justified.
 
char * uint64_integer_to_buffer_hex (uint64_t value, char *buffer)
 Renders an uint64_t to string, left-justified.
 
char * int64_integer_to_buffer_hex (int64_t value, char *buffer)
 Renders an int64_t to string, left-justified.
 
string integer_to_string (int value, unsigned padding=0)
 Renders an integer to std::string, left-justified.
 
string uint64_to_string (uint64_t value, unsigned padding=0)
 Renders an uint64_t to std::string, left-justified.
 
string int64_to_string (int64_t value, unsigned padding=0)
 Renders an int64_t to std::string, left-justified.
 
string uint64_to_string_hex (uint64_t value, unsigned padding=0)
 Renders an uint64_t to std::string, left-justified.
 
string int64_to_string_hex (int64_t value, unsigned padding=0)
 Renders an int64_t to std::string, left-justified.
 
string string_to_hex (const string &arg)
 Converts a (binary) string into a sequence of hex digits.
 
size_t hex_to_string (const char *input, size_t len, string *output, bool ignore_nonhex=false)
 Converts hex bytes to binary representation.
 
string mac_to_string (uint8_t mac[6], char colon=':')
 Formats a MAC address to string.
 
string ipv4_to_string (uint8_t ip[4])
 Formats an IPv4 address to string.
 
string ipv4_to_string (uint32_t ip)
 Formats an IPv4 address to string.
 
template<unsigned int N>
void str_populate (char(&dst)[N], const char *src)
 Populates a character array with a C string.
 

Detailed Description

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  • Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  • Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Simple codefor some trivial formatting uses.

Author
Balazs Racz
Date
7 Feb 2016

Definition in file format_utils.hxx.

Function Documentation

◆ hex_to_string()

size_t hex_to_string ( const char *  input,
size_t  len,
string *  output,
bool  ignore_nonhex = false 
)

Converts hex bytes to binary representation.

Parameters
inputpoints to the input hexadecimal string
lenhow many bytes are there
outputparsed byte data will be appended here
ignore_nonhexif true, jumps over all nonhex characters. If false, stops at the first nonhex character.
Returns
number of ascii bytes consumed from the input. For example 0 if the first byte was not hex and ignore_nonhex=false. If the number of hex digits is not even, there will be data loss.

Definition at line 262 of file format_utils.cxx.

◆ int64_integer_to_buffer()

char * int64_integer_to_buffer ( int64_t  value,
char *  buffer 
)

Renders an int64_t to string, left-justified.

Parameters
buffermust be an at least 22 character long array.
valuewill be rendered into the buffer.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 156 of file format_utils.cxx.

◆ int64_integer_to_buffer_hex()

char * int64_integer_to_buffer_hex ( int64_t  value,
char *  buffer 
)

Renders an int64_t to string, left-justified.

Parameters
buffermust be an at least 18 character long array.
valuewill be rendered into the buffer.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 90 of file format_utils.cxx.

◆ int64_to_string()

string int64_to_string ( int64_t  value,
unsigned  padding = 0 
)

Renders an int64_t to std::string, left-justified.

Parameters
valuewill be rendered into the buffer.
paddingnumber of bytes that the resulting string should be.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 193 of file format_utils.cxx.

◆ int64_to_string_hex()

string int64_to_string_hex ( int64_t  value,
unsigned  padding = 0 
)

Renders an int64_t to std::string, left-justified.

Parameters
valuewill be rendered into the buffer.
paddingnumber of bytes that the resulting string should be.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 219 of file format_utils.cxx.

◆ integer_to_buffer()

char * integer_to_buffer ( int  value,
char *  buffer 
)

Renders an integer to string, left-justified.

Parameters
buffermust be an at
buffermust be an at least 10 character long array.
valuewill be rendered into the buffer.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 145 of file format_utils.cxx.

◆ integer_to_string()

string integer_to_string ( int  value,
unsigned  padding = 0 
)

Renders an integer to std::string, left-justified.

Parameters
valuewill be rendered into the buffer.
paddingnumber of bytes that the resulting string should be.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 167 of file format_utils.cxx.

◆ ipv4_to_string() [1/2]

string ipv4_to_string ( uint32_t  ip)
inline

Formats an IPv4 address to string.

Parameters
ipa uint32_t storing the IPv4 address. most significant bytes will be printed at the beginning.
Returns
a string containing a dot-separated printout of the given IPv4 address.

Definition at line 181 of file format_utils.hxx.

◆ ipv4_to_string() [2/2]

string ipv4_to_string ( uint8_t  ip[4])

Formats an IPv4 address to string.

Parameters
ipa 4-byte array storing the IPv4 address. ip[3] will be printed at the beginning.
Returns
a string containing a dot-separated printout of the given IPv4 address.

Definition at line 321 of file format_utils.cxx.

◆ mac_to_string()

string mac_to_string ( uint8_t  mac[6],
char  colon = ':' 
)

Formats a MAC address to string.

Works both for Ethernet addresses as well as for OpenLCB node IDs.

Parameters
maca 6-byte array storing the MAC address. mac[0] will be printed at the beginning.
colonsSpecifies byte separators. Leave as default (':') to print colons, an alternate character to use as separator, or set to 0 to exclude the seperators entirely.
Returns
a string containing a hexadecimal printout of the given MAC address.

Definition at line 296 of file format_utils.cxx.

◆ str_populate()

template<unsigned int N>
void str_populate ( char(&)  dst[N],
const char *  src 
)
inline

Populates a character array with a C string.

Copies the C string, appropriately truncating if it is too long and filling the remaining space with zeroes. Ensures that at least one null terminator character is present.

Parameters
dsta character array of fixed length, declared as char sdata[N]
srca C string to fill it with.

Definition at line 193 of file format_utils.hxx.

◆ string_to_hex()

string string_to_hex ( const string &  arg)

Converts a (binary) string into a sequence of hex digits.

Parameters
arginput string
Returns
string twice the length of arg with hex digits representing the original data.

Definition at line 232 of file format_utils.cxx.

◆ uint64_integer_to_buffer()

char * uint64_integer_to_buffer ( uint64_t  value,
char *  buffer 
)

Renders an uint64_t to string, left-justified.

Parameters
buffermust be an at least 21 character long array.
valuewill be rendered into the buffer.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 123 of file format_utils.cxx.

◆ uint64_integer_to_buffer_hex()

char * uint64_integer_to_buffer_hex ( uint64_t  value,
char *  buffer 
)

Renders an uint64_t to string, left-justified.

Parameters
buffermust be an at least 17 character long array.
valuewill be rendered into the buffer.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 68 of file format_utils.cxx.

◆ uint64_to_string()

string uint64_to_string ( uint64_t  value,
unsigned  padding = 0 
)

Renders an uint64_t to std::string, left-justified.

Parameters
valuewill be rendered into the buffer.
paddingnumber of bytes that the resulting string should be.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 180 of file format_utils.cxx.

◆ uint64_to_string_hex()

string uint64_to_string_hex ( uint64_t  value,
unsigned  padding = 0 
)

Renders an uint64_t to std::string, left-justified.

Parameters
valuewill be rendered into the buffer.
paddingnumber of bytes that the resulting string should be.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 206 of file format_utils.cxx.

◆ unsigned_integer_to_buffer()

char * unsigned_integer_to_buffer ( int  value,
char *  buffer 
)

Renders an unsigned integer to string, left-justified.

Parameters
buffermust be an at least 10 character long array.
valuewill be rendered into the buffer.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 101 of file format_utils.cxx.

◆ unsigned_integer_to_buffer_hex()

char * unsigned_integer_to_buffer_hex ( unsigned int  value,
char *  buffer 
)

Renders an unsigned integer to string, left-justified.

Parameters
buffermust be an at least 10 character long array.
valuewill be rendered into the buffer.
Returns
the pointer to the null character at the end of the rendering.

Definition at line 46 of file format_utils.cxx.