FreeNOS
vsnprintf.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <String.h>
19 #include "stdarg.h"
20 #include "stdio.h"
21 #include "stdlib.h"
22 
23 int vsnprintf(char *buffer, unsigned int size, const char *fmt, va_list args)
24 {
25  char buf[20], *ptr;
26  int ch, length = -1, i;
27  unsigned int written = 0;
28  String s;
29 
30  // Loop formatted message
31  while ((ch = *fmt++) && written < size)
32  {
33  if (ch != '%')
34  {
35  *buffer++ = ch;
36  written++;
37  }
38  else
39  {
40  switch_again:
41 
42  switch (*fmt)
43  {
44  // Length modifier
45  case '0' ... '9':
46 
47  for (i = 0; i < 19 && *fmt >= '0' && *fmt <= '9'; i++)
48  {
49  buf[i] = *fmt++;
50  buf[i+1] = ZERO;
51  }
52  length = atoi(buf);
53  goto switch_again;
54 
55  // Integer
56  case 'u':
57  s.setUnsigned(va_arg(args, unsigned), Number::Dec, buf);
58  ptr = buf;
59  goto string;
60 
61  case 'd':
62  itoa(buf, 10, va_arg(args, int));
63  ptr = buf;
64  goto string;
65 
66  // Long integer
67  case 'l':
68  itoa(buf, 10, va_arg(args, long));
69  ptr = buf;
70  goto string;
71 
72  // Hexadecimal
73  case 'x':
74  s.setUnsigned(va_arg(args, unsigned), Number::Hex, buf);
75  ptr = buf;
76  goto string;
77 
78  // Character
79  case 'c':
80  buf[0] = va_arg(args, int);
81  buf[1] = ZERO;
82  ptr = buf;
83  goto string;
84 
85  // String
86  case 's':
87  ptr = va_arg(args, char *);
88 
89  string:
90  while( ((length == -1 && *ptr) ||
91  (length > 0 && length--)) && written++ < size)
92  {
93  if (*ptr)
94  {
95  *buffer++ = *ptr++;
96  }
97  else
98  *buffer++ = ' ';
99  }
100  break;
101 
102  // Unsupported
103  default:
104  *buffer++ = ch;
105  written++;
106  break;
107  }
108  fmt++;
109  length = -1;
110  }
111  }
112 
113  // Null terminate
114  if (written < size)
115  *buffer = 0;
116  return (written);
117 }
itoa
C void itoa(char *buffer, int divisor, int number)
Convert a number to a string.
Definition: itoa.cpp:21
va_arg
#define va_arg(v, l)
Definition: stdarg.h:52
String
Abstraction of strings.
Definition: String.h:41
atoi
C int atoi(const char *nptr)
Convert a string to an integer.
Definition: atoi.cpp:21
vsnprintf
int vsnprintf(char *buffer, unsigned int size, const char *fmt, va_list args)
Write a formatted string into a buffer.
Definition: vsnprintf.cpp:23
Number::Dec
@ Dec
Definition: Types.h:170
stdarg.h
va_list
__gnuc_va_list va_list
Definition: stdarg.h:105
stdio.h
String.h
stdlib.h
ZERO
#define ZERO
Zero value.
Definition: Macros.h:43
Number::Hex
@ Hex
Decimal: 0-10.
Definition: Types.h:171
length
u32 length
Definition: IntelACPI.h:64
String::setUnsigned
Size setUnsigned(const ulong number, const Number::Base base=Number::Dec, char *string=ZERO, const bool sign=false)
Set text-representation of an unsigned number.
Definition: String.cpp:538