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 #define BIN_SPRINTF_MAX_LEN (10240)
23 static void binPrintchar(char **str, int c)
24 {
25  extern int putchar(int c);
26  if (str) {
27  **str = c;
28  ++(*str);
29  }
30  else (void)putchar(c);
31 }
32 
33 #define PAD_RIGHT 1
34 #define PAD_ZERO 2
35 
36 static int binPrints(char **out, const char *string, int width, int pad)
37 {
38  register int pc = 0, padchar = ' ';
39 
40  if (width > 0) {
41  register int len = 0;
42  register const char *ptr;
43  for (ptr = string; *ptr; ++ptr) ++len;
44  if (len >= width) width = 0;
45  else width -= len;
46  if (pad & PAD_ZERO) padchar = '0';
47  }
48  if (!(pad & PAD_RIGHT)) {
49  for ( ; width > 0; --width) {
50  binPrintchar (out, padchar);
51  ++pc;
52  }
53  }
54  for ( ; *string ; ++string) {
55  binPrintchar (out, *string);
56  ++pc;
57  }
58  for ( ; width > 0; --width) {
59  binPrintchar (out, padchar);
60  ++pc;
61  }
62 
63  return pc;
64 }
65 
66 /* the following should be enough for 32 bit int */
67 #define PRINT_BUF_LEN 12
68 
69 static int binPrinti(char **out, int i, int b, int sg, int width, int pad, int letbase)
70 {
71  char print_buf[PRINT_BUF_LEN];
72  register char *s;
73  register int t, neg = 0, pc = 0;
74  register unsigned int u = i;
75 
76  if (b == 1)
77  {
78  int cnt = 0;
79  while (width > 0)
80  {
81  ++pc;
82  ++cnt;
83  --width;
84  char ch = (char)(0xFF & (i >> (width * 8)));
85  binPrintchar(out, ch);
86  }
87  return(cnt);
88  }
89 
90  if (i == 0) {
91  print_buf[0] = '0';
92  print_buf[1] = '\0';
93  return binPrints (out, print_buf, width, pad);
94  }
95 
96  if (sg && b == 10 && i < 0) {
97  neg = 1;
98  u = -i;
99  }
100 
101  s = print_buf + PRINT_BUF_LEN-1;
102  *s = '\0';
103 
104  while (u) {
105  t = u % b;
106  if( t >= 10 )
107  t += letbase - '0' - 10;
108  *--s = t + '0';
109  u /= b;
110  }
111 
112  if (neg) {
113  if( width && (pad & PAD_ZERO) ) {
114  binPrintchar (out, '-');
115  ++pc;
116  --width;
117  }
118  else {
119  *--s = '-';
120  }
121  }
122 
123  return pc + binPrints (out, s, width, pad);
124 }
125 
126 static int binPrint(char **out, long long *varg)
127 {
128  int width, pad;
129  int pc = 0;
130  char *format = (char *)(*varg++);
131  char scr[2];
132 
133  for (; *format != 0; ++format) {
134  if (*format == '%') {
135  ++format;
136  width = pad = 0;
137  if (*format == '\0') break;
138  if (*format == '%') goto out;
139  if (*format == '-') {
140  ++format;
141  pad = PAD_RIGHT;
142  }
143  while (*format == '0') {
144  ++format;
145  pad |= PAD_ZERO;
146  }
147  for ( ; *format >= '0' && *format <= '9'; ++format) {
148  width *= 10;
149  width += *format - '0';
150  }
151  if( *format == 's' ) {
152  register char *s = *((char **)varg++);
153  pc += binPrints (out, s?s:"(null)", width, pad);
154  continue;
155  }
156  if( *format == 'd' ) {
157  pc += binPrinti (out, *varg++, 10, 1, width, pad, 'a');
158  continue;
159  }
160  if( *format == 'x' ) {
161  pc += binPrinti (out, *varg++, 16, 0, width, pad, 'a');
162  continue;
163  }
164  if( *format == 'X' ) {
165  pc += binPrinti (out, *varg++, 16, 0, width, pad, 'A');
166  continue;
167  }
168  if (*format == 'y') {
169  pc += binPrinti(out, *varg++, 1, 0, width, pad, 'A');
170  continue;
171  }
172  if( *format == 'u' ) {
173  pc += binPrinti (out, *varg++, 10, 0, width, pad, 'a');
174  continue;
175  }
176  if( *format == 'c' ) {
177  /* char are converted to int then pushed on the stack */
178  scr[0] = *varg++;
179  scr[1] = '\0';
180  pc += binPrints (out, scr, width, pad);
181  continue;
182  }
183  }
184  else {
185  out:
186  binPrintchar (out, *format);
187  ++pc;
188  }
189  }
190  if (out) **out = '\0';
191  return pc;
192 }
193 
194 /* assuming sizeof(void *) == sizeof(int) */
195 
196 int binPrintf(const char *format, ...)
197 {
198  long long *varg = (long long *)(&format);
199  return binPrint(0, varg);
200 }
201 
202 int binSprintf(char *out, const char *format, ...)
203 {
204  long long *varg = (long long *)(&format);
205  return binPrint(&out, varg);
206 }
207 
208 int binSprintfVec(std::vector<unsigned char>* outvec, const char *fmt, ...)
209 {
210  outvec->clear();
211  char buffer[BIN_SPRINTF_MAX_LEN];
212  char *bufferPtr = &(buffer[0]);
213  void *tttt = &fmt;
214  long long *varg = (long long *)(&fmt);
215  int retCode = binPrint(&bufferPtr, varg);
216  if (retCode > 0)
217  {
218  for (int i = 0; i < retCode; i++)
219  {
220  outvec->push_back(buffer[i]);
221  }
222  }
223 
224  return retCode;
225 }
226 
227 
228 std::string binDumpVecToString(std::vector<unsigned char>* outvec, bool appendReadableText /*= false*/)
229 {
230  std::string s;
231  for (int i = 0; i < outvec->size(); i++)
232  {
233  char szDummy[255] = { 0 };
234  sprintf(szDummy, "%02x ", (int)(0xFF & (*outvec)[i]));
235  s += szDummy;
236  }
237  if (appendReadableText)
238  {
239  for (int i = 0; i < outvec->size(); i++)
240  {
241  char szDummy[255] = { 0 };
242  sprintf(szDummy, "%c", (*outvec)[i] < 0x20 ? '.' : (*outvec)[i]);
243  s += szDummy;
244  }
245  }
246  return(s);
247 }
248 
249 
250 
#define PAD_RIGHT
Definition: binPrintf.cpp:33
#define PAD_ZERO
Definition: binPrintf.cpp:34
int binSprintf(char *out, const char *format,...)
Definition: binPrintf.cpp:202
static void binPrintchar(char **str, int c)
Definition: binPrintf.cpp:23
static int binPrints(char **out, const char *string, int width, int pad)
Definition: binPrintf.cpp:36
int binPrintf(const char *format,...)
Definition: binPrintf.cpp:196
int binSprintfVec(std::vector< unsigned char > *outvec, const char *fmt,...)
Definition: binPrintf.cpp:208
#define BIN_SPRINTF_MAX_LEN
Definition: binPrintf.cpp:22
std::string binDumpVecToString(std::vector< unsigned char > *outvec, bool appendReadableText)
Definition: binPrintf.cpp:228
#define PRINT_BUF_LEN
Definition: binPrintf.cpp:67
static int binPrinti(char **out, int i, int b, int sg, int width, int pad, int letbase)
Definition: binPrintf.cpp:69
static int binPrint(char **out, long long *varg)
Definition: binPrintf.cpp:126
static sick_scan::SickScanCommonTcp * s


sick_scan
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Tue Jul 9 2019 04:55:32