Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 1 | //===-- Environment.cpp -----------------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "lldb/Utility/Environment.h" |
| 10 | |
| 11 | using namespace lldb_private; |
| 12 | |
| 13 | char *Environment::Envp::make_entry(llvm::StringRef Key, |
| 14 | llvm::StringRef Value) { |
| 15 | const size_t size = Key.size() + 1 /*=*/ + Value.size() + 1 /*\0*/; |
Raphael Isemann | 65fdb34 | 2020-01-07 12:13:03 +0100 | [diff] [blame] | 16 | char *Result = static_cast<char *>( |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 17 | Allocator.Allocate(sizeof(char) * size, alignof(char))); |
| 18 | char *Next = Result; |
| 19 | |
| 20 | Next = std::copy(Key.begin(), Key.end(), Next); |
| 21 | *Next++ = '='; |
| 22 | Next = std::copy(Value.begin(), Value.end(), Next); |
| 23 | *Next++ = '\0'; |
| 24 | |
| 25 | return Result; |
| 26 | } |
| 27 | |
| 28 | Environment::Envp::Envp(const Environment &Env) { |
Raphael Isemann | 65fdb34 | 2020-01-07 12:13:03 +0100 | [diff] [blame] | 29 | Data = static_cast<char **>( |
Pavel Labath | 62930e5 | 2018-01-10 11:57:31 +0000 | [diff] [blame] | 30 | Allocator.Allocate(sizeof(char *) * (Env.size() + 1), alignof(char *))); |
| 31 | char **Next = Data; |
| 32 | for (const auto &KV : Env) |
| 33 | *Next++ = make_entry(KV.first(), KV.second); |
| 34 | *Next++ = nullptr; |
| 35 | } |
| 36 | |
| 37 | Environment::Environment(const char *const *Env) { |
| 38 | if (!Env) |
| 39 | return; |
| 40 | while (*Env) |
| 41 | insert(*Env++); |
| 42 | } |
| 43 | |
| 44 | void Environment::insert(const_iterator first, const_iterator last) { |
| 45 | while (first != last) { |
| 46 | try_emplace(first->first(), first->second); |
| 47 | ++first; |
| 48 | } |
| 49 | } |