Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
Classes | Macros | Typedefs | Functions | Variables
util.h File Reference
#include "uint256.h"
#include <stdarg.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <map>
#include <list>
#include <utility>
#include <vector>
#include <string>
#include <boost/version.hpp>
#include <boost/thread.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "netbase.h"
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 loop   for (;;)
 
#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 PRI64d   "lld"
 
#define PRI64u   "llu"
 
#define PRI64x   "llx"
 
#define PRIszx   "zx"
 
#define PRIszu   "zu"
 
#define PRIszd   "zd"
 
#define PRIpdx   "tx"
 
#define PRIpdu   "tu"
 
#define PRIpdd   "td"
 
#define PAIRTYPE(t1, t2)   std::pair<t1, t2>
 
#define MAX_PATH   1024
 
#define ATTR_WARN_PRINTF(X, Y)
 
#define strprintf(format,...)   real_strprintf(format, 0, __VA_ARGS__)
 
#define printf   OutputDebugStringF
 
#define THREAD_PRIORITY_LOWEST   PRIO_MAX
 
#define THREAD_PRIORITY_BELOW_NORMAL   2
 
#define THREAD_PRIORITY_NORMAL   0
 
#define THREAD_PRIORITY_ABOVE_NORMAL   0
 

Typedefs

typedef long long int64
 
typedef unsigned long long uint64
 

Functions

template<size_t nBytes, typename T >
T * alignup (T *p)
 
void MilliSleep (int64 n)
 
void RandAddSeed ()
 
void RandAddSeedPerfmon ()
 
int ATTR_WARN_PRINTF (1, 2) OutputDebugStringF(const char *pszFormat
 
int std::string ATTR_WARN_PRINTF (1, 3) real_strprintf(const char *format
 Overload strprintf for char*, so that GCC format type warnings can be given. More...
 
int std::string int std::string real_strprintf (const std::string &format, int dummy,...)
 Overload strprintf for std::string, to be able to use it with _ (translation). More...
 
std::string vstrprintf (const char *format, va_list ap)
 
void LogException (std::exception *pex, const char *pszThread)
 
void PrintException (std::exception *pex, const char *pszThread)
 
void PrintExceptionContinue (std::exception *pex, const char *pszThread)
 
void ParseString (const std::string &str, char c, std::vector< std::string > &v)
 
std::string FormatMoney (int64 n, bool fPlus=false)
 
bool ParseMoney (const std::string &str, int64 &nRet)
 
bool ParseMoney (const char *pszIn, int64 &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)
 
int GetFilesize (FILE *file)
 
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)
 
boost::filesystem::path GetDefaultDataDir ()
 
const boost::filesystem::path & GetDataDir (bool fNetSpecific=true)
 
boost::filesystem::path GetConfigFile ()
 
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 GetRand (uint64 nMax)
 
uint256 GetRandHash ()
 
int64 GetTime ()
 
void SetMockTime (int64 nMockTimeIn)
 
int64 GetAdjustedTime ()
 
int64 GetTimeOffset ()
 
std::string FormatFullVersion ()
 
std::string FormatSubVersion (const std::string &name, int nClientVersion, const std::vector< std::string > &comments)
 
void AddTimeData (const CNetAddr &ip, int64 nTime)
 
void runCommand (std::string strCommand)
 
std::string i64tostr (int64 n)
 
std::string itostr (int n)
 
int64 atoi64 (const char *psz)
 
int64 atoi64 (const std::string &str)
 
int atoi (const std::string &str)
 
int roundint (double d)
 
int64 roundint64 (double d)
 
int64 abs64 (int64 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 GetPerformanceCounter ()
 
int64 GetTimeMillis ()
 
int64 GetTimeMicros ()
 
std::string DateTimeStrFormat (const char *pszFormat, int64 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 GetArg (const std::string &strArg, int64 nDefault)
 Return integer argument or default value. More...
 
bool GetBoolArg (const std::string &strArg, bool fDefault=false)
 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...
 
bool NewThread (void(*pfn)(void *), void *parg)
 
void SetThreadPriority (int nPriority)
 
void ExitThread (size_t nExitCode)
 
void RenameThread (const char *name)
 
uint32_t ByteReverse (uint32_t value)
 
template<typename Callable >
void LoopForever (const char *name, Callable func, int64 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 fDebugNet
 
bool fPrintToConsole
 
bool fPrintToDebugger
 
bool fDaemon
 
bool fServer
 
bool fCommandLine
 
std::string strMiscWarning
 
bool fTestNet
 
bool fBloomFilters
 
bool fNoListen
 
bool fLogTimestamps
 
volatile bool fReopenDebugLog
 
int std::string int dummy
 
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 43 of file util.h.

#define ATTR_WARN_PRINTF (   X,
 
)

Definition at line 126 of file util.h.

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

Definition at line 39 of file util.h.

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

Definition at line 40 of file util.h.

#define loop   for (;;)

Definition at line 38 of file util.h.

#define MAX_PATH   1024

Definition at line 103 of file util.h.

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

Definition at line 78 of file util.h.

#define PRI64d   "lld"

Definition at line 51 of file util.h.

#define PRI64u   "llu"

Definition at line 52 of file util.h.

#define PRI64x   "llx"

Definition at line 53 of file util.h.

#define printf   OutputDebugStringF

Definition at line 180 of file util.h.

#define PRIpdd   "td"

Definition at line 74 of file util.h.

#define PRIpdu   "tu"

Definition at line 73 of file util.h.

#define PRIpdx   "tx"

Definition at line 72 of file util.h.

#define PRIszd   "zd"

Definition at line 71 of file util.h.

#define PRIszu   "zu"

Definition at line 70 of file util.h.

#define PRIszx   "zx"

Definition at line 69 of file util.h.

#define strprintf (   format,
  ... 
)    real_strprintf(format, 0, __VA_ARGS__)

Definition at line 169 of file util.h.

#define THREAD_PRIORITY_ABOVE_NORMAL   0

Definition at line 527 of file util.h.

#define THREAD_PRIORITY_BELOW_NORMAL   2

Definition at line 525 of file util.h.

#define THREAD_PRIORITY_LOWEST   PRIO_MAX

Definition at line 524 of file util.h.

#define THREAD_PRIORITY_NORMAL   0

Definition at line 526 of file util.h.

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

Definition at line 41 of file util.h.

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

Definition at line 42 of file util.h.

Typedef Documentation

typedef long long int64

Definition at line 32 of file util.h.

typedef unsigned long long uint64

Definition at line 33 of file util.h.

Function Documentation

int64 abs64 ( int64  n)
inline

Definition at line 286 of file util.h.

void AddTimeData ( const CNetAddr ip,
int64  nTime 
)

Definition at line 1322 of file util.cpp.

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

Definition at line 82 of file util.h.

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

Definition at line 1216 of file util.cpp.

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

Definition at line 271 of file util.h.

int64 atoi64 ( const char *  psz)
inline

Definition at line 253 of file util.h.

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

Definition at line 262 of file util.h.

bool ATTR_WARN_PRINTF ( ,
 
) const
int std::string ATTR_WARN_PRINTF ( ,
 
) const

Overload strprintf for char*, so that GCC format type warnings can be given.

uint32_t ByteReverse ( uint32_t  value)
inline

Definition at line 548 of file util.h.

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

Definition at line 1136 of file util.cpp.

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

Definition at line 352 of file util.h.

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

Definition at line 832 of file util.cpp.

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

Definition at line 679 of file util.cpp.

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

Definition at line 768 of file util.cpp.

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

Definition at line 628 of file util.cpp.

std::string EncodeBase64 ( const std::string &  str)
void ExitThread ( size_t  nExitCode)
inline

Definition at line 540 of file util.h.

void FileCommit ( FILE *  fileout)

Definition at line 1158 of file util.cpp.

std::string FormatFullVersion ( )

Definition at line 1404 of file util.cpp.

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

Definition at line 389 of file util.cpp.

std::string FormatSubVersion ( const std::string &  name,
int  nClientVersion,
const std::vector< std::string > &  comments 
)

Definition at line 1410 of file util.cpp.

int64 GetAdjustedTime ( )

Definition at line 1317 of file util.cpp.

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 586 of file util.cpp.

int64 GetArg ( const std::string &  strArg,
int64  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 593 of file util.cpp.

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

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 600 of file util.cpp.

boost::filesystem::path GetConfigFile ( )

Definition at line 1094 of file util.cpp.

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

Definition at line 1060 of file util.cpp.

boost::filesystem::path GetDefaultDataDir ( )

Definition at line 1031 of file util.cpp.

int GetFilesize ( FILE *  file)

Definition at line 1174 of file util.cpp.

int64 GetPerformanceCounter ( )
inline

Definition at line 327 of file util.h.

boost::filesystem::path GetPidFile ( )

Definition at line 1128 of file util.cpp.

uint64 GetRand ( uint64  nMax)

Definition at line 175 of file util.cpp.

uint256 GetRandHash ( )

Definition at line 195 of file util.cpp.

int GetRandInt ( int  nMax)

Definition at line 190 of file util.cpp.

boost::filesystem::path GetTempPath ( )

Definition at line 1438 of file util.cpp.

int64 GetTime ( )

Definition at line 1298 of file util.cpp.

int64 GetTimeMicros ( )
inline

Definition at line 346 of file util.h.

int64 GetTimeMillis ( )
inline

Definition at line 340 of file util.h.

int64 GetTimeOffset ( )

Definition at line 1312 of file util.cpp.

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

Definition at line 292 of file util.h.

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

Definition at line 311 of file util.h.

std::string i64tostr ( int64  n)
inline

Definition at line 243 of file util.h.

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

Definition at line 368 of file util.h.

std::string itostr ( int  n)
inline

Definition at line 248 of file util.h.

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

Definition at line 1008 of file util.cpp.

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

Definition at line 561 of file util.h.

void MilliSleep ( int64  n)
inline

Definition at line 106 of file util.h.

bool NewThread ( void(*)(void *)  pfn,
void *  parg 
)

Definition at line 1491 of file util.cpp.

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

Definition at line 500 of file util.cpp.

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

Definition at line 418 of file util.cpp.

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

Definition at line 541 of file util.cpp.

void ParseString ( const std::string &  str,
char  c,
std::vector< std::string > &  v 
)
void PrintException ( std::exception *  pex,
const char *  pszThread 
)

Definition at line 1014 of file util.cpp.

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

Definition at line 1023 of file util.cpp.

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

Definition at line 317 of file util.h.

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

Definition at line 322 of file util.h.

int RaiseFileDescriptorLimit ( int  nMinFD)

Definition at line 1195 of file util.cpp.

void RandAddSeed ( )

Definition at line 140 of file util.cpp.

void RandAddSeedPerfmon ( )

Definition at line 148 of file util.cpp.

void ReadConfigFile ( std::map< std::string, std::string > &  mapSettingsRet,
std::map< std::string, std::vector< std::string > > &  mapMultiSettingsRet 
)
int std::string int std::string real_strprintf ( const std::string &  format,
int  dummy,
  ... 
)

Overload strprintf for std::string, to be able to use it with _ (translation).

This will not support GCC format type warnings (-Wformat) so be careful.

Definition at line 349 of file util.cpp.

bool RenameOver ( boost::filesystem::path  src,
boost::filesystem::path  dest 
)

Definition at line 1147 of file util.cpp.

void RenameThread ( const char *  name)

Definition at line 1467 of file util.cpp.

int roundint ( double  d)
inline

Definition at line 276 of file util.h.

int64 roundint64 ( double  d)
inline

Definition at line 281 of file util.h.

void runCommand ( std::string  strCommand)

Definition at line 1460 of file util.cpp.

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 1377 of file util.cpp.

void SetMockTime ( int64  nMockTimeIn)

Definition at line 1305 of file util.cpp.

void SetThreadPriority ( int  nPriority)
inline

Definition at line 529 of file util.h.

void ShrinkDebugFile ( )

Definition at line 1258 of file util.cpp.

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

Definition at line 362 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 611 of file util.cpp.

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 619 of file util.cpp.

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 450 of file util.h.

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

Definition at line 587 of file util.h.

bool TruncateFile ( FILE *  file,
unsigned int  length 
)

Definition at line 1184 of file util.cpp.

std::string vstrprintf ( const char *  format,
va_list  ap 
)

Definition at line 309 of file util.cpp.

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

Definition at line 956 of file util.cpp.

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

Variable Documentation

int std::string int dummy

Definition at line 164 of file util.h.

bool fBloomFilters

Definition at line 82 of file util.cpp.

bool fCommandLine

Definition at line 79 of file util.cpp.

bool fDaemon

Definition at line 77 of file util.cpp.

bool fDebug

Definition at line 73 of file util.cpp.

bool fDebugNet

Definition at line 74 of file util.cpp.

bool fLogTimestamps

Definition at line 84 of file util.cpp.

bool fNoListen

Definition at line 83 of file util.cpp.

bool fPrintToConsole

Definition at line 75 of file util.cpp.

bool fPrintToDebugger

Definition at line 76 of file util.cpp.

volatile bool fReopenDebugLog

Definition at line 86 of file util.cpp.

bool fServer

Definition at line 78 of file util.cpp.

bool fTestNet

Definition at line 81 of file util.cpp.

uint32_t insecure_rand_Rw

Definition at line 1376 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 1375 of file util.cpp.

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

Definition at line 71 of file util.cpp.

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

Definition at line 72 of file util.cpp.

std::string strMiscWarning

Definition at line 80 of file util.cpp.