Skip to content

Commit 7d9d134

Browse files
committed
Remove redundant .c_str()s
After the tinyformat switch sprintf() family functions support passing actual std::string objects. Remove unnecessary c_str calls (236 of them) in logging and formatting.
1 parent b77dfdc commit 7d9d134

22 files changed

+236
-236
lines changed

src/addrman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ void CAddrMan::Good_(const CService &addr, int64_t nTime)
304304
// TODO: maybe re-add the node, but for now, just bail out
305305
if (nUBucket == -1) return;
306306

307-
LogPrint("addrman", "Moving %s to tried\n", addr.ToString().c_str());
307+
LogPrint("addrman", "Moving %s to tried\n", addr.ToString());
308308

309309
// move nId to the tried tables
310310
MakeTried(info, nId, nUBucket);

src/alert.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,18 @@ std::string CUnsignedAlert::ToString() const
6969
nExpiration,
7070
nID,
7171
nCancel,
72-
strSetCancel.c_str(),
72+
strSetCancel,
7373
nMinVer,
7474
nMaxVer,
75-
strSetSubVer.c_str(),
75+
strSetSubVer,
7676
nPriority,
77-
strComment.c_str(),
78-
strStatusBar.c_str());
77+
strComment,
78+
strStatusBar);
7979
}
8080

8181
void CUnsignedAlert::print() const
8282
{
83-
LogPrintf("%s", ToString().c_str());
83+
LogPrintf("%s", ToString());
8484
}
8585

8686
void CAlert::SetNull()

src/core.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
std::string COutPoint::ToString() const
1111
{
12-
return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10).c_str(), n);
12+
return strprintf("COutPoint(%s, %u)", hash.ToString().substr(0,10), n);
1313
}
1414

1515
void COutPoint::print() const
1616
{
17-
LogPrintf("%s\n", ToString().c_str());
17+
LogPrintf("%s\n", ToString());
1818
}
1919

2020
CTxIn::CTxIn(COutPoint prevoutIn, CScript scriptSigIn, unsigned int nSequenceIn)
@@ -37,9 +37,9 @@ std::string CTxIn::ToString() const
3737
str += "CTxIn(";
3838
str += prevout.ToString();
3939
if (prevout.IsNull())
40-
str += strprintf(", coinbase %s", HexStr(scriptSig).c_str());
40+
str += strprintf(", coinbase %s", HexStr(scriptSig));
4141
else
42-
str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24).c_str());
42+
str += strprintf(", scriptSig=%s", scriptSig.ToString().substr(0,24));
4343
if (nSequence != std::numeric_limits<unsigned int>::max())
4444
str += strprintf(", nSequence=%u", nSequence);
4545
str += ")";
@@ -48,7 +48,7 @@ std::string CTxIn::ToString() const
4848

4949
void CTxIn::print() const
5050
{
51-
LogPrintf("%s\n", ToString().c_str());
51+
LogPrintf("%s\n", ToString());
5252
}
5353

5454
CTxOut::CTxOut(int64_t nValueIn, CScript scriptPubKeyIn)
@@ -64,12 +64,12 @@ uint256 CTxOut::GetHash() const
6464

6565
std::string CTxOut::ToString() const
6666
{
67-
return strprintf("CTxOut(nValue=%"PRId64".%08"PRId64", scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30).c_str());
67+
return strprintf("CTxOut(nValue=%"PRId64".%08"PRId64", scriptPubKey=%s)", nValue / COIN, nValue % COIN, scriptPubKey.ToString().substr(0,30));
6868
}
6969

7070
void CTxOut::print() const
7171
{
72-
LogPrintf("%s\n", ToString().c_str());
72+
LogPrintf("%s\n", ToString());
7373
}
7474

7575
uint256 CTransaction::GetHash() const
@@ -141,7 +141,7 @@ std::string CTransaction::ToString() const
141141
{
142142
std::string str;
143143
str += strprintf("CTransaction(hash=%s, ver=%d, vin.size=%"PRIszu", vout.size=%"PRIszu", nLockTime=%u)\n",
144-
GetHash().ToString().substr(0,10).c_str(),
144+
GetHash().ToString().substr(0,10),
145145
nVersion,
146146
vin.size(),
147147
vout.size(),
@@ -155,7 +155,7 @@ std::string CTransaction::ToString() const
155155

156156
void CTransaction::print() const
157157
{
158-
LogPrintf("%s", ToString().c_str());
158+
LogPrintf("%s", ToString());
159159
}
160160

161161
// Amount compression:
@@ -270,10 +270,10 @@ uint256 CBlock::CheckMerkleBranch(uint256 hash, const std::vector<uint256>& vMer
270270
void CBlock::print() const
271271
{
272272
LogPrintf("CBlock(hash=%s, ver=%d, hashPrevBlock=%s, hashMerkleRoot=%s, nTime=%u, nBits=%08x, nNonce=%u, vtx=%"PRIszu")\n",
273-
GetHash().ToString().c_str(),
273+
GetHash().ToString(),
274274
nVersion,
275-
hashPrevBlock.ToString().c_str(),
276-
hashMerkleRoot.ToString().c_str(),
275+
hashPrevBlock.ToString(),
276+
hashMerkleRoot.ToString(),
277277
nTime, nBits, nNonce,
278278
vtx.size());
279279
for (unsigned int i = 0; i < vtx.size(); i++)
@@ -283,6 +283,6 @@ void CBlock::print() const
283283
}
284284
LogPrintf(" vMerkleTree: ");
285285
for (unsigned int i = 0; i < vMerkleTree.size(); i++)
286-
LogPrintf("%s ", vMerkleTree[i].ToString().c_str());
286+
LogPrintf("%s ", vMerkleTree[i].ToString());
287287
LogPrintf("\n");
288288
}

src/db.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ bool CDBEnv::Open(const boost::filesystem::path& pathIn)
7575
filesystem::path pathLogDir = path / "database";
7676
filesystem::create_directory(pathLogDir);
7777
filesystem::path pathErrorFile = path / "db.log";
78-
LogPrintf("dbenv.open LogDir=%s ErrorFile=%s\n", pathLogDir.string().c_str(), pathErrorFile.string().c_str());
78+
LogPrintf("dbenv.open LogDir=%s ErrorFile=%s\n", pathLogDir.string(), pathErrorFile.string());
7979

8080
unsigned int nEnvFlags = 0;
8181
if (GetBoolArg("-privdb", true))
@@ -353,7 +353,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
353353
bitdb.mapFileUseCount.erase(strFile);
354354

355355
bool fSuccess = true;
356-
LogPrintf("Rewriting %s...\n", strFile.c_str());
356+
LogPrintf("Rewriting %s...\n", strFile);
357357
string strFileRes = strFile + ".rewrite";
358358
{ // surround usage of db with extra {}
359359
CDB db(strFile.c_str(), "r");
@@ -367,7 +367,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
367367
0);
368368
if (ret > 0)
369369
{
370-
LogPrintf("Cannot create database file %s\n", strFileRes.c_str());
370+
LogPrintf("Cannot create database file %s\n", strFileRes);
371371
fSuccess = false;
372372
}
373373

@@ -423,7 +423,7 @@ bool CDB::Rewrite(const string& strFile, const char* pszSkip)
423423
fSuccess = false;
424424
}
425425
if (!fSuccess)
426-
LogPrintf("Rewriting of %s FAILED!\n", strFileRes.c_str());
426+
LogPrintf("Rewriting of %s FAILED!\n", strFileRes);
427427
return fSuccess;
428428
}
429429
}
@@ -448,17 +448,17 @@ void CDBEnv::Flush(bool fShutdown)
448448
{
449449
string strFile = (*mi).first;
450450
int nRefCount = (*mi).second;
451-
LogPrint("db", "%s refcount=%d\n", strFile.c_str(), nRefCount);
451+
LogPrint("db", "%s refcount=%d\n", strFile, nRefCount);
452452
if (nRefCount == 0)
453453
{
454454
// Move log data to the dat file
455455
CloseDb(strFile);
456-
LogPrint("db", "%s checkpoint\n", strFile.c_str());
456+
LogPrint("db", "%s checkpoint\n", strFile);
457457
dbenv.txn_checkpoint(0, 0, 0);
458-
LogPrint("db", "%s detach\n", strFile.c_str());
458+
LogPrint("db", "%s detach\n", strFile);
459459
if (!fMockDb)
460460
dbenv.lsn_reset(strFile.c_str(), 0);
461-
LogPrint("db", "%s closed\n", strFile.c_str());
461+
LogPrint("db", "%s closed\n", strFile);
462462
mapFileUseCount.erase(mi++);
463463
}
464464
else

src/init.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ void ThreadImport(std::vector<boost::filesystem::path> vImportFiles)
344344
FILE *file = fopen(path.string().c_str(), "rb");
345345
if (file) {
346346
CImportingNow imp;
347-
LogPrintf("Importing %s...\n", path.string().c_str());
347+
LogPrintf("Importing %s...\n", path.string());
348348
LoadExternalBlockFile(file);
349349
}
350350
}
@@ -512,22 +512,22 @@ bool AppInit2(boost::thread_group& threadGroup)
512512
if (ParseMoney(mapArgs["-mintxfee"], n) && n > 0)
513513
CTransaction::nMinTxFee = n;
514514
else
515-
return InitError(strprintf(_("Invalid amount for -mintxfee=<amount>: '%s'"), mapArgs["-mintxfee"].c_str()));
515+
return InitError(strprintf(_("Invalid amount for -mintxfee=<amount>: '%s'"), mapArgs["-mintxfee"]));
516516
}
517517
if (mapArgs.count("-minrelaytxfee"))
518518
{
519519
int64_t n = 0;
520520
if (ParseMoney(mapArgs["-minrelaytxfee"], n) && n > 0)
521521
CTransaction::nMinRelayTxFee = n;
522522
else
523-
return InitError(strprintf(_("Invalid amount for -minrelaytxfee=<amount>: '%s'"), mapArgs["-minrelaytxfee"].c_str()));
523+
return InitError(strprintf(_("Invalid amount for -minrelaytxfee=<amount>: '%s'"), mapArgs["-minrelaytxfee"]));
524524
}
525525

526526
#ifdef ENABLE_WALLET
527527
if (mapArgs.count("-paytxfee"))
528528
{
529529
if (!ParseMoney(mapArgs["-paytxfee"], nTransactionFee))
530-
return InitError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s'"), mapArgs["-paytxfee"].c_str()));
530+
return InitError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s'"), mapArgs["-paytxfee"]));
531531
if (nTransactionFee > 0.25 * COIN)
532532
InitWarning(_("Warning: -paytxfee is set very high! This is the transaction fee you will pay if you send a transaction."));
533533
}
@@ -540,25 +540,25 @@ bool AppInit2(boost::thread_group& threadGroup)
540540
#ifdef ENABLE_WALLET
541541
// Wallet file must be a plain filename without a directory
542542
if (strWalletFile != boost::filesystem::basename(strWalletFile) + boost::filesystem::extension(strWalletFile))
543-
return InitError(strprintf(_("Wallet %s resides outside data directory %s"), strWalletFile.c_str(), strDataDir.c_str()));
543+
return InitError(strprintf(_("Wallet %s resides outside data directory %s"), strWalletFile, strDataDir));
544544
#endif
545545
// Make sure only a single Bitcoin process is using the data directory.
546546
boost::filesystem::path pathLockFile = GetDataDir() / ".lock";
547547
FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist.
548548
if (file) fclose(file);
549549
static boost::interprocess::file_lock lock(pathLockFile.string().c_str());
550550
if (!lock.try_lock())
551-
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin is probably already running."), strDataDir.c_str()));
551+
return InitError(strprintf(_("Cannot obtain a lock on data directory %s. Bitcoin is probably already running."), strDataDir));
552552

553553
if (GetBoolArg("-shrinkdebugfile", !fDebug))
554554
ShrinkDebugFile();
555555
LogPrintf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
556-
LogPrintf("Bitcoin version %s (%s)\n", FormatFullVersion().c_str(), CLIENT_DATE.c_str());
556+
LogPrintf("Bitcoin version %s (%s)\n", FormatFullVersion(), CLIENT_DATE);
557557
LogPrintf("Using OpenSSL version %s\n", SSLeay_version(SSLEAY_VERSION));
558558
if (!fLogTimestamps)
559-
LogPrintf("Startup time: %s\n", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()).c_str());
560-
LogPrintf("Default data directory %s\n", GetDefaultDataDir().string().c_str());
561-
LogPrintf("Using data directory %s\n", strDataDir.c_str());
559+
LogPrintf("Startup time: %s\n", DateTimeStrFormat("%Y-%m-%d %H:%M:%S", GetTime()));
560+
LogPrintf("Default data directory %s\n", GetDefaultDataDir().string());
561+
LogPrintf("Using data directory %s\n", strDataDir);
562562
LogPrintf("Using at most %i connections (%i file descriptors available)\n", nMaxConnections, nFD);
563563
std::ostringstream strErrors;
564564

@@ -582,15 +582,15 @@ bool AppInit2(boost::thread_group& threadGroup)
582582
boost::filesystem::path pathDatabaseBak = GetDataDir() / strprintf("database.%"PRId64".bak", GetTime());
583583
try {
584584
boost::filesystem::rename(pathDatabase, pathDatabaseBak);
585-
LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string().c_str(), pathDatabaseBak.string().c_str());
585+
LogPrintf("Moved old %s to %s. Retrying.\n", pathDatabase.string(), pathDatabaseBak.string());
586586
} catch(boost::filesystem::filesystem_error &error) {
587587
// failure is ok (well, not really, but it's not worse than what we started with)
588588
}
589589

590590
// try again
591591
if (!bitdb.Open(GetDataDir())) {
592592
// if it still fails, it probably means we can't even create the database env
593-
string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir.c_str());
593+
string msg = strprintf(_("Error initializing wallet database environment %s!"), strDataDir);
594594
return InitError(msg);
595595
}
596596
}
@@ -610,7 +610,7 @@ bool AppInit2(boost::thread_group& threadGroup)
610610
string msg = strprintf(_("Warning: wallet.dat corrupt, data salvaged!"
611611
" Original wallet.dat saved as wallet.{timestamp}.bak in %s; if"
612612
" your balance or transactions are incorrect you should"
613-
" restore from a backup."), strDataDir.c_str());
613+
" restore from a backup."), strDataDir);
614614
InitWarning(msg);
615615
}
616616
if (r == CDBEnv::RECOVER_FAIL)
@@ -631,7 +631,7 @@ bool AppInit2(boost::thread_group& threadGroup)
631631
BOOST_FOREACH(std::string snet, mapMultiArgs["-onlynet"]) {
632632
enum Network net = ParseNetwork(snet);
633633
if (net == NET_UNROUTABLE)
634-
return InitError(strprintf(_("Unknown network specified in -onlynet: '%s'"), snet.c_str()));
634+
return InitError(strprintf(_("Unknown network specified in -onlynet: '%s'"), snet));
635635
nets.insert(net);
636636
}
637637
for (int n = 0; n < NET_MAX; n++) {
@@ -652,7 +652,7 @@ bool AppInit2(boost::thread_group& threadGroup)
652652
if (mapArgs.count("-proxy")) {
653653
addrProxy = CService(mapArgs["-proxy"], 9050);
654654
if (!addrProxy.IsValid())
655-
return InitError(strprintf(_("Invalid -proxy address: '%s'"), mapArgs["-proxy"].c_str()));
655+
return InitError(strprintf(_("Invalid -proxy address: '%s'"), mapArgs["-proxy"]));
656656

657657
if (!IsLimited(NET_IPV4))
658658
SetProxy(NET_IPV4, addrProxy, nSocksVersion);
@@ -679,7 +679,7 @@ bool AppInit2(boost::thread_group& threadGroup)
679679
else
680680
addrOnion = mapArgs.count("-onion")?CService(mapArgs["-onion"], 9050):CService(mapArgs["-tor"], 9050);
681681
if (!addrOnion.IsValid())
682-
return InitError(strprintf(_("Invalid -onion address: '%s'"), mapArgs.count("-onion")?mapArgs["-onion"].c_str():mapArgs["-tor"].c_str()));
682+
return InitError(strprintf(_("Invalid -onion address: '%s'"), mapArgs.count("-onion")?mapArgs["-onion"]:mapArgs["-tor"]));
683683
SetProxy(NET_TOR, addrOnion, 5);
684684
SetReachable(NET_TOR);
685685
}
@@ -695,7 +695,7 @@ bool AppInit2(boost::thread_group& threadGroup)
695695
BOOST_FOREACH(std::string strBind, mapMultiArgs["-bind"]) {
696696
CService addrBind;
697697
if (!Lookup(strBind.c_str(), addrBind, GetListenPort(), false))
698-
return InitError(strprintf(_("Cannot resolve -bind address: '%s'"), strBind.c_str()));
698+
return InitError(strprintf(_("Cannot resolve -bind address: '%s'"), strBind));
699699
fBound |= Bind(addrBind, (BF_EXPLICIT | BF_REPORT_ERROR));
700700
}
701701
}
@@ -715,7 +715,7 @@ bool AppInit2(boost::thread_group& threadGroup)
715715
BOOST_FOREACH(string strAddr, mapMultiArgs["-externalip"]) {
716716
CService addrLocal(strAddr, GetListenPort(), fNameLookup);
717717
if (!addrLocal.IsValid())
718-
return InitError(strprintf(_("Cannot resolve -externalip address: '%s'"), strAddr.c_str()));
718+
return InitError(strprintf(_("Cannot resolve -externalip address: '%s'"), strAddr));
719719
AddLocal(CService(strAddr, GetListenPort(), fNameLookup), LOCAL_MANUAL);
720720
}
721721
}
@@ -739,7 +739,7 @@ bool AppInit2(boost::thread_group& threadGroup)
739739
filesystem::path dest = blocksDir / strprintf("blk%05u.dat", i-1);
740740
try {
741741
filesystem::create_hard_link(source, dest);
742-
LogPrintf("Hardlinked %s -> %s\n", source.string().c_str(), dest.string().c_str());
742+
LogPrintf("Hardlinked %s -> %s\n", source.string(), dest.string());
743743
linked = true;
744744
} catch (filesystem::filesystem_error & e) {
745745
// Note: hardlink creation failing is not a disaster, it just means
@@ -879,7 +879,7 @@ bool AppInit2(boost::thread_group& threadGroup)
879879
}
880880
}
881881
if (nFound == 0)
882-
LogPrintf("No blocks matching %s were found\n", strMatch.c_str());
882+
LogPrintf("No blocks matching %s were found\n", strMatch);
883883
return false;
884884
}
885885

@@ -910,7 +910,7 @@ bool AppInit2(boost::thread_group& threadGroup)
910910
else if (nLoadWalletRet == DB_NEED_REWRITE)
911911
{
912912
strErrors << _("Wallet needed to be rewritten: restart Bitcoin to complete") << "\n";
913-
LogPrintf("%s", strErrors.str().c_str());
913+
LogPrintf("%s", strErrors.str());
914914
return InitError(strErrors.str());
915915
}
916916
else
@@ -948,7 +948,7 @@ bool AppInit2(boost::thread_group& threadGroup)
948948
pwalletMain->SetBestChain(chainActive.GetLocator());
949949
}
950950

951-
LogPrintf("%s", strErrors.str().c_str());
951+
LogPrintf("%s", strErrors.str());
952952
LogPrintf(" wallet %15"PRId64"ms\n", GetTimeMillis() - nStart);
953953

954954
RegisterWallet(pwalletMain);

src/leveldbwrapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
void HandleError(const leveldb::Status &status) throw(leveldb_error) {
1616
if (status.ok())
1717
return;
18-
LogPrintf("%s\n", status.ToString().c_str());
18+
LogPrintf("%s\n", status.ToString());
1919
if (status.IsCorruption())
2020
throw leveldb_error("Database corrupted");
2121
if (status.IsIOError())
@@ -48,11 +48,11 @@ CLevelDBWrapper::CLevelDBWrapper(const boost::filesystem::path &path, size_t nCa
4848
options.env = penv;
4949
} else {
5050
if (fWipe) {
51-
LogPrintf("Wiping LevelDB in %s\n", path.string().c_str());
51+
LogPrintf("Wiping LevelDB in %s\n", path.string());
5252
leveldb::DestroyDB(path.string(), options);
5353
}
5454
boost::filesystem::create_directory(path);
55-
LogPrintf("Opening LevelDB in %s\n", path.string().c_str());
55+
LogPrintf("Opening LevelDB in %s\n", path.string());
5656
}
5757
leveldb::Status status = leveldb::DB::Open(options, path.string(), &pdb);
5858
HandleError(status);

0 commit comments

Comments
 (0)