Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
Make utils::strprintf use variadic template
Browse files Browse the repository at this point in the history
  • Loading branch information
Minoru committed Oct 27, 2016
1 parent daf395c commit 26522a9
Show file tree
Hide file tree
Showing 33 changed files with 173 additions and 167 deletions.
35 changes: 34 additions & 1 deletion include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vector>
#include <string>
#include <stdexcept>
#include <memory>

#include <logger.h>
#include <curl/curl.h>
Expand Down Expand Up @@ -71,7 +72,39 @@ class utils {

static std::string absolute_url(const std::string& url, const std::string& link);

static std::string strprintf(const char * format, ...);
static std::string strprintf(const std::string& format) {
return format;
}

template<typename... Args>
static std::string strprintf(
const std::string& format, const std::string& argument, Args... args)
{
return strprintf(format, argument.c_str(), args...);
}

template<typename T, typename... Args>
static std::string strprintf(
const std::string& format, const T& argument, Args... args)
{
std::string local_format, remaining_format;
std::tie(local_format, remaining_format) =
utils::split_format(format);

char buffer[1024];
std::string result;
unsigned int len = 1 + snprintf(
buffer, sizeof(buffer), local_format.c_str(), argument);
if (len <= sizeof(buffer)) {
result = buffer;
} else {
std::unique_ptr<char> buf(new char[len]);
snprintf(buf.get(), len, local_format.c_str(), argument);
result = *buf;
}

return result + strprintf(remaining_format, args...);
}

static std::string get_useragent(configcontainer * cfgcont);

Expand Down
2 changes: 1 addition & 1 deletion rss/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ feed parser::parse_url(const std::string& url, time_t lastmodified, const std::s
}

if (etag.length() > 0) {
auto header = utils::strprintf("If-None-Match: %s", etag.c_str());
auto header = utils::strprintf("If-None-Match: %s", etag);
custom_headers = curl_slist_append(custom_headers, header.c_str());
custom_headers = curl_slist_append(custom_headers, "A-IM: feed");
}
Expand Down
10 changes: 5 additions & 5 deletions src/colormanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ void colormanager::handle_action(const std::string& action, const std::vector<st
std::string bgcolor = params[2];

if (!utils::is_valid_color(fgcolor))
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), fgcolor.c_str()));
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), fgcolor));
if (!utils::is_valid_color(bgcolor))
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), bgcolor.c_str()));
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid color"), bgcolor));

std::vector<std::string> attribs;
for (unsigned int i=3; i<params.size(); ++i) {
if (!utils::is_valid_attribute(params[i]))
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid attribute"), params[i].c_str()));
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid attribute"), params[i]));
attribs.push_back(params[i]);
}

Expand All @@ -60,15 +60,15 @@ void colormanager::handle_action(const std::string& action, const std::vector<st
attributes[element] = attribs;
colors_loaded_ = true;
} else
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid configuration element"), element.c_str()));
throw confighandlerexception(utils::strprintf(_("`%s' is not a valid configuration element"), element));

} else
throw confighandlerexception(AHS_INVALID_COMMAND);
}

void colormanager::dump_config(std::vector<std::string>& config_output) {
for (auto color : fg_colors) {
std::string configline = utils::strprintf("color %s %s %s", color.first.c_str(), color.second.c_str(), bg_colors[color.first].c_str());
std::string configline = utils::strprintf("color %s %s %s", color.first, color.second, bg_colors[color.first]);
for (auto attrib : attributes[color.first]) {
configline.append(" ");
configline.append(attrib);
Expand Down
10 changes: 5 additions & 5 deletions src/configcontainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,19 @@ void configcontainer::handle_action(const std::string& action, const std::vector
switch (cfgdata.type) {
case configdata::BOOL:
if (!is_bool(params[0]))
throw confighandlerexception(utils::strprintf(_("expected boolean value, found `%s' instead"), params[0].c_str()));
throw confighandlerexception(utils::strprintf(_("expected boolean value, found `%s' instead"), params[0]));
cfgdata.value = params[0];
break;

case configdata::INT:
if (!is_int(params[0]))
throw confighandlerexception(utils::strprintf(_("expected integer value, found `%s' instead"), params[0].c_str()));
throw confighandlerexception(utils::strprintf(_("expected integer value, found `%s' instead"), params[0]));
cfgdata.value = params[0];
break;

case configdata::ENUM:
if (cfgdata.enum_values.find(params[0]) == cfgdata.enum_values.end())
throw confighandlerexception(utils::strprintf(_("invalid configuration value `%s'"), params[0].c_str()));
throw confighandlerexception(utils::strprintf(_("invalid configuration value `%s'"), params[0]));
// fall-through
case configdata::STR:
case configdata::PATH:
Expand Down Expand Up @@ -317,7 +317,7 @@ void configcontainer::dump_config(std::vector<std::string>& config_output) {
case configdata::INT:
configline.append(cfg.second.value);
if (cfg.second.value != cfg.second.default_value)
configline.append(utils::strprintf(" # default: %s", cfg.second.default_value.c_str()));
configline.append(utils::strprintf(" # default: %s", cfg.second.default_value));
break;
case configdata::ENUM:
case configdata::STR:
Expand All @@ -330,7 +330,7 @@ void configcontainer::dump_config(std::vector<std::string>& config_output) {
} else {
configline.append(utils::quote(cfg.second.value));
if (cfg.second.value != cfg.second.default_value) {
configline.append(utils::strprintf(" # default: %s", cfg.second.default_value.c_str()));
configline.append(utils::strprintf(" # default: %s", cfg.second.default_value));
}
}
break;
Expand Down
4 changes: 2 additions & 2 deletions src/configparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ bool configparser::parse(const std::string& filename, bool double_include) {
evaluate_backticks(tokens);
handler->handle_action(cmd,tokens);
} catch (const confighandlerexception& e) {
throw configexception(utils::strprintf(_("Error while processing command `%s' (%s line %u): %s"), line.c_str(), filename.c_str(), linecounter, e.what()));
throw configexception(utils::strprintf(_("Error while processing command `%s' (%s line %u): %s"), line, filename, linecounter, e.what()));
}
} else {
throw configexception(utils::strprintf(_("unknown command `%s'"), cmd.c_str()));
throw configexception(utils::strprintf(_("unknown command `%s'"), cmd));
}
}
getline(f,line);
Expand Down
42 changes: 21 additions & 21 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ bool controller::setup_dirs_xdg(const char *env_home, bool silent) {
<< utils::strprintf(
_("XDG: configuration directory '%s' not accessible, "
"using '%s' instead."),
xdg_config_dir.c_str(),
config_dir.c_str())
xdg_config_dir,
config_dir)
<< std::endl;
}

Expand All @@ -159,8 +159,8 @@ bool controller::setup_dirs_xdg(const char *env_home, bool silent) {
cache_file = xdg_data_dir + std::string(NEWSBEUTER_PATH_SEP) + cache_file;
lock_file = cache_file + LOCK_SUFFIX;
queue_file = xdg_data_dir + std::string(NEWSBEUTER_PATH_SEP) + queue_file;
searchfile = utils::strprintf("%s%shistory.search", xdg_data_dir.c_str(), NEWSBEUTER_PATH_SEP);
cmdlinefile = utils::strprintf("%s%shistory.cmdline", xdg_data_dir.c_str(), NEWSBEUTER_PATH_SEP);
searchfile = utils::strprintf("%s%shistory.search", xdg_data_dir, NEWSBEUTER_PATH_SEP);
cmdlinefile = utils::strprintf("%s%shistory.cmdline", xdg_data_dir, NEWSBEUTER_PATH_SEP);

return true;
}
Expand Down Expand Up @@ -193,8 +193,8 @@ void controller::setup_dirs(bool silent) {
config_file = config_dir + std::string(NEWSBEUTER_PATH_SEP) + config_file;
queue_file = config_dir + std::string(NEWSBEUTER_PATH_SEP) + queue_file;

searchfile = utils::strprintf("%s%shistory.search", config_dir.c_str(), NEWSBEUTER_PATH_SEP);
cmdlinefile = utils::strprintf("%s%shistory.cmdline", config_dir.c_str(), NEWSBEUTER_PATH_SEP);
searchfile = utils::strprintf("%s%shistory.search", config_dir, NEWSBEUTER_PATH_SEP);
cmdlinefile = utils::strprintf("%s%shistory.cmdline", config_dir, NEWSBEUTER_PATH_SEP);
}

controller::~controller() {
Expand Down Expand Up @@ -444,7 +444,7 @@ void controller::run(int argc, char * argv[]) {
try {
rsscache = new cache(cache_file,&cfg);
} catch (const dbexception& e) {
std::cerr << utils::strprintf(_("Error: opening the cache file `%s' failed: %s"), cache_file.c_str(), e.what()) << std::endl;
std::cerr << utils::strprintf(_("Error: opening the cache file `%s' failed: %s"), cache_file, e.what()) << std::endl;
utils::remove_fs_lock(lock_file);
::exit(EXIT_FAILURE);
}
Expand Down Expand Up @@ -480,7 +480,7 @@ void controller::run(int argc, char * argv[]) {
}

if (!do_export && !silent) {
std::cout << utils::strprintf(_("Loading URLs from %s..."), urlcfg->get_source().c_str());
std::cout << utils::strprintf(_("Loading URLs from %s..."), urlcfg->get_source());
std::cout.flush();
}
if (api) {
Expand All @@ -499,7 +499,7 @@ void controller::run(int argc, char * argv[]) {
LOG(LOG_ERROR,"no URLs configured.");
std::string msg;
if (type == "local") {
msg = utils::strprintf(_("Error: no URLs configured. Please fill the file %s with RSS feed URLs or import an OPML file."), url_file.c_str());
msg = utils::strprintf(_("Error: no URLs configured. Please fill the file %s with RSS feed URLs or import an OPML file."), url_file);
} else if (type == "opml") {
msg = utils::strprintf(_("It looks like the OPML feed you subscribed contains no feeds. Please fill it with feeds, and try again."));
} else if (type == "oldreader") {
Expand Down Expand Up @@ -546,7 +546,7 @@ void controller::run(int argc, char * argv[]) {
utils::remove_fs_lock(lock_file);
return;
} catch(const std::string& str) {
std::cout << utils::strprintf(_("Error while loading feed '%s': %s"), url.c_str(), str.c_str()) << std::endl;
std::cout << utils::strprintf(_("Error while loading feed '%s': %s"), url, str) << std::endl;
utils::remove_fs_lock(lock_file);
return;
}
Expand Down Expand Up @@ -720,7 +720,7 @@ void controller::reload(unsigned int pos, unsigned int max, bool unattended, cur
std::shared_ptr<rss_feed> oldfeed = feeds[pos];
std::string errmsg;
if (!unattended)
v->set_status(utils::strprintf(_("%sLoading %s..."), prepare_message(pos+1, max).c_str(), utils::censor_url(oldfeed->rssurl()).c_str()));
v->set_status(utils::strprintf(_("%sLoading %s..."), prepare_message(pos+1, max), utils::censor_url(oldfeed->rssurl())));

bool ignore_dl = (cfg.get_configvalue("ignore-mode") == "download");

Expand Down Expand Up @@ -755,11 +755,11 @@ void controller::reload(unsigned int pos, unsigned int max, bool unattended, cur
oldfeed->set_status(SUCCESS);
v->set_status("");
} catch (const dbexception& e) {
errmsg = utils::strprintf(_("Error while retrieving %s: %s"), utils::censor_url(oldfeed->rssurl()).c_str(), e.what());
errmsg = utils::strprintf(_("Error while retrieving %s: %s"), utils::censor_url(oldfeed->rssurl()), e.what());
} catch (const std::string& emsg) {
errmsg = utils::strprintf(_("Error while retrieving %s: %s"), utils::censor_url(oldfeed->rssurl()).c_str(), emsg.c_str());
errmsg = utils::strprintf(_("Error while retrieving %s: %s"), utils::censor_url(oldfeed->rssurl()), emsg);
} catch (rsspp::exception& e) {
errmsg = utils::strprintf(_("Error while retrieving %s: %s"), utils::censor_url(oldfeed->rssurl()).c_str(), e.what());
errmsg = utils::strprintf(_("Error while retrieving %s: %s"), utils::censor_url(oldfeed->rssurl()), e.what());
}
if (errmsg != "") {
oldfeed->set_status(DL_ERROR);
Expand Down Expand Up @@ -1306,7 +1306,7 @@ void controller::edit_urls_file() {
if (!editor)
editor = "vi";

std::string cmdline = utils::strprintf("%s \"%s\"", editor, utils::replace_all(url_file,"\"","\\\"").c_str());
std::string cmdline = utils::strprintf("%s \"%s\"", editor, utils::replace_all(url_file,"\"","\\\""));

v->push_empty_formaction();
stfl::reset();
Expand Down Expand Up @@ -1340,11 +1340,11 @@ std::string controller::bookmark(
bool is_interactive = cfg.get_configvalue_as_bool("bookmark-interactive");
if (bookmark_cmd.length() > 0) {
std::string cmdline = utils::strprintf("%s '%s' %s %s %s",
bookmark_cmd.c_str(),
utils::replace_all(url,"'", "%27").c_str(),
quote_empty(stfl::quote(title)).c_str(),
quote_empty(stfl::quote(description)).c_str(),
quote_empty(stfl::quote(feed_title)).c_str());
bookmark_cmd,
utils::replace_all(url,"'", "%27"),
quote_empty(stfl::quote(title)),
quote_empty(stfl::quote(description)),
quote_empty(stfl::quote(feed_title)));

LOG(LOG_DEBUG, "controller::bookmark: cmd = %s", cmdline.c_str());

Expand Down Expand Up @@ -1614,7 +1614,7 @@ void controller::load_configfile(const std::string& filename) {
if (cfgparser.parse(filename, true)) {
update_config();
} else {
v->show_error(utils::strprintf(_("Error: couldn't open configuration file `%s'!"), filename.c_str()));
v->show_error(utils::strprintf(_("Error: couldn't open configuration file `%s'!"), filename));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/dialogs_formaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void dialogs_formaction::prepare() {
unsigned int i = 1;
for (auto fa : v->get_formaction_names()) {
LOG(LOG_DEBUG, "dialogs_formaction::prepare: p1 = %p p2 = %p", v->get_formaction(fa.first).get(), get_parent_formaction().get());
listfmt.add_line(utils::strprintf("%4u %s %s", i, (v->get_formaction(fa.first).get() == get_parent_formaction().get()) ? "*" : " ", fa.second.c_str()), fa.first);
listfmt.add_line(utils::strprintf("%4u %s %s", i, (v->get_formaction(fa.first).get() == get_parent_formaction().get()) ? "*" : " ", fa.second), fa.first);
i++;
}

Expand Down
4 changes: 2 additions & 2 deletions src/exception.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ const char * matcherexception::what() const throw() {
static std::string errmsg;
switch (type) {
case ATTRIB_UNAVAIL:
errmsg = utils::strprintf(_("attribute `%s' is not available."), addinfo.c_str());
errmsg = utils::strprintf(_("attribute `%s' is not available."), addinfo);
break;
case INVALID_REGEX:
errmsg = utils::strprintf(_("regular expression '%s' is invalid: %s"), addinfo.c_str(), addinfo2.c_str());
errmsg = utils::strprintf(_("regular expression '%s' is invalid: %s"), addinfo, addinfo2);
break;
}
return errmsg.c_str();
Expand Down
18 changes: 9 additions & 9 deletions src/feedhq_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ std::vector<tagged_feedurl> feedhq_api::get_subscribed_urls() {

auto url = utils::strprintf(
"%s%s%s?n=%u",
cfg->get_configvalue("feedhq-url").c_str(),
cfg->get_configvalue("feedhq-url"),
FEEDHQ_FEED_PREFIX,
escaped_id,
cfg->get_configvalue_as_int("feedhq-min-items"));
Expand All @@ -181,7 +181,7 @@ std::vector<tagged_feedurl> feedhq_api::get_subscribed_urls() {
void feedhq_api::add_custom_headers(curl_slist** custom_headers) {
if (auth_header.empty()) {
auth_header = utils::strprintf(
"Authorization: GoogleLogin auth=%s", auth.c_str());
"Authorization: GoogleLogin auth=%s", auth);
}
LOG(LOG_DEBUG,
"feedhq_api::add_custom_headers header = %s",
Expand All @@ -196,7 +196,7 @@ bool feedhq_api::mark_all_read(const std::string& feedurl) {
real_feedurl = utils::unescape_url(elems[0]);
std::string token = get_new_token();

std::string postcontent = utils::strprintf("s=%s&T=%s", real_feedurl.c_str(), token.c_str());
std::string postcontent = utils::strprintf("s=%s&T=%s", real_feedurl, token);

std::string result = post_content(cfg->get_configvalue("feedhq-url") + FEEDHQ_API_MARK_ALL_READ_URL, postcontent);

Expand All @@ -212,9 +212,9 @@ bool feedhq_api::mark_article_read_with_token(const std::string& guid, bool read
std::string postcontent;

if (read) {
postcontent = utils::strprintf("i=%s&a=user/-/state/com.google/read&r=user/-/state/com.google/kept-unread&ac=edit&T=%s", guid.c_str(), token.c_str());
postcontent = utils::strprintf("i=%s&a=user/-/state/com.google/read&r=user/-/state/com.google/kept-unread&ac=edit&T=%s", guid, token);
} else {
postcontent = utils::strprintf("i=%s&r=user/-/state/com.google/read&a=user/-/state/com.google/kept-unread&a=user/-/state/com.google/tracking-kept-unread&ac=edit&T=%s", guid.c_str(), token.c_str());
postcontent = utils::strprintf("i=%s&r=user/-/state/com.google/read&a=user/-/state/com.google/kept-unread&a=user/-/state/com.google/tracking-kept-unread&ac=edit&T=%s", guid, token);
}

std::string result = post_content(cfg->get_configvalue("feedhq-url") + FEEDHQ_API_EDIT_TAG_URL, postcontent);
Expand Down Expand Up @@ -272,9 +272,9 @@ bool feedhq_api::star_article(const std::string& guid, bool star) {
std::string postcontent;

if (star) {
postcontent = utils::strprintf("i=%s&a=user/-/state/com.google/starred&ac=edit&T=%s", guid.c_str(), token.c_str());
postcontent = utils::strprintf("i=%s&a=user/-/state/com.google/starred&ac=edit&T=%s", guid, token);
} else {
postcontent = utils::strprintf("i=%s&r=user/-/state/com.google/starred&ac=edit&T=%s", guid.c_str(), token.c_str());
postcontent = utils::strprintf("i=%s&r=user/-/state/com.google/starred&ac=edit&T=%s", guid, token);
}

std::string result = post_content(cfg->get_configvalue("feedhq-url") + FEEDHQ_API_EDIT_TAG_URL, postcontent);
Expand All @@ -287,9 +287,9 @@ bool feedhq_api::share_article(const std::string& guid, bool share) {
std::string postcontent;

if (share) {
postcontent = utils::strprintf("i=%s&a=user/-/state/com.google/broadcast&ac=edit&T=%s", guid.c_str(), token.c_str());
postcontent = utils::strprintf("i=%s&a=user/-/state/com.google/broadcast&ac=edit&T=%s", guid, token);
} else {
postcontent = utils::strprintf("i=%s&r=user/-/state/com.google/broadcast&ac=edit&T=%s", guid.c_str(), token.c_str());
postcontent = utils::strprintf("i=%s&r=user/-/state/com.google/broadcast&ac=edit&T=%s", guid, token);
}

std::string result = post_content(cfg->get_configvalue("feedhq-url") + FEEDHQ_API_EDIT_TAG_URL, postcontent);
Expand Down
Loading

0 comments on commit 26522a9

Please sign in to comment.