ucx/printf.c

changeset 888
af685cc9d623
parent 852
83fdf679df99
equal deleted inserted replaced
877:b60487c3ec36 888:af685cc9d623
66 } else if (ret < CX_PRINTF_SBO_SIZE) { 66 } else if (ret < CX_PRINTF_SBO_SIZE) {
67 va_end(ap2); 67 va_end(ap2);
68 return (int) wfc(buf, 1, ret, stream); 68 return (int) wfc(buf, 1, ret, stream);
69 } else { 69 } else {
70 int len = ret + 1; 70 int len = ret + 1;
71 char *newbuf = malloc(len); 71 char *newbuf = cxMallocDefault(len);
72 if (!newbuf) { // LCOV_EXCL_START 72 if (!newbuf) { // LCOV_EXCL_START
73 va_end(ap2); 73 va_end(ap2);
74 return -1; 74 return -1;
75 } // LCOV_EXCL_STOP 75 } // LCOV_EXCL_STOP
76 76
77 ret = vsnprintf(newbuf, len, fmt, ap2); 77 ret = vsnprintf(newbuf, len, fmt, ap2);
78 va_end(ap2); 78 va_end(ap2);
79 if (ret > 0) { 79 if (ret > 0) {
80 ret = (int) wfc(newbuf, 1, ret, stream); 80 ret = (int) wfc(newbuf, 1, ret, stream);
81 } 81 }
82 free(newbuf); 82 cxFreeDefault(newbuf);
83 } 83 }
84 return ret; 84 return ret;
85 } 85 }
86 86
87 cxmutstr cx_asprintf_a( 87 cxmutstr cx_asprintf_a(
119 int len = ret + 1; 119 int len = ret + 1;
120 s.ptr = cxMalloc(a, len); 120 s.ptr = cxMalloc(a, len);
121 if (s.ptr) { 121 if (s.ptr) {
122 ret = vsnprintf(s.ptr, len, fmt, ap2); 122 ret = vsnprintf(s.ptr, len, fmt, ap2);
123 if (ret < 0) { 123 if (ret < 0) {
124 free(s.ptr); 124 cxFree(a, s.ptr);
125 s.ptr = NULL; 125 s.ptr = NULL;
126 } else { 126 } else {
127 s.length = (size_t) ret; 127 s.length = (size_t) ret;
128 } 128 }
129 } 129 }
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(
136 const CxAllocator *alloc,
137 char **str,
138 size_t *len,
139 const char *fmt,
140 ...
141 ) {
136 va_list ap; 142 va_list ap;
137 va_start(ap, fmt); 143 va_start(ap, fmt);
138 int ret = cx_vsprintf_a(alloc, str, len, fmt, ap); 144 int ret = cx_vsprintf_a(alloc, str, len, fmt, ap);
139 va_end(ap); 145 va_end(ap);
140 return ret; 146 return ret;
141 } 147 }
142 148
143 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t *len, const char *fmt, va_list ap) { 149 int cx_vsprintf_a(
150 const CxAllocator *alloc,
151 char **str,
152 size_t *len,
153 const char *fmt,
154 va_list ap
155 ) {
144 va_list ap2; 156 va_list ap2;
145 va_copy(ap2, ap); 157 va_copy(ap2, ap);
146 int ret = vsnprintf(*str, *len, fmt, ap); 158 int ret = vsnprintf(*str, *len, fmt, ap);
147 if ((unsigned) ret >= *len) { 159 if ((unsigned) ret >= *len) {
148 unsigned newlen = ret + 1; 160 unsigned newlen = ret + 1;
160 } 172 }
161 va_end(ap2); 173 va_end(ap2);
162 return ret; 174 return ret;
163 } 175 }
164 176
165 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, ... ) { 177 int cx_sprintf_sa(
178 const CxAllocator *alloc,
179 char *buf,
180 size_t *len,
181 char **str,
182 const char *fmt,
183 ...
184 ) {
166 va_list ap; 185 va_list ap;
167 va_start(ap, fmt); 186 va_start(ap, fmt);
168 int ret = cx_vsprintf_sa(alloc, buf, len, str, fmt, ap); 187 int ret = cx_vsprintf_sa(alloc, buf, len, str, fmt, ap);
169 va_end(ap); 188 va_end(ap);
170 return ret; 189 return ret;
171 } 190 }
172 191
173 int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t *len, char **str, const char *fmt, va_list ap) { 192 int cx_vsprintf_sa(
193 const CxAllocator *alloc,
194 char *buf,
195 size_t *len,
196 char **str,
197 const char *fmt,
198 va_list ap
199 ) {
174 va_list ap2; 200 va_list ap2;
175 va_copy(ap2, ap); 201 va_copy(ap2, ap);
176 int ret = vsnprintf(buf, *len, fmt, ap); 202 int ret = vsnprintf(buf, *len, fmt, ap);
177 *str = buf; 203 *str = buf;
178 if ((unsigned) ret >= *len) { 204 if ((unsigned) ret >= *len) {

mercurial