1 /*
2 BAREOS® - Backup Archiving REcovery Open Sourced
3
4 Copyright (C) 2018-2019 Bareos GmbH & Co. KG
5
6 This program is Free Software; you can redistribute it and/or
7 modify it under the terms of version three of the GNU Affero General Public
8 License as published by the Free Software Foundation and included
9 in the file LICENSE.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Affero General Public License for more details.
15
16 You should have received a copy of the GNU Affero General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA.
20 */
21
22 #include "bstringlist.h"
23
24 #include <sstream>
25 #include <algorithm>
26 #include <iterator>
27
BStringList()28 BStringList::BStringList() : std::vector<std::string>() { return; }
29
BStringList(const std::string & string_to_split,std::string string_separator)30 BStringList::BStringList(const std::string& string_to_split,
31 std::string string_separator)
32 : std::vector<std::string>()
33 {
34 std::size_t find_pos = 0;
35 std::size_t start_pos = 0;
36
37 do {
38 find_pos = string_to_split.find(string_separator, start_pos);
39 std::string temp;
40 temp.assign(string_to_split, start_pos, find_pos - start_pos);
41 push_back(temp);
42 start_pos = find_pos + string_separator.size();
43 } while (find_pos != std::string::npos);
44 }
45
BStringList(const std::string & string_to_split,char separator)46 BStringList::BStringList(const std::string& string_to_split, char separator)
47 : std::vector<std::string>()
48 {
49 std::stringstream ss(string_to_split);
50 std::string token;
51 while (std::getline(ss, token, separator)) { push_back(token); }
52 }
53
BStringList(const BStringList & other)54 BStringList::BStringList(const BStringList& other) : std::vector<std::string>()
55 {
56 *this = other;
57 }
58
operator =(const BStringList & rhs)59 BStringList& BStringList::operator=(const BStringList& rhs)
60 {
61 std::vector<std::string>::const_iterator it = rhs.cbegin();
62 while (it != rhs.cend()) { push_back(*it++); }
63 return *this;
64 }
65
operator <<(const std::string & rhs)66 BStringList& BStringList::operator<<(const std::string& rhs)
67 {
68 push_back(rhs);
69 return *this;
70 }
71
operator <<(const int & rhs)72 BStringList& BStringList::operator<<(const int& rhs)
73 {
74 push_back(std::to_string(rhs));
75 return *this;
76 }
77
operator <<(const std::vector<std::string> & rhs)78 BStringList& BStringList::operator<<(const std::vector<std::string>& rhs)
79 {
80 Append(rhs);
81 return *this;
82 }
83
operator <<(const char * rhs)84 BStringList& BStringList::operator<<(const char* rhs)
85 {
86 emplace_back(rhs);
87 return *this;
88 }
89
Append(const std::vector<std::string> & vec)90 void BStringList::Append(const std::vector<std::string>& vec)
91 {
92 for (auto str : vec) { push_back(str); }
93 }
94
Append(char character)95 void BStringList::Append(char character)
96 {
97 push_back(std::string(1, character));
98 }
99
Append(const char * str)100 void BStringList::Append(const char* str) { emplace_back(str); }
101
PopFront()102 void BStringList::PopFront()
103 {
104 if (size() >= 1) { erase(begin()); }
105 }
106
Join(char separator) const107 std::string BStringList::Join(char separator) const { return Join(&separator); }
108
Join() const109 std::string BStringList::Join() const { return Join(nullptr); }
110
JoinReadable() const111 std::string BStringList::JoinReadable() const { return Join(' '); }
112
Join(const char * separator) const113 std::string BStringList::Join(const char* separator) const
114 {
115 std::vector<std::string>::const_iterator it = cbegin();
116 std::string output;
117
118 while (it != cend()) {
119 output += *it++;
120 if (separator) {
121 if (it != cend()) { output += *separator; }
122 }
123 }
124 return output;
125 }
126