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