This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharpp.hpp
71 lines (58 loc) · 1.98 KB
/
charpp.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2013-2017 Dimitry Ishenko
// Contact: dimitry (dot) ishenko (at) (gee) mail (dot) com
//
// Distributed under the GNU GPL license. See the LICENSE.md file for details.
////////////////////////////////////////////////////////////////////////////////
#ifndef PGM_CHARPP_HPP
#define PGM_CHARPP_HPP
////////////////////////////////////////////////////////////////////////////////
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <memory>
#include <string>
////////////////////////////////////////////////////////////////////////////////
namespace pgm
{
////////////////////////////////////////////////////////////////////////////////
struct charpp_delete
{
void operator()(char* pp[])
{
for(auto pi = pp; *pi; ++pi) std::free(*pi);
std::free(pp);
}
};
using charpp = std::unique_ptr<char*[], charpp_delete>;
////////////////////////////////////////////////////////////////////////////////
template<typename Iter>
inline charpp
make_charpp(Iter first, Iter last)
{
auto np = static_cast<char**>(
std::calloc(std::distance(first, last) + 1, sizeof(char*))
);
if(!np) throw std::bad_alloc();
charpp cpp(np);
for(Iter it = first; it != last; ++it) *np++ = ::strdup(it->data());
return cpp;
}
////////////////////////////////////////////////////////////////////////////////
template<typename Iter>
inline charpp
make_charpp(const std::string& first, Iter second, Iter last)
{
auto np = static_cast<char**>(
std::calloc(1 + std::distance(second, last) + 1, sizeof(char*))
);
if(!np) throw std::bad_alloc();
charpp cpp(np);
*np++ = ::strdup(first.data());
for(Iter it = second; it != last; ++it) *np++ = ::strdup(it->data());
return cpp;
}
////////////////////////////////////////////////////////////////////////////////
}
////////////////////////////////////////////////////////////////////////////////
#endif