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"
11 #include "lldb/API/SBStringList.h"
15 
16 using namespace lldb;
17 using namespace lldb_private;
18 
19 SBEnvironment::SBEnvironment() : m_opaque_up(new Environment()) {
20  LLDB_INSTRUMENT_VA(this);
21 }
22 
24  : m_opaque_up(clone(rhs.m_opaque_up)) {
25  LLDB_INSTRUMENT_VA(this, rhs);
26 }
27 
29  : m_opaque_up(new Environment(std::move(rhs))) {}
30 
32 
34  LLDB_INSTRUMENT_VA(this, rhs);
35 
36  if (this != &rhs)
38  return *this;
39 }
40 
42  LLDB_INSTRUMENT_VA(this);
43 
44  return m_opaque_up->size();
45 }
46 
47 const 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 
57 const 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 
66 const 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 
75 bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {
76  LLDB_INSTRUMENT_VA(this, name, value, overwrite);
77 
78  if (overwrite) {
79  m_opaque_up->insert_or_assign(name, std::string(value));
80  return true;
81  }
82  return m_opaque_up->try_emplace(name, std::string(value)).second;
83 }
84 
85 bool SBEnvironment::Unset(const char *name) {
86  LLDB_INSTRUMENT_VA(this, name);
87 
88  return m_opaque_up->erase(name);
89 }
90 
92  LLDB_INSTRUMENT_VA(this);
93 
94  SBStringList entries;
95  for (const auto &KV : *m_opaque_up) {
96  entries.AppendString(Environment::compose(KV).c_str());
97  }
98  return entries;
99 }
100 
101 void SBEnvironment::PutEntry(const char *name_and_value) {
102  LLDB_INSTRUMENT_VA(this, name_and_value);
103 
104  auto split = llvm::StringRef(name_and_value).split('=');
105  m_opaque_up->insert_or_assign(split.first.str(), split.second.str());
106 }
107 
108 void SBEnvironment::SetEntries(const SBStringList &entries, bool append) {
109  LLDB_INSTRUMENT_VA(this, entries, append);
110 
111  if (!append)
112  m_opaque_up->clear();
113  for (size_t i = 0; i < entries.GetSize(); i++) {
114  PutEntry(entries.GetStringAtIndex(i));
115  }
116 }
117 
119  LLDB_INSTRUMENT_VA(this);
120 
121  m_opaque_up->clear();
122 }
123 
lldb::SBStringList
Definition: SBStringList.h:16
Instrumentation.h
lldb::SBStringList::AppendString
void AppendString(const char *str)
Definition: SBStringList.cpp:58
lldb::SBStringList::GetSize
uint32_t GetSize() const
Definition: SBStringList.cpp:96
lldb::SBEnvironment::operator=
const lldb::SBEnvironment & operator=(const lldb::SBEnvironment &rhs)
Definition: SBEnvironment.cpp:33
lldb::SBEnvironment::ref
lldb_private::Environment & ref() const
Definition: SBEnvironment.cpp:124
SBStringList.h
lldb_private::ConstString::AsCString
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:192
lldb::SBEnvironment::Clear
void Clear()
Delete all the environment variables.
Definition: SBEnvironment.cpp:118
lldb::SBEnvironment::SBEnvironment
SBEnvironment()
Definition: SBEnvironment.cpp:19
lldb::SBEnvironment::Get
const char * Get(const char *name)
Return the value of a given environment variable.
Definition: SBEnvironment.cpp:47
lldb::SBStringList::GetStringAtIndex
const char * GetStringAtIndex(size_t idx)
Definition: SBStringList.cpp:105
lldb_private::clone
std::unique_ptr< T > clone(const std::unique_ptr< T > &src)
Definition: Utils.h:17
Utils.h
lldb::SBEnvironment::GetEntries
SBStringList GetEntries()
Return all environment variables contained in this object.
Definition: SBEnvironment.cpp:91
lldb_private::ConstString
Definition: ConstString.h:39
lldb::SBEnvironment::~SBEnvironment
~SBEnvironment()
lldb::SBEnvironment
Definition: SBEnvironment.h:16
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb::SBEnvironment::Unset
bool Unset(const char *name)
Unset an environment variable if exists.
Definition: SBEnvironment.cpp:85
lldb::SBEnvironment::GetValueAtIndex
const char * GetValueAtIndex(size_t index)
Return the value of the environment variable at a given index from the internal list of environment v...
Definition: SBEnvironment.cpp:66
lldb_private::Environment
Definition: Environment.h:18
lldb::SBEnvironment::m_opaque_up
std::unique_ptr< lldb_private::Environment > m_opaque_up
Definition: SBEnvironment.h:132
lldb::SBEnvironment::PutEntry
void PutEntry(const char *name_and_value)
Add or replace an existing environment variable.
Definition: SBEnvironment.cpp:101
Environment.h
SBEnvironment.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb::SBEnvironment::GetNameAtIndex
const char * GetNameAtIndex(size_t index)
Return the name of the environment variable at a given index from the internal list of environment va...
Definition: SBEnvironment.cpp:57
LLDB_INSTRUMENT_VA
#define LLDB_INSTRUMENT_VA(...)
Definition: Instrumentation.h:98
ConstString.h
lldb::SBEnvironment::SetEntries
void SetEntries(const SBStringList &entries, bool append)
Update this object with the given environment variables.
Definition: SBEnvironment.cpp:108
lldb::SBEnvironment::Set
bool Set(const char *name, const char *value, bool overwrite)
Set the value of a given environment variable.
Definition: SBEnvironment.cpp:75
lldb
Definition: SBAddress.h:15
lldb::SBEnvironment::GetNumValues
size_t GetNumValues()
Definition: SBEnvironment.cpp:41