1 //===-- SBEnvironment.cpp -------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/API/SBEnvironment.h"
10 #include "SBReproducerPrivate.h"
12 #include "lldb/API/SBStringList.h"
13 #include "lldb/Utility/ConstString.h"
14 #include "lldb/Utility/Environment.h"
17 using namespace lldb_private;
19 SBEnvironment::SBEnvironment() : m_opaque_up(new Environment()) {
20 LLDB_RECORD_CONSTRUCTOR_NO_ARGS(SBEnvironment);
23 SBEnvironment::SBEnvironment(const SBEnvironment &rhs)
24 : m_opaque_up(clone(rhs.m_opaque_up)) {
25 LLDB_RECORD_CONSTRUCTOR(SBEnvironment, (const lldb::SBEnvironment &), rhs);
28 SBEnvironment::SBEnvironment(Environment rhs)
29 : m_opaque_up(new Environment(std::move(rhs))) {}
31 SBEnvironment::~SBEnvironment() = default;
33 const SBEnvironment &SBEnvironment::operator=(const SBEnvironment &rhs) {
34 LLDB_RECORD_METHOD(const lldb::SBEnvironment &,
35 SBEnvironment, operator=,(const lldb::SBEnvironment &),
39 m_opaque_up = clone(rhs.m_opaque_up);
40 return LLDB_RECORD_RESULT(*this);
43 size_t SBEnvironment::GetNumValues() {
44 LLDB_RECORD_METHOD_NO_ARGS(size_t, SBEnvironment, GetNumValues);
46 return m_opaque_up->size();
49 const char *SBEnvironment::Get(const char *name) {
50 LLDB_RECORD_METHOD(const char *, SBEnvironment, Get, (const char *), name);
52 auto entry = m_opaque_up->find(name);
53 if (entry == m_opaque_up->end()) {
56 return ConstString(entry->second).AsCString("");
59 const char *SBEnvironment::GetNameAtIndex(size_t index) {
60 LLDB_RECORD_METHOD(const char *, SBEnvironment, GetNameAtIndex, (size_t),
63 if (index >= GetNumValues())
65 return ConstString(std::next(m_opaque_up->begin(), index)->first())
69 const char *SBEnvironment::GetValueAtIndex(size_t index) {
70 LLDB_RECORD_METHOD(const char *, SBEnvironment, GetValueAtIndex, (size_t),
73 if (index >= GetNumValues())
75 return ConstString(std::next(m_opaque_up->begin(), index)->second)
79 bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
80 LLDB_RECORD_METHOD(bool, SBEnvironment, Set,
81 (const char *, const char *, bool), name, value,
85 m_opaque_up->insert_or_assign(name, std::string(value));
88 return m_opaque_up->try_emplace(name, std::string(value)).second;
91 bool SBEnvironment::Unset(const char *name) {
92 LLDB_RECORD_METHOD(bool, SBEnvironment, Unset, (const char *), name);
94 return m_opaque_up->erase(name);
97 SBStringList SBEnvironment::GetEntries() {
98 LLDB_RECORD_METHOD_NO_ARGS(lldb::SBStringList, SBEnvironment, GetEntries);
100 SBStringList entries;
101 for (const auto &KV : *m_opaque_up) {
102 entries.AppendString(Environment::compose(KV).c_str());
104 return LLDB_RECORD_RESULT(entries);
107 void SBEnvironment::PutEntry(const char *name_and_value) {
108 LLDB_RECORD_METHOD(void, SBEnvironment, PutEntry, (const char *),
111 auto split = llvm::StringRef(name_and_value).split('=');
112 m_opaque_up->insert_or_assign(split.first.str(), split.second.str());
115 void SBEnvironment::SetEntries(const SBStringList &entries, bool append) {
116 LLDB_RECORD_METHOD(void, SBEnvironment, SetEntries,
117 (const lldb::SBStringList &, bool), entries, append);
120 m_opaque_up->clear();
121 for (size_t i = 0; i < entries.GetSize(); i++) {
122 PutEntry(entries.GetStringAtIndex(i));
126 void SBEnvironment::Clear() {
127 LLDB_RECORD_METHOD_NO_ARGS(void, SBEnvironment, Clear);
129 m_opaque_up->clear();
132 Environment &SBEnvironment::ref() const { return *m_opaque_up; }
134 namespace lldb_private {
136 template <> void RegisterMethods<SBEnvironment>(Registry &R) {
137 LLDB_REGISTER_CONSTRUCTOR(SBEnvironment, ());
138 LLDB_REGISTER_CONSTRUCTOR(SBEnvironment, (const lldb::SBEnvironment &));
139 LLDB_REGISTER_METHOD(const lldb::SBEnvironment &,
140 SBEnvironment, operator=,(const lldb::SBEnvironment &));
141 LLDB_REGISTER_METHOD(size_t, SBEnvironment, GetNumValues, ());
142 LLDB_REGISTER_METHOD(const char *, SBEnvironment, Get, (const char *));
143 LLDB_REGISTER_METHOD(const char *, SBEnvironment, GetNameAtIndex, (size_t));
144 LLDB_REGISTER_METHOD(const char *, SBEnvironment, GetValueAtIndex, (size_t));
145 LLDB_REGISTER_METHOD(bool, SBEnvironment, Set,
146 (const char *, const char *, bool));
147 LLDB_REGISTER_METHOD(bool, SBEnvironment, Unset, (const char *));
148 LLDB_REGISTER_METHOD(lldb::SBStringList, SBEnvironment, GetEntries, ());
149 LLDB_REGISTER_METHOD(void, SBEnvironment, PutEntry, (const char *));
150 LLDB_REGISTER_METHOD(void, SBEnvironment, SetEntries,
151 (const lldb::SBStringList &, bool));
152 LLDB_REGISTER_METHOD(void, SBEnvironment, Clear, ());
155 } // namespace lldb_private