nsnake
Classic snake game for the terminal
Utils.hpp
1 #ifndef UTILS_H_DEFINED
2 #define UTILS_H_DEFINED
3 
4 #include <string>
5 #include <vector>
6 #include <sys/stat.h> // mkdir() and off_t
7 #include <sstream>
8 #include <stdexcept>
9 
12 namespace Utils
13 {
15  namespace Random
16  {
18  void seed();
19 
21  int between(int min, int max);
22 
24  bool boolean();
25 
28  bool booleanWithChance(float percent);
29  };
30 
31  namespace Time
32  {
34  void delay_ms(int delay);
35  };
36 
44  namespace File
45  {
49  bool exists(std::string path);
50 
55  off_t size(std::string path);
56 
59  void mkdir_p(std::string path);
60 
63  void rm_rf(std::string path);
64 
67  void rm_f(std::string path);
68 
74  bool create(std::string path);
75 
80  void write(std::string path, std::string contents);
81 
85  bool isDirectory(std::string path);
86 
91  bool isFile(std::string path);
92 
98  std::vector<std::string> ls(std::string path);
99 
105  std::string getHome();
106 
108  std::string getUser();
109 
120  std::string basename(std::string path);
121 
127  std::string dropBasename(std::string path);
128 
139  std::string extension(std::string path);
140 
144  std::string dropExtension(std::string path);
145  };
146 
147  namespace String
148  {
155  template <typename T>
156  inline std::string toString(T const& x)
157  {
158  std::ostringstream o;
159  if (!(o << x))
160  throw std::runtime_error("Utils::String::toString");
161 
162  return o.str();
163  }
164 
171  template <typename T>
172  inline void convert(std::string const& s, T& x,
173  bool failIfLeftOverChars=true)
174  {
175  std::istringstream i(s);
176  char c;
177  if (!(i >> x) || (failIfLeftOverChars && i.get(c)))
178  throw std::runtime_error("Utils::String::convert");
179  }
180 
183  template <typename T>
184  inline T to(std::string const& s,
185  bool failIfLeftOverChars=true)
186  {
187  T x;
188  convert(s, x, failIfLeftOverChars);
189  return x;
190  }
191 
196  char back(std::string& str);
197 
201  char front(std::string& str);
202 
206  void pop_back(std::string* str);
207 
210  std::string pop_back(std::string& str);
211 
213  std::string ltrim(const std::string &str);
214 
216  std::string rtrim(const std::string& str);
217 
219  std::string trim(const std::string& str);
220 
225  std::vector<std::string> split(const std::string& str, char delim);
226 
230  bool caseInsensitiveSmallerChar(const char x, const char y);
231 
244  bool caseInsensitiveSmallerString(const std::string &a, const std::string &b);
245  };
246 
248  namespace Base64
249  {
251  std::string encode(std::string str);
252 
254  std::string decode(std::string const& s);
255  };
256 };
257 
258 // Useful #defines collected over the years.
259 
261 #define SAFE_DELETE(pointer) \
262  { \
263  if (pointer) \
264  { \
265  delete(pointer); \
266  pointer = NULL; \
267  } \
268  }
269 
272 #define UNUSED(thing) \
273  { \
274  (void)(thing); \
275  \
276  }
277 
278 #endif //UTILS_H_DEFINED
279 
bool boolean()
Random boolean.
Definition: Utils.cpp:56
std::string dropBasename(std::string path)
Returns the full pathname up to the last component.
Definition: Utils.cpp:280
std::string getHome()
Gets the full path of the home directory for the user running this program.
Definition: Utils.cpp:235
std::string encode(std::string str)
Transforms #str into a Base64 equivalent.
Definition: Utils.cpp:439
off_t size(std::string path)
Returns the file size of #path in bytes.
Definition: Utils.cpp:88
void seed()
Must be called before any of those.
Definition: Utils.cpp:41
bool isFile(std::string path)
Tells if #path is a regular file (not a directory, socket, FIFO device or whatever).
Definition: Utils.cpp:190
std::string extension(std::string path)
Returns the extension of a file.
Definition: Utils.cpp:294
bool isDirectory(std::string path)
Tells if #path is a directory.
Definition: Utils.cpp:179
std::string decode(std::string const &s)
Transforms a Base64-encoded #str into it&#39;s regular string equivalent.
Definition: Utils.cpp:488
bool create(std::string path)
Creates empty file #path.
Definition: Utils.cpp:164
void mkdir_p(std::string path)
Creates #path directory hierarchy recursively, just like UNIX command mkdir -p.
Definition: Utils.cpp:97
std::vector< std::string > ls(std::string path)
Lists all files withing #path.
Definition: Utils.cpp:201
std::string basename(std::string path)
Returns the component of a pathname (file name and extension).
Definition: Utils.cpp:263
std::string dropExtension(std::string path)
Returns the filename without it&#39;s extension.
Definition: Utils.cpp:305
void rm_rf(std::string path)
Removes recursively all files within directory at #path, just like UNIX command rm -rf...
Definition: Utils.cpp:117
void write(std::string path, std::string contents)
Writes #contents to #path.
Definition: Utils.cpp:173
int between(int min, int max)
Random number between min and max.
Definition: Utils.cpp:48
Random useful things accumulated over the years.
Definition: Utils.hpp:12
bool exists(std::string path)
Tells if #path exists.
Definition: Utils.cpp:84
std::string getUser()
Gets the user name of the person running this program.
Definition: Utils.cpp:246
bool booleanWithChance(float percent)
Random boolean with chance of #percent.
Definition: Utils.cpp:64
void rm_f(std::string path)
Forcibly removes file within #path.
Definition: Utils.cpp:152