OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
strings.cpp
Go to the documentation of this file.
1 
8 #include <algorithm>
9 
10 #include "global.h"
11 #include "utils/filesystem.h"
12 #include "utils/strings.h"
13 
14 std::string findAndReplace(std::string s, std::string find, std::string replace) {
15  size_t p = s.find(find);
16  while (p != std::string::npos) {
17  s.erase(p, find.length());
18  s.insert(p, replace);
19  p = s.find(find);
20  }
21  return s;
22 }
23 
24 std::string expandHomeDirectory(std::string s) {
25  if ((s.length() > 0) && (s[0] == '~')) {
26  s.erase(0, 1);
27  s.insert(0, getHomeDirectory());
28  }
29  return s;
30 }
31 
32 bool stringEndsWith(std::string s, std::string suffix, bool casesensitive) {
33  if (!casesensitive) {
34  std::transform(s.begin(), s.end(), s.begin(), ::tolower);
35  std::transform(suffix.begin(), suffix.end(), suffix.begin(), ::tolower);
36  }
37 
38  if (s.length() >= suffix.length()) {
39  std::string end = s.substr(s.length() - suffix.length());
40  return (end == suffix);
41  }
42 
43  return false;
44 }
45 
46 std::string removeLastPathElement(std::string s) {
47  auto pos = s.find_last_of("/\\");
48  if (pos == std::string::npos)
49  return s;
50  return s.erase(pos);
51 }
52 
53 std::string getLastPathElement(std::string s) {
54  auto pos = s.find_last_of("/\\");
55  if (pos == std::string::npos)
56  return s;
57  return s.erase(0, pos + 1);
58 }
59 
60 std::string convertPathDelimiter(std::string s) {
61  std::string::size_type pos;
62 
63 #ifdef _WIN32
64  while ((pos = s.find('/')) != std::string::npos) {
65  s.replace(pos, 1, 1, '\\');
66  }
67 #else
68  while ((pos = s.find('\\')) != std::string::npos) {
69  s.replace(pos, 1, 1, '/');
70  }
71 #endif
72 
73  return s;
74 }
75 
std::string expandHomeDirectory(std::string s)
Definition: strings.cpp:24
String handling utilities.
std::string getLastPathElement(std::string s)
Definition: strings.cpp:53
Included everywhere.
file-system utilities
bool stringEndsWith(std::string s, std::string suffix, bool casesensitive)
Definition: strings.cpp:32
std::string findAndReplace(std::string s, std::string find, std::string replace)
Definition: strings.cpp:14
std::string getHomeDirectory()
Definition: filesystem.cpp:39
std::string convertPathDelimiter(std::string s)
Definition: strings.cpp:60
std::string removeLastPathElement(std::string s)
Definition: strings.cpp:46