Contiki 2.6
|
00001 #include <stdio.h> 00002 #include <strformat.h> 00003 #include <string.h> 00004 00005 struct FmtBuffer 00006 { 00007 char *pos; 00008 size_t left; 00009 }; 00010 00011 static StrFormatResult 00012 buffer_str(void *user_data, const char *data, unsigned int len) 00013 { 00014 struct FmtBuffer *buffer = (struct FmtBuffer*)user_data; 00015 if (len >= buffer->left) { 00016 len = buffer->left; 00017 len--; 00018 } 00019 00020 memcpy(buffer->pos, data, len); 00021 buffer->pos += len; 00022 buffer->left -= len; 00023 return STRFORMAT_OK; 00024 } 00025 00026 int snprintf(char *str, size_t size, const char *format, ...) 00027 { 00028 int res; 00029 va_list ap; 00030 va_start(ap, format); 00031 res = vsnprintf(str, size, format, ap); 00032 va_end(ap); 00033 return res; 00034 } 00035 00036 int vsnprintf(char *str, size_t size, const char *format, va_list ap) 00037 { 00038 struct FmtBuffer buffer; 00039 StrFormatContext ctxt; 00040 int res; 00041 ctxt.write_str = buffer_str; 00042 ctxt.user_data = &buffer; 00043 buffer.pos = str; 00044 buffer.left = size; 00045 res = format_str_v(&ctxt, format, ap); 00046 *buffer.pos = '\0'; 00047 return res; 00048 }