diff options
Diffstat (limited to 'src/mmo/utils.hpp')
-rw-r--r-- | src/mmo/utils.hpp | 68 |
1 files changed, 50 insertions, 18 deletions
diff --git a/src/mmo/utils.hpp b/src/mmo/utils.hpp index d59f7ac..fc3ea74 100644 --- a/src/mmo/utils.hpp +++ b/src/mmo/utils.hpp @@ -1,5 +1,4 @@ -#ifndef TMWA_MMO_UTILS_HPP -#define TMWA_MMO_UTILS_HPP +#pragma once // utils.hpp - Useful stuff that hasn't been categorized. // // Copyright © ????-2004 Athena Dev Teams @@ -21,19 +20,25 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/>. -# include "../sanity.hpp" +#include "fwd.hpp" -# include <cstdio> +#include <cstring> +#include <ctime> -# include <type_traits> +#include <type_traits> -# include "../strings/fwd.hpp" -# include "../strings/vstring.hpp" +#include "../ints/little.hpp" -# include "../generic/operators.hpp" +#include "../strings/fwd.hpp" +#include "../strings/vstring.hpp" -# include "../io/fwd.hpp" +#include "../generic/operators.hpp" +#include "../io/fwd.hpp" + + +namespace tmwa +{ template<class T> struct is_trivially_copyable : std::integral_constant<bool, @@ -82,7 +87,7 @@ struct TimeT : Comparable TimeT now() { // poisoned, but this is still in header-land - return time(NULL); + return time(nullptr); } bool error() const @@ -101,12 +106,40 @@ long long convert_for_printf(TimeT t) return t.value; } -inline -long long& convert_for_scanf(TimeT& t) +// 2038 problem +inline __attribute__((warn_unused_result)) +bool native_to_network(Little32 *net, TimeT nat) { - return t.value; + time_t tmp = nat; + return native_to_network(net, static_cast<uint32_t>(tmp)); +} + +inline __attribute__((warn_unused_result)) +bool network_to_native(TimeT *nat, Little32 net) +{ + uint32_t tmp; + bool rv = network_to_native(&tmp, net); + *nat = static_cast<time_t>(tmp); + return rv; } +inline __attribute__((warn_unused_result)) +bool native_to_network(Little64 *net, TimeT nat) +{ + time_t tmp = nat; + return native_to_network(net, static_cast<uint64_t>(tmp)); +} + +inline __attribute__((warn_unused_result)) +bool network_to_native(TimeT *nat, Little64 net) +{ + uint64_t tmp; + bool rv = network_to_native(&tmp, net); + *nat = static_cast<time_t>(tmp); + return rv; +} + + struct timestamp_seconds_buffer : VString<19> {}; struct timestamp_milliseconds_buffer : VString<23> {}; void stamp_time(timestamp_seconds_buffer&, const TimeT *t=nullptr); @@ -115,21 +148,20 @@ void stamp_time(timestamp_milliseconds_buffer&); void log_with_timestamp(io::WriteFile& out, XString line); // TODO VString? -# define TIMESTAMP_DUMMY "YYYY-MM-DD HH:MM:SS" +#define TIMESTAMP_DUMMY "YYYY-MM-DD HH:MM:SS" static_assert(sizeof(TIMESTAMP_DUMMY) == sizeof(timestamp_seconds_buffer), "timestamp size"); -# define WITH_TIMESTAMP(str) str TIMESTAMP_DUMMY +#define WITH_TIMESTAMP(str) str TIMESTAMP_DUMMY // str: prefix: YYYY-MM-DD HH:MM:SS // sizeof: 01234567890123456789012345678 // str + sizeof: ^ // -1: ^ // there's probably a better way to do this now -# define REPLACE_TIMESTAMP(str, t) \ +#define REPLACE_TIMESTAMP(str, t) \ stamp_time( \ reinterpret_cast<timestamp_seconds_buffer *>( \ str + sizeof(str) \ )[-1], \ &t \ ) - -#endif // TMWA_MMO_UTILS_HPP +} // namespace tmwa |