Quellcode durchsuchen

Add faux_str_c_esc_quote() function and test

Serj Kalichev vor 10 Monaten
Ursprung
Commit
a633510c77
5 geänderte Dateien mit 66 neuen und 1 gelöschten Zeilen
  1. 1 1
      faux/faux.map
  2. 1 0
      faux/str.h
  3. 31 0
      faux/str/str.c
  4. 32 0
      faux/str/testc_str.c
  5. 1 0
      faux/testc_module/testc_module.c

+ 1 - 1
faux/faux.map

@@ -225,7 +225,6 @@ FAUX_2.0 {
 		faux_msg_deserialize;
 		faux_msg_debug;
 
-
 		faux_send;
 		faux_send_block;
 		faux_sendv;
@@ -314,6 +313,7 @@ FAUX_2.0 {
 		faux_str_is_empty;
 		faux_str_has_content;
 		faux_str_c_esc;
+		faux_str_c_esc_quote;
 		faux_str_c_bin;
 		faux_str_nextword;
 		faux_str_suffix;

+ 1 - 0
faux/str.h

@@ -43,6 +43,7 @@ bool_t faux_str_is_empty(const char *str);
 bool_t faux_str_has_content(const char *str);
 
 char *faux_str_c_esc(const char *src);
+char *faux_str_c_esc_quote(const char *src);
 char *faux_str_c_bin(const char *src, size_t n);
 
 char *faux_str_nextword(const char *str, const char **saveptr,

+ 31 - 0
faux/str/str.c

@@ -605,6 +605,37 @@ char *faux_str_c_esc(const char *src)
 }
 
 
+/** Escape string and add quotes if necessary.
+ *
+ * Quotes will be added if string contains spaces.
+ *
+ * @warning The returned pointer must be freed by faux_str_free().
+ * @param [in] src Input string.
+ * @return Processed string or NULL on error.
+ */
+char *faux_str_c_esc_quote(const char *src)
+{
+	char *space = NULL;
+	char *escaped = NULL;
+	char *result = NULL;
+
+	if (!src)
+		return NULL;
+
+	escaped = faux_str_c_esc(src);
+	// String with space must have quotes
+	space = strchr(escaped, ' ');
+	if (space) {
+		result = faux_str_sprintf("\"%s\"", escaped);
+		faux_str_free(escaped);
+	} else {
+		result = escaped;
+	}
+
+	return result;
+}
+
+
 #define BYTE_CONV_LEN 4 // Length of one byte converted to string
 
 /** Prepare binary block for embedding to C-code.

+ 32 - 0
faux/str/testc_str.c

@@ -124,3 +124,35 @@ int testc_faux_str_numcmp(void)
 
 	return 0;
 }
+
+
+int testc_faux_str_c_esc_quote(void)
+{
+	char *src = NULL;
+	char *etalon = NULL;
+	char *esc = NULL;
+
+	src = "aaa\\bbb\"";
+	etalon = "aaa\\\\bbb\\\"";
+	esc = faux_str_c_esc_quote(src);
+	if (strcmp(esc, etalon) != 0) {
+		printf("Problem with string without spaces\n");
+		printf("src=[%s], etalon=[%s], esc=[%s]\n",
+			src, etalon, esc);
+		return -1;
+	}
+	faux_str_free(esc);
+
+	src = "aaa\\ bbb\"";
+	etalon = "\"aaa\\\\ bbb\\\"\"";
+	esc = faux_str_c_esc_quote(src);
+	if (strcmp(esc, etalon) != 0) {
+		printf("Problem with string with spaces\n");
+		printf("src=[%s], etalon=[%s], esc=[%s]\n",
+			src, etalon, esc);
+		return -1;
+	}
+	faux_str_free(esc);
+
+	return 0;
+}

+ 1 - 0
faux/testc_module/testc_module.c

@@ -17,6 +17,7 @@ const char *testc_module[][2] = {
 	{"testc_faux_str_nextword", "Find next word (quotation)"},
 	{"testc_faux_str_getline", "Get line from string"},
 	{"testc_faux_str_numcmp", "Numeric comparison"},
+	{"testc_faux_str_c_esc_quote", "Escape and add quotes for string with spaces"},
 
 	// ini
 	{"testc_faux_ini_parse_file", "Complex test of INI file parsing"},