Contiki 2.5
dbg-snprintf.c
1 #include <stdio.h>
2 #include <strformat.h>
3 #include <string.h>
4 
5 struct FmtBuffer
6 {
7  char *pos;
8  size_t left;
9 };
10 
11 static StrFormatResult
12 buffer_str(void *user_data, const char *data, unsigned int len)
13 {
14  struct FmtBuffer *buffer = (struct FmtBuffer*)user_data;
15  if (len >= buffer->left) {
16  len = buffer->left;
17  len--;
18  }
19 
20  memcpy(buffer->pos, data, len);
21  buffer->pos += len;
22  buffer->left -= len;
23  return STRFORMAT_OK;
24 }
25 
26 int snprintf(char *str, size_t size, const char *format, ...)
27 {
28  int res;
29  va_list ap;
30  va_start(ap, format);
31  res = vsnprintf(str, size, format, ap);
32  va_end(ap);
33  return res;
34 }
35 
36 int vsnprintf(char *str, size_t size, const char *format, va_list ap)
37 {
38  struct FmtBuffer buffer;
39  StrFormatContext ctxt;
40  int res;
41  ctxt.write_str = buffer_str;
42  ctxt.user_data = &buffer;
43  buffer.pos = str;
44  buffer.left = size;
45  res = format_str_v(&ctxt, format, ap);
46  *buffer.pos = '\0';
47  return res;
48 }