src/printf.c

changeset 811
b02ad05c7f2d
parent 810
85859399a0cc
child 812
fda0f1910d7e
equal deleted inserted replaced
810:85859399a0cc 811:b02ad05c7f2d
59 char buf[CX_PRINTF_SBO_SIZE]; 59 char buf[CX_PRINTF_SBO_SIZE];
60 va_list ap2; 60 va_list ap2;
61 va_copy(ap2, ap); 61 va_copy(ap2, ap);
62 int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap); 62 int ret = vsnprintf(buf, CX_PRINTF_SBO_SIZE, fmt, ap);
63 if (ret < 0) { 63 if (ret < 0) {
64 va_end(ap2);
64 return ret; 65 return ret;
65 } else if (ret < CX_PRINTF_SBO_SIZE) { 66 } else if (ret < CX_PRINTF_SBO_SIZE) {
67 va_end(ap2);
66 return (int) wfc(buf, 1, ret, stream); 68 return (int) wfc(buf, 1, ret, stream);
67 } else { 69 } else {
68 int len = ret + 1; 70 int len = ret + 1;
69 char *newbuf = malloc(len); 71 char *newbuf = malloc(len);
70 if (!newbuf) { 72 if (!newbuf) {
73 va_end(ap2);
71 return -1; 74 return -1;
72 } 75 }
73 76
74 ret = vsnprintf(newbuf, len, fmt, ap2); 77 ret = vsnprintf(newbuf, len, fmt, ap2);
78 va_end(ap2);
75 if (ret > 0) { 79 if (ret > 0) {
76 ret = (int) wfc(newbuf, 1, ret, stream); 80 ret = (int) wfc(newbuf, 1, ret, stream);
77 } 81 }
78 free(newbuf); 82 free(newbuf);
79 } 83 }
122 } else { 126 } else {
123 s.length = (size_t) ret; 127 s.length = (size_t) ret;
124 } 128 }
125 } 129 }
126 } 130 }
131 va_end(ap2);
127 return s; 132 return s;
128 } 133 }
129 134
130 int cx_sprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, ... ) { 135 int cx_sprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, ... ) {
131 va_list ap; 136 va_list ap;

mercurial