save
[protos/libecoli.git] / lib / ecoli_string.c
index 49e4a37..d51533f 100644 (file)
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <stdarg.h>
 #include <stddef.h>
 #include <string.h>
+#include <stdio.h>
 
+#include <ecoli_assert.h>
+#include <ecoli_malloc.h>
 #include <ecoli_string.h>
 
 /* count the number of identical chars at the beginning of 2 strings */
@@ -51,3 +55,47 @@ int ec_str_startswith(const char *s, const char *beginning)
 
        return 0;
 }
+
+int ec_vasprintf(char **buf, const char *fmt, va_list ap)
+{
+       char dummy;
+       int buflen, ret;
+       va_list aq;
+
+       va_copy(aq, ap);
+       *buf = NULL;
+       ret = vsnprintf(&dummy, 1, fmt, aq);
+       va_end(aq);
+       if (ret < 0)
+               return ret;
+
+       buflen = ret + 1;
+       *buf = ec_malloc(buflen);
+       if (*buf == NULL)
+               return -1;
+
+       va_copy(aq, ap);
+       ret = vsnprintf(*buf, buflen, fmt, aq);
+       va_end(aq);
+
+       ec_assert_print(ret < buflen, "invalid return value for vsnprintf");
+       if (ret < 0) {
+               free(*buf);
+               *buf = NULL;
+               return -1;
+       }
+
+       return ret;
+}
+
+int ec_asprintf(char **buf, const char *fmt, ...)
+{
+       va_list ap;
+       int ret;
+
+       va_start(ap, fmt);
+       ret = ec_vasprintf(buf, fmt, ap);
+       va_end(ap);
+
+       return ret;
+}