summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJannis M. Hoffmann <jannis@fehcom.de>2023-10-11 22:29:52 +0200
committerJannis M. Hoffmann <jannis@fehcom.de>2023-10-11 22:29:52 +0200
commitb33912838f8d86284d5e4efa18bb8d23686106d8 (patch)
treea769af1cdab14e59e9e81f70f61cecc2e91e9475
parentbc946633e0bcae5fe63528ad743bcc67de7e347d (diff)
removed toString functions in convert
small type adjustments in BincStream use from_chars instead of atoi
-rw-r--r--src/convert.cc20
-rw-r--r--src/imapparser.cc2
-rw-r--r--src/include/convert.h68
-rw-r--r--src/session-initialize-bincimap-up.cc54
-rw-r--r--src/session-initialize-bincimapd.cc63
5 files changed, 87 insertions, 120 deletions
diff --git a/src/convert.cc b/src/convert.cc
index 3af143a..d0ffa81 100644
--- a/src/convert.cc
+++ b/src/convert.cc
@@ -1,8 +1,8 @@
/**
- * @file convert.cc
- * @brief Implementation of miscellaneous convertion functions
- * @author Andreas Aardal Hanssen
- * @date 2002-2005
+ * @file convert.cc
+ * @brief Implementation of miscellaneous convertion functions
+ * @author Andreas Aardal Hanssen
+ * @date 2002-2005
*/
#include "convert.h"
@@ -19,7 +19,7 @@ BincStream::~BincStream()
clear();
}
-string BincStream::popString(unsigned int size)
+string BincStream::popString(size_t size)
{
if (size > nstr.length()) size = nstr.length();
string tmp = nstr.substr(0, size);
@@ -56,9 +56,9 @@ void BincStream::clear()
nstr = "";
}
-unsigned int BincStream::getSize() const
+size_t BincStream::getSize() const
{
- return (unsigned int)nstr.length();
+ return nstr.length();
}
BincStream &BincStream::operator<<(std::ostream &(*)(std::ostream &))
@@ -75,19 +75,19 @@ BincStream &BincStream::operator<<(const string &t)
BincStream &BincStream::operator<<(int t)
{
- nstr += toString(t);
+ nstr += std::to_string(t);
return *this;
}
BincStream &BincStream::operator<<(unsigned long t)
{
- nstr += toString(t);
+ nstr += std::to_string(t);
return *this;
}
BincStream &BincStream::operator<<(unsigned int t)
{
- nstr += toString(t);
+ nstr += std::to_string(t);
return *this;
}
diff --git a/src/imapparser.cc b/src/imapparser.cc
index e004feb..90ae6a4 100644
--- a/src/imapparser.cc
+++ b/src/imapparser.cc
@@ -310,7 +310,7 @@ string BincImapParserFetchAtt::toString()
if (offsetstart == 0 && offsetlength == (unsigned int)-1)
tmp += " ";
else
- tmp += "<" + Binc::toString(offsetstart) + "> ";
+ tmp += "<" + std::to_string(offsetstart) + "> ";
}
}
diff --git a/src/include/convert.h b/src/include/convert.h
index 030c1b5..cc5f053 100644
--- a/src/include/convert.h
+++ b/src/include/convert.h
@@ -1,8 +1,8 @@
/**
- * @file convert.h
- * @brief Declaration of miscellaneous convertion functions.
- * @author Andreas Aardal Hanssen
- * @date 2002-2005
+ * @file convert.h
+ * @brief Declaration of miscellaneous convertion functions.
+ * @author Andreas Aardal Hanssen
+ * @date 2002-2005
*/
#ifndef convert_h_included
@@ -18,50 +18,19 @@
#include <string_view>
#include <vector>
-#include <stdio.h>
-
#include <sys/stat.h>
namespace Binc {
- inline std::string toString(int i_in)
- {
- char intbuf[16];
- snprintf(intbuf, sizeof(intbuf), "%d", i_in);
- return std::string(intbuf);
- }
-
- inline std::string toString(unsigned int i_in)
- {
- char intbuf[16];
- snprintf(intbuf, sizeof(intbuf), "%u", i_in);
- return std::string(intbuf);
- }
-
- inline std::string toString(unsigned long i_in)
- {
- char longbuf[40];
- snprintf(longbuf, sizeof(longbuf), "%lu", i_in);
- return std::string(longbuf);
- }
-
- inline std::string toString(const char *i_in)
+ inline std::string toHex(std::string_view s)
{
- return std::string(i_in);
- }
-
- inline int atoi(const std::string &s_in)
- {
- return ::atoi(s_in.c_str());
- }
-
- inline std::string toHex(const std::string &s)
- {
- const char hexchars[] = "0123456789abcdef";
+ std::string_view hexchars = "0123456789abcdef";
std::string tmp;
- for (auto i : s) {
- unsigned char c = static_cast<unsigned char>(i);
- tmp += hexchars[((c & 0xf0) >> 4)];
+ tmp.reserve(s.length() * 2);
+
+ for (char i : s) {
+ auto c = static_cast<unsigned char>(i);
+ tmp += hexchars[(c & 0xf0) >> 4];
tmp += hexchars[c & 0x0f];
}
@@ -70,15 +39,14 @@ namespace Binc {
inline std::string fromHex(const std::string &s)
{
- // const
- char hexchars[] = "0123456789abcdef";
+ const char hexchars[] = "0123456789abcdef";
std::string tmp;
for (auto i = s.cbegin(); i != s.cend() && i + 1 != s.cend(); i += 2) {
int n;
unsigned char c = *i;
unsigned char d = *(i + 1);
- char *t;
+ const char *t;
if ((t = strchr(hexchars, c)) == nullptr) return "out of range";
n = (t - hexchars) << 4;
@@ -99,7 +67,7 @@ namespace Binc {
for (const auto i : s_in) {
auto c = static_cast<unsigned char>(i);
if (c <= 31 || c >= 127 || c == '\"' || c == '\\')
- return "{" + toString(s_in.length()) + "}\r\n" + s_in;
+ return "{" + std::to_string(s_in.length()) + "}\r\n" + s_in;
}
return "\"" + s_in + "\"";
@@ -117,14 +85,14 @@ namespace Binc {
i = tolower(i);
}
- inline void chomp(std::string &s_in, const std::string &chars = " \t\r\n")
+ inline void chomp(std::string &s_in, std::string_view chars = " \t\r\n")
{
int n = s_in.length();
while (n > 1 && chars.find(s_in[n - 1]) != std::string::npos)
s_in.resize(n-- - 1);
}
- inline void trim(std::string &s_in, const std::string &chars = " \t\r\n")
+ inline void trim(std::string &s_in, std::string_view chars = " \t\r\n")
{
while (s_in != "" && chars.find(s_in[0]) != std::string::npos)
s_in = s_in.substr(1);
@@ -275,7 +243,7 @@ namespace Binc {
BincStream &operator<<(int t);
BincStream &operator<<(char t);
- std::string popString(unsigned int size);
+ std::string popString(size_t size);
char popChar();
void unpopChar(char c);
@@ -283,7 +251,7 @@ namespace Binc {
const std::string &str() const;
- unsigned int getSize() const;
+ size_t getSize() const;
void clear();
diff --git a/src/session-initialize-bincimap-up.cc b/src/session-initialize-bincimap-up.cc
index 646315a..ee9f8db 100644
--- a/src/session-initialize-bincimap-up.cc
+++ b/src/session-initialize-bincimap-up.cc
@@ -11,16 +11,17 @@
#include "iodevice.h"
#include "iofactory.h"
#include "multilogdevice.h"
+#include "operators.h"
#include "session.h"
#include "stdiodevice.h"
#include "syslogdevice.h"
#include "tools.h"
+#include <cctype>
+#include <charconv>
#include <map>
#include <string>
-#include <ctype.h>
-
#include <fcntl.h>
#include <syslog.h>
@@ -71,30 +72,31 @@ bool Session::initialize(int argc, char *argv[])
ioFactory.addDevice(device);
} else if (logtype == "syslog") {
const string f = session.getEnv("SYSLOG_FACILITY");
- int facility;
-
- if (isdigit(f[0]))
- facility = atoi(f);
- else if (f == "LOG_USER")
- facility = LOG_USER;
- else if (f == "LOG_LOCAL0")
- facility = LOG_LOCAL0;
- else if (f == "LOG_LOCAL1")
- facility = LOG_LOCAL1;
- else if (f == "LOG_LOCAL2")
- facility = LOG_LOCAL2;
- else if (f == "LOG_LOCAL3")
- facility = LOG_LOCAL3;
- else if (f == "LOG_LOCAL4")
- facility = LOG_LOCAL4;
- else if (f == "LOG_LOCAL5")
- facility = LOG_LOCAL5;
- else if (f == "LOG_LOCAL6")
- facility = LOG_LOCAL6;
- else if (f == "LOG_LOCAL7")
- facility = LOG_LOCAL7;
- else
- facility = LOG_DAEMON;
+ int facility = 0;
+ auto fcr = std::from_chars(f.c_str(), f.c_str() + f.size(), facility);
+
+ if (fcr.ec != std::errc{}) {
+ if (f == "LOG_USER")
+ facility = LOG_USER;
+ else if (f == "LOG_LOCAL0")
+ facility = LOG_LOCAL0;
+ else if (f == "LOG_LOCAL1")
+ facility = LOG_LOCAL1;
+ else if (f == "LOG_LOCAL2")
+ facility = LOG_LOCAL2;
+ else if (f == "LOG_LOCAL3")
+ facility = LOG_LOCAL3;
+ else if (f == "LOG_LOCAL4")
+ facility = LOG_LOCAL4;
+ else if (f == "LOG_LOCAL5")
+ facility = LOG_LOCAL5;
+ else if (f == "LOG_LOCAL6")
+ facility = LOG_LOCAL6;
+ else if (f == "LOG_LOCAL7")
+ facility = LOG_LOCAL7;
+ else
+ facility = LOG_DAEMON;
+ }
SyslogDevice *device = new SyslogDevice(IODevice::IsEnabled | IODevice::FlushesOnEndl,
"bincimap-up",
diff --git a/src/session-initialize-bincimapd.cc b/src/session-initialize-bincimapd.cc
index 2590403..60543b5 100644
--- a/src/session-initialize-bincimapd.cc
+++ b/src/session-initialize-bincimapd.cc
@@ -14,20 +14,16 @@
#include "iofactory.h"
#include "maildir.h"
#include "multilogdevice.h"
+#include "operators.h"
#include "session.h"
#include "stdiodevice.h"
#include "syslogdevice.h"
#include "tools.h"
+#include <charconv>
#include <map>
#include <string>
-#include <errno.h>
-#include <signal.h>
-
-#include <syslog.h>
-#include <unistd.h>
-
using namespace Binc;
using std::endl;
using std::string;
@@ -75,32 +71,33 @@ bool Session::initialize(int argc, char *argv[])
} else if (logtype == "syslog") {
const string f = session.getEnv("SYSLOG_FACILITY");
- int facility;
-
- if (isdigit(f[0]))
- facility = atoi(f);
- else if (f == "LOG_USER")
- facility = LOG_USER;
- else if (f == "LOG_LOCAL0")
- facility = LOG_LOCAL0;
- else if (f == "LOG_LOCAL1")
- facility = LOG_LOCAL1;
- else if (f == "LOG_LOCAL2")
- facility = LOG_LOCAL2;
- else if (f == "LOG_LOCAL3")
- facility = LOG_LOCAL3;
- else if (f == "LOG_LOCAL4")
- facility = LOG_LOCAL4;
- else if (f == "LOG_LOCAL5")
- facility = LOG_LOCAL5;
- else if (f == "LOG_LOCAL6")
- facility = LOG_LOCAL6;
- else if (f == "LOG_LOCAL7")
- facility = LOG_LOCAL7;
- else
- facility = LOG_DAEMON;
-
- session.setEnv("SYSLOG_FACILITY", toString(facility));
+ int facility = 0;
+ auto fcr = std::from_chars(f.c_str(), f.c_str() + f.size(), facility);
+
+ if (fcr.ec != std::errc{}) {
+ if (f == "LOG_USER")
+ facility = LOG_USER;
+ else if (f == "LOG_LOCAL0")
+ facility = LOG_LOCAL0;
+ else if (f == "LOG_LOCAL1")
+ facility = LOG_LOCAL1;
+ else if (f == "LOG_LOCAL2")
+ facility = LOG_LOCAL2;
+ else if (f == "LOG_LOCAL3")
+ facility = LOG_LOCAL3;
+ else if (f == "LOG_LOCAL4")
+ facility = LOG_LOCAL4;
+ else if (f == "LOG_LOCAL5")
+ facility = LOG_LOCAL5;
+ else if (f == "LOG_LOCAL6")
+ facility = LOG_LOCAL6;
+ else if (f == "LOG_LOCAL7")
+ facility = LOG_LOCAL7;
+ else
+ facility = LOG_DAEMON;
+ }
+
+ session.setEnv("SYSLOG_FACILITY", std::to_string(facility));
ioFactory.addDevice(
new SyslogDevice(IODevice::IsEnabled, "bincimapd", LOG_NDELAY | LOG_PID, facility));
@@ -114,7 +111,7 @@ bool Session::initialize(int argc, char *argv[])
string pid = std::to_string(session.getPid());
auto logindetails = Tools::getenv("BINCIMAP_LOGIN");
- if (!logindetails) {
+ if (!logindetails || logindetails->empty()) {
bincLog << "bincimapd: pid " << pid
<< " BINCIMAP_LOGIN missing from environment (are you sure you invoked " << argv[0]
<< " properly?)\n";