helper_string.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions
  5. * are met:
  6. * * Redistributions of source code must retain the above copyright
  7. * notice, this list of conditions and the following disclaimer.
  8. * * Redistributions in binary form must reproduce the above copyright
  9. * notice, this list of conditions and the following disclaimer in the
  10. * documentation and/or other materials provided with the distribution.
  11. * * Neither the name of NVIDIA CORPORATION nor the names of its
  12. * contributors may be used to endorse or promote products derived
  13. * from this software without specific prior written permission.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
  16. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  23. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. // These are helper functions for the SDK samples (string parsing, timers, etc)
  28. #ifndef COMMON_HELPER_STRING_H_
  29. #define COMMON_HELPER_STRING_H_
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <fstream>
  33. #include <string>
  34. #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
  35. #ifndef _CRT_SECURE_NO_DEPRECATE
  36. #define _CRT_SECURE_NO_DEPRECATE
  37. #endif
  38. #ifndef STRCASECMP
  39. #define STRCASECMP _stricmp
  40. #endif
  41. #ifndef STRNCASECMP
  42. #define STRNCASECMP _strnicmp
  43. #endif
  44. #ifndef STRCPY
  45. #define STRCPY(sFilePath, nLength, sPath) strcpy_s(sFilePath, nLength, sPath)
  46. #endif
  47. #ifndef FOPEN
  48. #define FOPEN(fHandle, filename, mode) fopen_s(&fHandle, filename, mode)
  49. #endif
  50. #ifndef FOPEN_FAIL
  51. #define FOPEN_FAIL(result) (result != 0)
  52. #endif
  53. #ifndef SSCANF
  54. #define SSCANF sscanf_s
  55. #endif
  56. #ifndef SPRINTF
  57. #define SPRINTF sprintf_s
  58. #endif
  59. #else // Linux Includes
  60. #include <string.h>
  61. #include <strings.h>
  62. #ifndef STRCASECMP
  63. #define STRCASECMP strcasecmp
  64. #endif
  65. #ifndef STRNCASECMP
  66. #define STRNCASECMP strncasecmp
  67. #endif
  68. #ifndef STRCPY
  69. #define STRCPY(sFilePath, nLength, sPath) strcpy(sFilePath, sPath)
  70. #endif
  71. #ifndef FOPEN
  72. #define FOPEN(fHandle, filename, mode) (fHandle = fopen(filename, mode))
  73. #endif
  74. #ifndef FOPEN_FAIL
  75. #define FOPEN_FAIL(result) (result == NULL)
  76. #endif
  77. #ifndef SSCANF
  78. #define SSCANF sscanf
  79. #endif
  80. #ifndef SPRINTF
  81. #define SPRINTF sprintf
  82. #endif
  83. #endif
  84. #ifndef EXIT_WAIVED
  85. #define EXIT_WAIVED 2
  86. #endif
  87. // CUDA Utility Helper Functions
  88. inline int stringRemoveDelimiter(char delimiter, const char *string) {
  89. int string_start = 0;
  90. while (string[string_start] == delimiter) {
  91. string_start++;
  92. }
  93. if (string_start >= static_cast<int>(strlen(string) - 1)) {
  94. return 0;
  95. }
  96. return string_start;
  97. }
  98. inline int getFileExtension(char *filename, char **extension) {
  99. int string_length = static_cast<int>(strlen(filename));
  100. while (filename[string_length--] != '.') {
  101. if (string_length == 0) break;
  102. }
  103. if (string_length > 0) string_length += 2;
  104. if (string_length == 0)
  105. *extension = NULL;
  106. else
  107. *extension = &filename[string_length];
  108. return string_length;
  109. }
  110. inline bool checkCmdLineFlag(const int argc, const char **argv,
  111. const char *string_ref) {
  112. bool bFound = false;
  113. if (argc >= 1) {
  114. for (int i = 1; i < argc; i++) {
  115. int string_start = stringRemoveDelimiter('-', argv[i]);
  116. const char *string_argv = &argv[i][string_start];
  117. const char *equal_pos = strchr(string_argv, '=');
  118. int argv_length = static_cast<int>(
  119. equal_pos == 0 ? strlen(string_argv) : equal_pos - string_argv);
  120. int length = static_cast<int>(strlen(string_ref));
  121. if (length == argv_length &&
  122. !STRNCASECMP(string_argv, string_ref, length)) {
  123. bFound = true;
  124. continue;
  125. }
  126. }
  127. }
  128. return bFound;
  129. }
  130. // This function wraps the CUDA Driver API into a template function
  131. template <class T>
  132. inline bool getCmdLineArgumentValue(const int argc, const char **argv,
  133. const char *string_ref, T *value) {
  134. bool bFound = false;
  135. if (argc >= 1) {
  136. for (int i = 1; i < argc; i++) {
  137. int string_start = stringRemoveDelimiter('-', argv[i]);
  138. const char *string_argv = &argv[i][string_start];
  139. int length = static_cast<int>(strlen(string_ref));
  140. if (!STRNCASECMP(string_argv, string_ref, length)) {
  141. if (length + 1 <= static_cast<int>(strlen(string_argv))) {
  142. int auto_inc = (string_argv[length] == '=') ? 1 : 0;
  143. *value = (T)atoi(&string_argv[length + auto_inc]);
  144. }
  145. bFound = true;
  146. i = argc;
  147. }
  148. }
  149. }
  150. return bFound;
  151. }
  152. inline int getCmdLineArgumentInt(const int argc, const char **argv,
  153. const char *string_ref) {
  154. bool bFound = false;
  155. int value = -1;
  156. if (argc >= 1) {
  157. for (int i = 1; i < argc; i++) {
  158. int string_start = stringRemoveDelimiter('-', argv[i]);
  159. const char *string_argv = &argv[i][string_start];
  160. int length = static_cast<int>(strlen(string_ref));
  161. if (!STRNCASECMP(string_argv, string_ref, length)) {
  162. if (length + 1 <= static_cast<int>(strlen(string_argv))) {
  163. int auto_inc = (string_argv[length] == '=') ? 1 : 0;
  164. value = atoi(&string_argv[length + auto_inc]);
  165. } else {
  166. value = 0;
  167. }
  168. bFound = true;
  169. continue;
  170. }
  171. }
  172. }
  173. if (bFound) {
  174. return value;
  175. } else {
  176. return 0;
  177. }
  178. }
  179. inline float getCmdLineArgumentFloat(const int argc, const char **argv,
  180. const char *string_ref) {
  181. bool bFound = false;
  182. float value = -1;
  183. if (argc >= 1) {
  184. for (int i = 1; i < argc; i++) {
  185. int string_start = stringRemoveDelimiter('-', argv[i]);
  186. const char *string_argv = &argv[i][string_start];
  187. int length = static_cast<int>(strlen(string_ref));
  188. if (!STRNCASECMP(string_argv, string_ref, length)) {
  189. if (length + 1 <= static_cast<int>(strlen(string_argv))) {
  190. int auto_inc = (string_argv[length] == '=') ? 1 : 0;
  191. value = static_cast<float>(atof(&string_argv[length + auto_inc]));
  192. } else {
  193. value = 0.f;
  194. }
  195. bFound = true;
  196. continue;
  197. }
  198. }
  199. }
  200. if (bFound) {
  201. return value;
  202. } else {
  203. return 0;
  204. }
  205. }
  206. inline bool getCmdLineArgumentString(const int argc, const char **argv,
  207. const char *string_ref,
  208. char **string_retval) {
  209. bool bFound = false;
  210. if (argc >= 1) {
  211. for (int i = 1; i < argc; i++) {
  212. int string_start = stringRemoveDelimiter('-', argv[i]);
  213. char *string_argv = const_cast<char *>(&argv[i][string_start]);
  214. int length = static_cast<int>(strlen(string_ref));
  215. if (!STRNCASECMP(string_argv, string_ref, length)) {
  216. *string_retval = &string_argv[length + 1];
  217. bFound = true;
  218. continue;
  219. }
  220. }
  221. }
  222. if (!bFound) {
  223. *string_retval = NULL;
  224. }
  225. return bFound;
  226. }
  227. //////////////////////////////////////////////////////////////////////////////
  228. //! Find the path for a file assuming that
  229. //! files are found in the searchPath.
  230. //!
  231. //! @return the path if succeeded, otherwise 0
  232. //! @param filename name of the file
  233. //! @param executable_path optional absolute path of the executable
  234. //////////////////////////////////////////////////////////////////////////////
  235. inline char *sdkFindFilePath(const char *filename,
  236. const char *executable_path) {
  237. // <executable_name> defines a variable that is replaced with the name of the
  238. // executable
  239. // Typical relative search paths to locate needed companion files (e.g. sample
  240. // input data, or JIT source files) The origin for the relative search may be
  241. // the .exe file, a .bat file launching an .exe, a browser .exe launching the
  242. // .exe or .bat, etc
  243. const char *searchPath[] = {
  244. "./", // same dir
  245. "./data/", // same dir
  246. "../../../../Samples/<executable_name>/", // up 4 in tree
  247. "../../../Samples/<executable_name>/", // up 3 in tree
  248. "../../Samples/<executable_name>/", // up 2 in tree
  249. "../../../../Samples/<executable_name>/data/", // up 4 in tree
  250. "../../../Samples/<executable_name>/data/", // up 3 in tree
  251. "../../Samples/<executable_name>/data/", // up 2 in tree
  252. "../../../../Common/data/", // up 4 in tree
  253. "../../../Common/data/", // up 3 in tree
  254. "../../Common/data/" // up 2 in tree
  255. };
  256. // Extract the executable name
  257. std::string executable_name;
  258. if (executable_path != 0) {
  259. executable_name = std::string(executable_path);
  260. #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
  261. // Windows path delimiter
  262. size_t delimiter_pos = executable_name.find_last_of('\\');
  263. executable_name.erase(0, delimiter_pos + 1);
  264. if (executable_name.rfind(".exe") != std::string::npos) {
  265. // we strip .exe, only if the .exe is found
  266. executable_name.resize(executable_name.size() - 4);
  267. }
  268. #else
  269. // Linux & OSX path delimiter
  270. size_t delimiter_pos = executable_name.find_last_of('/');
  271. executable_name.erase(0, delimiter_pos + 1);
  272. #endif
  273. }
  274. // Loop over all search paths and return the first hit
  275. for (unsigned int i = 0; i < sizeof(searchPath) / sizeof(char *); ++i) {
  276. std::string path(searchPath[i]);
  277. size_t executable_name_pos = path.find("<executable_name>");
  278. // If there is executable_name variable in the searchPath
  279. // replace it with the value
  280. if (executable_name_pos != std::string::npos) {
  281. if (executable_path != 0) {
  282. path.replace(executable_name_pos, strlen("<executable_name>"),
  283. executable_name);
  284. } else {
  285. // Skip this path entry if no executable argument is given
  286. continue;
  287. }
  288. }
  289. #ifdef _DEBUG
  290. printf("sdkFindFilePath <%s> in %s\n", filename, path.c_str());
  291. #endif
  292. // Test if the file exists
  293. path.append(filename);
  294. FILE *fp;
  295. FOPEN(fp, path.c_str(), "rb");
  296. if (fp != NULL) {
  297. fclose(fp);
  298. // File found
  299. // returning an allocated array here for backwards compatibility reasons
  300. char *file_path = reinterpret_cast<char *>(malloc(path.length() + 1));
  301. STRCPY(file_path, path.length() + 1, path.c_str());
  302. return file_path;
  303. }
  304. if (fp) {
  305. fclose(fp);
  306. }
  307. }
  308. // File not found
  309. return 0;
  310. }
  311. #endif // COMMON_HELPER_STRING_H_