|
1
|
1 #include "pfc-lite.h"
|
|
|
2 #include "string_base.h"
|
|
|
3
|
|
|
4 //implementations of deprecated string_printf methods, with a pragma to disable warnings when they reference other deprecated methods.
|
|
|
5
|
|
|
6 #ifndef _MSC_VER
|
|
|
7 #define _itoa_s itoa
|
|
|
8 #define _ultoa_s ultoa
|
|
|
9 #endif
|
|
|
10
|
|
|
11 #ifdef _MSC_VER
|
|
|
12 #pragma warning(disable:4996)
|
|
|
13 #endif
|
|
|
14
|
|
|
15 namespace pfc {
|
|
|
16 string8 string_printf(const char * fmt, ...) {
|
|
|
17 string8 ret;
|
|
|
18 va_list list;
|
|
|
19 va_start(list, fmt);
|
|
|
20 string_printf_here_va(ret, fmt, list);
|
|
|
21 va_end(list);
|
|
|
22 return ret;
|
|
|
23 }
|
|
|
24 string8 string_printf_va(const char * fmt, va_list list) {
|
|
|
25 string8 ret;
|
|
|
26 string_printf_here_va(ret, fmt, list);
|
|
|
27 return ret;
|
|
|
28 }
|
|
|
29 void string_printf_here(string_base & out, const char * fmt, ...) {
|
|
|
30 va_list list;
|
|
|
31 va_start(list, fmt);
|
|
|
32 string_printf_here_va(out, fmt, list);
|
|
|
33 va_end(list);
|
|
|
34 }
|
|
|
35 void string_printf_here_va(string_base & out, const char * fmt, va_list list) {
|
|
|
36 out.reset();
|
|
|
37 while(*fmt)
|
|
|
38 {
|
|
|
39 if (*fmt=='%')
|
|
|
40 {
|
|
|
41 fmt++;
|
|
|
42 if (*fmt=='%')
|
|
|
43 {
|
|
|
44 out.add_char('%');
|
|
|
45 fmt++;
|
|
|
46 }
|
|
|
47 else
|
|
|
48 {
|
|
|
49 bool force_sign = false;
|
|
|
50 if (*fmt=='+')
|
|
|
51 {
|
|
|
52 force_sign = true;
|
|
|
53 fmt++;
|
|
|
54 }
|
|
|
55 char padchar = (*fmt == '0') ? '0' : ' ';
|
|
|
56 t_size pad = 0;
|
|
|
57 while(*fmt>='0' && *fmt<='9')
|
|
|
58 {
|
|
|
59 pad = pad * 10 + (*fmt - '0');
|
|
|
60 fmt++;
|
|
|
61 }
|
|
|
62
|
|
|
63 if (*fmt=='s' || *fmt=='S')
|
|
|
64 {
|
|
|
65 const char * ptr = va_arg(list,const char*);
|
|
|
66 t_size len = strlen(ptr);
|
|
|
67 if (pad>len) out.add_chars(padchar,pad-len);
|
|
|
68 out.add_string(ptr);
|
|
|
69 fmt++;
|
|
|
70
|
|
|
71 }
|
|
|
72 else if (*fmt=='i' || *fmt=='I' || *fmt=='d' || *fmt=='D')
|
|
|
73 {
|
|
|
74 int val = va_arg(list,int);
|
|
|
75 if (force_sign && val>0) out.add_char('+');
|
|
|
76 auto temp = pfc::format_int( val );
|
|
|
77 t_size len = strlen(temp);
|
|
|
78 if (pad>len) out.add_chars(padchar,pad-len);
|
|
|
79 out.add_string(temp);
|
|
|
80 fmt++;
|
|
|
81 }
|
|
|
82 else if (*fmt=='u' || *fmt=='U')
|
|
|
83 {
|
|
|
84 int val = va_arg(list,int);
|
|
|
85 if (force_sign && val>0) out.add_char('+');
|
|
|
86 auto temp = pfc::format_uint(val);
|
|
|
87 t_size len = strlen(temp);
|
|
|
88 if (pad>len) out.add_chars(padchar,pad-len);
|
|
|
89 out.add_string(temp);
|
|
|
90 fmt++;
|
|
|
91 }
|
|
|
92 else if (*fmt=='x' || *fmt=='X')
|
|
|
93 {
|
|
|
94 auto val = va_arg(list,unsigned);
|
|
|
95 if (force_sign && val>0) out.add_char('+');
|
|
|
96 auto temp = pfc::format_uint(val, 0, 16);
|
|
|
97 if (*fmt=='X')
|
|
|
98 {
|
|
|
99 char * t = const_cast< char* > ( temp.c_str() );
|
|
|
100 while(*t)
|
|
|
101 {
|
|
|
102 *t = pfc::ascii_toupper(*t);
|
|
|
103 t++;
|
|
|
104 }
|
|
|
105 } else {
|
|
|
106 char* t = const_cast<char*> (temp.c_str());
|
|
|
107 while (*t)
|
|
|
108 {
|
|
|
109 *t = pfc::ascii_tolower(*t);
|
|
|
110 t++;
|
|
|
111 }
|
|
|
112 }
|
|
|
113 t_size len = strlen(temp);
|
|
|
114 if (pad>len) out.add_chars(padchar,pad-len);
|
|
|
115 out.add_string(temp);
|
|
|
116 fmt++;
|
|
|
117 }
|
|
|
118 else if (*fmt=='c' || *fmt=='C')
|
|
|
119 {
|
|
|
120 out.add_char(va_arg(list,int));
|
|
|
121 fmt++;
|
|
|
122 }
|
|
|
123 }
|
|
|
124 }
|
|
|
125 else
|
|
|
126 {
|
|
|
127 out.add_char(*(fmt++));
|
|
|
128 }
|
|
|
129 }
|
|
|
130 }
|
|
|
131
|
|
|
132 }
|