aboutsummaryrefslogtreecommitdiff
path: root/bbot/variable.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-03-29 00:45:30 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-04-04 13:35:14 +0300
commitdd973d03bf5f3f439dcdacbb22470105e66e698a (patch)
tree6856ab313c75f3459df80511c98d3f788bf1c22b /bbot/variable.cxx
parentc72bbd5d04084547c53e9593af4d76d9e5135a53 (diff)
Implement manifests and build_config
Diffstat (limited to 'bbot/variable.cxx')
-rw-r--r--bbot/variable.cxx94
1 files changed, 94 insertions, 0 deletions
diff --git a/bbot/variable.cxx b/bbot/variable.cxx
new file mode 100644
index 0000000..a25b007
--- /dev/null
+++ b/bbot/variable.cxx
@@ -0,0 +1,94 @@
+// file : bbot/variable.cxx -*- C++ -*-
+// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd
+// license : MIT; see accompanying LICENSE file
+
+#include <bbot/variable>
+
+#include <utility> // move()
+
+using namespace std;
+
+namespace bbot
+{
+ variable::
+ variable (string v): string (move (v))
+ {
+ // Scan the string untill the end to check that the quoting is terminated.
+ // We will also make sure that the name doesn't contain spaces and the
+ // value is provided.
+ //
+ char quoting ('\0'); // Current quoting mode, can be used as bool.
+ bool name (true); // True while we are parsing the variable name.
+
+ auto b (cbegin ());
+ auto i (b);
+
+ auto bad_variable = [&b, &i] (const string& d)
+ {
+ throw invalid_variable (i - b, d);
+ };
+
+ for (auto e (cend ()); i != e; ++i)
+ {
+ char c (*i);
+
+ if (!quoting)
+ {
+ if (c == '"' || c == '\'') // Begin of quoted string,
+ {
+ quoting = c;
+ continue;
+ }
+ }
+ else if (c == quoting) // End of quoted string,
+ {
+ quoting = '\0';
+ continue;
+ }
+
+ if (name)
+ {
+ if (c == ' ' || c == '\t')
+ bad_variable ("expected variable assignment");
+ else if (c == '=')
+ name = false;
+ }
+ }
+
+ if (quoting)
+ bad_variable ("unterminated quoted string");
+
+ if (name)
+ bad_variable ("no variable value");
+ }
+
+ string variable::
+ unquoted () const
+ {
+ string r;
+ char quoting ('\0'); // Current quoting mode, can be used as bool.
+
+ for (auto i (cbegin ()), e (cend ()); i != e; ++i)
+ {
+ char c (*i);
+
+ if (!quoting)
+ {
+ if (c == '"' || c == '\'') // Begin of quoted string.
+ {
+ quoting = c;
+ continue;
+ }
+ }
+ else if (c == quoting) // End of quoted string.
+ {
+ quoting = '\0';
+ continue;
+ }
+
+ r += c;
+ }
+
+ return r;
+ }
+}