291
|
1 /**
|
|
2 * Copyright 1993-2013 NVIDIA Corporation. All rights reserved.
|
|
3 *
|
|
4 * Please refer to the NVIDIA end user license agreement (EULA) associated
|
|
5 * with this source code for terms and conditions that govern your use of
|
|
6 * this software. Any use, reproduction, disclosure, or distribution of
|
|
7 * this software and related documentation outside the terms of the EULA
|
|
8 * is strictly prohibited.
|
|
9 *
|
|
10 */
|
|
11
|
|
12 // These are helper functions for the SDK samples (string parsing, timers, etc)
|
|
13 #ifndef STRING_HELPER_H
|
|
14 #define STRING_HELPER_H
|
|
15
|
|
16 #include <stdio.h>
|
|
17 #include <stdlib.h>
|
303
|
18 #include <string.h>
|
291
|
19
|
|
20 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
|
|
21 #ifndef _CRT_SECURE_NO_DEPRECATE
|
|
22 #define _CRT_SECURE_NO_DEPRECATE
|
|
23 #endif
|
|
24 #ifndef STRCASECMP
|
|
25 #define STRCASECMP _stricmp
|
|
26 #endif
|
|
27 #ifndef STRNCASECMP
|
|
28 #define STRNCASECMP _strnicmp
|
|
29 #endif
|
|
30 #ifndef STRCPY
|
|
31 #define STRCPY(sFilePath, nLength, sPath) strcpy_s(sFilePath, nLength, sPath)
|
|
32 #endif
|
|
33
|
|
34 #ifndef FOPEN
|
|
35 #define FOPEN(fHandle,filename,mode) fopen_s(&fHandle, filename, mode)
|
|
36 #endif
|
|
37 #ifndef FOPEN_FAIL
|
|
38 #define FOPEN_FAIL(result) (result != 0)
|
|
39 #endif
|
|
40 #ifndef SSCANF
|
|
41 #define SSCANF sscanf_s
|
|
42 #endif
|
|
43 #ifndef SPRINTF
|
|
44 #define SPRINTF sprintf_s
|
|
45 #endif
|
|
46 #else // Linux Includes
|
|
47 #include <string.h>
|
|
48 #include <strings.h>
|
|
49
|
|
50 #ifndef STRCASECMP
|
|
51 #define STRCASECMP strcasecmp
|
|
52 #endif
|
|
53 #ifndef STRNCASECMP
|
|
54 #define STRNCASECMP strncasecmp
|
|
55 #endif
|
|
56 #ifndef STRCPY
|
|
57 #define STRCPY(sFilePath, nLength, sPath) strcpy(sFilePath, sPath)
|
|
58 #endif
|
|
59
|
|
60 #ifndef FOPEN
|
|
61 #define FOPEN(fHandle,filename,mode) (fHandle = fopen(filename, mode))
|
|
62 #endif
|
|
63 #ifndef FOPEN_FAIL
|
|
64 #define FOPEN_FAIL(result) (result == NULL)
|
|
65 #endif
|
|
66 #ifndef SSCANF
|
|
67 #define SSCANF sscanf
|
|
68 #endif
|
|
69 #ifndef SPRINTF
|
|
70 #define SPRINTF sprintf
|
|
71 #endif
|
|
72 #endif
|
|
73
|
|
74 #ifndef EXIT_WAIVED
|
|
75 #define EXIT_WAIVED 2
|
|
76 #endif
|
|
77
|
309
|
78 #ifndef bool
|
|
79 typedef int bool;
|
|
80 #define false 0
|
|
81 #define true 1
|
|
82 #endif
|
|
83
|
291
|
84 // CUDA Utility Helper Functions
|
|
85 inline int stringRemoveDelimiter(char delimiter, const char *string)
|
|
86 {
|
|
87 int string_start = 0;
|
|
88
|
|
89 while (string[string_start] == delimiter)
|
|
90 {
|
|
91 string_start++;
|
|
92 }
|
|
93
|
|
94 if (string_start >= (int)strlen(string)-1)
|
|
95 {
|
|
96 return 0;
|
|
97 }
|
|
98
|
|
99 return string_start;
|
|
100 }
|
|
101
|
|
102 inline int getFileExtension(char *filename, char **extension)
|
|
103 {
|
|
104 int string_length = (int)strlen(filename);
|
|
105
|
|
106 while (filename[string_length--] != '.')
|
|
107 {
|
|
108 if (string_length == 0)
|
|
109 break;
|
|
110 }
|
|
111
|
|
112 if (string_length > 0) string_length += 2;
|
|
113
|
|
114 if (string_length == 0)
|
|
115 *extension = NULL;
|
|
116 else
|
|
117 *extension = &filename[string_length];
|
|
118
|
|
119 return string_length;
|
|
120 }
|
|
121
|
|
122
|
|
123 inline bool checkCmdLineFlag(const int argc, const char **argv, const char *string_ref)
|
|
124 {
|
|
125 bool bFound = false;
|
|
126
|
|
127 if (argc >= 1)
|
|
128 {
|
|
129 for (int i=1; i < argc; i++)
|
|
130 {
|
|
131 int string_start = stringRemoveDelimiter('-', argv[i]);
|
|
132 const char *string_argv = &argv[i][string_start];
|
|
133
|
|
134 const char *equal_pos = strchr(string_argv, '=');
|
|
135 int argv_length = (int)(equal_pos == 0 ? strlen(string_argv) : equal_pos - string_argv);
|
|
136
|
|
137 int length = (int)strlen(string_ref);
|
|
138
|
|
139 if (length == argv_length && !STRNCASECMP(string_argv, string_ref, length))
|
|
140 {
|
|
141 bFound = true;
|
|
142 continue;
|
|
143 }
|
|
144 }
|
|
145 }
|
|
146
|
|
147 return bFound;
|
|
148 }
|
|
149
|
|
150
|
|
151 inline int getCmdLineArgumentInt(const int argc, const char **argv, const char *string_ref)
|
|
152 {
|
|
153 bool bFound = false;
|
|
154 int value = -1;
|
|
155
|
|
156 if (argc >= 1)
|
|
157 {
|
|
158 for (int i=1; i < argc; i++)
|
|
159 {
|
|
160 int string_start = stringRemoveDelimiter('-', argv[i]);
|
|
161 const char *string_argv = &argv[i][string_start];
|
|
162 int length = (int)strlen(string_ref);
|
|
163
|
|
164 if (!STRNCASECMP(string_argv, string_ref, length))
|
|
165 {
|
|
166 if (length+1 <= (int)strlen(string_argv))
|
|
167 {
|
|
168 int auto_inc = (string_argv[length] == '=') ? 1 : 0;
|
|
169 value = atoi(&string_argv[length + auto_inc]);
|
|
170 }
|
|
171 else
|
|
172 {
|
|
173 value = 0;
|
|
174 }
|
|
175
|
|
176 bFound = true;
|
|
177 continue;
|
|
178 }
|
|
179 }
|
|
180 }
|
|
181
|
|
182 if (bFound)
|
|
183 {
|
|
184 return value;
|
|
185 }
|
|
186 else
|
|
187 {
|
|
188 return 0;
|
|
189 }
|
|
190 }
|
|
191
|
|
192 inline float getCmdLineArgumentFloat(const int argc, const char **argv, const char *string_ref)
|
|
193 {
|
|
194 bool bFound = false;
|
|
195 float value = -1;
|
|
196
|
|
197 if (argc >= 1)
|
|
198 {
|
|
199 for (int i=1; i < argc; i++)
|
|
200 {
|
|
201 int string_start = stringRemoveDelimiter('-', argv[i]);
|
|
202 const char *string_argv = &argv[i][string_start];
|
|
203 int length = (int)strlen(string_ref);
|
|
204
|
|
205 if (!STRNCASECMP(string_argv, string_ref, length))
|
|
206 {
|
|
207 if (length+1 <= (int)strlen(string_argv))
|
|
208 {
|
|
209 int auto_inc = (string_argv[length] == '=') ? 1 : 0;
|
|
210 value = (float)atof(&string_argv[length + auto_inc]);
|
|
211 }
|
|
212 else
|
|
213 {
|
|
214 value = 0.f;
|
|
215 }
|
|
216
|
|
217 bFound = true;
|
|
218 continue;
|
|
219 }
|
|
220 }
|
|
221 }
|
|
222
|
|
223 if (bFound)
|
|
224 {
|
|
225 return value;
|
|
226 }
|
|
227 else
|
|
228 {
|
|
229 return 0;
|
|
230 }
|
|
231 }
|
|
232
|
|
233 inline bool getCmdLineArgumentString(const int argc, const char **argv,
|
|
234 const char *string_ref, char **string_retval)
|
|
235 {
|
|
236 bool bFound = false;
|
|
237
|
|
238 if (argc >= 1)
|
|
239 {
|
|
240 for (int i=1; i < argc; i++)
|
|
241 {
|
|
242 int string_start = stringRemoveDelimiter('-', argv[i]);
|
|
243 char *string_argv = (char *)&argv[i][string_start];
|
|
244 int length = (int)strlen(string_ref);
|
|
245
|
|
246 if (!STRNCASECMP(string_argv, string_ref, length))
|
|
247 {
|
|
248 *string_retval = &string_argv[length+1];
|
|
249 bFound = true;
|
|
250 continue;
|
|
251 }
|
|
252 }
|
|
253 }
|
|
254
|
|
255 if (!bFound)
|
|
256 {
|
|
257 *string_retval = NULL;
|
|
258 }
|
|
259
|
|
260 return bFound;
|
|
261 }
|
|
262
|
|
263
|
|
264 #endif
|