125 } |
125 } |
126 } |
126 } |
127 return s; |
127 return s; |
128 } |
128 } |
129 |
129 |
|
130 int cx_sprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, ... ) { |
|
131 va_list ap; |
|
132 va_start(ap, fmt); |
|
133 int ret = cx_vsprintf_a(alloc, str, len, fmt, ap); |
|
134 va_end(ap); |
|
135 return ret; |
|
136 } |
|
137 |
|
138 int cx_vsprintf_a(CxAllocator *alloc, char **str, size_t len, const char *fmt, va_list ap) { |
|
139 va_list ap2; |
|
140 va_copy(ap2, ap); |
|
141 int ret = vsnprintf(*str, len, fmt, ap); |
|
142 if (ret < 0 || ((unsigned)ret) < len) { |
|
143 va_end(ap2); |
|
144 return ret; |
|
145 } else { |
|
146 unsigned newlen = ret + 1; |
|
147 char *ptr = cxRealloc(alloc, *str, newlen); |
|
148 if (ptr) { |
|
149 int newret = vsnprintf(ptr, newlen, fmt, ap2); |
|
150 va_end(ap2); |
|
151 if (newret < 0) { |
|
152 cxFree(alloc, ptr); |
|
153 return ret; |
|
154 } else { |
|
155 *str = ptr; |
|
156 return newret; |
|
157 } |
|
158 } else { |
|
159 va_end(ap2); |
|
160 return ret; |
|
161 } |
|
162 } |
|
163 } |
|
164 |
|
165 int cx_sprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, ... ) { |
|
166 va_list ap; |
|
167 va_start(ap, fmt); |
|
168 int ret = cx_vsprintf_sa(alloc, buf, len, str, fmt, ap); |
|
169 va_end(ap); |
|
170 return ret; |
|
171 } |
|
172 |
|
173 int cx_vsprintf_sa(CxAllocator *alloc, char *buf, size_t len, char **str, const char *fmt, va_list ap) { |
|
174 va_list ap2; |
|
175 va_copy(ap2, ap); |
|
176 int ret = vsnprintf(buf, len, fmt, ap); |
|
177 *str = buf; |
|
178 if (ret < 0 || ((unsigned)ret) < len) { |
|
179 va_end(ap2); |
|
180 return ret; |
|
181 } else { |
|
182 unsigned newlen = ret + 1; |
|
183 char *ptr = cxMalloc(alloc, newlen); |
|
184 if (ptr) { |
|
185 int newret = vsnprintf(ptr, newlen, fmt, ap2); |
|
186 va_end(ap2); |
|
187 if (newret < 0) { |
|
188 cxFree(alloc, ptr); |
|
189 return ret; |
|
190 } else { |
|
191 *str = ptr; |
|
192 return newret; |
|
193 } |
|
194 } else { |
|
195 va_end(ap2); |
|
196 return ret; |
|
197 } |
|
198 } |
|
199 } |