forked from RoyalCaliber/vertexAPI2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.cu
102 lines (90 loc) · 2.47 KB
/
util.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/******************************************************************************
Copyright 2013 Royal Caliber LLC. (http://www.royal-caliber.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/
#include "util.cuh"
#include <cstdarg>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sys/time.h>
#include <map>
int64_t currentTime()
{
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec * 1000000l + tv.tv_usec;
}
int parseCmdLineSimple(int argc, char** argv, const char* fmt, ...)
{
std::map<char, bool> optChars;
//scan all optional arguments
for(int i = 1; i < argc; ++i)
{
const char* arg = argv[i];
if( arg[0] == '-' )
{
if( arg[1] == 0 || arg[2] != 0 )
{
printf("parseCmdLineSimple: invalid option '%s'\n", arg);
return 0;
}
optChars[arg[1]] = true;
}
}
//now go through positional arguments
va_list args;
va_start(args, fmt);
int iArg = 1;
bool isOpt = false;
bool required = true;
for( const char *f = fmt; *f; ++f )
{
if( isOpt )
{
isOpt = false;
*(va_arg(args, bool*)) = (optChars.find(*f) != optChars.end());
}
else if( *f == '-' )
isOpt = true;
else if( *f == '|' )
{
required = false;
}
else
{
while( iArg < argc && argv[iArg][0] == '-' )
++iArg;
if( iArg == argc )
{
if( required )
{
printf("parseCmdLineSimple: expected argument of type %c\n", *f);
return 0;
}
else
return 1;
}
switch( *f )
{
case 's': *(va_arg(args, char**)) = strdup(argv[iArg]); break;
case 'i': *(va_arg(args, int*)) = atoi(argv[iArg]); break;
case 'f': *(va_arg(args, float*)) = atof(argv[iArg]); break;
default:
printf("parseCmdLineSimple: bad format character '%c'\n", *f);
return 0;
}
++iArg;
}
}
va_end(args);
return 1;
}