return str_number(string, number, &bogus);\r
}\r
\r
+/* Does a char need to be escaped? */\r
+static bool needs_escape(const TCHAR c) {\r
+ if (c == _T('"')) return true;\r
+ if (c == _T('&')) return true;\r
+ if (c == _T('%')) return true;\r
+ if (c == _T('^')) return true;\r
+ if (c == _T('<')) return true;\r
+ if (c == _T('>')) return true;\r
+ if (c == _T('|')) return true;\r
+ return false;\r
+}\r
+\r
+/* Does a char need to be quoted? */\r
+static bool needs_quote(const TCHAR c) {\r
+ if (c == _T(' ')) return true;\r
+ if (c == _T('\t')) return true;\r
+ if (c == _T('\n')) return true;\r
+ if (c == _T('\v')) return true;\r
+ if (c == _T('"')) return true;\r
+ if (c == _T('*')) return true;\r
+ return needs_escape(c);\r
+}\r
+\r
+/* https://blogs.msdn.microsoft.com/twistylittlepassagesallalike/2011/04/23/everyone-quotes-command-line-arguments-the-wrong-way/ */\r
+/* http://www.robvanderwoude.com/escapechars.php */\r
+int quote(const TCHAR *unquoted, TCHAR *buffer, size_t buflen) {\r
+ size_t i, j, n;\r
+ size_t len = _tcslen(unquoted);\r
+ if (len > buflen - 1) return 1;\r
+\r
+ bool escape = false;\r
+ bool quotes = false;\r
+\r
+ for (i = 0; i < len; i++) {\r
+ if (needs_escape(unquoted[i])) {\r
+ escape = quotes = true;\r
+ break;\r
+ }\r
+ if (needs_quote(unquoted[i])) quotes = true;\r
+ }\r
+ if (! quotes) {\r
+ memmove(buffer, unquoted, (len + 1) * sizeof(TCHAR));\r
+ return 0;\r
+ }\r
+\r
+ /* "" */\r
+ size_t quoted_len = 2;\r
+ if (escape) quoted_len += 2;\r
+ for (i = 0; ; i++) {\r
+ n = 0;\r
+\r
+ while (i != len && unquoted[i] == _T('\\')) {\r
+ i++;\r
+ n++;\r
+ }\r
+\r
+ if (i == len) {\r
+ quoted_len += n * 2;\r
+ break;\r
+ }\r
+ else if (unquoted[i] == _T('"')) quoted_len += n * 2 + 2;\r
+ else quoted_len += n + 1;\r
+ if (needs_escape(unquoted[i])) quoted_len += n;\r
+ }\r
+ if (quoted_len > buflen - 1) return 1;\r
+\r
+ TCHAR *s = buffer;\r
+ if (escape) *s++ = _T('^');\r
+ *s++ = _T('"');\r
+\r
+ for (i = 0; ; i++) {\r
+ n = 0;\r
+\r
+ while (i != len && unquoted[i] == _T('\\')) {\r
+ i++;\r
+ n++;\r
+ }\r
+\r
+ if (i == len) {\r
+ for (j = 0; j < n * 2; j++) {\r
+ if (escape) *s++ = _T('^');\r
+ *s++ = _T('\\');\r
+ }\r
+ break;\r
+ }\r
+ else if (unquoted[i] == _T('"')) {\r
+ for (j = 0; j < n * 2 + 1; j++) {\r
+ if (escape) *s++ = _T('^');\r
+ *s++ = _T('\\');\r
+ }\r
+ if (escape && needs_escape(unquoted[i])) *s++ = _T('^');\r
+ *s++ = unquoted[i];\r
+ }\r
+ else {\r
+ for (j = 0; j < n; j++) {\r
+ if (escape) *s++ = _T('^');\r
+ *s++ = _T('\\');\r
+ }\r
+ if (escape && needs_escape(unquoted[i])) *s++ = _T('^');\r
+ *s++ = unquoted[i];\r
+ }\r
+ }\r
+ if (escape) *s++ = _T('^');\r
+ *s++ = _T('"');\r
+ *s++ = _T('\0');\r
+\r
+ return 0;\r
+}\r
+\r
/* Remove basename of a path. */\r
void strip_basename(TCHAR *buffer) {\r
size_t len = _tcslen(buffer);\r