summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/cxxstdio.hpp28
-rw-r--r--src/common/md5calc.cpp6
-rw-r--r--src/common/md5calc.hpp4
-rw-r--r--src/common/utils.cpp14
-rw-r--r--src/common/utils.hpp4
-rw-r--r--src/common/utils2.hpp8
6 files changed, 40 insertions, 24 deletions
diff --git a/src/common/cxxstdio.hpp b/src/common/cxxstdio.hpp
index 89cc5de..1ae1be3 100644
--- a/src/common/cxxstdio.hpp
+++ b/src/common/cxxstdio.hpp
@@ -24,6 +24,8 @@
#include <cstdarg>
#include <cstdio>
+#include "../io/fwd.hpp"
+
#include "const_array.hpp"
#include "utils2.hpp"
@@ -211,28 +213,28 @@ namespace cxxstdio
};
#define XPRINTF(out, fmt, ...) \
- ([&]() -> int \
+ (/*[&]() -> int*/ \
{ \
struct format_impl \
{ \
constexpr static \
const char *print_format() { return fmt; } \
}; \
- return cxxstdio::PrintFormatter<format_impl>::print(out, ## __VA_ARGS__); \
- }())
+ /*return*/ cxxstdio::PrintFormatter<format_impl>::print(out, ## __VA_ARGS__); \
+ }/*()*/)
#define XSCANF(out, fmt, ...) \
- ([&]() -> int \
+ (/*[&]() -> int*/ \
{ \
struct format_impl \
{ \
constexpr static \
const char *scan_format() { return fmt; } \
}; \
- return cxxstdio::ScanFormatter<format_impl>::scan(out, ## __VA_ARGS__); \
- }())
+ /*return*/ cxxstdio::ScanFormatter<format_impl>::scan(out, ## __VA_ARGS__); \
+ }/*()*/)
-#define FPRINTF(file, fmt, ...) XPRINTF(no_cast<FILE *>(file), fmt, ## __VA_ARGS__)
+#define FPRINTF(file, fmt, ...) XPRINTF(/*no_cast<FILE *>*/(file), fmt, ## __VA_ARGS__)
#define FSCANF(file, fmt, ...) XSCANF(no_cast<FILE *>(file), fmt, ## __VA_ARGS__)
#define PRINTF(fmt, ...) FPRINTF(stdout, fmt, ## __VA_ARGS__)
#define SPRINTF(str, fmt, ...) XPRINTF(base_cast<FString&>(str), fmt, ## __VA_ARGS__)
@@ -241,20 +243,20 @@ namespace cxxstdio
#define SSCANF(str, fmt, ...) XSCANF(/*ZString or compatible*/str, fmt, ## __VA_ARGS__)
#define STRPRINTF(fmt, ...) \
- ([&]() -> FString \
+ (/*[&]() -> FString*/ \
{ \
FString _out_impl; \
SPRINTF(_out_impl, fmt, ## __VA_ARGS__);\
- return _out_impl; \
- }())
+ /*return*/ _out_impl; \
+ }/*()*/)
#define STRNPRINTF(n, fmt, ...) \
- ([&]() -> VString<n - 1> \
+ (/*[&]() -> VString<n - 1>*/ \
{ \
VString<n - 1> _out_impl; \
SNPRINTF(_out_impl, n, fmt, ## __VA_ARGS__);\
- return _out_impl; \
- }())
+ /*return*/ _out_impl; \
+ }/*()*/)
} // namespace cxxstdio
diff --git a/src/common/md5calc.cpp b/src/common/md5calc.cpp
index 1961fa6..70b0e1f 100644
--- a/src/common/md5calc.cpp
+++ b/src/common/md5calc.cpp
@@ -5,6 +5,8 @@
#include "../strings/xstring.hpp"
#include "../strings/vstring.hpp"
+#include "../io/read.hpp"
+
#include "cxxstdio.hpp"
#include "random.hpp"
#include "utils.hpp"
@@ -234,7 +236,7 @@ MD5_state MD5_from_string(XString msg)
// TODO - refactor MD5 into a stream, and merge the implementations
// I once implemented an ostream that does it ...
-MD5_state MD5_from_FILE(FILE* in)
+MD5_state MD5_from_FILE(io::ReadFile& in)
{
uint64_t total_len = 0;
@@ -248,7 +250,7 @@ MD5_state MD5_from_FILE(FILE* in)
while (true)
{
- size_t rv = fread(buf + block_len, 1, 0x40 - block_len, in);
+ size_t rv = in.get(sign_cast<char *>(buf + block_len), 0x40 - block_len);
if (!rv)
break;
total_len += 8 * rv; // in bits
diff --git a/src/common/md5calc.hpp b/src/common/md5calc.hpp
index e44a7a7..205c21a 100644
--- a/src/common/md5calc.hpp
+++ b/src/common/md5calc.hpp
@@ -14,6 +14,8 @@
#include "../strings/fwd.hpp"
#include "../strings/vstring.hpp"
+#include "../io/fwd.hpp"
+
#include "ip.hpp"
#include "mmo.hpp"
@@ -43,7 +45,7 @@ void MD5_to_str(MD5_state state, md5_string& out);
// Convenience
MD5_state MD5_from_string(XString msg);
-MD5_state MD5_from_FILE(FILE* in);
+MD5_state MD5_from_FILE(io::ReadFile& in);
// whoever wrote this fails basic understanding of
diff --git a/src/common/utils.cpp b/src/common/utils.cpp
index 383c711..0f8a0af 100644
--- a/src/common/utils.cpp
+++ b/src/common/utils.cpp
@@ -9,6 +9,8 @@
#include "../strings/zstring.hpp"
#include "../strings/xstring.hpp"
+#include "../io/write.hpp"
+
#include "cxxstdio.hpp"
#include "extract.hpp"
@@ -106,18 +108,16 @@ void stamp_time(timestamp_milliseconds_buffer& out)
out = stringish<timestamp_milliseconds_buffer>(const_(buf));
}
-void log_with_timestamp(FILE *out, XString line)
+void log_with_timestamp(io::WriteFile& out, XString line)
{
if (!line)
{
- fputc('\n', out);
+ out.put('\n');
return;
}
timestamp_milliseconds_buffer tmpstr;
stamp_time(tmpstr);
- fputs(tmpstr.c_str(), out);
- fputs(": ", out);
- fwrite(line.data(), 1, line.size(), out);
- if (line.back() != '\n')
- fputc('\n', out);
+ out.really_put(tmpstr.data(), tmpstr.size());
+ out.really_put(": ", 2);
+ out.put_line(line);
}
diff --git a/src/common/utils.hpp b/src/common/utils.hpp
index b0ee2bc..4171759 100644
--- a/src/common/utils.hpp
+++ b/src/common/utils.hpp
@@ -11,6 +11,8 @@
#include "../strings/fwd.hpp"
#include "../strings/vstring.hpp"
+#include "../io/fwd.hpp"
+
#include "const_array.hpp"
#include "operators.hpp"
#include "utils2.hpp"
@@ -117,7 +119,7 @@ struct timestamp_milliseconds_buffer : VString<23> {};
void stamp_time(timestamp_seconds_buffer&, const TimeT *t=nullptr);
void stamp_time(timestamp_milliseconds_buffer&);
-void log_with_timestamp(FILE *out, XString line);
+void log_with_timestamp(io::WriteFile& out, XString line);
// TODO VString?
#define TIMESTAMP_DUMMY "YYYY-MM-DD HH:MM:SS"
diff --git a/src/common/utils2.hpp b/src/common/utils2.hpp
index 978ae54..adcb465 100644
--- a/src/common/utils2.hpp
+++ b/src/common/utils2.hpp
@@ -284,4 +284,12 @@ T maybe_cast(U u)
return u;
}
+template<class T, class U>
+typename std::remove_pointer<T>::type *sign_cast(U *u)
+{
+ typedef typename std::remove_pointer<T>::type T_;
+ static_assert(sizeof(T_) == sizeof(U), "sign cast must be same size");
+ return reinterpret_cast<T_ *>(u);
+}
+
#endif // UTILS2_HPP