Path: blob/main/contrib/llvm-project/lldb/source/API/SBEnvironment.cpp
39587 views
//===-- SBEnvironment.cpp -------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/API/SBEnvironment.h"9#include "Utils.h"10#include "lldb/API/SBStringList.h"11#include "lldb/Utility/ConstString.h"12#include "lldb/Utility/Environment.h"13#include "lldb/Utility/Instrumentation.h"1415using namespace lldb;16using namespace lldb_private;1718SBEnvironment::SBEnvironment() : m_opaque_up(new Environment()) {19LLDB_INSTRUMENT_VA(this);20}2122SBEnvironment::SBEnvironment(const SBEnvironment &rhs)23: m_opaque_up(clone(rhs.m_opaque_up)) {24LLDB_INSTRUMENT_VA(this, rhs);25}2627SBEnvironment::SBEnvironment(Environment rhs)28: m_opaque_up(new Environment(std::move(rhs))) {}2930SBEnvironment::~SBEnvironment() = default;3132const SBEnvironment &SBEnvironment::operator=(const SBEnvironment &rhs) {33LLDB_INSTRUMENT_VA(this, rhs);3435if (this != &rhs)36m_opaque_up = clone(rhs.m_opaque_up);37return *this;38}3940size_t SBEnvironment::GetNumValues() {41LLDB_INSTRUMENT_VA(this);4243return m_opaque_up->size();44}4546const char *SBEnvironment::Get(const char *name) {47LLDB_INSTRUMENT_VA(this, name);4849auto entry = m_opaque_up->find(name);50if (entry == m_opaque_up->end()) {51return nullptr;52}53return ConstString(entry->second).AsCString("");54}5556const char *SBEnvironment::GetNameAtIndex(size_t index) {57LLDB_INSTRUMENT_VA(this, index);5859if (index >= GetNumValues())60return nullptr;61return ConstString(std::next(m_opaque_up->begin(), index)->first())62.AsCString("");63}6465const char *SBEnvironment::GetValueAtIndex(size_t index) {66LLDB_INSTRUMENT_VA(this, index);6768if (index >= GetNumValues())69return nullptr;70return ConstString(std::next(m_opaque_up->begin(), index)->second)71.AsCString("");72}7374bool SBEnvironment::Set(const char *name, const char *value, bool overwrite) {75LLDB_INSTRUMENT_VA(this, name, value, overwrite);7677if (overwrite) {78m_opaque_up->insert_or_assign(name, std::string(value));79return true;80}81return m_opaque_up->try_emplace(name, std::string(value)).second;82}8384bool SBEnvironment::Unset(const char *name) {85LLDB_INSTRUMENT_VA(this, name);8687return m_opaque_up->erase(name);88}8990SBStringList SBEnvironment::GetEntries() {91LLDB_INSTRUMENT_VA(this);9293SBStringList entries;94for (const auto &KV : *m_opaque_up) {95entries.AppendString(Environment::compose(KV).c_str());96}97return entries;98}99100void SBEnvironment::PutEntry(const char *name_and_value) {101LLDB_INSTRUMENT_VA(this, name_and_value);102103auto split = llvm::StringRef(name_and_value).split('=');104m_opaque_up->insert_or_assign(split.first.str(), split.second.str());105}106107void SBEnvironment::SetEntries(const SBStringList &entries, bool append) {108LLDB_INSTRUMENT_VA(this, entries, append);109110if (!append)111m_opaque_up->clear();112for (size_t i = 0; i < entries.GetSize(); i++) {113PutEntry(entries.GetStringAtIndex(i));114}115}116117void SBEnvironment::Clear() {118LLDB_INSTRUMENT_VA(this);119120m_opaque_up->clear();121}122123Environment &SBEnvironment::ref() const { return *m_opaque_up; }124125126