utils.h File Reference


Detailed Description

Various useful utility functions.

Definition in file utils.h.

#include <stdexcept>
#include <iostream>
#include <sstream>
#include <hn/osdep.h>
#include <hn/endian.h>
#include <hn/gettickcount.h>
#include <sys/stat.h>
#include <boost/static_assert.hpp>
#include <boost/scoped_array.hpp>
#include <boost/shared_array.hpp>
#include <boost/filesystem/path.hpp>

Include dependency graph for utils.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.


Namespaces

namespace  Utils

Specializations for reading various datas

template<class T>
getVal (std::istream &i)
 Primary template is not implemented, and causes compile-time error if instanciated.
template<class T>
getVal (std::istream &i, uint16_t len)

Writing function template specializations.

template<>
void putVal (std::ostream &o, uint8_t val)
template<>
void putVal (std::ostream &o, uint16_t val)
template<>
void putVal (std::ostream &o, uint32_t val)
template<>
void putVal (std::ostream &o, uint64_t val)
template<>
void putVal (std::ostream &o, float val)
template<>
void putVal (std::ostream &o, const std::string &str)
template<>
void putVal (std::ostream &o, std::string str)
void putVal (std::ostream &o, const std::string &str, uint16_t len)
void putVal (std::ostream &o, const char *const str, uint16_t len)
void putVal (std::ostream &o, const uint8_t *const str, uint16_t len)
void putVal (std::ostream &o, const boost::shared_array< char > &str, uint16_t len)

Functions

std::string decode (const std::string &data)
std::string decode (const std::string &data, uint32_t length)
std::string decode (boost::shared_array< char > data, uint32_t len)
std::string encode (const std::string &data)
std::string encode (const std::string &data, uint32_t length)
std::string encode (boost::shared_array< char > data, uint32_t len)
void copyString (const char *src, char *&dest)
 Copy string from source to destination, overwriting destination memory.
uint64_t getFileSize (const boost::filesystem::path &loc)
 Retrieve the size of a file.
uint32_t getModDate (const boost::filesystem::path &loc)
 Retrieve the last modification date of a file.
template<class T>
void putVal (std::ostream &o, T val)
 Write.
template<class T>
std::ostream & hexDump (std::ostream &o, T val)
 Dumps the hexadecimal representation of a value to stream.
template<typename T>
std::string hexDump (T val)
 Creates hexadecimal dump of value into a string.
template<typename T>
std::string hexDump (T *val)
 Specalization - for dumping pointer types.
std::string bytesToString (uint64_t bytes)
 Convert bytes to string representation.

Function Documentation

std::string bytesToString uint64_t  bytes  )  [inline]
 

Convert bytes to string representation.

Parameters:
bytes Amount of bytes to check
Returns:
Human-readable string, e.g. "4.56 GB"

Definition at line 392 of file utils.h.

Referenced by SchedBase::handleEvents(), PartData::printCompleted(), and FilesList::~FilesList().

void copyString const char *  src,
char *&  dest
[inline]
 

Copy string from source to destination, overwriting destination memory.

This function assumes source string is null-terminated, and copies until the terminating zero (including it). The destination pointer is passed by reference and modified as neccesery during memory reallocations to point to newly allocated memory region.

Parameters:
src Source, null-terminated string
dest Destination

Definition at line 90 of file utils.h.

Referenced by AudioMetaData::setAlbum(), AudioMetaData::setArtist(), ImageMetaData::setComment(), ArchiveMetaData::setComment(), AudioMetaData::setComment(), AudioMetaData::setComposer(), AudioMetaData::setCopyright(), AudioMetaData::setEncoded(), AudioMetaData::setGenre(), AudioMetaData::setOrigArtist(), AudioMetaData::setTitle(), and AudioMetaData::setUrl().

std::string decode boost::shared_array< char >  data,
uint32_t  len
[inline]
 

Definition at line 67 of file utils.h.

References Utils::decode().

Here is the call graph for this function:

std::string decode const std::string &  data,
uint32_t  length
[inline]
 

Definition at line 64 of file utils.h.

References Utils::decode().

Here is the call graph for this function:

std::string decode const std::string &  data  )  [inline]
 

Definition at line 61 of file utils.h.

References Utils::decode().

Here is the call graph for this function:

std::string encode boost::shared_array< char >  data,
uint32_t  len
[inline]
 

Definition at line 76 of file utils.h.

References Utils::encode().

Here is the call graph for this function:

std::string encode const std::string &  data,
uint32_t  length
[inline]
 

Definition at line 73 of file utils.h.

References Utils::encode().

Here is the call graph for this function:

std::string encode const std::string &  data  )  [inline]
 

Definition at line 70 of file utils.h.

References Utils::encode().

Here is the call graph for this function:

uint64_t getFileSize const boost::filesystem::path &  loc  )  [inline]
 

Retrieve the size of a file.

Parameters:
path Full path to the file being interested in.
Returns:
Size of the file.

Definition at line 106 of file utils.h.

Referenced by SharedFile::findMetaData(), HashWork::finish(), and HashWork::initState().

uint32_t getModDate const boost::filesystem::path &  loc  )  [inline]
 

Retrieve the last modification date of a file.

Parameters:
path Full path to the file being interested in.
Returns:
Last modification time as reported by the OS.

Definition at line 118 of file utils.h.

Referenced by SharedFile::findMetaData(), HashWork::finish(), HydraNode::init(), PartData::PartData(), and SharedFile::read().

template<class T>
std::string Utils::getVal std::istream &  i,
uint16_t  len
[inline]
 

Definition at line 208 of file utils.h.

template<class T>
std::string Utils::getVal std::istream &  i  )  [inline]
 

Primary template is not implemented, and causes compile-time error if instanciated.

Definition at line 137 of file utils.h.

template<typename T>
std::string hexDump T *  val  ) 
 

Specalization - for dumping pointer types.

Note: using p argument at boost::format generally works better and is more readable for this purpose...

Parameters:
val Value to dump
Returns:
Hexadeciman representation of value

Definition at line 338 of file utils.h.

template<typename T>
std::string hexDump val  ) 
 

Creates hexadecimal dump of value into a string.

Parameters:
val Value to convert to hex
Returns:
String containing the hex representation of the value

Definition at line 321 of file utils.h.

template<class T>
std::ostream& hexDump std::ostream &  o,
val
 

Dumps the hexadecimal representation of a value to stream.

Parameters:
o Stream to write to
val Value to write
Returns:
The original stream

Definition at line 307 of file utils.h.

void putVal std::ostream &  o,
const boost::shared_array< char > &  str,
uint16_t  len
[inline]
 

Definition at line 291 of file utils.h.

References CHECK_THROW.

void putVal std::ostream &  o,
const uint8_t *const   str,
uint16_t  len
[inline]
 

Definition at line 285 of file utils.h.

References CHECK_THROW.

void putVal std::ostream &  o,
const char *const   str,
uint16_t  len
[inline]
 

Definition at line 279 of file utils.h.

References CHECK_THROW.

void putVal std::ostream &  o,
const std::string &  str,
uint16_t  len
[inline]
 

Definition at line 274 of file utils.h.

template<>
void putVal std::ostream &  o,
std::string  str
[inline]
 

Definition at line 270 of file utils.h.

template<>
void putVal std::ostream &  o,
const std::string &  str
[inline]
 

Definition at line 265 of file utils.h.

template<>
void putVal std::ostream &  o,
float  val
[inline]
 

Definition at line 259 of file utils.h.

References SWAP32_ON_BE.

template<>
void putVal std::ostream &  o,
uint64_t  val
[inline]
 

Definition at line 253 of file utils.h.

References SWAP64_ON_BE.

template<>
void putVal std::ostream &  o,
uint32_t  val
[inline]
 

Definition at line 247 of file utils.h.

References SWAP32_ON_BE.

template<>
void putVal std::ostream &  o,
uint16_t  val
[inline]
 

Definition at line 241 of file utils.h.

References SWAP16_ON_BE.

template<>
void putVal std::ostream &  o,
uint8_t  val
[inline]
 

Definition at line 236 of file utils.h.

template<class T>
void putVal std::ostream &  o,
val
[inline]
 

Write.

Primary template not implemented and causes compile-time failure if instanciated.

Definition at line 229 of file utils.h.

Referenced by operator<<().