crawlserv++  [under development]
Application for crawling and analyzing textual content of websites.
Text.hpp
Go to the documentation of this file.
1 /*
2  *
3  * ---
4  *
5  * Copyright (C) 2020 Anselm Schmidt (ans[ät]ohai.su)
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version in addition to the terms of any
11  * licences already herein identified.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  * ---
22  *
23  * Text.hpp
24  *
25  * Namespace for functions to import/export data from/to text file.
26  *
27  * Created on: May 4, 2019
28  * Author: ans
29  */
30 
31 #ifndef DATA_IMPORTEXPORT_TEXT_H_
32 #define DATA_IMPORTEXPORT_TEXT_H_
33 
34 #include "../../Helper/Strings.hpp"
35 
36 #include <optional> // std::optional
37 #include <queue> // std::queue
38 #include <string> // std::string
39 
42 
43  /*
44  * DECLARATION
45  */
46 
49 
50  std::queue<std::string> importList(
51  const std::string& content,
52  bool skipFirstLine,
53  bool ignoreEmpty
54  );
55 
56  std::string exportList(
57  std::queue<std::string>& list,
58  const std::optional<std::string>& header,
59  bool ignoreEmpty
60  );
61 
63 
64  /*
65  * IMPLEMENTATION
66  */
67 
69 
79  inline std::queue<std::string> importList(
80  const std::string& content,
81  bool skipFirstLine,
82  bool ignoreEmpty
83  ) {
84  // split content into entries
85  std::queue<std::string> result{Helper::Strings::splitToQueue(content, '\n', ignoreEmpty)};
86 
87  // delete header if necessary
88  if(skipFirstLine && !result.empty()) {
89  result.pop();
90  }
91 
92  // return list
93  return result;
94  }
95 
97 
111  inline std::string exportList(
112  std::queue<std::string>& list,
113  const std::optional<std::string>& header,
114  bool ignoreEmpty
115  ) {
116  std::string result;
117 
118  // write header to string if necessary
119  if(header.has_value()) {
120  result.reserve(header.value().size() + 1);
121 
122  result = header.value() + "\n";
123  }
124 
125  // write list entries to string
126  Helper::Strings::join(list, '\n', ignoreEmpty, result);
127 
128  // return string
129  return result;
130  }
131 
132 } /* namespace crawlservpp::Data::ImportExport::Text */
133 
134 #endif /* DATA_IMPORTEXPORT_TEXT_H_ */
std::string exportList(std::queue< std::string > &list, const std::optional< std::string > &header, bool ignoreEmpty)
Exports a list to raw text content, with each line representing a list entry.
Definition: Text.hpp:111
std::string join(const std::vector< std::string > &strings, char delimiter, bool ignoreEmpty)
Concatenates all elements of a vector into a single string.
Definition: Strings.hpp:396
std::queue< std::string > splitToQueue(std::string_view str, char delimiter, bool removeEmpty)
Splits a string into a queue of strings using the given delimiter.
Definition: Strings.hpp:794
std::queue< std::string > importList(const std::string &content, bool skipFirstLine, bool ignoreEmpty)
Imports a list from raw text content, with each line representing a list entry.
Definition: Text.hpp:79
Namespace for importing and exporting raw text.
Definition: Text.hpp:41