MinVR  0.9.0
A multi-platform virtual reality library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
StringUtils.H
Go to the documentation of this file.
1 //========================================================================
2 // MinVR
3 // Platform: Any
4 // API version: 1.0
5 //------------------------------------------------------------------------
6 // The MIT License (MIT)
7 //
8 // Copyright (c) 2013 Regents of the University of Minnesota
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a copy of
11 // this software and associated documentation files (the "Software"), to deal in
12 // the Software without restriction, including without limitation the rights to
13 // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
14 // the Software, and to permit persons to whom the Software is furnished to do so,
15 // subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included in all
18 // copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
22 // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
23 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24 // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25 // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //========================================================================
27 
28 
29 #ifndef STRINGUTILS_H
30 #define STRINGUTILS_H
31 
32 #include <glm/glm.hpp>
33 #include <vector>
34 #include <boost/log/trivial.hpp>
35 #include <boost/filesystem.hpp>
36 #include <boost/filesystem/fstream.hpp>
37 #define BOOST_ASSERT_MSG_OSTREAM std::cout
38 #include <boost/assert.hpp>
39 
40 // Can be used to get a quoted version of the value of a particular #define
41 #define DEFINE_TO_QUOTED_STR( d ) #d
42 
43 namespace MinVR {
44 
45 
46 std::vector<std::string> splitStringIntoArray(const std::string &in);
47 
56 std::string decygifyPath(const std::string &in);
57 
58 
64 std::string replaceEnvVars(const std::string &in);
65 
66 unsigned int hashCode(const double d);
67 
68 // Vector2
69 std::ostream & operator<< ( std::ostream &os, const glm::vec2 &vec2);
70 std::istream & operator>> ( std::istream &is, glm::vec2 &vec2);
71 // Vector3
72 std::ostream & operator<< ( std::ostream &os, const glm::vec3 &vec3);
73 std::istream & operator>> ( std::istream &is, glm::vec3 &vec3);
74 // Vector4
75 std::ostream & operator<< ( std::ostream &os, const glm::vec4 &vec4);
76 std::istream & operator<< ( std::istream &is, glm::vec4 &vec4);
77 
78 // Matrix3
79 std::ostream & operator<< ( std::ostream &os, const glm::mat3 &m);
80 std::istream & operator>> ( std::istream &is, glm::mat3 &m);
81 // Matrix4
82 std::ostream & operator<< ( std::ostream &os, const glm::mat4 &m);
83 std::istream & operator>> ( std::istream &is, glm::mat4 &m);
84 // etc.. add on as necessary..
85 
86 std::string intToString(int i);
87 int stringToInt(const std::string &in);
88 
89 std::string realToString(double r);
90 double stringToReal(const std::string &in);
91 
92 
93 
101 bool popNextToken(std::string &in, std::string &token,
102  bool returnFalseOnSemiColon = false);
103 
104 
108 bool popUntilSemicolon(std::string &in, std::string &popped);
109 
116 std::vector< std::vector< std::string > > readDelimitedData(const std::string &csvString,
117  const std::string &delimiter,
118  bool removeQuotes=true);
119 
122 std::string joinIntoString(const std::vector<std::string>& in,
123  const std::string& delimiter=std::string(" "));
124 
126 std::string convertNewlinesAndTabsToSpaces(std::string input);
127 
130 int iMinNonNeg(int i1, int i2);
131 
132 
137 template <class T>
138 static inline bool retypeString(const std::string &str, T &val) {
139  std::istringstream is(str.c_str());
140  is >> val;
141  if (!is) {
142  BOOST_LOG_TRIVIAL(error) << "Error retyping string: " <<str
143  return false;
144  }
145  else return true;
146 }
147 
148 
150 std::string spacesString(int num);
151 
153 int numSubstringOccurances(const std::string &str, const std::string &substr);
154 
157 int findNth(const std::string &str, const std::string &substr, const int n);
158 
161 template <class T>
162 std::vector<T> insertIntoArray(std::vector<T> inputArray, T newElement,
163  int insertBeforeElementNum)
164 {
165  debugAssert(insertBeforeElementNum >= 0);
166  debugAssert(insertBeforeElementNum <= inputArray.size());
167 
168  std::vector<T> anew;
169 
170  for (int i=0;i<insertBeforeElementNum;i++)
171  anew.push_back(inputArray[i]);
172 
173  anew.push_back(newElement);
174 
175  for (int i=insertBeforeElementNum;i<inputArray.size();i++)
176  anew.push_back(inputArray[i]);
177 
178  debugAssert(anew.size() == (inputArray.size() + 1));
179  return anew;
180 }
181 
183 std::string trimWhitespace(const std::string& s);
184 
185 std::string readWholeFile(const std::string& filename);
186 
187 } // end namespace
188 #endif