From f906959a09d58c85d87b445fd1791d91bf278bfa Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Fri, 15 Nov 2013 17:51:29 -0800 Subject: Use new IO classes --- src/common/cxxstdio.hpp | 28 +++++++++++++++------------- src/common/md5calc.cpp | 6 ++++-- src/common/md5calc.hpp | 4 +++- src/common/utils.cpp | 14 +++++++------- src/common/utils.hpp | 4 +++- src/common/utils2.hpp | 8 ++++++++ 6 files changed, 40 insertions(+), 24 deletions(-) (limited to 'src/common') 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 #include +#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::print(out, ## __VA_ARGS__); \ - }()) + /*return*/ cxxstdio::PrintFormatter::print(out, ## __VA_ARGS__); \ + }/*()*/) #define XSCANF(out, fmt, ...) \ - ([&]() -> int \ + (/*[&]() -> int*/ \ { \ struct format_impl \ { \ constexpr static \ const char *scan_format() { return fmt; } \ }; \ - return cxxstdio::ScanFormatter::scan(out, ## __VA_ARGS__); \ - }()) + /*return*/ cxxstdio::ScanFormatter::scan(out, ## __VA_ARGS__); \ + }/*()*/) -#define FPRINTF(file, fmt, ...) XPRINTF(no_cast(file), fmt, ## __VA_ARGS__) +#define FPRINTF(file, fmt, ...) XPRINTF(/*no_cast*/(file), fmt, ## __VA_ARGS__) #define FSCANF(file, fmt, ...) XSCANF(no_cast(file), fmt, ## __VA_ARGS__) #define PRINTF(fmt, ...) FPRINTF(stdout, fmt, ## __VA_ARGS__) #define SPRINTF(str, fmt, ...) XPRINTF(base_cast(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 \ + (/*[&]() -> VString*/ \ { \ VString _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(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(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 +typename std::remove_pointer::type *sign_cast(U *u) +{ + typedef typename std::remove_pointer::type T_; + static_assert(sizeof(T_) == sizeof(U), "sign cast must be same size"); + return reinterpret_cast(u); +} + #endif // UTILS2_HPP -- cgit v1.2.3-70-g09d2