1 //===-- Environment.h -------------------------------------------*- C++ -*-===// 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 #ifndef LLDB_UTILITY_ENVIRONMENT_H 10 #define LLDB_UTILITY_ENVIRONMENT_H 11 12 #include "llvm/ADT/StringMap.h" 13 #include "llvm/Support/Allocator.h" 14 #include "llvm/Support/FormatProviders.h" 15 16 namespace lldb_private { 17 18 class Environment : private llvm::StringMap<std::string> { 19 using Base = llvm::StringMap<std::string>; 20 21 public: 22 class Envp { 23 public: 24 Envp(Envp &&RHS) = default; 25 Envp &operator=(Envp &&RHS) = default; 26 27 char *const *get() const { return Data; } 28 operator char *const *() const { return get(); } 29 30 private: 31 explicit Envp(const Environment &Env); 32 char *make_entry(llvm::StringRef Key, llvm::StringRef Value); 33 Envp(const Envp &) = delete; 34 Envp &operator=(const Envp &) = delete; 35 friend class Environment; 36 37 llvm::BumpPtrAllocator Allocator; 38 char **Data; 39 }; 40 41 using Base::const_iterator; 42 using Base::iterator; 43 using Base::value_type; 44 45 using Base::begin; 46 using Base::clear; 47 using Base::count; 48 using Base::empty; 49 using Base::end; 50 using Base::erase; 51 using Base::find; 52 using Base::insert; 53 using Base::lookup; 54 using Base::size; 55 using Base::try_emplace; 56 using Base::operator[]; 57 58 Environment() : Base() {} 59 Environment(const Environment &RHS) : Base(RHS) {} 60 Environment(Environment &&RHS) : Base(std::move(RHS)) {} 61 Environment(char *const *Env) 62 : Environment(const_cast<const char *const *>(Env)) {} 63 Environment(const char *const *Env); 64 65 Environment &operator=(Environment RHS) { 66 Base::operator=(std::move(RHS)); 67 return *this; 68 } 69 70 std::pair<iterator, bool> insert(llvm::StringRef KeyEqValue) { 71 return insert(KeyEqValue.split('=')); 72 } 73 74 void insert(const_iterator first, const_iterator last); 75 76 Envp getEnvp() const { return Envp(*this); } 77 78 static std::string compose(const value_type &KeyValue) { 79 return (KeyValue.first() + "=" + KeyValue.second).str(); 80 } 81 }; 82 83 } // namespace lldb_private 84 85 namespace llvm { 86 template <> struct format_provider<lldb_private::Environment> { 87 static void format(const lldb_private::Environment &Env, raw_ostream &Stream, 88 StringRef Style) { 89 for (const auto &KV : Env) 90 Stream << "env[" << KV.first() << "] = " << KV.second << "\n"; 91 } 92 }; 93 } // namespace llvm 94 95 #endif // #ifndef LLDB_UTILITY_ENVIRONMENT_H 96