src/printf.c

changeset 634
f78d3b77d456
parent 600
19211b88cc0f
child 643
5700ba9154ab
equal deleted inserted replaced
633:63a692642aa2 634:f78d3b77d456
29 #include "cx/printf.h" 29 #include "cx/printf.h"
30 30
31 #include <stdio.h> 31 #include <stdio.h>
32 #include <string.h> 32 #include <string.h>
33 33
34 #define UCX_PRINTF_BUFSIZE 256 34 #define CX_PRINTF_BUFSIZE 256
35 35
36 int cx_fprintf(void *stream, cx_write_func wfc, char const *fmt, ...) { 36 int cx_fprintf(void *stream, cx_write_func wfc, char const *fmt, ...) {
37 int ret; 37 int ret;
38 va_list ap; 38 va_list ap;
39 va_start(ap, fmt); 39 va_start(ap, fmt);
41 va_end(ap); 41 va_end(ap);
42 return ret; 42 return ret;
43 } 43 }
44 44
45 int cx_vfprintf(void *stream, cx_write_func wfc, char const *fmt, va_list ap) { 45 int cx_vfprintf(void *stream, cx_write_func wfc, char const *fmt, va_list ap) {
46 char buf[UCX_PRINTF_BUFSIZE]; 46 char buf[CX_PRINTF_BUFSIZE];
47 va_list ap2; 47 va_list ap2;
48 va_copy(ap2, ap); 48 va_copy(ap2, ap);
49 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); 49 int ret = vsnprintf(buf, CX_PRINTF_BUFSIZE, fmt, ap);
50 if (ret < 0) { 50 if (ret < 0) {
51 return ret; 51 return ret;
52 } else if (ret < UCX_PRINTF_BUFSIZE) { 52 } else if (ret < CX_PRINTF_BUFSIZE) {
53 return (int) wfc(buf, 1, ret, stream); 53 return (int) wfc(buf, 1, ret, stream);
54 } else { 54 } else {
55 int len = ret + 1; 55 int len = ret + 1;
56 char *newbuf = malloc(len); 56 char *newbuf = malloc(len);
57 if (!newbuf) { 57 if (!newbuf) {
78 78
79 cxmutstr cx_vasprintf_a(CxAllocator *a, char const *fmt, va_list ap) { 79 cxmutstr cx_vasprintf_a(CxAllocator *a, char const *fmt, va_list ap) {
80 cxmutstr s; 80 cxmutstr s;
81 s.ptr = NULL; 81 s.ptr = NULL;
82 s.length = 0; 82 s.length = 0;
83 char buf[UCX_PRINTF_BUFSIZE]; 83 char buf[CX_PRINTF_BUFSIZE];
84 va_list ap2; 84 va_list ap2;
85 va_copy(ap2, ap); 85 va_copy(ap2, ap);
86 int ret = vsnprintf(buf, UCX_PRINTF_BUFSIZE, fmt, ap); 86 int ret = vsnprintf(buf, CX_PRINTF_BUFSIZE, fmt, ap);
87 if (ret > 0 && ret < UCX_PRINTF_BUFSIZE) { 87 if (ret > 0 && ret < CX_PRINTF_BUFSIZE) {
88 s.ptr = cxMalloc(a, ret + 1); 88 s.ptr = cxMalloc(a, ret + 1);
89 if (s.ptr) { 89 if (s.ptr) {
90 s.length = (size_t) ret; 90 s.length = (size_t) ret;
91 memcpy(s.ptr, buf, ret); 91 memcpy(s.ptr, buf, ret);
92 s.ptr[s.length] = '\0'; 92 s.ptr[s.length] = '\0';

mercurial