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"
15
16using namespace lldb;
17using namespace lldb_private;
18
22
27
30
32
34 LLDB_INSTRUMENT_VA(this, rhs);
35
36 if (this != &rhs)
38 return *this;
39}
40
43
44 return m_opaque_up->size();
45}
46
47const char *SBEnvironment::Get(const char *name) {
48 LLDB_INSTRUMENT_VA(this, name);
49
50 auto entry = m_opaque_up->find(name);
51 if (entry == m_opaque_up->end()) {
52 return nullptr;
53 }
54 return ConstString(entry->second).AsCString("");
55}
56
57const char *SBEnvironment::GetNameAtIndex(size_t index) {
58 LLDB_INSTRUMENT_VA(this, index);
59
60 if (index >= GetNumValues())
61 return nullptr;
62 return ConstString(std::next(m_opaque_up->begin(), index)->first())
63 .AsCString("");
64}
65
66const char *SBEnvironment::GetValueAtIndex(size_t index) {
67 LLDB_INSTRUMENT_VA(this, index);
68
69 if (index >= GetNumValues())
70 return nullptr;
71 return ConstString(std::next(m_opaque_up->begin(), index)->second)
72 .AsCString("");
73}
74
75bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
76 LLDB_INSTRUMENT_VA(this, name, value, overwrite);
77
78 llvm::StringRef name_ref{name};
79 if (name_ref.trim().empty())
80 return false;
81
82 llvm::StringRef value_ref{value};
83 if (overwrite) {
84 m_opaque_up->insert_or_assign(name_ref, value_ref.str());
85 return true;
86 }
87 return m_opaque_up->try_emplace(name_ref, value_ref.str()).second;
88}
89
90bool SBEnvironment::Unset(const char *name) {
91 LLDB_INSTRUMENT_VA(this, name);
92
93 llvm::StringRef name_ref{name};
94 if (name_ref.trim().empty())
95 return false;
96
97 return m_opaque_up->erase(name_ref);
98}
99
101 LLDB_INSTRUMENT_VA(this);
102
103 SBStringList entries;
104 for (const auto &KV : *m_opaque_up) {
105 entries.AppendString(Environment::compose(KV).c_str());
106 }
107 return entries;
108}
109
110void SBEnvironment::PutEntry(const char *name_and_value) {
111 LLDB_INSTRUMENT_VA(this, name_and_value);
112
113 auto split = llvm::StringRef(name_and_value).split('=');
114 m_opaque_up->insert_or_assign(split.first.str(), split.second.str());
115}
116
117void SBEnvironment::SetEntries(const SBStringList &entries, bool append) {
118 LLDB_INSTRUMENT_VA(this, entries, append);
119
120 if (!append)
121 m_opaque_up->clear();
122 for (size_t i = 0; i < entries.GetSize(); i++) {
123 PutEntry(entries.GetStringAtIndex(i));
124 }
125}
126
128 LLDB_INSTRUMENT_VA(this);
129
130 m_opaque_up->clear();
131}
132
#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)
A uniqued constant string class.
Definition ConstString.h:40
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
static std::string compose(const value_type &KeyValue)
Definition Environment.h:81
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