forked from pig4210/xlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxmsg.cpp
More file actions
144 lines (122 loc) · 2.59 KB
/
xmsg.cpp
File metadata and controls
144 lines (122 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "xmsg.h"
#include <stdio.h>
#include <stdarg.h>
#include "ws_s.h"
using namespace std;
#pragma warning(push)
#pragma warning(disable:4127) //warning C4127: 条件表达式是常量
xmsg& xmsg::prt(const char* const fmt,...)
{
va_list ap;
va_start(ap,fmt);
while(true)
{
if(capacity() - size() <= 1)
{
reserve(capacity() + 0x10);
}
const size_t rst = capacity() - size();
#ifdef FOR_RING0
char* lpend = end();
#else
char* lpend = const_cast<char*>(end()._Ptr);
#endif
#if defined(FOR_RING0) && !defined(_WIN64)
//目前WIN7以下Ring0只能使用不安全的函数,否则编译出现链接错误
const size_t rt = _vsnprintf(lpend,rst,fmt,ap); //数据不足,返回-1
#else
const size_t rt = _vsnprintf_s(lpend, rst, rst - 1, fmt, ap);
#endif
if(rt < rst)
{
append(lpend, rt);
break;
}
reserve(capacity() + 0x10);
}
va_end(ap);
return *this;
}
#pragma warning(pop)
xmsg& xmsg::operator<<(const char& v)
{
return prt("%c",v);
}
xmsg& xmsg::operator<<(const unsigned char& v)
{
return prt("%02X",v);
}
xmsg& xmsg::operator<<(const short& v)
{
return prt("%hd",v);
}
xmsg& xmsg::operator<<(const unsigned short& v)
{
return prt("%04X",v);
}
xmsg& xmsg::operator<<(const int& v)
{
return prt("%d",v);
}
xmsg& xmsg::operator<<(const unsigned int& v)
{
return prt("%08X",v);
}
xmsg& xmsg::operator<<(const __int64& v)
{
return prt("%lld", v);
}
xmsg& xmsg::operator<<(const unsigned __int64& v)
{
return prt("%08X%08X", (int)(v >> 32), (int)v);
}
xmsg& xmsg::operator<<(const char* v)
{
return prt("%s", v);
}
xmsg& xmsg::operator<<(const unsigned char* v)
{
return prt("%s", v);
}
xmsg& xmsg::operator<<(const bool& v)
{
const char* const tmp = v ? ":true." : ":false.";
return this->operator <<(tmp);
}
xmsg& xmsg::operator<<(const wchar_t& v)
{
wchar_t tmpws[2] = { v, L'\0' };
return prt("%s", ws2s(tmpws).c_str());
}
xmsg& xmsg::operator<<(const wchar_t* v)
{
return prt("%s", ws2s(v).c_str());
}
xmsg& xmsg::operator<<(const float& v)
{
return prt("%f", v);
}
xmsg& xmsg::operator<<(const double& v)
{
return prt("%f", v);
}
xmsg& xmsg::operator<<(const void* v)
{
return prt("%p",v);
}
xmsg& xmsg::operator<<(const string& v)
{
return prt("%s", v.c_str());
}
xmsg& xmsg::operator<<(const wstring& v)
{
return this->operator<<(v.c_str());
}
xmsg& xmsg::operator<<(const xutf8& v)
{
return this->operator<<(utf82ws(v));
}
xmsg& xmsg::operator<<(xmsg& (*pfn)(xmsg&))
{
return pfn(*this);
}