LLDB mainline
SBEnvironment.cpp
Go to the documentation of this file.
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
10#include "Utils.h"
16
17using namespace lldb;
18using namespace lldb_private;
19
23
28
31
33
35 LLDB_INSTRUMENT_VA(this, rhs);
36
37 if (this != &rhs)
39 return *this;
40}
41
44
45 return m_opaque_up->size();
46}
47
48const char *SBEnvironment::Get(const char *name) {
49 LLDB_INSTRUMENT_VA(this, name);
50
51 auto entry = m_opaque_up->find(name);
52 if (entry == m_opaque_up->end()) {
53 return nullptr;
54 }
55 return StringPool::GetSystemPool().Intern(entry->second);
56}
57
58const char *SBEnvironment::GetNameAtIndex(size_t index) {
59 LLDB_INSTRUMENT_VA(this, index);
60
61 if (index >= GetNumValues())
62 return nullptr;
64 std::next(m_opaque_up->begin(), index)->first());
65}
66
67const char *SBEnvironment::GetValueAtIndex(size_t index) {
68 LLDB_INSTRUMENT_VA(this, index);
69
70 if (index >= GetNumValues())
71 return nullptr;
73 std::next(m_opaque_up->begin(), index)->second);
74}
75
76bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
77 LLDB_INSTRUMENT_VA(this, name, value, overwrite);
78
79 llvm::StringRef name_ref{name};
80 if (name_ref.trim().empty())
81 return false;
82
83 llvm::StringRef value_ref{value};
84 if (overwrite) {
85 m_opaque_up->insert_or_assign(name_ref, value_ref.str());
86 return true;
87 }
88 return m_opaque_up->try_emplace(name_ref, value_ref.str()).second;
89}
90
91bool SBEnvironment::Unset(const char *name) {
92 LLDB_INSTRUMENT_VA(this, name);
93
94 llvm::StringRef name_ref{name};
95 if (name_ref.trim().empty())
96 return false;
97
98 return m_opaque_up->erase(name_ref);
99}
100
102 LLDB_INSTRUMENT_VA(this);
103
104 SBStringList entries;
105 for (const auto &KV : *m_opaque_up) {
106 entries.AppendString(Environment::compose(KV).c_str());
107 }
108 return entries;
109}
110
111void SBEnvironment::PutEntry(const char *name_and_value) {
112 LLDB_INSTRUMENT_VA(this, name_and_value);
113
114 auto split = llvm::StringRef(name_and_value).split('=');
115 m_opaque_up->insert_or_assign(split.first.str(), split.second.str());
116}
117
118void SBEnvironment::SetEntries(const SBStringList &entries, bool append) {
119 LLDB_INSTRUMENT_VA(this, entries, append);
120
121 if (!append)
122 m_opaque_up->clear();
123 for (size_t i = 0; i < entries.GetSize(); i++) {
124 PutEntry(entries.GetStringAtIndex(i));
125 }
126}
127
129 LLDB_INSTRUMENT_VA(this);
130
131 m_opaque_up->clear();
132}
133
#define LLDB_INSTRUMENT_VA(...)
const char * GetValueAtIndex(size_t index)
Return the value of the environment variable at a given index from the internal list of environment v...
void PutEntry(const char *name_and_value)
Add or replace an existing environment variable.
bool Set(const char *name, const char *value, bool overwrite)
Set the value of a given environment variable.
void Clear()
Delete all the environment variables.
SBStringList GetEntries()
Return all environment variables contained in this object.
const char * GetNameAtIndex(size_t index)
Return the name of the environment variable at a given index from the internal list of environment va...
bool Unset(const char *name)
Unset an environment variable if exists.
lldb_private::Environment & ref() const
std::unique_ptr< lldb_private::Environment > m_opaque_up
const lldb::SBEnvironment & operator=(const lldb::SBEnvironment &rhs)
const char * Get(const char *name)
Return the value of a given environment variable.
void SetEntries(const SBStringList &entries, bool append)
Update this object with the given environment variables.
uint32_t GetSize() const
void AppendString(const char *str)
const char * GetStringAtIndex(size_t idx)
static std::string compose(const value_type &KeyValue)
Definition Environment.h:81
const char * Intern(llvm::StringRef str) const
Definition StringPool.h:78
static StringPoolRef GetSystemPool()
The system pool. Only valid between Initialize and Terminate.
A class that represents a running process on the host machine.
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition Utils.h:17