cpp-bencoding
Classes
bencoding Namespace Reference

Main namespace of the bencoding library. More...

Classes

class  BDictionary
 Representation of a dictionary. More...
 
class  BInteger
 Representation of an integer. More...
 
class  BItem
 Base class for all items (integers, strings, etc.). More...
 
class  BItemVisitor
 Base class for all visitors of the BItem subclasses. More...
 
class  BList
 Representation of a list. More...
 
class  BString
 Representation of a string. More...
 
class  Decoder
 Decoder of bencoded data. More...
 
class  DecodingError
 Exception thrown when there is an error during the decoding. More...
 
class  Encoder
 Data encoder. More...
 
class  PrettyPrinter
 Pretty printer of data. More...
 

Functions

Decoding Without Explicit Decoder Creation
std::unique_ptr< BItemdecode (const std::string &data)
 Decodes the given bencoded data and returns them. More...
 
std::unique_ptr< BItemdecode (std::istream &input)
 Reads all the data from the given input, decodes them and returns them. More...
 
Encoding Without Explicit Encoder Creation
std::string encode (std::shared_ptr< BItem > data)
 Encodes the given data and returns them. More...
 
Printing Without Explicit Printer Creation
std::string getPrettyRepr (std::shared_ptr< BItem > data, const std::string &indent)
 Returns a pretty representation of data. More...
 
Conversions
template<typename N >
bool strToNum (const std::string &str, N &num, std::ios_base &(*format)(std::ios_base &)=std::dec)
 Converts the given string into a number. More...
 
Data Reading
bool readUpTo (std::istream &stream, std::string &readData, char sentinel)
 Reads data from the given stream up to sentinel, which is left in stream. More...
 
bool readUntil (std::istream &stream, std::string &readData, char last)
 Reads data from the given stream until last, which is also read. More...
 
String Operations
std::string replace (const std::string &str, char what, const std::string &withWhat)
 Replaces all occurrences of what with withWhat in str and returns the resulting string. More...
 

Detailed Description

Main namespace of the bencoding library.

Function Documentation

◆ decode() [1/2]

std::unique_ptr< BItem > bencoding::decode ( const std::string &  data)

Decodes the given bencoded data and returns them.

This function can be handy if you just want to decode bencoded data without explicitly creating a decoder and calling decode() on it.

See Decoder::decode() for more details.

◆ decode() [2/2]

std::unique_ptr< BItem > bencoding::decode ( std::istream &  input)

Reads all the data from the given input, decodes them and returns them.

This function can be handy if you just want to decode bencoded data without explicitly creating a decoder and calling decode() on it.

See Decoder::decode() for more details.

◆ encode()

std::string bencoding::encode ( std::shared_ptr< BItem data)

Encodes the given data and returns them.

This function can be handy if you just want to encode data without explicitly creating an encoder and calling encode() on it.

See Encoder::encode() for more details.

◆ getPrettyRepr()

std::string bencoding::getPrettyRepr ( std::shared_ptr< BItem data,
const std::string &  indent 
)

Returns a pretty representation of data.

This function can be handy if you just want to pretty-print data without explicitly creating a pretty printer and calling encode() on it.

See PrettyPrinter::getPrettyRepr() for more details.

◆ readUntil()

bool bencoding::readUntil ( std::istream &  stream,
std::string &  readData,
char  last 
)

Reads data from the given stream until last, which is also read.

Parameters
[in]streamStream from which the data are read.
[out]readDataString into which the read data are stored.
[in]lastThe last character to be read.
Returns
true if all the data were read correctly up and including last, false otherwise.

In contrast to readUpTo(), last is also read into readData. If last is not found during the reading, this function returns false. Read data are appended into readData.

◆ readUpTo()

bool bencoding::readUpTo ( std::istream &  stream,
std::string &  readData,
char  sentinel 
)

Reads data from the given stream up to sentinel, which is left in stream.

Parameters
[in]streamStream from which the data are read.
[out]readDataString into which the read data are stored.
[in]sentinelThe data are up to this character.
Returns
true if all the data were read correctly up to sentinel, false otherwise.

sentinel is not read and is kept in the stream. If sentinel is not found during the reading, this function returns false. Read data are appended into readData.

◆ replace()

std::string bencoding::replace ( const std::string &  str,
char  what,
const std::string &  withWhat 
)

Replaces all occurrences of what with withWhat in str and returns the resulting string.

◆ strToNum()

template<typename N >
bool bencoding::strToNum ( const std::string &  str,
N &  num,
std::ios_base &(*)(std::ios_base &)  format = std::dec 
)

Converts the given string into a number.

Parameters
[in]strString to be converted into a number.
[out]numPlace to store the converted number.
[in]formatNumber format (e.g. std::dec or std::hex).
Returns
true if the conversion was successful, false otherwise.

If the conversion fails, num is left unchanged.