Chi-Tech
chi_misc_utils.cc
Go to the documentation of this file.
1#include "chi_utils.h"
2
3#include <fstream>
4
6
7namespace chi
8{
9// #################################################################
10std::string StringLTrim(const std::string& s)
11{
12 size_t start = s.find_first_not_of(WHITESPACE);
13 return (start == std::string::npos) ? "" : s.substr(start);
14}
15
16// #################################################################
17std::string StringRTrim(const std::string& s)
18{
19 size_t end = s.find_last_not_of(WHITESPACE);
20 return (end == std::string::npos) ? "" : s.substr(0, end + 1);
21}
22
23// #################################################################
24std::string StringTrim(const std::string& s)
25{
26 return StringRTrim(StringLTrim(s));
27}
28
29// #################################################################
30std::vector<std::string> StringSplit(const std::string& input,
31 const std::string& delim /*=" "*/)
32{
33 constexpr size_t NPOS = std::string::npos;
34 std::vector<std::string> output;
35
36 std::string remainder = input;
37 size_t first_scope = remainder.find_first_of(delim);
38
39 while (first_scope != NPOS)
40 {
41 if (first_scope != 0) output.push_back(remainder.substr(0, first_scope));
42
43 remainder = remainder.substr(first_scope + delim.size(), NPOS);
44 first_scope = remainder.find_first_of(delim);
45 }
46 output.push_back(remainder);
47
48 return output;
49}
50
51// #################################################################
52std::string StringUpToFirstReverse(const std::string& input,
53 const std::string& search_string)
54{
55 constexpr size_t NPOS = std::string::npos;
56 std::string output = input;
57 const size_t last_scope = input.find_last_of(search_string);
58 if (last_scope != NPOS)
59 output = input.substr(last_scope + search_string.size(), NPOS);
60
61 return output;
62}
63
64void AssertReadibleFile(const std::string& file_name)
65{
66 std::ifstream file(file_name.c_str(), std::ifstream::in);
68 file.fail(),
69 "Failed to open file \"" + file_name +
70 "\"."
71 "Either the file does not exist or you do not have read permissions.");
72
73 file.close();
74}
75} // namespace chi_misc_utils
#define ChiLogicalErrorIf(condition, message)
std::string StringUpToFirstReverse(const std::string &input, const std::string &search_string)
const std::string WHITESPACE
Definition: chi_utils.h:20
void AssertReadibleFile(const std::string &file_name)
std::string StringLTrim(const std::string &s)
std::vector< std::string > StringSplit(const std::string &input, const std::string &delim)
std::string StringRTrim(const std::string &s)
std::string StringTrim(const std::string &s)