aboutsummaryrefslogtreecommitdiff
path: root/libbuild2/utility.cxx
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2024-07-28 16:23:13 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2024-07-28 16:23:13 +0200
commitab4c1b8a8b67fd9ffc89c804efa260584530897d (patch)
tree050bd00ec20c7668ac249cab4a9b777bc577f8d5 /libbuild2/utility.cxx
parentff07b9a24212fd8cb92504f51b650afd52d7d408 (diff)
Factor out and generalize/extend to_stream_quoted(string)
Diffstat (limited to 'libbuild2/utility.cxx')
-rw-r--r--libbuild2/utility.cxx46
1 files changed, 45 insertions, 1 deletions
diff --git a/libbuild2/utility.cxx b/libbuild2/utility.cxx
index 1135851..ae7c9b0 100644
--- a/libbuild2/utility.cxx
+++ b/libbuild2/utility.cxx
@@ -16,7 +16,7 @@
#endif
#include <cerrno> // ENOENT
-#include <cstring> // strlen(), str[n]cmp()
+#include <cstring> // strlen(), str[n]cmp(), strchr()
#include <iostream> // cerr
#include <libbuild2/target.hxx>
@@ -1011,4 +1011,48 @@ namespace build2
return r;
}
+
+ void
+ to_stream_quoted (ostream& o, const char* s)
+ {
+ if (strchr (s, '\'') != nullptr)
+ {
+ o << '"';
+
+ for (; *s != '\0'; ++s)
+ {
+ // Escape characters special inside double quotes.
+ //
+ if (strchr ("\\\"", *s) != nullptr)
+ o << '\\';
+
+ o << *s;
+ }
+
+ o << '"';
+ }
+ else
+ o << '\'' << s << '\'';
+ }
+
+ void
+ to_stream_quoted (ostream& o, const string& s, const char* special, bool e)
+ {
+ if ((e && s.empty ()) || s.find_first_of (special) != string::npos)
+ to_stream_quoted (o, s);
+ else
+ o << s;
+ }
+
+ void
+ to_stream_quoted (ostream& o, const strings& ss, const char* special)
+ {
+ for (auto b (ss.begin ()), i (b), e (ss.end ()); i != e; ++i)
+ {
+ if (i != b)
+ o << ' ';
+
+ to_stream_quoted (o, *i, special);
+ }
+ }
}