Browse Source

faux.str: faux_str_vsprintf() with va_list

Serj Kalichev 3 years ago
parent
commit
75e6475cce
2 changed files with 38 additions and 11 deletions
  1. 2 0
      faux/str.h
  2. 36 11
      faux/str/str.c

+ 2 - 0
faux/str.h

@@ -6,6 +6,7 @@
 #define _faux_str_h
 
 #include <stddef.h>
+#include <stdarg.h>
 
 #include <faux/faux.h>
 
@@ -24,6 +25,7 @@ char *faux_str_dup(const char *str);
 char *faux_str_catn(char **str, const char *text, size_t n);
 char *faux_str_cat(char **str, const char *text);
 char *faux_str_mcat(char **str, ...);
+char *faux_str_vsprintf(const char *fmt, va_list ap);
 char *faux_str_sprintf(const char *fmt, ...);
 
 char *faux_str_tolower(const char *str);

+ 36 - 11
faux/str/str.c

@@ -225,31 +225,31 @@ char *faux_str_mcat(char **str, ...)
 }
 
 
-/** @brief Allocates memory and sprintf() to it.
+/** @brief Allocates memory and vsprintf() to it.
  *
  * Function tries to find out necessary amount of memory for specified format
- * string and arguments. Format is same as for sprintf() function. Then
- * function allocates memory for resulting string and sprintf() to it. So
+ * string and arguments. Format is same as for vsprintf() function. Then
+ * function allocates memory for resulting string and vsprintf() to it. So
  * user doesn't need to allocate buffer himself. Function returns allocated
  * string that need to be freed by faux_str_free() function later.
  *
  * @warning The returned pointer must be free by faux_str_free().
  *
  * @param [in] fmt Format string like the sprintf()'s fmt.
- * @param [in] arg Number of arguments.
+ * @param [in] ap The va_list argument.
  * @return Allocated resulting string or NULL on error.
  */
-char *faux_str_sprintf(const char *fmt, ...)
+char *faux_str_vsprintf(const char *fmt, va_list ap)
 {
 	int size = 1;
 	char calc_buf[1] = "";
 	char *line = NULL;
-	va_list ap;
+	va_list ap2;
 
 	// Calculate buffer size
-	va_start(ap, fmt);
-	size = vsnprintf(calc_buf, size, fmt, ap);
-	va_end(ap);
+	va_copy(ap2, ap);
+	size = vsnprintf(calc_buf, size, fmt, ap2);
+	va_end(ap2);
 	// The snprintf() prior to 2.0.6 glibc version returns -1 if string
 	// was truncated. The later glibc returns required buffer size.
 	// The calc_buf can be NULL and size can be 0 for recent glibc but
@@ -264,9 +264,7 @@ char *faux_str_sprintf(const char *fmt, ...)
 		return NULL;
 
 	// Format real string
-	va_start(ap, fmt);
 	size = vsnprintf(line, size, fmt, ap);
-	va_end(ap);
 	if (size < 0) { // Some problems
 		faux_str_free(line);
 		return NULL;
@@ -276,6 +274,33 @@ char *faux_str_sprintf(const char *fmt, ...)
 }
 
 
+/** @brief Allocates memory and sprintf() to it.
+ *
+ * Function tries to find out necessary amount of memory for specified format
+ * string and arguments. Format is same as for sprintf() function. Then
+ * function allocates memory for resulting string and sprintf() to it. So
+ * user doesn't need to allocate buffer himself. Function returns allocated
+ * string that need to be freed by faux_str_free() function later.
+ *
+ * @warning The returned pointer must be free by faux_str_free().
+ *
+ * @param [in] fmt Format string like the sprintf()'s fmt.
+ * @param [in] arg Number of arguments.
+ * @return Allocated resulting string or NULL on error.
+ */
+char *faux_str_sprintf(const char *fmt, ...)
+{
+	char *line = NULL;
+	va_list ap;
+
+	va_start(ap, fmt);
+	line = faux_str_vsprintf(fmt, ap);
+	va_end(ap);
+
+	return line;
+}
+
+
 /** @brief Service function to compare to chars in right way.
  *
  * The problem is char type can be signed or unsigned on different