1 //===-- SBEnvironment.cpp -------------------------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/API/SBEnvironment.h" 10 #include "Utils.h" 11 #include "lldb/API/SBStringList.h" 12 #include "lldb/Utility/ConstString.h" 13 #include "lldb/Utility/Environment.h" 14 15 using namespace lldb; 16 using namespace lldb_private; 17 18 /// This class is highly mutable, therefore we don't reproducers. 19 20 SBEnvironment::SBEnvironment() : m_opaque_up(new Environment()) {} 21 22 SBEnvironment::SBEnvironment(const SBEnvironment &rhs) 23 : m_opaque_up(clone(rhs.m_opaque_up)) {} 24 25 SBEnvironment::SBEnvironment(Environment rhs) 26 : m_opaque_up(new Environment(std::move(rhs))) {} 27 28 SBEnvironment::~SBEnvironment() = default; 29 30 const SBEnvironment &SBEnvironment::operator=(const SBEnvironment &rhs) { 31 if (this != &rhs) 32 m_opaque_up = clone(rhs.m_opaque_up); 33 return *this; 34 } 35 36 size_t SBEnvironment::GetNumValues() { 37 return m_opaque_up->size(); 38 } 39 40 const char *SBEnvironment::Get(const char *name) { 41 auto entry = m_opaque_up->find(name); 42 if (entry == m_opaque_up->end()) { 43 return nullptr; 44 } 45 return ConstString(entry->second).AsCString(""); 46 } 47 48 const char *SBEnvironment::GetNameAtIndex(size_t index) { 49 if (index >= GetNumValues()) 50 return nullptr; 51 return ConstString(std::next(m_opaque_up->begin(), index)->first()) 52 .AsCString(""); 53 } 54 55 const char *SBEnvironment::GetValueAtIndex(size_t index) { 56 if (index >= GetNumValues()) 57 return nullptr; 58 return ConstString(std::next(m_opaque_up->begin(), index)->second) 59 .AsCString(""); 60 } 61 62 bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) { 63 if (overwrite) { 64 m_opaque_up->insert_or_assign(name, std::string(value)); 65 return true; 66 } 67 return m_opaque_up->try_emplace(name, std::string(value)).second; 68 } 69 70 bool SBEnvironment::Unset(const char *name) { 71 return m_opaque_up->erase(name); 72 } 73 74 SBStringList SBEnvironment::GetEntries() { 75 SBStringList entries; 76 for (const auto &KV : *m_opaque_up) { 77 entries.AppendString(Environment::compose(KV).c_str()); 78 } 79 return entries; 80 } 81 82 void SBEnvironment::PutEntry(const char *name_and_value) { 83 auto split = llvm::StringRef(name_and_value).split('='); 84 m_opaque_up->insert_or_assign(split.first.str(), split.second.str()); 85 } 86 87 void SBEnvironment::SetEntries(const SBStringList &entries, bool append) { 88 if (!append) 89 m_opaque_up->clear(); 90 for (size_t i = 0; i < entries.GetSize(); i++) { 91 PutEntry(entries.GetStringAtIndex(i)); 92 } 93 } 94 95 void SBEnvironment::Clear() { 96 m_opaque_up->clear(); 97 } 98 99 Environment &SBEnvironment::ref() const { return *m_opaque_up; } 100