1// These are useful methods inside the nix library that ought to be exported.
2// Since they are not, copy/paste them here.
3// TODO: Delete these and use the ones in the library as they become available.
4
5#include <nix/config.h> // for nix/globals.hh's reference to SYSTEM
6
7#include "libnix-copy-paste.hh"
8#include <boost/format/alt_sstream.hpp> // for basic_altstringbuf...
9#include <boost/format/alt_sstream_impl.hpp> // for basic_altstringbuf...
10#include <boost/format/format_class.hpp> // for basic_format
11#include <boost/format/format_fwd.hpp> // for format
12#include <boost/format/format_implementation.hpp> // for basic_format::basi...
13#include <boost/optional/optional.hpp> // for get_pointer
14#include <iostream> // for operator<<, basic_...
15#include <nix/types.hh> // for Strings, Error
16#include <string> // for string, basic_string
17
18using boost::format;
19using nix::Error;
20using nix::Strings;
21using std::string;
22
23// From nix/src/libexpr/attr-path.cc
24Strings parseAttrPath(const string & s)
25{
26 Strings res;
27 string cur;
28 string::const_iterator i = s.begin();
29 while (i != s.end()) {
30 if (*i == '.') {
31 res.push_back(cur);
32 cur.clear();
33 } else if (*i == '"') {
34 ++i;
35 while (1) {
36 if (i == s.end())
37 throw Error(format("missing closing quote in selection path '%1%'") % s);
38 if (*i == '"')
39 break;
40 cur.push_back(*i++);
41 }
42 } else
43 cur.push_back(*i);
44 ++i;
45 }
46 if (!cur.empty())
47 res.push_back(cur);
48 return res;
49}
50
51// From nix/src/nix/repl.cc
52bool isVarName(const string & s)
53{
54 if (s.size() == 0)
55 return false;
56 char c = s[0];
57 if ((c >= '0' && c <= '9') || c == '-' || c == '\'')
58 return false;
59 for (auto & i : s)
60 if (!((i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z') || (i >= '0' && i <= '9') || i == '_' || i == '-' ||
61 i == '\''))
62 return false;
63 return true;
64}
65
66// From nix/src/nix/repl.cc
67std::ostream & printStringValue(std::ostream & str, const char * string)
68{
69 str << "\"";
70 for (const char * i = string; *i; i++)
71 if (*i == '\"' || *i == '\\')
72 str << "\\" << *i;
73 else if (*i == '\n')
74 str << "\\n";
75 else if (*i == '\r')
76 str << "\\r";
77 else if (*i == '\t')
78 str << "\\t";
79 else
80 str << *i;
81 str << "\"";
82 return str;
83}