Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 1 | //===-- Environment.cpp -----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Utility/Environment.h" |
| 11 | |
| 12 | using namespace lldb_private; |
| 13 | |
| 14 | char *Environment::Envp::make_entry(llvm::StringRef Key, |
| 15 | llvm::StringRef Value) { |
| 16 | const size_t size = Key.size() + 1 /*=*/ + Value.size() + 1 /*\0*/; |
| 17 | char *Result = reinterpret_cast<char *>( |
| 18 | Allocator.Allocate(sizeof(char) * size, alignof(char))); |
| 19 | char *Next = Result; |
| 20 | |
| 21 | Next = std::copy(Key.begin(), Key.end(), Next); |
| 22 | *Next++ = '='; |
| 23 | Next = std::copy(Value.begin(), Value.end(), Next); |
| 24 | *Next++ = '\0'; |
| 25 | |
| 26 | return Result; |
| 27 | } |
| 28 | |
| 29 | Environment::Envp::Envp(const Environment &Env) { |
| 30 | Data = reinterpret_cast<char **>( |
| 31 | Allocator.Allocate(sizeof(char *) * (Env.size() + 1), alignof(char *))); |
| 32 | char **Next = Data; |
| 33 | for (const auto &KV : Env) |
| 34 | *Next++ = make_entry(KV.first(), KV.second); |
| 35 | *Next++ = nullptr; |
| 36 | } |
| 37 | |
| 38 | Environment::Environment(const char *const *Env) { |
| 39 | if (!Env) |
| 40 | return; |
| 41 | while (*Env) |
| 42 | insert(*Env++); |
| 43 | } |
| 44 | |
| 45 | void Environment::insert(const_iterator first, const_iterator last) { |
| 46 | while (first != last) { |
| 47 | try_emplace(first->first(), first->second); |
| 48 | ++first; |
| 49 | } |
| 50 | } |