COVISE Core
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
helper_string.h
Go to the documentation of this file.
1 /* This file is part of COVISE.
2 
3  You can use it under the terms of the GNU Lesser General Public License
4  version 2.1 or later, see lgpl-2.1.txt.
5 
6  * License: LGPL 2+ */
7 
19 // These are helper functions for the SDK samples (string parsing, timers, etc)
20 #ifndef STRING_HELPER_H
21 #define STRING_HELPER_H
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <fstream>
26 #include <string>
27 
28 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
29 #ifndef _CRT_SECURE_NO_DEPRECATE
30 #define _CRT_SECURE_NO_DEPRECATE
31 #endif
32 #ifndef STRCASECMP
33 #define STRCASECMP _stricmp
34 #endif
35 #ifndef STRNCASECMP
36 #define STRNCASECMP _strnicmp
37 #endif
38 #ifndef STRCPY
39 #define STRCPY(sFilePath, nLength, sPath) strcpy_s(sFilePath, nLength, sPath)
40 #endif
41 
42 #ifndef FOPEN
43 #define FOPEN(fHandle, filename, mode) fopen_s(&fHandle, filename, mode)
44 #endif
45 #ifndef FOPEN_FAIL
46 #define FOPEN_FAIL(result) (result != 0)
47 #endif
48 #ifndef SSCANF
49 #define SSCANF sscanf_s
50 #endif
51 #ifndef SPRINTF
52 #define SPRINTF sprintf_s
53 #endif
54 #else // Linux Includes
55 #include <string.h>
56 #include <strings.h>
57 
58 #ifndef STRCASECMP
59 #define STRCASECMP strcasecmp
60 #endif
61 #ifndef STRNCASECMP
62 #define STRNCASECMP strncasecmp
63 #endif
64 #ifndef STRCPY
65 #define STRCPY(sFilePath, nLength, sPath) strcpy(sFilePath, sPath)
66 #endif
67 
68 #ifndef FOPEN
69 #define FOPEN(fHandle, filename, mode) (fHandle = fopen(filename, mode))
70 #endif
71 #ifndef FOPEN_FAIL
72 #define FOPEN_FAIL(result) (result == NULL)
73 #endif
74 #ifndef SSCANF
75 #define SSCANF sscanf
76 #endif
77 #ifndef SPRINTF
78 #define SPRINTF sprintf
79 #endif
80 #endif
81 
82 #ifndef EXIT_WAIVED
83 #define EXIT_WAIVED 2
84 #endif
85 
86 // CUDA Utility Helper Functions
87 inline int stringRemoveDelimiter(char delimiter, const char *string)
88 {
89  int string_start = 0;
90 
91  while (string[string_start] == delimiter)
92  {
93  string_start++;
94  }
95 
96  if (string_start >= (int)strlen(string) - 1)
97  {
98  return 0;
99  }
100 
101  return string_start;
102 }
103 
104 inline int getFileExtension(char *filename, char **extension)
105 {
106  int string_length = (int)strlen(filename);
107 
108  while (filename[string_length--] != '.')
109  {
110  if (string_length == 0)
111  break;
112  }
113 
114  if (string_length > 0)
115  string_length += 2;
116 
117  if (string_length == 0)
118  *extension = NULL;
119  else
120  *extension = &filename[string_length];
121 
122  return string_length;
123 }
124 
125 inline bool checkCmdLineFlag(const int argc, const char **argv, const char *string_ref)
126 {
127  bool bFound = false;
128 
129  if (argc >= 1)
130  {
131  for (int i = 1; i < argc; i++)
132  {
133  int string_start = stringRemoveDelimiter('-', argv[i]);
134  const char *string_argv = &argv[i][string_start];
135 
136  const char *equal_pos = strchr(string_argv, '=');
137  int argv_length = (int)(equal_pos == 0 ? strlen(string_argv) : equal_pos - string_argv);
138 
139  int length = (int)strlen(string_ref);
140 
141  if (length == argv_length && !STRNCASECMP(string_argv, string_ref, length))
142  {
143  bFound = true;
144  continue;
145  }
146  }
147  }
148 
149  return bFound;
150 }
151 
152 // This function wraps the CUDA Driver API into a template function
153 template <class T>
154 inline bool getCmdLineArgumentValue(const int argc, const char **argv, const char *string_ref, T *value)
155 {
156  bool bFound = false;
157 
158  if (argc >= 1)
159  {
160  for (int i = 1; i < argc; i++)
161  {
162  int string_start = stringRemoveDelimiter('-', argv[i]);
163  const char *string_argv = &argv[i][string_start];
164  int length = (int)strlen(string_ref);
165 
166  if (!STRNCASECMP(string_argv, string_ref, length))
167  {
168  if (length + 1 <= (int)strlen(string_argv))
169  {
170  int auto_inc = (string_argv[length] == '=') ? 1 : 0;
171  *value = (T)atoi(&string_argv[length + auto_inc]);
172  }
173 
174  bFound = true;
175  i = argc;
176  }
177  }
178  }
179 
180  return bFound;
181 }
182 
183 inline int getCmdLineArgumentInt(const int argc, const char **argv, const char *string_ref)
184 {
185  bool bFound = false;
186  int value = -1;
187 
188  if (argc >= 1)
189  {
190  for (int i = 1; i < argc; i++)
191  {
192  int string_start = stringRemoveDelimiter('-', argv[i]);
193  const char *string_argv = &argv[i][string_start];
194  int length = (int)strlen(string_ref);
195 
196  if (!STRNCASECMP(string_argv, string_ref, length))
197  {
198  if (length + 1 <= (int)strlen(string_argv))
199  {
200  int auto_inc = (string_argv[length] == '=') ? 1 : 0;
201  value = atoi(&string_argv[length + auto_inc]);
202  }
203  else
204  {
205  value = 0;
206  }
207 
208  bFound = true;
209  continue;
210  }
211  }
212  }
213 
214  if (bFound)
215  {
216  return value;
217  }
218  else
219  {
220  return 0;
221  }
222 }
223 
224 inline float getCmdLineArgumentFloat(const int argc, const char **argv, const char *string_ref)
225 {
226  bool bFound = false;
227  float value = -1;
228 
229  if (argc >= 1)
230  {
231  for (int i = 1; i < argc; i++)
232  {
233  int string_start = stringRemoveDelimiter('-', argv[i]);
234  const char *string_argv = &argv[i][string_start];
235  int length = (int)strlen(string_ref);
236 
237  if (!STRNCASECMP(string_argv, string_ref, length))
238  {
239  if (length + 1 <= (int)strlen(string_argv))
240  {
241  int auto_inc = (string_argv[length] == '=') ? 1 : 0;
242  value = (float)atof(&string_argv[length + auto_inc]);
243  }
244  else
245  {
246  value = 0.f;
247  }
248 
249  bFound = true;
250  continue;
251  }
252  }
253  }
254 
255  if (bFound)
256  {
257  return value;
258  }
259  else
260  {
261  return 0;
262  }
263 }
264 
265 inline bool getCmdLineArgumentString(const int argc, const char **argv,
266  const char *string_ref, char **string_retval)
267 {
268  bool bFound = false;
269 
270  if (argc >= 1)
271  {
272  for (int i = 1; i < argc; i++)
273  {
274  int string_start = stringRemoveDelimiter('-', argv[i]);
275  char *string_argv = (char *)&argv[i][string_start];
276  int length = (int)strlen(string_ref);
277 
278  if (!STRNCASECMP(string_argv, string_ref, length))
279  {
280  *string_retval = &string_argv[length + 1];
281  bFound = true;
282  continue;
283  }
284  }
285  }
286 
287  if (!bFound)
288  {
289  *string_retval = NULL;
290  }
291 
292  return bFound;
293 }
294 
303 inline char *sdkFindFilePath(const char *filename, const char *executable_path)
304 {
305  // <executable_name> defines a variable that is replaced with the name of the executable
306 
307  // Typical relative search paths to locate needed companion files (e.g. sample input data, or JIT source files)
308  // The origin for the relative search may be the .exe file, a .bat file launching an .exe, a browser .exe launching the .exe or .bat, etc
309  const char *searchPath[] = {
310  "./", // same dir
311  "./common/", // "/common/" subdir
312  "./common/data/", // "/common/data/" subdir
313  "./data/", // "/data/" subdir
314  "./src/", // "/src/" subdir
315  "./src/<executable_name>/data/", // "/src/<executable_name>/data/" subdir
316  "./inc/", // "/inc/" subdir
317  "./0_Simple/", // "/0_Simple/" subdir
318  "./1_Utilities/", // "/1_Utilities/" subdir
319  "./2_Graphics/", // "/2_Graphics/" subdir
320  "./3_Imaging/", // "/3_Imaging/" subdir
321  "./4_Financial/", // "/4_Financial/" subdir
322  "./5_Simulations/", // "/5_Simulations/" subdir
323  "./6_Advanced/", // "/6_Advanced/" subdir
324  "./7_CUDALibraries/", // "/7_CUDALibraries/" subdir
325  "./samples/", // "/samples/" subdir
326  "../", // up 1 in tree
327  "../common/", // up 1 in tree, "/common/" subdir
328  "../common/data/", // up 1 in tree, "/common/data/" subdir
329  "../data/", // up 1 in tree, "/data/" subdir
330  "../src/", // up 1 in tree, "/src/" subdir
331  "../inc/", // up 1 in tree, "/inc/" subdir
332  "../0_Simple/<executable_name>/data/", // up 1 in tree, "/0_Simple/<executable_name>/" subdir
333  "../1_Utilities/<executable_name>/data/", // up 1 in tree, "/1_Utilities/<executable_name>/" subdir
334  "../2_Graphics/<executable_name>/data/", // up 1 in tree, "/2_Graphics/<executable_name>/" subdir
335  "../3_Imaging/<executable_name>/data/", // up 1 in tree, "/3_Imaging/<executable_name>/" subdir
336  "../4_Financial/<executable_name>/data/", // up 1 in tree, "/4_Financial/<executable_name>/" subdir
337  "../5_Simulations/<executable_name>/data/", // up 1 in tree, "/5_Simulations/<executable_name>/" subdir
338  "../6_Advanced/<executable_name>/data/", // up 1 in tree, "/6_Advanced/<executable_name>/" subdir
339  "../7_CUDALibraries/<executable_name>/data/", // up 1 in tree, "/7_CUDALibraries/<executable_name>/" subdir
340  "../samples/<executable_name>/data/", // up 1 in tree, "/samples/<executable_name>/" subdir
341  "../../", // up 2 in tree
342  "../../common/", // up 2 in tree, "/common/" subdir
343  "../../common/data/", // up 2 in tree, "/common/data/" subdir
344  "../../data/", // up 2 in tree, "/data/" subdir
345  "../../src/", // up 2 in tree, "/src/" subdir
346  "../../inc/", // up 2 in tree, "/inc/" subdir
347  "../../sandbox/<executable_name>/data/", // up 2 in tree, "/sandbox/<executable_name>/" subdir
348  "../../0_Simple/<executable_name>/data/", // up 2 in tree, "/0_Simple/<executable_name>/" subdir
349  "../../1_Utilities/<executable_name>/data/", // up 2 in tree, "/1_Utilities/<executable_name>/" subdir
350  "../../2_Graphics/<executable_name>/data/", // up 2 in tree, "/2_Graphics/<executable_name>/" subdir
351  "../../3_Imaging/<executable_name>/data/", // up 2 in tree, "/3_Imaging/<executable_name>/" subdir
352  "../../4_Financial/<executable_name>/data/", // up 2 in tree, "/4_Financial/<executable_name>/" subdir
353  "../../5_Simulations/<executable_name>/data/", // up 2 in tree, "/5_Simulations/<executable_name>/" subdir
354  "../../6_Advanced/<executable_name>/data/", // up 2 in tree, "/6_Advanced/<executable_name>/" subdir
355  "../../7_CUDALibraries/<executable_name>/data/", // up 2 in tree, "/7_CUDALibraries/<executable_name>/" subdir
356  "../../samples/<executable_name>/data/", // up 2 in tree, "/samples/<executable_name>/" subdir
357  "../../../", // up 3 in tree
358  "../../../src/<executable_name>/", // up 3 in tree, "/src/<executable_name>/" subdir
359  "../../../src/<executable_name>/data/", // up 3 in tree, "/src/<executable_name>/data/" subdir
360  "../../../src/<executable_name>/src/", // up 3 in tree, "/src/<executable_name>/src/" subdir
361  "../../../src/<executable_name>/inc/", // up 3 in tree, "/src/<executable_name>/inc/" subdir
362  "../../../sandbox/<executable_name>/", // up 3 in tree, "/sandbox/<executable_name>/" subdir
363  "../../../sandbox/<executable_name>/data/", // up 3 in tree, "/sandbox/<executable_name>/data/" subdir
364  "../../../sandbox/<executable_name>/src/", // up 3 in tree, "/sandbox/<executable_name>/src/" subdir
365  "../../../sandbox/<executable_name>/inc/", // up 3 in tree, "/sandbox/<executable_name>/inc/" subdir
366  "../../../0_Simple/<executable_name>/data/", // up 3 in tree, "/0_Simple/<executable_name>/" subdir
367  "../../../1_Utilities/<executable_name>/data/", // up 3 in tree, "/1_Utilities/<executable_name>/" subdir
368  "../../../2_Graphics/<executable_name>/data/", // up 3 in tree, "/2_Graphics/<executable_name>/" subdir
369  "../../../3_Imaging/<executable_name>/data/", // up 3 in tree, "/3_Imaging/<executable_name>/" subdir
370  "../../../4_Financial/<executable_name>/data/", // up 3 in tree, "/4_Financial/<executable_name>/" subdir
371  "../../../5_Simulations/<executable_name>/data/", // up 3 in tree, "/5_Simulations/<executable_name>/" subdir
372  "../../../6_Advanced/<executable_name>/data/", // up 3 in tree, "/6_Advanced/<executable_name>/" subdir
373  "../../../7_CUDALibraries/<executable_name>/data/", // up 3 in tree, "/7_CUDALibraries/<executable_name>/" subdir
374  "../../../samples/<executable_name>/data/", // up 3 in tree, "/samples/<executable_name>/" subdir
375  "../../../common/", // up 3 in tree, "../../../common/" subdir
376  "../../../common/data/", // up 3 in tree, "../../../common/data/" subdir
377  "../../../data/", // up 3 in tree, "../../../data/" subdir
378  "../../../../", // up 4 in tree
379  "../../../../src/<executable_name>/", // up 4 in tree, "/src/<executable_name>/" subdir
380  "../../../../src/<executable_name>/data/", // up 4 in tree, "/src/<executable_name>/data/" subdir
381  "../../../../src/<executable_name>/src/", // up 4 in tree, "/src/<executable_name>/src/" subdir
382  "../../../../src/<executable_name>/inc/", // up 4 in tree, "/src/<executable_name>/inc/" subdir
383  "../../../../sandbox/<executable_name>/", // up 4 in tree, "/sandbox/<executable_name>/" subdir
384  "../../../../sandbox/<executable_name>/data/", // up 4 in tree, "/sandbox/<executable_name>/data/" subdir
385  "../../../../sandbox/<executable_name>/src/", // up 4 in tree, "/sandbox/<executable_name>/src/" subdir
386  "../../../../sandbox/<executable_name>/inc/", // up 4 in tree, "/sandbox/<executable_name>/inc/" subdir
387  "../../../../0_Simple/<executable_name>/data/", // up 4 in tree, "/0_Simple/<executable_name>/" subdir
388  "../../../../1_Utilities/<executable_name>/data/", // up 4 in tree, "/1_Utilities/<executable_name>/" subdir
389  "../../../../2_Graphics/<executable_name>/data/", // up 4 in tree, "/2_Graphics/<executable_name>/" subdir
390  "../../../../3_Imaging/<executable_name>/data/", // up 4 in tree, "/3_Imaging/<executable_name>/" subdir
391  "../../../../4_Financial/<executable_name>/data/", // up 4 in tree, "/4_Financial/<executable_name>/" subdir
392  "../../../../5_Simulations/<executable_name>/data/", // up 4 in tree, "/5_Simulations/<executable_name>/" subdir
393  "../../../../6_Advanced/<executable_name>/data/", // up 4 in tree, "/6_Advanced/<executable_name>/" subdir
394  "../../../../7_CUDALibraries/<executable_name>/data/", // up 4 in tree, "/7_CUDALibraries/<executable_name>/" subdir
395  "../../../../samples/<executable_name>/data/", // up 4 in tree, "/samples/<executable_name>/" subdir
396  "../../../../common/", // up 4 in tree, "../../../common/" subdir
397  "../../../../common/data/", // up 4 in tree, "../../../common/data/" subdir
398  "../../../../data/", // up 4 in tree, "../../../data/" subdir
399  "../../../../../", // up 5 in tree
400  "../../../../../src/<executable_name>/", // up 5 in tree, "/src/<executable_name>/" subdir
401  "../../../../../src/<executable_name>/data/", // up 5 in tree, "/src/<executable_name>/data/" subdir
402  "../../../../../src/<executable_name>/src/", // up 5 in tree, "/src/<executable_name>/src/" subdir
403  "../../../../../src/<executable_name>/inc/", // up 5 in tree, "/src/<executable_name>/inc/" subdir
404  "../../../../../sandbox/<executable_name>/", // up 5 in tree, "/sandbox/<executable_name>/" subdir
405  "../../../../../sandbox/<executable_name>/data/", // up 5 in tree, "/sandbox/<executable_name>/data/" subdir
406  "../../../../../sandbox/<executable_name>/src/", // up 5 in tree, "/sandbox/<executable_name>/src/" subdir
407  "../../../../../sandbox/<executable_name>/inc/", // up 5 in tree, "/sandbox/<executable_name>/inc/" subdir
408  "../../../../../0_Simple/<executable_name>/data/", // up 5 in tree, "/0_Simple/<executable_name>/" subdir
409  "../../../../../1_Utilities/<executable_name>/data/", // up 5 in tree, "/1_Utilities/<executable_name>/" subdir
410  "../../../../../2_Graphics/<executable_name>/data/", // up 5 in tree, "/2_Graphics/<executable_name>/" subdir
411  "../../../../../3_Imaging/<executable_name>/data/", // up 5 in tree, "/3_Imaging/<executable_name>/" subdir
412  "../../../../../4_Financial/<executable_name>/data/", // up 5 in tree, "/4_Financial/<executable_name>/" subdir
413  "../../../../../5_Simulations/<executable_name>/data/", // up 5 in tree, "/5_Simulations/<executable_name>/" subdir
414  "../../../../../6_Advanced/<executable_name>/data/", // up 5 in tree, "/6_Advanced/<executable_name>/" subdir
415  "../../../../../7_CUDALibraries/<executable_name>/data/", // up 5 in tree, "/7_CUDALibraries/<executable_name>/" subdir
416  "../../../../../samples/<executable_name>/data/", // up 5 in tree, "/samples/<executable_name>/" subdir
417  "../../../../../common/", // up 5 in tree, "../../../common/" subdir
418  "../../../../../common/data/", // up 5 in tree, "../../../common/data/" subdir
419  };
420 
421  // Extract the executable name
422  std::string executable_name;
423 
424  if (executable_path != 0)
425  {
426  executable_name = std::string(executable_path);
427 
428 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
429  // Windows path delimiter
430  size_t delimiter_pos = executable_name.find_last_of('\\');
431  executable_name.erase(0, delimiter_pos + 1);
432 
433  if (executable_name.rfind(".exe") != std::string::npos)
434  {
435  // we strip .exe, only if the .exe is found
436  executable_name.resize(executable_name.size() - 4);
437  }
438 
439 #else
440  // Linux & OSX path delimiter
441  size_t delimiter_pos = executable_name.find_last_of('/');
442  executable_name.erase(0, delimiter_pos + 1);
443 #endif
444  }
445 
446  // Loop over all search paths and return the first hit
447  for (unsigned int i = 0; i < sizeof(searchPath) / sizeof(char *); ++i)
448  {
449  std::string path(searchPath[i]);
450  size_t executable_name_pos = path.find("<executable_name>");
451 
452  // If there is executable_name variable in the searchPath
453  // replace it with the value
454  if (executable_name_pos != std::string::npos)
455  {
456  if (executable_path != 0)
457  {
458  path.replace(executable_name_pos, strlen("<executable_name>"), executable_name);
459  }
460  else
461  {
462  // Skip this path entry if no executable argument is given
463  continue;
464  }
465  }
466 
467 #ifdef _DEBUG
468  printf("sdkFindFilePath <%s> in %s\n", filename, path.c_str());
469 #endif
470 
471  // Test if the file exists
472  path.append(filename);
473  FILE *fp;
474  FOPEN(fp, path.c_str(), "rb");
475 
476  if (fp != NULL)
477  {
478  fclose(fp);
479  // File found
480  // returning an allocated array here for backwards compatibility reasons
481  char *file_path = (char *)malloc(path.length() + 1);
482  STRCPY(file_path, path.length() + 1, path.c_str());
483  return file_path;
484  }
485 
486  if (fp)
487  {
488  fclose(fp);
489  }
490  }
491 
492  // File not found
493  return 0;
494 }
495 
496 #endif
GLsizei const GLchar ** path
Definition: khronos-glext.h:7952
bool checkCmdLineFlag(const int argc, const char **argv, const char *string_ref)
Definition: helper_string.h:125
GLsizei const GLchar *const * string
Definition: khronos-glext.h:6750
GLenum GLuint GLenum GLsizei length
Definition: khronos-glext.h:6279
bool getCmdLineArgumentValue(const int argc, const char **argv, const char *string_ref, T *value)
Definition: helper_string.h:154
GLsizei const GLfloat * value
Definition: khronos-glext.h:6760
#define STRCPY(sFilePath, nLength, sPath)
Definition: helper_string.h:65
float getCmdLineArgumentFloat(const int argc, const char **argv, const char *string_ref)
Definition: helper_string.h:224
int stringRemoveDelimiter(char delimiter, const char *string)
Definition: helper_string.h:87
char * sdkFindFilePath(const char *filename, const char *executable_path)
Definition: helper_string.h:303
bool getCmdLineArgumentString(const int argc, const char **argv, const char *string_ref, char **string_retval)
Definition: helper_string.h:265
#define FOPEN(fHandle, filename, mode)
Definition: helper_string.h:69
int getCmdLineArgumentInt(const int argc, const char **argv, const char *string_ref)
Definition: helper_string.h:183
int getFileExtension(char *filename, char **extension)
Definition: helper_string.h:104
#define NULL
Definition: covise_list.h:22
#define STRNCASECMP
Definition: helper_string.h:62