Browse Source

faux.str: Implement faux_str_vcat()

Serj Kalichev 4 years ago
parent
commit
4e9a9a5e05
3 changed files with 31 additions and 8 deletions
  1. 3 8
      faux/ini/ini.c
  2. 1 0
      faux/str.h
  3. 27 0
      faux/str/str.c

+ 3 - 8
faux/ini/ini.c

@@ -451,14 +451,9 @@ int faux_ini_write_file(const faux_ini_t *ini, const char *fn) {
 		quote_value = faux_str_chars(value, spaces) ? "\"" : "";
 
 		// Prepare INI line
-		faux_str_cat(&line, quote_name);
-		faux_str_cat(&line, name);
-		faux_str_cat(&line, quote_name);
-		faux_str_cat(&line, "=");
-		faux_str_cat(&line, quote_value);
-		faux_str_cat(&line, value);
-		faux_str_cat(&line, quote_value);
-		faux_str_cat(&line, "\n");
+		faux_str_vcat(&line,
+			quote_name, name, quote_name, "=",
+			quote_value, value, quote_value, "\n", NULL);
 
 		bytes_written = faux_file_write(f, line, strlen(line));
 		faux_str_free(line);

+ 1 - 0
faux/str.h

@@ -23,6 +23,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_vcat(char **str, ...);
 
 char *faux_str_tolower(const char *str);
 char *faux_str_toupper(const char *str);

+ 27 - 0
faux/str/str.c

@@ -10,6 +10,7 @@
 #include <string.h>
 #include <assert.h>
 #include <stdio.h>
+#include <stdarg.h>
 
 #include "faux/ctype.h"
 #include "faux/str.h"
@@ -195,6 +196,32 @@ char *faux_str_cat(char **str, const char *text) {
 	return faux_str_catn(str, text, len);
 }
 
+/** @brief Add some text to existent string.
+ *
+ * Concatenate two strings. Add second string to the end of the first one.
+ * The first argument is address of string pointer. The pointer can be
+ * changed due to realloc() features. The first pointer can be NULL. In this
+ * case the memory will be malloc()-ed and stored to the first pointer.
+ *
+ * @param [in,out] str Address of first string pointer.
+ * @param [in] text Text to add to the first string.
+ * @return Pointer to resulting string or NULL.
+ */
+char *faux_str_vcat(char **str, ...) {
+
+	va_list ap;
+	const char *arg = NULL;
+	char *retval = NULL;
+
+	va_start(ap, str);
+	while ((arg = va_arg(ap, const char *))) {
+		retval = faux_str_cat(str, arg);
+	}
+	va_end(ap);
+
+	return retval;
+}
+
 
 /** @brief Service function to compare to chars in right way.
  *