ucx/printf.c

changeset 49
2f71f4ee247a
parent 2
fbdfaacc4182
equal deleted inserted replaced
48:ae61523bce20 49:2f71f4ee247a
37 unsigned const cx_printf_sbo_size = CX_PRINTF_SBO_SIZE; 37 unsigned const cx_printf_sbo_size = CX_PRINTF_SBO_SIZE;
38 38
39 int cx_fprintf( 39 int cx_fprintf(
40 void *stream, 40 void *stream,
41 cx_write_func wfc, 41 cx_write_func wfc,
42 char const *fmt, 42 const char *fmt,
43 ... 43 ...
44 ) { 44 ) {
45 int ret; 45 int ret;
46 va_list ap; 46 va_list ap;
47 va_start(ap, fmt); 47 va_start(ap, fmt);
51 } 51 }
52 52
53 int cx_vfprintf( 53 int cx_vfprintf(
54 void *stream, 54 void *stream,
55 cx_write_func wfc, 55 cx_write_func wfc,
56 char const *fmt, 56 const char *fmt,
57 va_list ap 57 va_list ap
58 ) { 58 ) {
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);
83 } 83 }
84 return ret; 84 return ret;
85 } 85 }
86 86
87 cxmutstr cx_asprintf_a( 87 cxmutstr cx_asprintf_a(
88 CxAllocator const *allocator, 88 const CxAllocator *allocator,
89 char const *fmt, 89 const char *fmt,
90 ... 90 ...
91 ) { 91 ) {
92 va_list ap; 92 va_list ap;
93 va_start(ap, fmt); 93 va_start(ap, fmt);
94 cxmutstr ret = cx_vasprintf_a(allocator, fmt, ap); 94 cxmutstr ret = cx_vasprintf_a(allocator, fmt, ap);
95 va_end(ap); 95 va_end(ap);
96 return ret; 96 return ret;
97 } 97 }
98 98
99 cxmutstr cx_vasprintf_a( 99 cxmutstr cx_vasprintf_a(
100 CxAllocator const *a, 100 const CxAllocator *a,
101 char const *fmt, 101 const char *fmt,
102 va_list ap 102 va_list ap
103 ) { 103 ) {
104 cxmutstr s; 104 cxmutstr s;
105 s.ptr = NULL; 105 s.ptr = NULL;
106 s.length = 0; 106 s.length = 0;
130 } 130 }
131 va_end(ap2); 131 va_end(ap2);
132 return s; 132 return s;
133 } 133 }
134 134
135 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, ... ) {
136 va_list ap; 136 va_list ap;
137 va_start(ap, fmt); 137 va_start(ap, fmt);
138 int ret = cx_vsprintf_a(alloc, str, len, fmt, ap); 138 int ret = cx_vsprintf_a(alloc, str, len, fmt, ap);
139 va_end(ap); 139 va_end(ap);
140 return ret; 140 return ret;
141 } 141 }
142 142
143 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, va_list ap) { 143 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap) {
144 va_list ap2; 144 va_list ap2;
145 va_copy(ap2, ap); 145 va_copy(ap2, ap);
146 int ret = vsnprintf(*str, len, fmt, ap); 146 int ret = vsnprintf(*str, *len, fmt, ap);
147 if ((unsigned) ret >= len) { 147 if ((unsigned) ret >= *len) {
148 unsigned newlen = ret + 1; 148 unsigned newlen = ret + 1;
149 char *ptr = cxRealloc(alloc, *str, newlen); 149 char *ptr = cxRealloc(alloc, *str, newlen);
150 if (ptr) { 150 if (ptr) {
151 int newret = vsnprintf(ptr, newlen, fmt, ap2); 151 int newret = vsnprintf(ptr, newlen, fmt, ap2);
152 if (newret < 0) { 152 if (newret < 0) {
153 cxFree(alloc, ptr); 153 cxFree(alloc, ptr);
154 } else { 154 } else {
155 *len = newlen;
155 *str = ptr; 156 *str = ptr;
156 ret = newret; 157 ret = newret;
157 } 158 }
158 } 159 }
159 } 160 }
160 va_end(ap2); 161 va_end(ap2);
161 return ret; 162 return ret;
162 } 163 }
163 164
164 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, ... ) { 165 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ... ) {
165 va_list ap; 166 va_list ap;
166 va_start(ap, fmt); 167 va_start(ap, fmt);
167 int ret = cx_vsprintf_sa(alloc, buf, len, str, fmt, ap); 168 int ret = cx_vsprintf_sa(alloc, buf, len, str, fmt, ap);
168 va_end(ap); 169 va_end(ap);
169 return ret; 170 return ret;
170 } 171 }
171 172
172 int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, va_list ap) { 173 int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap) {
173 va_list ap2; 174 va_list ap2;
174 va_copy(ap2, ap); 175 va_copy(ap2, ap);
175 int ret = vsnprintf(buf, len, fmt, ap); 176 int ret = vsnprintf(buf, *len, fmt, ap);
176 *str = buf; 177 *str = buf;
177 if ((unsigned) ret >= len) { 178 if ((unsigned) ret >= *len) {
178 unsigned newlen = ret + 1; 179 unsigned newlen = ret + 1;
179 char *ptr = cxMalloc(alloc, newlen); 180 char *ptr = cxMalloc(alloc, newlen);
180 if (ptr) { 181 if (ptr) {
181 int newret = vsnprintf(ptr, newlen, fmt, ap2); 182 int newret = vsnprintf(ptr, newlen, fmt, ap2);
182 if (newret < 0) { 183 if (newret < 0) {
183 cxFree(alloc, ptr); 184 cxFree(alloc, ptr);
184 } else { 185 } else {
186 *len = newlen;
185 *str = ptr; 187 *str = ptr;
186 ret = newret; 188 ret = newret;
187 } 189 }
188 } 190 }
189 } 191 }

mercurial