vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
StringHelpers.h
Go to the documentation of this file.
1 #ifndef STRINGHELPERSH_H
2 #define STRINGHELPERSH_H
3 
4 #ifndef HELPERS_IMP_EXP
5  #ifdef VRECKO_LIBRARY
6  #define HELPERS_IMP_EXP __declspec(dllexport)
7  #else
8  #define HELPERS_IMP_EXP __declspec(dllimport)
9  #endif
10 #endif
11 
12 
13 #if defined(__linux__)
14  #ifndef _itoa
15  #define _itoa itoa
16  HELPERS_IMP_EXP char *strrev(char *str);
17  HELPERS_IMP_EXP char* itoa(int value, char* str, int radix);
18  #endif
19 #endif
20 
21 /* Is a string beginning with another specified string? */
22 HELPERS_IMP_EXP bool isBeginningWith(const char *fullString, const char *beginString);
23 
24 
25 
26 inline char* uniCpy(char* str1, const char* str2) { return strcpy(str1, str2); }
27 inline WCHAR* uniCpy(WCHAR* str1, const WCHAR* str2) { return wcscpy(str1, str2); }
28 inline char* uniNCpy(char* str1, const char* str2, size_t count) { return strncpy(str1, str2, count); }
29 inline WCHAR* uniNCpy(WCHAR* str1, const WCHAR* str2, size_t count) { return wcsncpy(str1, str2, count); }
30 inline char* uniCat(char* str1, const char* str2) { return strcat(str1, str2); }
31 inline WCHAR* uniCat(WCHAR* str1, const WCHAR* str2) { return wcscat(str1, str2); }
32 inline const char* uniStr(const char* str1, const char* str2) { return strstr(str1, str2); }
33 inline const WCHAR* uniStr(const WCHAR* str1, const WCHAR* str2) { return wcsstr(str1, str2); }
34 inline char* uniStr(char* str1, const char* str2) { return strstr(str1, str2); }
35 inline WCHAR* uniStr(WCHAR* str1, const WCHAR* str2) { return wcsstr(str1, str2); }
36 inline size_t uniLen(const char* str1) { return strlen(str1); }
37 inline size_t uniLen(const WCHAR* str1) { return wcslen(str1); }
38 
39 
40 
41 template <class _Type>
42 void getFileNameFromPathFile(_Type* outFile, const _Type* inPath, const _Type* inFile)
43  // Adds together the path and the file name (containing absolute, relative or no path) using common rules.
44  {
45  int i;
46  _Type backslashStr[2] = {'\\', 0x00};
47  _Type colonStr[2] = {':', 0x00};
48 
49  if ((inFile[0] == '\\') || (inFile[0] == '/')) {
50  // root directory (but might be on non-current disk)
51  uniCpy(outFile, inPath);
52  i = (int)uniLen(outFile) - 1;
53  while (i >= 0) {
54  if (outFile[i] == ':')
55  break;
56  --i;
57  }
58  outFile[i + 1] = 0;
59 
60  uniCat(outFile, inFile);
61  } else if (uniStr(inFile, colonStr)) {
62  // full path including the disk
63  uniCpy(outFile, inFile);
64  } else {
65  uniCpy(outFile, inPath);
66  i = (int)uniLen(outFile);
67  if ((i > 0) && (outFile[i - 1] != '\\') && (outFile[i - 1] != '/'))
68  uniCat(outFile, backslashStr);
69  uniCat(outFile, inFile);
70  }
71 }
72 
73 
74 template <class _Type>
75 void getRelativePath(_Type *outPathFile, const _Type *inPath, const _Type *inPathFile)
76  // Adjusts the [inPathFile] to be a path relative to [inPath]
77 {
78  int i;
79 
80  _Type tmpInPath[1000];
81  strcpy (tmpInPath, inPath);
82  i = uniLen(tmpInPath);
83  if ((i <= 0) || ((tmpInPath[i - 1] != '\\') && (tmpInPath[i - 1] != '/'))) {
84  // we require terminating backslash at the end of the input path
85  tmpInPath[i] = '\\';
86  tmpInPath[i + 1] = 0x00;
87  }
88 
89  _Type tmpPath[1000];
90  getFileNameFromPathFile(tmpPath, inPath, inPathFile);
91 
92  int inPos = 0;
93  while ((tmpInPath[inPos] != 0x00) && (tolower(tmpInPath[inPos]) == tolower(tmpPath[inPos]))) {
94  // find the first difference or the end of either string
95  ++inPos;
96  }
97 
98  if (tmpInPath[inPos] == 0x00) {
99  // in-path ended first (or both paths ended). The specified directory/file is in the (sub)directory of the in-path.
100  if (tmpPath[inPos] == 0x00) {
101  // Path with the specified directory/file ended too.
102  // This means the user gave us exactly the main directory (with backslash at the end).
103  outPathFile[0] = '.';
104  outPathFile[1] = 0x00;
105  return;
106  }
107 
108  uniCpy(outPathFile, tmpPath + inPos);
109  return;
110  }
111 
112 
113  // => The specified path lies outside the [inPath] directory.
114  int outPos = 0;
115  i = inPos;
116  while (tmpInPath[i] != 0x00) {
117  if ((tmpInPath[i] == '\\') || (tmpInPath[i] == '/')) {
118  outPathFile[outPos] = '.';
119  outPathFile[outPos + 1] = '.';
120  outPathFile[outPos + 2] = '\\';
121  outPos += 3;
122  }
123  ++i;
124  }
125 
126  while ((inPos >= 0) && (tmpPath[inPos - 1] != '\\') && (tmpPath[inPos - 1] != '/')) {
127  // find the beginning of the current directory
128  --inPos;
129  }
130 
131  uniCpy(outPathFile + outPos, tmpPath + inPos);
132 }
133 
134 
135 /*template <class _Type>
136 void getRelativePath(_Type* outPath, _Type* inPath, const _Type* basePath) {
137  if (inPath != uniStr(inPath, basePath)) {
138  uniCpy(outPath, inPath);
139  } else {
140  outPath[0] = 0;
141 
142  int i = (int)uniLen(basePath);
143  int i2 = (int)uniLen(inPath);
144  if ((i2 > i) && (basePath[i] == '\\')) {
145  uniNCpy(outPath, inPath + i + 1, i2 - i - 1);
146  outPath[i2 - i - 1] = 0;
147  } else {
148  uniNCpy(outPath, inPath + i, i2 - i);
149  outPath[i2 - i] = 0;
150  }
151  }
152 }*/
153 
154 
155 
156 template <class _Type>
157 void getFilePath(_Type* outPath, const _Type* inFile)
158  // Will include the terminating '\\' (or will be completely empty)
159 {
160  int i = (int)uniLen(inFile) - 1;
161  while (i >= 0) {
162  if ((inFile[i] == '\\') || (inFile[i] == '/')) {
163  uniNCpy(outPath, inFile, i + 1);
164  outPath[i + 1] = 0;
165  return;
166  }
167  --i;
168  }
169 
170  outPath[0] = 0;
171 }
172 
173 template <class _Type>
174 void getFileNameWithoutPath(_Type* outFile, const _Type* inFile) {
175  int i = (int)uniLen(inFile) - 1;
176  while (i >= 0) {
177  if ((inFile[i] == '\\') || (inFile[i] == '/')) {
178  uniNCpy(outFile, inFile + i + 1, uniLen(inFile) - i - 1);
179  outFile[uniLen(inFile) - i - 1] = 0;
180  return;
181  }
182  --i;
183  }
184  uniCpy(outFile, inFile);
185 }
186 
187 
188 template <class _Type>
189 void getFileNameWithoutExt(_Type* outFile, const _Type* inFile)
190  // Will NOT include the "."
191 {
192  int i = (int)uniLen(inFile) - 1;
193  while (i >= 0) {
194  if ((inFile[i] == '\\') || (inFile[i] == '/'))
195  // expecting '.', but found '\\' -> there was no extension
196  break;
197 
198  if (inFile[i] == '.') {
199  uniNCpy(outFile, inFile, i);
200  outFile[i] = 0;
201  return;
202  }
203  --i;
204  }
205 
206  // no extension
207  uniCpy(outFile, inFile);
208 }
209 
210 
211 template <class _Type>
212 void getFileExtension(_Type* outExt, const _Type* inFile)
213  // Will NOT include the "."
214 {
215  int i = (int)uniLen(inFile) - 1;
216  while (i >= 0) {
217  if ((inFile[i] == '\\') || (inFile[i] == '/'))
218  // expected '.', but found '\\' -> there was no extension
219  break;
220 
221  if (inFile[i] == '.') {
222  uniNCpy(outExt, inFile + i + 1, uniLen(inFile) - i - 1);
223  outExt[uniLen(inFile) - i - 1] = 0;
224  return;
225  }
226  --i;
227  }
228 
229  // no extension
230  outExt[0] = 0;
231 }
232 
233 
234 #endif