binPrintf.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2001 Georges Menie
3  https://www.menie.org/georges/embedded/small_printf_source_code.html
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19 
20 #include "sick_scan/binPrintf.hpp"
21 #include <stdio.h>
22 
23 #define BIN_SPRINTF_MAX_LEN (10240)
24 
25 static void binPrintchar(char **str, int c)
26 {
27  extern int putchar(int c);
28  if (str)
29  {
30  **str = c;
31  ++(*str);
32  }
33  else
34  { (void) putchar(c); }
35 }
36 
37 #define PAD_RIGHT 1
38 #define PAD_ZERO 2
39 
40 static int binPrints(char **out, const char *string, int width, int pad)
41 {
42  register int pc = 0, padchar = ' ';
43 
44  if (width > 0)
45  {
46  register int len = 0;
47  register const char *ptr;
48  for (ptr = string; *ptr; ++ptr) ++len;
49  if (len >= width)
50  { width = 0; }
51  else
52  { width -= len; }
53  if (pad & PAD_ZERO)
54  { padchar = '0'; }
55  }
56  if (!(pad & PAD_RIGHT))
57  {
58  for (; width > 0; --width)
59  {
60  binPrintchar(out, padchar);
61  ++pc;
62  }
63  }
64  for (; *string; ++string)
65  {
66  binPrintchar(out, *string);
67  ++pc;
68  }
69  for (; width > 0; --width)
70  {
71  binPrintchar(out, padchar);
72  ++pc;
73  }
74 
75  return pc;
76 }
77 
78 /* the following should be enough for 32 bit int */
79 #define PRINT_BUF_LEN 12
80 
81 static int binPrinti(char **out, int i, int b, int sg, int width, int pad, int letbase)
82 {
83  char print_buf[PRINT_BUF_LEN];
84  register char *s;
85  register int t, neg = 0, pc = 0;
86  register unsigned int u = i;
87 
88  if (b == 1)
89  {
90  int cnt = 0;
91  while (width > 0)
92  {
93  ++pc;
94  ++cnt;
95  --width;
96  char ch = (char) (0xFF & (i >> (width * 8)));
97  binPrintchar(out, ch);
98  }
99  return (cnt);
100  }
101 
102  if (i == 0)
103  {
104  print_buf[0] = '0';
105  print_buf[1] = '\0';
106  return binPrints(out, print_buf, width, pad);
107  }
108 
109  if (sg && b == 10 && i < 0)
110  {
111  neg = 1;
112  u = -i;
113  }
114 
115  s = print_buf + PRINT_BUF_LEN - 1;
116  *s = '\0';
117 
118  while (u)
119  {
120  t = u % b;
121  if (t >= 10)
122  {
123  t += letbase - '0' - 10;
124  }
125  *--s = t + '0';
126  u /= b;
127  }
128 
129  if (neg)
130  {
131  if (width && (pad & PAD_ZERO))
132  {
133  binPrintchar(out, '-');
134  ++pc;
135  --width;
136  }
137  else
138  {
139  *--s = '-';
140  }
141  }
142 
143  return pc + binPrints(out, s, width, pad);
144 }
145 
146 static int binPrint(char **out, long long *varg)
147 {
148  int width, pad;
149  int pc = 0;
150  char *format = (char *) (*varg++);
151  char scr[2];
152 
153  for (; *format != 0; ++format)
154  {
155  if (*format == '%')
156  {
157  ++format;
158  width = pad = 0;
159  if (*format == '\0')
160  { break; }
161  if (*format == '%')
162  { goto out; }
163  if (*format == '-')
164  {
165  ++format;
166  pad = PAD_RIGHT;
167  }
168  while (*format == '0')
169  {
170  ++format;
171  pad |= PAD_ZERO;
172  }
173  for (; *format >= '0' && *format <= '9'; ++format)
174  {
175  width *= 10;
176  width += *format - '0';
177  }
178  if (*format == 's')
179  {
180  register char *s = *((char **) varg++);
181  pc += binPrints(out, s ? s : "(null)", width, pad);
182  continue;
183  }
184  if (*format == 'd')
185  {
186  pc += binPrinti(out, *varg++, 10, 1, width, pad, 'a');
187  continue;
188  }
189  if (*format == 'x')
190  {
191  pc += binPrinti(out, *varg++, 16, 0, width, pad, 'a');
192  continue;
193  }
194  if (*format == 'X')
195  {
196  pc += binPrinti(out, *varg++, 16, 0, width, pad, 'A');
197  continue;
198  }
199  if (*format == 'y')
200  {
201  pc += binPrinti(out, *varg++, 1, 0, width, pad, 'A');
202  continue;
203  }
204  if (*format == 'u')
205  {
206  pc += binPrinti(out, *varg++, 10, 0, width, pad, 'a');
207  continue;
208  }
209  if (*format == 'c')
210  {
211  /* char are converted to int then pushed on the stack */
212  scr[0] = *varg++;
213  scr[1] = '\0';
214  pc += binPrints(out, scr, width, pad);
215  continue;
216  }
217  }
218  else
219  {
220  out:
221  binPrintchar(out, *format);
222  ++pc;
223  }
224  }
225  if (out)
226  { **out = '\0'; }
227  return pc;
228 }
229 
230 /* assuming sizeof(void *) == sizeof(int) */
231 
232 int binPrintf(const char *format, ...)
233 {
234  long long *varg = (long long *) (&format);
235  return binPrint(0, varg);
236 }
237 
238 int binSprintf(char *out, const char *format, ...)
239 {
240  long long *varg = (long long *) (&format);
241  return binPrint(&out, varg);
242 }
243 
244 int binSprintfVec(std::vector<unsigned char> *outvec, const char *fmt, ...)
245 {
246  outvec->clear();
247  char buffer[BIN_SPRINTF_MAX_LEN];
248  char *bufferPtr = &(buffer[0]);
249  void *tttt = &fmt;
250  long long *varg = (long long *) (&fmt);
251  int retCode = binPrint(&bufferPtr, varg);
252  if (retCode > 0)
253  {
254  for (int i = 0; i < retCode; i++)
255  {
256  outvec->push_back(buffer[i]);
257  }
258  }
259 
260  return retCode;
261 }
262 
263 
264 std::string binDumpVecToString(std::vector<unsigned char> *outvec, bool appendReadableText /*= false*/)
265 {
266  std::string s;
267  for (int i = 0; i < outvec->size(); i++)
268  {
269  char szDummy[255] = {0};
270  sprintf(szDummy, "%02x ", (int) (0xFF & (*outvec)[i]));
271  s += szDummy;
272  }
273  if (appendReadableText)
274  {
275  for (int i = 0; i < outvec->size(); i++)
276  {
277  char szDummy[255] = {0};
278  sprintf(szDummy, "%c", (*outvec)[i] < 0x20 ? '.' : (*outvec)[i]);
279  s += szDummy;
280  }
281  }
282  return (s);
283 }
284 
285 
286 
#define PAD_RIGHT
Definition: binPrintf.cpp:37
#define PAD_ZERO
Definition: binPrintf.cpp:38
int binSprintf(char *out, const char *format,...)
Definition: binPrintf.cpp:238
static void binPrintchar(char **str, int c)
Definition: binPrintf.cpp:25
static int binPrints(char **out, const char *string, int width, int pad)
Definition: binPrintf.cpp:40
int binPrintf(const char *format,...)
Definition: binPrintf.cpp:232
int binSprintfVec(std::vector< unsigned char > *outvec, const char *fmt,...)
Definition: binPrintf.cpp:244
#define BIN_SPRINTF_MAX_LEN
Definition: binPrintf.cpp:23
std::string binDumpVecToString(std::vector< unsigned char > *outvec, bool appendReadableText)
Definition: binPrintf.cpp:264
#define PRINT_BUF_LEN
Definition: binPrintf.cpp:79
static int binPrinti(char **out, int i, int b, int sg, int width, int pad, int letbase)
Definition: binPrintf.cpp:81
static int binPrint(char **out, long long *varg)
Definition: binPrintf.cpp:146
static sick_scan::SickScanCommonTcp * s


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Wed May 5 2021 03:05:47