forked from abcdls0905/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathandroid_wstring.cpp
183 lines (168 loc) · 4.82 KB
/
android_wstring.cpp
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdarg.h>
#include <string>
#include "android_wstring.h"
enum fmt_arg_type
{
fat_none,
fat_float,
fat_interger,
fat_pointer,
fat_char,
};
int xfind_fmt_tag(std::string& _fmt , const wchar_t *_buf , fmt_arg_type& _type)
{
const wchar_t* _base = _buf;
_type = fat_none;
//prefix
if(*_buf == '-' || *_buf == '+' || *_buf == ' ' || *_buf == '0' || *_buf == '#' )
{
_fmt += (char)(*_buf);
_buf ++;
}
//width
if(*_buf <='9' && *_buf >= '0' )
{
_fmt += (char)(*_buf);
_buf ++;
}
//.
if(*_buf =='.')
{
_fmt += (char)(*_buf);
_buf ++;
}
//precision
if(*_buf <='9' && *_buf >= '0' )
{
_fmt += (char)(*_buf);
_buf ++;
}
//[{h | l | I64 | L}]
if(*_buf == 'I' || *_buf == 'l' || *_buf == 'h' || *_buf == 'L' )
{
_fmt += (char)(*_buf);
_buf ++;
}
else if(*_buf == 'c' || *_buf == 'C')
{
_type = fat_char;
}
else if(*_buf == 'd' || *_buf == 'i' || *_buf == 'o' || *_buf == 'u' || *_buf == 'x' || *_buf == 'X' || *_buf == 'e' || *_buf == 'E')
{
_type = fat_interger;
}
else if( *_buf == 'f' || *_buf == 'g' || *_buf == 'G' || *_buf == 'n' || *_buf == 'p' )
{
_type = fat_float;
}
else if( *_buf == 'n' || *_buf == 'p' )
{
_type = fat_pointer;
}
if(_type != fat_none)
{
_fmt += (char)(*_buf);
_buf ++;
return _buf - _base;
}
return 0;
}
#define GUARD_COUNT {idx ++ ; if(idx >= ( (int)count - 1) ) { _out[idx] = 0 ; return idx; } }
int vsnwprintf_android(wchar_t* _out , size_t count , const wchar_t * wcsfmtStr , va_list ap)
{
int idx = 0;
wchar_t* _ret = _out;
const wchar_t *_fmtStr = wcsfmtStr;
while(*_fmtStr!=0)
{
if(*_fmtStr!='%')
{
*_ret = *_fmtStr;
_ret ++;
GUARD_COUNT(idx);
_fmtStr++;
}
else
{
switch(*(++_fmtStr))
{
case 's':
case 'S':
{
const wchar_t* wcsValue = va_arg(ap , const wchar_t*);
while( *wcsValue)
{
*_ret = *wcsValue;
wcsValue ++;
_ret ++;
GUARD_COUNT(idx);
}
_fmtStr++;
}
break;
default:
{
std::string fmtStr = "%";
fmt_arg_type _type;
int nSkip = xfind_fmt_tag(fmtStr , _fmtStr , _type);
if(nSkip > 0)
{
char ansiBuf[128] = {0};
switch(_type)
{
case fat_char:
{
char n = (char)va_arg(ap , int);
sprintf(ansiBuf , fmtStr.c_str() , n );
}
break;
case fat_interger:
{
int n = va_arg(ap , int);
sprintf(ansiBuf , fmtStr.c_str() , n );
}
break;
case fat_float:
{
float n = va_arg(ap , double);
sprintf(ansiBuf , fmtStr.c_str() , n );
}
break;
case fat_pointer:
{
void* n = va_arg(ap , void*);
sprintf(ansiBuf , fmtStr.c_str() , n );
}
break;
}
int i = 0;
while(ansiBuf[i])
{
*_ret = ansiBuf[i];
_ret ++; i ++;
GUARD_COUNT(idx);
}
_fmtStr += nSkip;
}
else
{
*_ret = *_fmtStr;
_ret ++;
_fmtStr++;
GUARD_COUNT(idx);
}
}
}
}
}
_out[idx] = 0;
return idx;
}
int swprintf_x(wchar_t* _out , size_t count , const wchar_t *buf , ...)
{
va_list ap;
va_start(ap,buf);
int _ret = vsnwprintf_android(_out , count , buf , ap);
va_end(ap);
return _ret;
}