breezyprintf.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004,2012 Kustaa Nyholm / SpareTimeLabs
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted provided that the following conditions are met:
8  *
9  * Redistributions of source code must retain the above copyright notice, this list
10  * of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright notice, this
13  * list of conditions and the following disclaimer in the documentation and/or other
14  * materials provided with the distribution.
15  *
16  * Neither the name of the Kustaa Nyholm or SpareTimeLabs nor the names of its
17  * contributors may be used to endorse or promote products derived from this software
18  * without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
26  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
29  * OF SUCH DAMAGE.
30  */
31 
32 #include "breezyprintf.h"
33 
34 typedef void (*putcf) (void*,char);
36 static void* stdout_putp;
37 
38 
39 #ifdef PRINTF_LONG_SUPPORT
40 
41 static void uli2a(unsigned long int num, unsigned int base, int uc,char * bf)
42  {
43  int n=0;
44  unsigned int d=1;
45  while (num/d >= base)
46  d*=base;
47  while (d!=0) {
48  int dgt = num / d;
49  num%=d;
50  d/=base;
51  if (n || dgt>0|| d==0) {
52  *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
53  ++n;
54  }
55  }
56  *bf=0;
57  }
58 
59 static void li2a (long num, char * bf)
60  {
61  if (num<0) {
62  num=-num;
63  *bf++ = '-';
64  }
65  uli2a(num,10,0,bf);
66  }
67 
68 #endif
69 
70 static void ui2a(unsigned int num, unsigned int base, int uc,char * bf)
71  {
72  int n=0;
73  unsigned int d=1;
74  while (num/d >= base)
75  d*=base;
76  while (d!=0) {
77  int dgt = num / d;
78  num%= d;
79  d/=base;
80  if (n || dgt>0 || d==0) {
81  *bf++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
82  ++n;
83  }
84  }
85  *bf=0;
86  }
87 
88 static void i2a (int num, char * bf)
89  {
90  if (num<0) {
91  num=-num;
92  *bf++ = '-';
93  }
94  ui2a(num,10,0,bf);
95  }
96 
97 static int a2d(char ch)
98  {
99  if (ch>='0' && ch<='9')
100  return ch-'0';
101  else if (ch>='a' && ch<='f')
102  return ch-'a'+10;
103  else if (ch>='A' && ch<='F')
104  return ch-'A'+10;
105  else return -1;
106  }
107 
108 static char a2i(char ch, char** src,int base,int* nump)
109  {
110  char* p= *src;
111  int num=0;
112  int digit;
113  while ((digit=a2d(ch))>=0) {
114  if (digit>base) break;
115  num=num*base+digit;
116  ch=*p++;
117  }
118  *src=p;
119  *nump=num;
120  return ch;
121  }
122 
123 static void putchw(void* putp,putcf putf,int n, char z, char* bf)
124  {
125  char fc=z? '0' : ' ';
126  char ch;
127  char* p=bf;
128  while (*p++ && n > 0)
129  n--;
130  while (n-- > 0)
131  putf(putp,fc);
132  while ((ch= *bf++))
133  putf(putp,ch);
134  }
135 
136 void tfp_format(void* putp, putcf putf, const char *fmt, va_list va)
137  {
138  char bf[12];
139 
140  char ch;
141 
142 
143  while ((ch=*(fmt++))) {
144  if (ch!='%')
145  putf(putp,ch);
146  else {
147  char lz=0;
148 #ifdef PRINTF_LONG_SUPPORT
149  char lng=0;
150 #endif
151  int w=0;
152  ch=*(fmt++);
153  if (ch=='0') {
154  ch=*(fmt++);
155  lz=1;
156  }
157  if (ch>='0' && ch<='9') {
158  ch=a2i(ch, (char **)&fmt, 10, &w);
159  }
160 #ifdef PRINTF_LONG_SUPPORT
161  if (ch=='l') {
162  ch=*(fmt++);
163  lng=1;
164  }
165 #endif
166  switch (ch) {
167  case 0:
168  goto abort;
169  case 'u' : {
170 #ifdef PRINTF_LONG_SUPPORT
171  if (lng)
172  uli2a(va_arg(va, unsigned long int),10,0,bf);
173  else
174 #endif
175  ui2a(va_arg(va, unsigned int),10,0,bf);
176  putchw(putp,putf,w,lz,bf);
177  break;
178  }
179  case 'd' : {
180 #ifdef PRINTF_LONG_SUPPORT
181  if (lng)
182  li2a(va_arg(va, unsigned long int),bf);
183  else
184 #endif
185  i2a(va_arg(va, int),bf);
186  putchw(putp,putf,w,lz,bf);
187  break;
188  }
189  case 'x': case 'X' :
190 #ifdef PRINTF_LONG_SUPPORT
191  if (lng)
192  uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bf);
193  else
194 #endif
195  ui2a(va_arg(va, unsigned int),16,(ch=='X'),bf);
196  putchw(putp,putf,w,lz,bf);
197  break;
198  case 'c' :
199  putf(putp,(char)(va_arg(va, int)));
200  break;
201  case 's' :
202  putchw(putp,putf,w,0,va_arg(va, char*));
203  break;
204  case '%' :
205  putf(putp,ch);
206  default:
207  break;
208  }
209  }
210  }
211  abort:;
212  }
213 
214 
215 void init_printf(void* putp, void (*putf) (void*, char))
216  {
217  stdout_putf=putf;
218  stdout_putp=putp;
219  }
220 
221 void tfp_printf(const char *fmt, ...)
222  {
223  va_list va;
224  va_start(va,fmt);
226  va_end(va);
227  }
228 
229 static void putcp(void* p,char c)
230  {
231  *(*((char**)p))++ = c;
232  }
233 
234 
235 
236 void tfp_sprintf(char* s, const char *fmt, ...)
237  {
238  va_list va;
239  va_start(va,fmt);
240  tfp_format(&s,putcp,fmt,va);
241  putcp(&s,0);
242  va_end(va);
243  }
244 
245 
d
static void putchw(void *putp, putcf putf, int n, char z, char *bf)
Definition: breezyprintf.c:123
void tfp_printf(const char *fmt,...)
Definition: breezyprintf.c:221
static putcf stdout_putf
Definition: breezyprintf.c:35
static char a2i(char ch, char **src, int base, int *nump)
Definition: breezyprintf.c:108
void tfp_format(void *putp, putcf putf, const char *fmt, va_list va)
Definition: breezyprintf.c:136
void init_printf(void *putp, void(*putf)(void *, char))
Definition: breezyprintf.c:215
static int a2d(char ch)
Definition: breezyprintf.c:97
void tfp_sprintf(char *s, const char *fmt,...)
Definition: breezyprintf.c:236
static void li2a(long num, char *bf)
Definition: printf.cpp:66
static void putcp(void *p, char c)
Definition: breezyprintf.c:229
static void ui2a(unsigned int num, unsigned int base, int uc, char *bf)
Definition: breezyprintf.c:70
static void i2a(int num, char *bf)
Definition: breezyprintf.c:88
static void uli2a(unsigned long int num, unsigned int base, int uc, char *bf)
Definition: printf.cpp:48
static void * stdout_putp
Definition: breezyprintf.c:36
void(* putcf)(void *, char)
Definition: breezyprintf.c:34


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:24