Anoncoin  0.9.4
P2P Digital Currency
Classes | Macros | Functions | Variables
util.h File Reference
#include "compat.h"
#include "serialize.h"
#include "tinyformat.h"
#include <cstdio>
#include <exception>
#include <map>
#include <stdarg.h>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#include <boost/filesystem/path.hpp>
#include <boost/thread.hpp>
Include dependency graph for util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  CMedianFilter< T >
 Median filter over a stream of values. More...
 

Macros

#define BEGIN(a)   ((char*)&(a))
 
#define END(a)   ((char*)&((&(a))[1]))
 
#define UBEGIN(a)   ((unsigned char*)&(a))
 
#define UEND(a)   ((unsigned char*)&((&(a))[1]))
 
#define ARRAYLEN(array)   (sizeof(array)/sizeof((array)[0]))
 
#define PAIRTYPE(t1, t2)   std::pair<t1, t2>
 
#define MAX_PATH   1024
 
#define MSG_NOSIGNAL   0
 
#define strprintf   tfm::format
 
#define LogPrintf(...)   LogPrint(NULL, __VA_ARGS__)
 
#define MAKE_ERROR_AND_LOG_FUNC(n)
 
#define PRIO_MAX   20
 
#define THREAD_PRIORITY_LOWEST   PRIO_MAX
 
#define THREAD_PRIORITY_BELOW_NORMAL   2
 
#define THREAD_PRIORITY_NORMAL   0
 
#define THREAD_PRIORITY_ABOVE_NORMAL   (-2)
 

Functions

template<size_t nBytes, typename T >
T * alignup (T *p)
 
void MilliSleep (int64_t n)
 
void RandAddSeed ()
 
void RandAddSeedPerfmon ()
 
void SetupEnvironment ()
 
bool LogAcceptCategory (const char *category)
 
int LogPrintStr (const std::string &str)
 
void LogException (std::exception *pex, const char *pszThread)
 
void PrintExceptionContinue (std::exception *pex, const char *pszThread)
 
std::string FormatMoney (int64_t n, bool fPlus=false)
 
bool ParseMoney (const std::string &str, int64_t &nRet)
 
bool ParseMoney (const char *pszIn, int64_t &nRet)
 
std::string SanitizeString (const std::string &str)
 
std::vector< unsigned char > ParseHex (const char *psz)
 
std::vector< unsigned char > ParseHex (const std::string &str)
 
bool IsHex (const std::string &str)
 
std::vector< unsigned char > DecodeBase64 (const char *p, bool *pfInvalid=NULL)
 
std::string DecodeBase64 (const std::string &str)
 
std::string EncodeBase64 (const unsigned char *pch, size_t len)
 
std::string EncodeBase64 (const std::string &str)
 
std::vector< unsigned char > DecodeBase32 (const char *p, bool *pfInvalid=NULL)
 
std::string DecodeBase32 (const std::string &str)
 
std::string EncodeBase32 (const unsigned char *pch, size_t len)
 
std::string EncodeBase32 (const std::string &str)
 
void ParseParameters (int argc, const char *const argv[])
 
bool WildcardMatch (const char *psz, const char *mask)
 
bool WildcardMatch (const std::string &str, const std::string &mask)
 
void FileCommit (FILE *fileout)
 
bool TruncateFile (FILE *file, unsigned int length)
 
int RaiseFileDescriptorLimit (int nMinFD)
 
void AllocateFileRange (FILE *file, unsigned int offset, unsigned int length)
 
bool RenameOver (boost::filesystem::path src, boost::filesystem::path dest)
 
bool TryCreateDirectory (const boost::filesystem::path &p)
 
boost::filesystem::path GetDefaultDataDir ()
 
const boost::filesystem::path & GetDataDir (bool fNetSpecific=true)
 
boost::filesystem::path GetConfigFile ()
 
boost::filesystem::path GetQtStyleFile ()
 
boost::filesystem::path GetPidFile ()
 
void CreatePidFile (const boost::filesystem::path &path, pid_t pid)
 
void ReadConfigFile (std::map< std::string, std::string > &mapSettingsRet, std::map< std::string, std::vector< std::string > > &mapMultiSettingsRet)
 
boost::filesystem::path GetTempPath ()
 
void ShrinkDebugFile ()
 
int GetRandInt (int nMax)
 
uint64_t GetRand (uint64_t nMax)
 
uint256 GetRandHash ()
 
int64_t GetTime ()
 
void SetMockTime (int64_t nMockTimeIn)
 
int64_t GetAdjustedTime ()
 
int64_t GetTimeOffset ()
 
void AddTimeData (const CNetAddr &ip, int64_t nTime)
 
void runCommand (std::string strCommand)
 
std::string i64tostr (int64_t n)
 
std::string itostr (int n)
 
int64_t atoi64 (const char *psz)
 
int64_t atoi64 (const std::string &str)
 
int atoi (const std::string &str)
 
int roundint (double d)
 
int64_t roundint64 (double d)
 
int64_t abs64 (int64_t n)
 
template<typename T >
std::string HexStr (const T itbegin, const T itend, bool fSpaces=false)
 
template<typename T >
std::string HexStr (const T &vch, bool fSpaces=false)
 
template<typename T >
void PrintHex (const T pbegin, const T pend, const char *pszFormat="%s", bool fSpaces=true)
 
void PrintHex (const std::vector< unsigned char > &vch, const char *pszFormat="%s", bool fSpaces=true)
 
int64_t GetPerformanceCounter ()
 
int64_t GetTimeMillis ()
 
int64_t GetTimeMicros ()
 
std::string DateTimeStrFormat (const char *pszFormat, int64_t nTime)
 
template<typename T >
void skipspaces (T &it)
 
bool IsSwitchChar (char c)
 
std::string GetArg (const std::string &strArg, const std::string &strDefault)
 Return string argument or default value. More...
 
int64_t GetArg (const std::string &strArg, int64_t nDefault)
 Return integer argument or default value. More...
 
bool GetBoolArg (const std::string &strArg, bool fDefault)
 Return boolean argument or default value. More...
 
bool SoftSetArg (const std::string &strArg, const std::string &strValue)
 Set an argument if it doesn't already have a value. More...
 
bool SoftSetBoolArg (const std::string &strArg, bool fValue)
 Set a boolean argument if it doesn't already have a value. More...
 
void seed_insecure_rand (bool fDeterministic=false)
 Seed insecure_rand using the random pool. More...
 
template<typename T >
bool TimingResistantEqual (const T &a, const T &b)
 Timing-attack-resistant comparison. More...
 
void SetThreadPriority (int nPriority)
 
void RenameThread (const char *name)
 
uint32_t ByteReverse (uint32_t value)
 
template<typename Callable >
void LoopForever (const char *name, Callable func, int64_t msecs)
 
template<typename Callable >
void TraceThread (const char *name, Callable func)
 

Variables

std::map< std::string, std::string > mapArgs
 
std::map< std::string, std::vector< std::string > > mapMultiArgs
 
bool fDebug
 
bool fPrintToConsole
 
bool fPrintToDebugLog
 
bool fServer
 
std::string strMiscWarning
 
bool fNoListen
 
bool fLogTimestamps
 
volatile bool fReopenDebugLog
 
uint32_t insecure_rand_Rz
 MWC RNG of George Marsaglia This is intended to be fast. More...
 
uint32_t insecure_rand_Rw
 

Macro Definition Documentation

#define ARRAYLEN (   array)    (sizeof(array)/sizeof((array)[0]))

Definition at line 46 of file util.h.

#define BEGIN (   a)    ((char*)&(a))

Definition at line 42 of file util.h.

#define END (   a)    ((char*)&((&(a))[1]))

Definition at line 43 of file util.h.

#define LogPrintf (   ...)    LogPrint(NULL, __VA_ARGS__)

Definition at line 118 of file util.h.

#define MAKE_ERROR_AND_LOG_FUNC (   n)
Value:
/* Print to debug.log if -debug=category switch is given OR category is NULL. */ \
template<TINYFORMAT_ARGTYPES(n)> \
static inline int LogPrint(const char* category, const char* format, TINYFORMAT_VARARGS(n)) \
{ \
if(!LogAcceptCategory(category)) return 0; \
return LogPrintStr(tfm::format(format, TINYFORMAT_PASSARGS(n))); \
} \
/* Log error and return false */ \
template<TINYFORMAT_ARGTYPES(n)> \
static inline bool error(const char* format, TINYFORMAT_VARARGS(n)) \
{ \
LogPrintStr("ERROR: " + tfm::format(format, TINYFORMAT_PASSARGS(n)) + "\n"); \
return false; \
}
#define TINYFORMAT_PASSARGS(n)
Definition: tinyformat.h:323
#define TINYFORMAT_VARARGS(n)
Definition: tinyformat.h:322
#define TINYFORMAT_ARGTYPES(n)
Definition: tinyformat.h:321
bool LogAcceptCategory(const char *category)
Definition: util.cpp:240
int LogPrintStr(const std::string &str)
Definition: util.cpp:268
void format(FormatIterator &fmtIter)
Definition: tinyformat.h:870

Definition at line 123 of file util.h.

#define MAX_PATH   1024

Definition at line 73 of file util.h.

#define MSG_NOSIGNAL   0

Definition at line 77 of file util.h.

#define PAIRTYPE (   t1,
  t2 
)    std::pair<t1, t2>

Definition at line 49 of file util.h.

#define PRIO_MAX   20

Definition at line 481 of file util.h.

#define strprintf   tfm::format

Definition at line 117 of file util.h.

#define THREAD_PRIORITY_ABOVE_NORMAL   (-2)

Definition at line 486 of file util.h.

#define THREAD_PRIORITY_BELOW_NORMAL   2

Definition at line 484 of file util.h.

#define THREAD_PRIORITY_LOWEST   PRIO_MAX

Definition at line 483 of file util.h.

#define THREAD_PRIORITY_NORMAL   0

Definition at line 485 of file util.h.

#define UBEGIN (   a)    ((unsigned char*)&(a))

Definition at line 44 of file util.h.

#define UEND (   a)    ((unsigned char*)&((&(a))[1]))

Definition at line 45 of file util.h.

Function Documentation

int64_t abs64 ( int64_t  n)
inline

Definition at line 250 of file util.h.

Here is the caller graph for this function:

void AddTimeData ( const CNetAddr ip,
int64_t  nTime 
)

Definition at line 1246 of file util.cpp.

Here is the call graph for this function:

template<size_t nBytes, typename T >
T* alignup ( T *  p)

Definition at line 53 of file util.h.

void AllocateFileRange ( FILE *  file,
unsigned int  offset,
unsigned int  length 
)

Definition at line 1145 of file util.cpp.

Here is the caller graph for this function:

int atoi ( const std::string &  str)
inline

Definition at line 235 of file util.h.

Here is the caller graph for this function:

int64_t atoi64 ( const char *  psz)
inline

Definition at line 217 of file util.h.

Here is the caller graph for this function:

int64_t atoi64 ( const std::string &  str)
inline

Definition at line 226 of file util.h.

uint32_t ByteReverse ( uint32_t  value)
inline

Definition at line 502 of file util.h.

Here is the caller graph for this function:

void CreatePidFile ( const boost::filesystem::path &  path,
pid_t  pid 
)

Definition at line 1058 of file util.cpp.

Here is the caller graph for this function:

std::string DateTimeStrFormat ( const char *  pszFormat,
int64_t  nTime 
)

Definition at line 1411 of file util.cpp.

Here is the caller graph for this function:

std::vector<unsigned char> DecodeBase32 ( const char *  p,
bool *  pfInvalid = NULL 
)

Definition at line 752 of file util.cpp.

Here is the caller graph for this function:

std::string DecodeBase32 ( const std::string &  str)
std::vector<unsigned char> DecodeBase64 ( const char *  p,
bool *  pfInvalid = NULL 
)

Definition at line 599 of file util.cpp.

Here is the caller graph for this function:

std::string DecodeBase64 ( const std::string &  str)
std::string EncodeBase32 ( const unsigned char *  pch,
size_t  len 
)

Definition at line 688 of file util.cpp.

Here is the caller graph for this function:

std::string EncodeBase32 ( const std::string &  str)
std::string EncodeBase64 ( const unsigned char *  pch,
size_t  len 
)

Definition at line 548 of file util.cpp.

Here is the caller graph for this function:

std::string EncodeBase64 ( const std::string &  str)
void FileCommit ( FILE *  fileout)

Definition at line 1097 of file util.cpp.

Here is the caller graph for this function:

std::string FormatMoney ( int64_t  n,
bool  fPlus = false 
)

Definition at line 308 of file util.cpp.

Here is the caller graph for this function:

int64_t GetAdjustedTime ( )

Definition at line 1241 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

std::string GetArg ( const std::string &  strArg,
const std::string &  strDefault 
)

Return string argument or default value.

Parameters
strArgArgument to get (e.g. "-foo")
default(e.g. "1")
Returns
command-line argument or default value

Definition at line 506 of file util.cpp.

Here is the caller graph for this function:

int64_t GetArg ( const std::string &  strArg,
int64_t  nDefault 
)

Return integer argument or default value.

Parameters
strArgArgument to get (e.g. "-foo")
default(e.g. 1)
Returns
command-line argument (0 if invalid number) or default value

Definition at line 513 of file util.cpp.

Here is the call graph for this function:

bool GetBoolArg ( const std::string &  strArg,
bool  fDefault 
)

Return boolean argument or default value.

Parameters
strArgArgument to get (e.g. "-foo")
default(true or false)
Returns
command-line argument or default value

Definition at line 520 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

boost::filesystem::path GetConfigFile ( )

Definition at line 1007 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

const boost::filesystem::path& GetDataDir ( bool  fNetSpecific = true)

Definition at line 968 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

boost::filesystem::path GetDefaultDataDir ( )

Definition at line 936 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

int64_t GetPerformanceCounter ( )
inline

Definition at line 291 of file util.h.

Here is the caller graph for this function:

boost::filesystem::path GetPidFile ( )

Definition at line 1050 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

boost::filesystem::path GetQtStyleFile ( )

Definition at line 1014 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

uint64_t GetRand ( uint64_t  nMax)

Definition at line 186 of file util.cpp.

Here is the caller graph for this function:

uint256 GetRandHash ( )

Definition at line 206 of file util.cpp.

Here is the caller graph for this function:

int GetRandInt ( int  nMax)

Definition at line 201 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

boost::filesystem::path GetTempPath ( )

Definition at line 1339 of file util.cpp.

int64_t GetTime ( )

Definition at line 1220 of file util.cpp.

Here is the caller graph for this function:

int64_t GetTimeMicros ( )
inline

Definition at line 310 of file util.h.

Here is the caller graph for this function:

int64_t GetTimeMillis ( )
inline

Definition at line 304 of file util.h.

Here is the caller graph for this function:

int64_t GetTimeOffset ( )

Definition at line 1235 of file util.cpp.

Here is the caller graph for this function:

template<typename T >
std::string HexStr ( const T  itbegin,
const T  itend,
bool  fSpaces = false 
)

Definition at line 256 of file util.h.

Here is the caller graph for this function:

template<typename T >
std::string HexStr ( const T &  vch,
bool  fSpaces = false 
)
inline

Definition at line 275 of file util.h.

Here is the call graph for this function:

std::string i64tostr ( int64_t  n)
inline

Definition at line 207 of file util.h.

bool IsHex ( const std::string &  str)
bool IsSwitchChar ( char  c)
inline

Definition at line 325 of file util.h.

Here is the caller graph for this function:

std::string itostr ( int  n)
inline

Definition at line 212 of file util.h.

Here is the caller graph for this function:

bool LogAcceptCategory ( const char *  category)

Definition at line 240 of file util.cpp.

void LogException ( std::exception *  pex,
const char *  pszThread 
)

Definition at line 922 of file util.cpp.

int LogPrintStr ( const std::string &  str)

Definition at line 268 of file util.cpp.

Here is the call graph for this function:

template<typename Callable >
void LoopForever ( const char *  name,
Callable  func,
int64_t  msecs 
)

Definition at line 515 of file util.h.

Here is the call graph for this function:

Here is the caller graph for this function:

void MilliSleep ( int64_t  n)
inline

Definition at line 80 of file util.h.

Here is the caller graph for this function:

std::vector<unsigned char> ParseHex ( const char *  psz)

Definition at line 419 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

std::vector<unsigned char> ParseHex ( const std::string &  str)
bool ParseMoney ( const std::string &  str,
int64_t &  nRet 
)
bool ParseMoney ( const char *  pszIn,
int64_t &  nRet 
)

Definition at line 337 of file util.cpp.

Here is the call graph for this function:

void ParseParameters ( int  argc,
const char *const  argv[] 
)

Definition at line 460 of file util.cpp.

Here is the caller graph for this function:

void PrintExceptionContinue ( std::exception *  pex,
const char *  pszThread 
)

Definition at line 928 of file util.cpp.

Here is the caller graph for this function:

template<typename T >
void PrintHex ( const T  pbegin,
const T  pend,
const char *  pszFormat = "%s",
bool  fSpaces = true 
)

Definition at line 281 of file util.h.

Here is the call graph for this function:

void PrintHex ( const std::vector< unsigned char > &  vch,
const char *  pszFormat = "%s",
bool  fSpaces = true 
)
inline

Definition at line 286 of file util.h.

Here is the call graph for this function:

int RaiseFileDescriptorLimit ( int  nMinFD)

Definition at line 1124 of file util.cpp.

Here is the caller graph for this function:

void RandAddSeed ( )

Definition at line 151 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void RandAddSeedPerfmon ( )

Definition at line 159 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void ReadConfigFile ( std::map< std::string, std::string > &  mapSettingsRet,
std::map< std::string, std::vector< std::string > > &  mapMultiSettingsRet 
)
bool RenameOver ( boost::filesystem::path  src,
boost::filesystem::path  dest 
)

Definition at line 1069 of file util.cpp.

Here is the caller graph for this function:

void RenameThread ( const char *  name)

Definition at line 1368 of file util.cpp.

Here is the caller graph for this function:

int roundint ( double  d)
inline

Definition at line 240 of file util.h.

int64_t roundint64 ( double  d)
inline

Definition at line 245 of file util.h.

Here is the caller graph for this function:

void runCommand ( std::string  strCommand)

Definition at line 1361 of file util.cpp.

Here is the caller graph for this function:

std::string SanitizeString ( const std::string &  str)
void seed_insecure_rand ( bool  fDeterministic = false)

Seed insecure_rand using the random pool.

Parameters
DeterministicUse a determinstic seed

Definition at line 1303 of file util.cpp.

void SetMockTime ( int64_t  nMockTimeIn)

Definition at line 1227 of file util.cpp.

void SetThreadPriority ( int  nPriority)
inline

Definition at line 488 of file util.h.

Here is the caller graph for this function:

void SetupEnvironment ( )

Definition at line 1394 of file util.cpp.

Here is the caller graph for this function:

void ShrinkDebugFile ( )

Definition at line 1187 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
void skipspaces ( T &  it)

Definition at line 319 of file util.h.

bool SoftSetArg ( const std::string &  strArg,
const std::string &  strValue 
)

Set an argument if it doesn't already have a value.

Parameters
strArgArgument to set (e.g. "-foo")
strValueValue (e.g. "1")
Returns
true if argument gets set, false if it already had a value

Definition at line 531 of file util.cpp.

Here is the caller graph for this function:

bool SoftSetBoolArg ( const std::string &  strArg,
bool  fValue 
)

Set a boolean argument if it doesn't already have a value.

Parameters
strArgArgument to set (e.g. "-foo")
fValueValue (e.g. false)
Returns
true if argument gets set, false if it already had a value

Definition at line 539 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

template<typename T >
bool TimingResistantEqual ( const T &  a,
const T &  b 
)

Timing-attack-resistant comparison.

Takes time proportional to length of first argument.

Definition at line 407 of file util.h.

Here is the caller graph for this function:

template<typename Callable >
void TraceThread ( const char *  name,
Callable  func 
)

Definition at line 543 of file util.h.

Here is the call graph for this function:

Here is the caller graph for this function:

bool TruncateFile ( FILE *  file,
unsigned int  length 
)

Definition at line 1114 of file util.cpp.

bool TryCreateDirectory ( const boost::filesystem::path &  p)

Definition at line 1083 of file util.cpp.

Here is the caller graph for this function:

bool WildcardMatch ( const char *  psz,
const char *  mask 
)

Definition at line 876 of file util.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

bool WildcardMatch ( const std::string &  str,
const std::string &  mask 
)

Variable Documentation

bool fDebug

Definition at line 91 of file util.cpp.

bool fLogTimestamps

Definition at line 98 of file util.cpp.

bool fNoListen

Definition at line 97 of file util.cpp.

bool fPrintToConsole

Definition at line 92 of file util.cpp.

bool fPrintToDebugLog

Definition at line 93 of file util.cpp.

volatile bool fReopenDebugLog

Definition at line 99 of file util.cpp.

bool fServer

Definition at line 95 of file util.cpp.

uint32_t insecure_rand_Rw

Definition at line 1302 of file util.cpp.

uint32_t insecure_rand_Rz

MWC RNG of George Marsaglia This is intended to be fast.

It has a period of 2^59.3, though the least significant 16 bits only have a period of about 2^30.1.

Returns
random value

Definition at line 1301 of file util.cpp.

std::map<std::string, std::string> mapArgs

Definition at line 89 of file util.cpp.

std::map<std::string, std::vector<std::string> > mapMultiArgs

Definition at line 90 of file util.cpp.

std::string strMiscWarning

Definition at line 96 of file util.cpp.