src/printf.c

changeset 849
edb9f875b7f9
parent 813
aba6d37b78bd
child 890
54565fd74e74
equal deleted inserted replaced
848:6456036bbb37 849:edb9f875b7f9
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