Upstream tarball 20080509
[amule.git] / src / utils / fileview / Print.h
blobea156412c8407475ce6427aa9369d89601393137
1 // -*- C++ -*-
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2008 Dévai Tamás ( gonosztopi@amule.org )
5 // Copyright (c) 2004-2008 aMule Team ( admin@amule.org / http://www.amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #ifndef FILEVIEW_PRINT_H
27 #define FILEVIEW_PRINT_H
29 #include <common/StringFunctions.h>
30 #include "../../kademlia/utils/UInt128.h"
31 #include "../../MD4Hash.h"
33 #include <cstdio>
34 #include <time.h>
36 class CTag;
39 wxString MakePrintableString(const wxString& str);
42 template<typename _Tp> void Print(const _Tp& value);
43 template<typename _Tp> void PrintHex(_Tp value);
45 void inline DoPrint(const wxString& str) { std::printf("%s", (const char *)unicode2char(str)); }
47 template<> inline void PrintHex<uint8_t>(uint8_t value) { DoPrint(wxString::Format(wxT("0x%02x"), value)); }
48 template<> inline void PrintHex<uint16_t>(uint16_t value) { DoPrint(wxString::Format(wxT("0x%04x"), value)); }
49 template<> inline void PrintHex<uint32_t>(uint32_t value) { DoPrint(wxString::Format(wxT("0x%08x"), value)); }
51 template<> inline void Print<bool>(const bool& value) { DoPrint(value ? wxT("true") : wxT("false")); }
52 template<> inline void Print<uint8_t>(const uint8_t& value) { DoPrint(wxString::Format(wxT("%u"), value)); }
53 template<> inline void Print<uint16_t>(const uint16_t& value) { DoPrint(wxString::Format(wxT("%u"), value)); }
54 template<> inline void Print<uint32_t>(const uint32_t& value) { DoPrint(wxString::Format(wxT("%u"), value)); }
55 template<> inline void Print<uint64_t>(const uint64_t& value) { DoPrint(wxString::Format(wxT("%") wxLongLongFmtSpec wxT("u"), value)); }
56 template<> inline void Print<float>(const float& value) { DoPrint(wxString::Format(wxT("%g"), value)); }
57 template<> inline void Print<double>(const double& value) { DoPrint(wxString::Format(wxT("%g"), value)); }
58 template<> inline void Print<wxString>(const wxString& value) { DoPrint(MakePrintableString(value)); }
59 template<> inline void Print<Kademlia::CUInt128>(const Kademlia::CUInt128& value) { DoPrint(value.ToHexString()); }
60 template<> inline void Print<CMD4Hash>(const CMD4Hash& value) { DoPrint(value.Encode()); }
62 // Transparent class for Kad IPs
63 class CKadIP
65 public:
66 CKadIP(uint32_t ip) { m_ip = ip; }
67 operator uint32_t() const { return m_ip; }
68 private:
69 uint32_t m_ip;
72 template<> void Print<time_t>(const time_t& time);
73 template<> void Print<CKadIP>(const CKadIP& ip);
74 template<> void Print<CTag>(const CTag& tag);
77 // Some formatting functions
79 inline wxString Uint32toStringIP(uint32_t ip)
81 return wxString::Format(wxT("%u.%u.%u.%u"), (uint8_t)ip, (uint8_t)(ip>>8), (uint8_t)(ip>>16), (uint8_t)(ip>>24));
84 inline wxString Uint32_16toStringIP_Port(uint32_t ip, uint16_t port)
86 return wxString::Format(wxT("%u.%u.%u.%u:%u"), (uint8_t)ip, (uint8_t)(ip>>8), (uint8_t)(ip>>16), (uint8_t)(ip>>24), port);
89 #endif /* FILEVIEW_PRINT_H */