Explorar o código

faux_str_c_esc_space() to escape spaces too

Serj Kalichev hai 10 meses
pai
achega
3bbf373229
Modificáronse 4 ficheiros con 39 adicións e 9 borrados
  1. 4 7
      faux/argv/argv.c
  2. 1 0
      faux/faux.map
  3. 1 0
      faux/str.h
  4. 33 2
      faux/str/str.c

+ 4 - 7
faux/argv/argv.c

@@ -405,18 +405,15 @@ char *faux_argv_line(const faux_argv_t *fargv)
 	iter = faux_argv_iter(fargv);
 	while ((arg = faux_argv_each(&iter))) {
 		bool_t space_found = BOOL_FALSE;
+		char *str = NULL;
 
 		if (is_first_arg)
 			is_first_arg = BOOL_FALSE;
 		else
 			faux_str_cat(&line, " ");
-		if (faux_str_chars(arg, " \t"))
-			space_found = BOOL_TRUE;
-		if (space_found)
-			faux_str_cat(&line, "\"");
-		faux_str_cat(&line, arg);
-		if (space_found)
-			faux_str_cat(&line, "\"");
+		str = faux_str_c_esc_quote(arg);
+		faux_str_cat(&line, str);
+		faux_str_free(str);
 	}
 
 	return line;

+ 1 - 0
faux/faux.map

@@ -313,6 +313,7 @@ FAUX_2.0 {
 		faux_str_is_empty;
 		faux_str_has_content;
 		faux_str_c_esc;
+		faux_str_c_esc_space;
 		faux_str_c_esc_quote;
 		faux_str_c_bin;
 		faux_str_nextword;

+ 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_space(const char *src);
 char *faux_str_c_esc_quote(const char *src);
 char *faux_str_c_bin(const char *src, size_t n);
 

+ 33 - 2
faux/str/str.c

@@ -524,13 +524,14 @@ char *faux_str_casestr(const char *haystack, const char *needle)
 }
 
 
-/** Prepare string for embedding to C-code (make escaping).
+/** Escape string.
  *
  * @warning The returned pointer must be freed by faux_str_free().
  * @param [in] src String for escaping.
+ * @param [in] escape_space Flag to escape spaces or not
  * @return Escaped string or NULL on error.
  */
-char *faux_str_c_esc(const char *src)
+static char *faux_str_c_esc_internal(const char *src, bool_t escape_space)
 {
 	const char *src_ptr = src;
 	char *dst = NULL;
@@ -577,6 +578,12 @@ char *faux_str_c_esc(const char *src)
 		case '\t':
 			esc = "\\t";
 			break;
+		case ' ':
+			if (escape_space)
+				esc = "\\ ";
+			else
+				esc = " ";
+			break;
 		default:
 			// Check is the symbol control character. Control
 			// characters has codes from 0x00 to 0x1f.
@@ -605,6 +612,30 @@ char *faux_str_c_esc(const char *src)
 }
 
 
+/** Prepare string for embedding to C-code (make escaping).
+ *
+ * @warning The returned pointer must be freed by faux_str_free().
+ * @param [in] src String for escaping.
+ * @return Escaped string or NULL on error.
+ */
+char *faux_str_c_esc(const char *src)
+{
+	return faux_str_c_esc_internal(src, BOOL_FALSE);
+}
+
+
+/** Escaping string (escape spaces).
+ *
+ * @warning The returned pointer must be freed by faux_str_free().
+ * @param [in] src String for escaping.
+ * @return Escaped string or NULL on error.
+ */
+char *faux_str_c_esc_space(const char *src)
+{
+	return faux_str_c_esc_internal(src, BOOL_TRUE);
+}
+
+
 /** Escape string and add quotes if necessary.
  *
  * Quotes will be added if string contains spaces.