commit d885fd33228f603a9adf8a77d96587e784223baa
parent 0028b20a7ac371ba4baa5a57c4059a01fdfc6c9a
Author: cfillion <cfillion@users.noreply.github.com>
Date: Mon, 13 Aug 2018 03:33:54 -0400
format strings in a dynamically-sized buffer more often
Diffstat:
8 files changed, 53 insertions(+), 80 deletions(-)
diff --git a/src/about.cpp b/src/about.cpp
@@ -323,12 +323,11 @@ void AboutIndexDelegate::initInstalledFiles()
}
}
catch(const reapack_error &e) {
- char msg[255];
- snprintf(msg, sizeof(msg),
+ Win32::setWindowText(report, String::format(
"The file list is currently unavailable.\x20"
"Retry later when all installation task are completed.\r\n"
- "\r\nError description: %s", e.what());
- Win32::setWindowText(report, msg);
+ "\r\nError description: %s", e.what()
+ ).c_str());
return;
}
diff --git a/src/browser.cpp b/src/browser.cpp
@@ -824,13 +824,11 @@ bool Browser::confirm() const
if(!count)
return true;
- char msg[255];
- snprintf(msg, sizeof(msg),
+ return IDYES == Win32::messageBox(handle(), String::format(
"Are you sure to uninstall %zu package%s?\nThe files and settings will"
" be permanently deleted from this computer.",
- count, count == 1 ? "" : "s");
-
- return IDYES == Win32::messageBox(handle(), msg, "ReaPack Query", MB_YESNO);
+ count, count == 1 ? "" : "s"
+ ).c_str(), "ReaPack Query", MB_YESNO);
}
bool Browser::apply()
diff --git a/src/download.cpp b/src/download.cpp
@@ -145,8 +145,8 @@ bool Download::run()
closeStream();
if(res != CURLE_OK) {
- char err[512];
- snprintf(err, sizeof(err), "%s (%d): %s", curl_easy_strerror(res), res, errbuf);
+ const string &err = String::format(
+ "%s (%d): %s", curl_easy_strerror(res), res, errbuf);
setError({err, m_url});
return false;
}
diff --git a/src/import.cpp b/src/import.cpp
@@ -139,14 +139,12 @@ void Import::fetch()
if(!read(dl, index))
m_pool->abort();
break;
- case ThreadTask::Failure: {
- char msg[1024];
- snprintf(msg, sizeof(msg), "Download failed: %s\n%s",
- dl->error().message.c_str(), url.c_str());
- Win32::messageBox(handle(), msg, TITLE, MB_OK);
+ case ThreadTask::Failure:
+ Win32::messageBox(handle(), String::format(
+ "Download failed: %s\n%s", dl->error().message.c_str(), url.c_str()
+ ).c_str(), TITLE, MB_OK);
m_pool->abort();
break;
- }
default:
break;
}
@@ -163,26 +161,23 @@ void Import::fetch()
bool Import::read(MemoryDownload *dl, const size_t idx)
try {
- char msg[1024];
-
IndexPtr index = Index::load({}, dl->contents().c_str());
Remote remote = g_reapack->remote(index->name());
const bool exists = !remote.isNull();
if(exists && remote.url() != dl->url()) {
if(remote.isProtected()) {
- snprintf(msg, sizeof(msg),
+ Win32::messageBox(handle(), String::format(
"The repository %s is protected and cannot be overwritten.",
- index->name().c_str());
- Win32::messageBox(handle(), msg, TITLE, MB_OK);
+ index->name().c_str()
+ ).c_str(), TITLE, MB_OK);
return false;
}
else {
- snprintf(msg, sizeof(msg),
+ const auto answer = Win32::messageBox(handle(), String::format(
"%s is already configured with a different URL.\n"
- "Do you want to overwrite it?", index->name().c_str());
-
- const auto answer = Win32::messageBox(handle(), msg, TITLE, MB_YESNO);
+ "Do you want to overwrite it?", index->name().c_str()
+ ).c_str(), TITLE, MB_YESNO);
if(answer != IDYES)
return true;
@@ -199,10 +194,9 @@ try {
return true;
}
catch(const reapack_error &e) {
- char msg[1024];
- snprintf(msg, sizeof(msg), "The received file is invalid: %s\n%s",
- e.what(), dl->url().c_str());
- Win32::messageBox(handle(), msg, TITLE, MB_OK);
+ Win32::messageBox(handle(), String::format(
+ "The received file is invalid: %s\n%s", e.what(), dl->url().c_str()
+ ).c_str(), TITLE, MB_OK);
return false;
}
diff --git a/src/main.cpp b/src/main.cpp
@@ -48,14 +48,11 @@ static bool loadAPI(void *(*getFunc)(const char *))
*func.ptr = getFunc(func.name);
if(func.required && *func.ptr == nullptr) {
- char msg[1024];
- snprintf(msg, sizeof(msg),
+ Win32::messageBox(Splash_GetWnd ? Splash_GetWnd() : nullptr, String::format(
"ReaPack v%s is incompatible with this version of REAPER.\n\n"
"(Unable to import the following API function: %s)",
- ReaPack::VERSION, func.name);
-
- Win32::messageBox(Splash_GetWnd ? Splash_GetWnd() : nullptr,
- msg, "ReaPack: Missing REAPER feature", MB_OK);
+ ReaPack::VERSION, func.name
+ ).c_str(), "ReaPack: Missing REAPER feature", MB_OK);
return false;
}
@@ -103,16 +100,13 @@ static bool checkLocation(REAPER_PLUGIN_HINSTANCE module)
if(current == expected)
return true;
- char msg[4096];
- snprintf(msg, sizeof(msg),
+ Win32::messageBox(Splash_GetWnd(), String::format(
"ReaPack was not loaded from the standard extension path"
" or its filename was altered.\n"
"Move or rename it to the expected location and retry.\n\n"
"Current: %s\n\nExpected: %s",
- current.join().c_str(), expected.join().c_str());
-
- Win32::messageBox(Splash_GetWnd(), msg,
- "ReaPack: Installation path mismatch", MB_OK);
+ current.join().c_str(), expected.join().c_str()
+ ).c_str(), "ReaPack: Installation path mismatch", MB_OK);
return false;
}
diff --git a/src/manager.cpp b/src/manager.cpp
@@ -502,10 +502,9 @@ void Manager::importArchive()
Archive::import(path);
}
catch(const reapack_error &e) {
- char msg[512];
- snprintf(msg, sizeof(msg), "An error occured while reading %s.\n\n%s.",
- path.c_str(), e.what());
- Win32::messageBox(handle(), msg, title, MB_OK);
+ Win32::messageBox(handle(), String::format(
+ "An error occured while reading %s.\n\n%s.", path.c_str(), e.what()
+ ).c_str(), title, MB_OK);
}
}
@@ -570,17 +569,16 @@ void Manager::setupNetwork()
bool Manager::confirm() const
{
- if(m_uninstall.empty())
- return true;
+ const size_t uninstallCount = m_uninstall.size();
- const size_t uninstallSize = m_uninstall.size();
+ if(!uninstallCount)
+ return true;
- char msg[255];
- snprintf(msg, sizeof(msg), "Uninstall %zu %s?\n"
+ return IDYES == Win32::messageBox(handle(), String::format(
+ "Uninstall %zu %s?\n"
"Every file they contain will be removed from your computer.",
- uninstallSize, uninstallSize == 1 ? "repository" : "repositories");
-
- return IDYES == Win32::messageBox(handle(), msg, "ReaPack Query", MB_YESNO);
+ uninstallCount, uninstallCount == 1 ? "repository" : "repositories"
+ ).c_str(), "ReaPack Query", MB_YESNO);
}
bool Manager::apply()
diff --git a/src/progress.cpp b/src/progress.cpp
@@ -88,23 +88,18 @@ void Progress::addTask(ThreadTask *task)
void Progress::updateProgress()
{
- const string &position = String::format("%s of %s",
- String::number(min(m_done + 1, m_total)).c_str(),
- String::number(m_total).c_str()
- );
+ Win32::setWindowText(m_label, String::format(m_current.c_str(),
+ String::format("%s of %s",
+ String::number(min(m_done + 1, m_total)).c_str(),
+ String::number(m_total).c_str()
+ ).c_str()
+ ).c_str());
- char label[1024];
- snprintf(label, sizeof(label), m_current.c_str(), position.c_str());
-
- Win32::setWindowText(m_label, label);
-
- const double pos = (double)(min(m_done+1, m_total)) / max(2, m_total);
- const int percent = (int)(pos * 100);
-
- char title[255];
- snprintf(title, sizeof(title),
- "ReaPack: Operation in progress (%d%%)", percent);
+ const double pos = static_cast<double>(min(m_done+1, m_total)) / max(2, m_total);
+ const int percent = static_cast<int>(pos * 100);
SendMessage(m_progress, PBM_SETPOS, percent, 0);
- Win32::setWindowText(handle(), title);
+ Win32::setWindowText(handle(), String::format(
+ "ReaPack: Operation in progress (%d%%)", percent
+ ).c_str());
}
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -275,13 +275,10 @@ Transaction *ReaPack::setupTransaction()
m_tx = new Transaction;
}
catch(const reapack_error &e) {
- char msg[512];
- snprintf(msg, sizeof(msg),
+ Win32::messageBox(m_mainWindow, String::format(
"The following error occurred while creating a transaction:\n\n%s",
e.what()
- );
-
- Win32::messageBox(m_mainWindow, msg, "ReaPack", MB_OK);
+ ).c_str(), "ReaPack", MB_OK);
return nullptr;
}
@@ -365,14 +362,12 @@ void ReaPack::createDirectories()
if(FS::mkdir(path))
return;
- char msg[255];
- snprintf(msg, sizeof(msg),
+ Win32::messageBox(Splash_GetWnd(), String::format(
"ReaPack could not create %s! "
"Please investigate or report this issue.\n\n"
"Error description: %s",
- path.prependRoot().join().c_str(), FS::lastError());
-
- Win32::messageBox(Splash_GetWnd(), msg, "ReaPack", MB_OK);
+ path.prependRoot().join().c_str(), FS::lastError()
+ ).c_str(), "ReaPack", MB_OK);
}
void ReaPack::registerSelf()