-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputtools.hpp
More file actions
124 lines (108 loc) · 2.4 KB
/
Copy pathinputtools.hpp
File metadata and controls
124 lines (108 loc) · 2.4 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#ifndef INPUT_TOOLS_H_INCL_GUARD
#define INPUT_TOOLS_H_INCL_GUARD
#include <iostream>
#include <limits>
#include <algorithm>
#include <cctype>
#include <sys/stat.h>
template <typename T>
inline void inputReturn(T &var , std::string prompt = "")
{
while (true)
{
if (!prompt.empty())
{
std::cout << prompt;
}
if (std::cin >> var)
{
std::cout << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return;
}
else
{
std::cout << std::endl << "That value is not valid." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}
inline void inputReturn(std::string &var , std::string prompt = "")
{
while (true)
{
if (!prompt.empty())
{
std::cout << prompt;
}
if (std::getline(std::cin,var))
{
std::cout << std::endl;
//std::cin.clear();
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return;
}
else
{
std::cout << std::endl << "That value is not valid." << std::endl;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
}
template <typename T>
inline bool isPositive(const T var)
{
return var >= 0 ? true : false;
}
template <typename T>
inline bool isZero(const T var)
{
return var == 0 ? true : false;
}
inline void trim(std::string &s, char ch = ' ')
{
const auto begin = s.find_first_not_of(ch);
const auto end = s.find_last_not_of(ch);
if (begin == std::string::npos)
{
return;
}
s = s.substr(begin, (end - begin + 1));
if (s.at(0) == '\t')
trim(s, '\t');
if (s.at(0) == ' ')
trim(s);
}
inline void truc(std::string &s, char ch = '=')
{
const auto begin = s.find_first_of(ch);
const auto end = s.size();
s = s.substr(begin + 1, (end - begin + 1));
}
inline void capitolize(std::string &s)
{
std::transform(s.begin(), s.end(), s.begin(), toupper);
}
inline void formatfilename(std::string &s)
{
std::transform(s.begin(), s.end(), s.begin(), tolower);
s[0] = std::toupper(s[0]);
auto pos = s.find_first_of('_');
if (pos != std::string::npos)
s[pos + 1] = std::toupper(s[pos + 1]);
}
inline bool fileExists(const std::string& f) {
struct stat buf;
if (stat(f.c_str(),&buf) != -1)
{
return true;
}
else
{
return false;
}
}
#endif