From fe326c477b0b109819653dc2657495f867ff8616 Mon Sep 17 00:00:00 2001 From: svost Date: Tue, 29 Sep 2015 17:27:06 +0300 Subject: [PATCH] Added GetArgInt function --- src/db.cpp | 2 +- src/init.cpp | 8 ++++---- src/net.cpp | 6 +++--- src/txdb-bdb.cpp | 4 ++-- src/txdb-leveldb.cpp | 6 +++--- src/util.cpp | 7 +++++++ src/util.h | 21 ++++++++++++++++++++- 7 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/db.cpp b/src/db.cpp index 88d9cfd..28d6032 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -78,7 +78,7 @@ bool CDBEnv::Open(boost::filesystem::path pathEnv_) if (GetBoolArg("-privdb", true)) nEnvFlags |= DB_PRIVATE; - int nDbCache = GetArg("-dbcache", 25); + int nDbCache = GetArgInt("-dbcache", 25); dbenv.set_lg_dir(pathLogDir.string().c_str()); dbenv.set_cachesize(nDbCache / 1024, (nDbCache % 1024)*1048576, 1); dbenv.set_lg_bsize(1048576); diff --git a/src/init.cpp b/src/init.cpp index 1eb28a5..76f28e8 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -445,7 +445,7 @@ bool AppInit2() // ********************************************************* Step 3: parameter-to-internal-flags // -par=0 means autodetect, but nScriptCheckThreads==0 means no concurrency - nScriptCheckThreads = (int)(GetArg("-par", 0)); + nScriptCheckThreads = GetArgInt("-par", 0); if (nScriptCheckThreads == 0) nScriptCheckThreads = boost::thread::hardware_concurrency(); if (nScriptCheckThreads <= 1) @@ -484,7 +484,7 @@ bool AppInit2() if (mapArgs.count("-timeout")) { - int nNewTimeout = (int)(GetArg("-timeout", 5000)); + int nNewTimeout = GetArgInt("-timeout", 5000); if (nNewTimeout > 0 && nNewTimeout < 600000) nConnectTimeout = nNewTimeout; } @@ -605,7 +605,7 @@ bool AppInit2() // ********************************************************* Step 6: network initialization - int nSocksVersion = (int)(GetArg("-socks", 5)); + int nSocksVersion = GetArgInt("-socks", 5); if (nSocksVersion != 4 && nSocksVersion != 5) return InitError(strprintf(_("Unknown -socks proxy version requested: %i"), nSocksVersion)); @@ -882,7 +882,7 @@ bool AppInit2() if (GetBoolArg("-upgradewallet", fFirstRun)) { - int nMaxVersion = (int)(GetArg("-upgradewallet", 0)); + int nMaxVersion = GetArgInt("-upgradewallet", 0); if (nMaxVersion == 0) // the -upgradewallet without argument case { printf("Performing wallet upgrade to %i\n", FEATURE_LATEST); diff --git a/src/net.cpp b/src/net.cpp index 9f53617..6f45e2d 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -545,7 +545,7 @@ bool CNode::Misbehaving(int howmuch) } nMisbehavior += howmuch; - if (nMisbehavior >= GetArg("-banscore", 100)) + if (nMisbehavior >= GetArgInt("-banscore", 100)) { int64_t banTime = GetTime()+GetArg("-bantime", 60*60*24); // Default 24-hour ban printf("Misbehaving: %s (%d -> %d) DISCONNECTING\n", addr.ToString().c_str(), nMisbehavior-howmuch, nMisbehavior); @@ -783,7 +783,7 @@ void ThreadSocketHandler2(void* parg) if (nErr != WSAEWOULDBLOCK) printf("socket error accept failed: %d\n", nErr); } - else if (nInbound >= GetArg("-maxconnections", 125) - MAX_OUTBOUND_CONNECTIONS) + else if (nInbound >= GetArgInt("-maxconnections", 125) - MAX_OUTBOUND_CONNECTIONS) { { LOCK(cs_setservAddNodeAddresses); @@ -1874,7 +1874,7 @@ void StartNode(void* parg) if (semOutbound == NULL) { // initialize semaphore - int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, (int)GetArg("-maxconnections", 125)); + int nMaxOutbound = min(MAX_OUTBOUND_CONNECTIONS, GetArgInt("-maxconnections", 125)); semOutbound = new CSemaphore(nMaxOutbound); } diff --git a/src/txdb-bdb.cpp b/src/txdb-bdb.cpp index 416247c..bbfb656 100644 --- a/src/txdb-bdb.cpp +++ b/src/txdb-bdb.cpp @@ -223,8 +223,8 @@ bool CTxDB::LoadBlockIndex() nBestInvalidTrust = bnBestInvalidTrust.getuint256(); // Verify blocks in the best chain - int nCheckLevel = GetArg("-checklevel", 1); - int nCheckDepth = GetArg( "-checkblocks", 2500); + int nCheckLevel = GetArgInt("-checklevel", 1); + int nCheckDepth = GetArgInt( "-checkblocks", 2500); if (nCheckDepth == 0) nCheckDepth = 1000000000; // suffices until the year 19000 if (nCheckDepth > nBestHeight) diff --git a/src/txdb-leveldb.cpp b/src/txdb-leveldb.cpp index 494971c..4a7a3cf 100644 --- a/src/txdb-leveldb.cpp +++ b/src/txdb-leveldb.cpp @@ -27,7 +27,7 @@ leveldb::DB *txdb; // global pointer for LevelDB object instance static leveldb::Options GetOptions() { leveldb::Options options; - int nCacheSizeMB = GetArg("-dbcache", 25); + int nCacheSizeMB = GetArgInt("-dbcache", 25); options.block_cache = leveldb::NewLRUCache(nCacheSizeMB * 1048576); options.filter_policy = leveldb::NewBloomFilterPolicy(10); return options; @@ -454,8 +454,8 @@ bool CTxDB::LoadBlockIndex() nBestInvalidTrust = bnBestInvalidTrust.getuint256(); // Verify blocks in the best chain - int nCheckLevel = GetArg("-checklevel", 1); - int nCheckDepth = GetArg( "-checkblocks", 2500); + int nCheckLevel = GetArgInt("-checklevel", 1); + int nCheckDepth = GetArgInt( "-checkblocks", 2500); if (nCheckDepth == 0) nCheckDepth = 1000000000; // suffices until the year 19000 if (nCheckDepth > nBestHeight) diff --git a/src/util.cpp b/src/util.cpp index 2f28b95..f540500 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -588,6 +588,13 @@ int64_t GetArg(const std::string& strArg, int64_t nDefault) return nDefault; } +int32_t GetArgInt(const std::string& strArg, int32_t nDefault) +{ + if (mapArgs.count(strArg)) + return strtol(mapArgs[strArg]); + return nDefault; +} + bool GetBoolArg(const std::string& strArg, bool fDefault) { if (mapArgs.count(strArg)) diff --git a/src/util.h b/src/util.h index 5835e90..25f6c2a 100644 --- a/src/util.h +++ b/src/util.h @@ -268,6 +268,16 @@ inline int64_t atoi64(const std::string& str) #endif } +inline int32_t strtol(const char* psz) +{ + return strtol(psz, NULL, 10); +} + +inline int32_t strtol(const std::string& str) +{ + return strtol(str.c_str(), NULL, 10); +} + inline int atoi(const std::string& str) { return atoi(str.c_str()); @@ -387,7 +397,7 @@ inline bool IsSwitchChar(char c) std::string GetArg(const std::string& strArg, const std::string& strDefault); /** - * Return integer argument or default value + * Return 64-bit integer argument or default value * * @param strArg Argument to get (e.g. "-foo") * @param default (e.g. 1) @@ -396,6 +406,15 @@ std::string GetArg(const std::string& strArg, const std::string& strDefault); int64_t GetArg(const std::string& strArg, int64_t nDefault); /** + * Return 32-bit integer argument or default value + * + * @param strArg Argument to get (e.g. "-foo") + * @param default (e.g. 1) + * @return command-line argument (0 if invalid number) or default value + */ +int32_t GetArgInt(const std::string& strArg, int32_t nDefault); + +/** * Return boolean argument or default value * * @param strArg Argument to get (e.g. "-foo") -- 1.7.1