Debian Codegraph @

src/thirdparty/PoissonRecon/CmdLineParser.inl

raw
1 /* -*- C++ -*-
2 Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
7
8 Redistributions of source code must retain the above copyright notice, this list of
9 conditions and the following disclaimer. Redistributions in binary form must reproduce
10 the above copyright notice, this list of conditions and the following disclaimer
11 in the documentation and/or other materials provided with the distribution.
12
13 Neither the name of the Johns Hopkins University nor the names of its contributors
14 may be used to endorse or promote products derived from this software without specific
15 prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22 TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGE.
27 */
28
29 #if defined( WIN32 ) || defined( _WIN64 )
30 inline int strcasecmp( const char* c1 , const char* c2 ){ return _stricmp( c1 , c2 ); }
31 #endif // WIN32 || _WIN64
32
33 template< > inline void CmdLineType< char* >::CleanUp( char** t ){ if( *t ) free( *t ) ; *t = NULL; }
34
35 template< > inline int CmdLineType< int >::Initialize( void ){ return 0; }
36 template< > inline unsigned int CmdLineType< unsigned int >::Initialize( void ){ return 0u; }
37 template< > inline float CmdLineType< float >::Initialize( void ){ return 0.f; }
38 template< > inline double CmdLineType< double >::Initialize( void ){ return 0.; }
39 template< > inline char * CmdLineType< char * >::Initialize( void ){ return NULL; }
40
41 template< > inline void CmdLineType< int >::WriteValue( int t , char* str ){ sprintf( str , "%d" , t ); }
42 template< > inline void CmdLineType< unsigned int >::WriteValue( unsigned int t , char* str ){ sprintf( str , "%u" , t ); }
43 template< > inline void CmdLineType< float >::WriteValue( float t , char* str ){ sprintf( str , "%f" , t ); }
44 template< > inline void CmdLineType< double >::WriteValue( double t , char* str ){ sprintf( str , "%f" , t ); }
45 template< > inline void CmdLineType< char * >::WriteValue( char *t , char* str ){ if( t ) sprintf( str , "%s" , t ) ; else str[0]=0; }
46 template< > inline void CmdLineType< std::string >::WriteValue( std::string t , char* str ){ sprintf( str , "%s" , t.c_str() ); }
47 template< typename Real , unsigned int Dim >
48 void CmdLineType< Point< Real , Dim > >::WriteValue( Point< Real , Dim > t , char *str )
49 {
50 size_t start = 0;
51 sprintf( str , "{" );
52 start = strlen( str );
53 for( unsigned int d=0 ; d<Dim ; d++ )
54 {
55 if( d ) sprintf( str+start , ",%f" , t[d] );
56 else sprintf( str+start , "%f" , t[d] );
57 start = strlen( str );
58 }
59 sprintf( str+start , "}" );
60 }
61
62 #if defined( WIN32 ) || defined( _WIN64 )
63 template< > inline char* CmdLineType< char* >::Copy( char* t ){ return _strdup( t ); }
64 #else // !WIN32 && !_WIN64
65 template< > inline char* CmdLineType< char* >::Copy( char* t ){ return strdup( t ); }
66 #endif // WIN32 || _WIN64
67
68 template< > inline int CmdLineType< int >::StringToType( const char* str ){ return atoi( str ); }
69 template< > inline unsigned int CmdLineType< unsigned int >::StringToType( const char* str ){ return (unsigned int)atoll( str ); }
70 template< > inline float CmdLineType< float >::StringToType( const char* str ){ return float( atof( str ) ); }
71 template< > inline double CmdLineType< double >::StringToType( const char* str ){ return double( atof( str ) ); }
72 #if defined( WIN32 ) || defined( _WIN64 )
73 template< > inline char * CmdLineType< char* >::StringToType( const char* str ){ return _strdup( str ); }
74 #else // !WIN32 && !_WIN64
75 template< > inline char * CmdLineType< char* >::StringToType( const char* str ){ return strdup( str ); }
76 #endif // WIN32 || _WIN64
77 template< > inline std::string CmdLineType< std::string >::StringToType( const char* str ){ return std::string( str ); }
78
79 template< typename Real , unsigned int Dim >
80 Point< Real , Dim > CmdLineType< Point< Real , Dim > >::StringToType( const char *str )
81 {
82 Point< Real , Dim > t;
83
84 char *_str = new char[ strlen(str)+1 ];
85 assert( _str );
86 strcpy( _str , str );
87 char *temp = strtok( _str , "," );
88
89 unsigned int d = 0;
90 while( temp!=NULL )
91 {
92 if( d==0 )
93 {
94 if( temp[0]!='{' ) MK_THROW( "Expected opening brace: " , std::string( str ) );
95 t[d++] = CmdLineType< Real >::StringToType( temp+1 );
96 }
97 else if( d==Dim-1 )
98 {
99 if( temp[ strlen(temp)-1 ]!='}' ) MK_THROW( "Expected closing brace: " , std::string( str ) );
100 temp[ strlen(temp)-1 ] = 0;
101 t[d++] = CmdLineType< Real >::StringToType( temp );
102 break;
103 }
104 else t[d++] = CmdLineType< Real >::StringToType( temp );
105 temp = strtok( NULL ,"," );
106 }
107 if( d<Dim ) MK_THROW( "Could not parse all coordinates: " , std::string( str ) , " (" , Dim , ")" );
108 delete[] _str;
109 return t;
110 }
111
112
113 /////////////////////
114 // CmdLineReadable //
115 /////////////////////
116 #if defined( WIN32 ) || defined( _WIN64 )
117 inline CmdLineReadable::CmdLineReadable( const char *name ) : set(false) { this->name = _strdup( name ); }
118 #else // !WIN32 && !_WIN64
119 inline CmdLineReadable::CmdLineReadable( const char *name ) : set(false) { this->name = strdup( name ); }
120 #endif // WIN32 || _WIN64
121
122 inline CmdLineReadable::~CmdLineReadable( void ){ if( name ) free( name ) ; name = NULL; }
123 inline int CmdLineReadable::read( char** , int ){ set = true ; return 0; }
124 inline void CmdLineReadable::writeValue( char* str ) const { str[0] = 0; }
125
126 //////////////////////
127 // CmdLineParameter //
128 //////////////////////
129 template< class Type > CmdLineParameter< Type >::~CmdLineParameter( void ) { CmdLineType< Type >::CleanUp( &value ); }
130 template< class Type > CmdLineParameter< Type >::CmdLineParameter( const char *name ) : CmdLineReadable( name ){ value = CmdLineType< Type >::Initialize(); }
131 template< class Type > CmdLineParameter< Type >::CmdLineParameter( const char *name , Type v ) : CmdLineReadable( name ){ value = CmdLineType< Type >::Copy( v ); }
132 template< class Type >
133 int CmdLineParameter< Type >::read( char** argv , int argc )
134 {
135 if( argc>0 )
136 {
137 CmdLineType< Type >::CleanUp( &value );
138 value = CmdLineType< Type >::StringToType( argv[0] );
139 set = true;
140 return 1;
141 }
142 else return 0;
143 }
144 template< class Type >
145 void CmdLineParameter< Type >::writeValue( char* str ) const { CmdLineType< Type >::WriteValue( value , str ); }
146
147
148 ///////////////////////////
149 // CmdLineParameterArray //
150 ///////////////////////////
151 template< class Type , int Dim >
152 CmdLineParameterArray< Type , Dim >::CmdLineParameterArray( const char *name , const Type* v ) : CmdLineReadable( name )
153 {
154 if( v ) for( int i=0 ; i<Dim ; i++ ) values[i] = CmdLineType< Type >::Copy( v[i] );
155 else for( int i=0 ; i<Dim ; i++ ) values[i] = CmdLineType< Type >::Initialize();
156 }
157
158 template< class Type , int Dim >
159 CmdLineParameterArray< Type , Dim >::~CmdLineParameterArray( void ){ for( int i=0 ; i<Dim ; i++ ) CmdLineType< Type >::CleanUp( values+i ); }
160
161 template< class Type , int Dim >
162 int CmdLineParameterArray< Type , Dim >::read( char** argv , int argc )
163 {
164 if( argc>=Dim )
165 {
166 for( int i=0 ; i<Dim ; i++ ) CmdLineType< Type >::CleanUp( values+i ) , values[i] = CmdLineType< Type >::StringToType( argv[i] );
167 set = true;
168 return Dim;
169 }
170 else return 0;
171 }
172 template< class Type , int Dim >
173 void CmdLineParameterArray< Type , Dim >::writeValue( char* str ) const
174 {
175 char* temp=str;
176 for( int i=0 ; i<Dim ; i++ )
177 {
178 CmdLineType< Type >::WriteValue( values[i] , temp );
179 temp = str+strlen( str );
180 }
181 }
182 ///////////////////////
183 // CmdLineParameters //
184 ///////////////////////
185 template< class Type >
186 CmdLineParameters< Type >::CmdLineParameters( const char* name ) : CmdLineReadable( name ) , values(NULL) , count(0) { }
187
188 template< class Type >
189 CmdLineParameters< Type >::~CmdLineParameters( void )
190 {
191 if( values ) delete[] values;
192 values = NULL;
193 count = 0;
194 }
195
196 template< class Type >
197 int CmdLineParameters< Type >::read( char** argv , int argc )
198 {
199 if( values ) delete[] values;
200 values = NULL;
201
202 if( argc>0 )
203 {
204 count = atoi(argv[0]);
205 if( count <= 0 || argc <= count ) return 1;
206 values = new Type[count];
207 if( !values ) return 0;
208 for( int i=0 ; i<count ; i++ ) values[i] = CmdLineType< Type >::StringToType( argv[i+1] );
209 set = true;
210 return count+1;
211 }
212 else return 0;
213 }
214
215 template< class Type >
216 void CmdLineParameters< Type >::writeValue( char* str ) const
217 {
218 char* temp=str;
219 CmdLineType< int >::WriteValue( count , temp );
220 temp = str + strlen( str );
221 for( int i=0 ; i<count ; i++ )
222 {
223 temp[0] = ' ' , temp++;
224 CmdLineType< Type >::WriteValue( values[i] , temp );
225 temp = str+strlen( str );
226 }
227 }
228
229 inline char* FileExtension( char* fileName )
230 {
231 char* temp = fileName;
232 for( int i=0 ; i<strlen(fileName) ; i++ ) if( fileName[i]=='.' ) temp = &fileName[i+1];
233 return temp;
234 }
235
236 inline char* GetFileExtension( const char* fileName )
237 {
238 char* fileNameCopy;
239 char* ext=NULL;
240 char* temp;
241
242 fileNameCopy=new char[strlen(fileName)+1];
243 assert(fileNameCopy);
244 strcpy(fileNameCopy,fileName);
245 temp=strtok(fileNameCopy,".");
246 while(temp!=NULL)
247 {
248 if(ext!=NULL){delete[] ext;}
249 ext=new char[strlen(temp)+1];
250 assert(ext);
251 strcpy(ext,temp);
252 temp=strtok(NULL,".");
253 }
254 delete[] fileNameCopy;
255 return ext;
256 }
257 inline char* GetLocalFileName( const char* fileName )
258 {
259 char* fileNameCopy;
260 char* name=NULL;
261 char* temp;
262
263 fileNameCopy=new char[strlen(fileName)+1];
264 assert(fileNameCopy);
265 strcpy(fileNameCopy,fileName);
266 temp=strtok(fileNameCopy,"\\");
267 while(temp!=NULL){
268 if(name!=NULL){delete[] name;}
269 name=new char[strlen(temp)+1];
270 assert(name);
271 strcpy(name,temp);
272 temp=strtok(NULL,"\\");
273 }
274 delete[] fileNameCopy;
275 return name;
276 }
277 inline char* LocalFileName( char* fileName )
278 {
279 char* temp = fileName;
280 for( int i=0 ; i<(int)strlen(fileName) ; i++ ) if( fileName[i] =='\\' ) temp = &fileName[i+1];
281 return temp;
282 }
283 inline char* DirectoryName( char* fileName )
284 {
285 for( int i=int( strlen(fileName) )-1 ; i>=0 ; i-- )
286 if( fileName[i] =='\\' )
287 {
288 fileName[i] = 0;
289 return fileName;
290 }
291 fileName[0] = 0;
292 return fileName;
293 }
294
295 inline void CmdLineParse( int argc , char **argv , CmdLineReadable** params )
296 {
297 while( argc>0 )
298 {
299 if( argv[0][0]=='-' && argv[0][1]=='-' )
300 {
301 CmdLineReadable* readable=NULL;
302 for( int i=0 ; params[i]!=NULL && readable==NULL ; i++ ) if( !strcasecmp( params[i]->name , argv[0]+2 ) ) readable = params[i];
303 if( readable )
304 {
305 int j = readable->read( argv+1 , argc-1 );
306 argv += j , argc -= j;
307 }
308 else
309 {
310 MK_WARN( "Invalid option: " , argv[0] );
311 for( int i=0 ; params[i]!=NULL ; i++ ) fprintf( stderr , "\t--%s\n" , params[i]->name );
312 }
313 }
314 else MK_WARN( "Parameter name should be of the form --<name>: " , argv[0] );
315 ++argv , --argc;
316 }
317 }
318
319 inline char** ReadWords(const char* fileName,int& cnt)
320 {
321 char** names;
322 char temp[500];
323 FILE* fp;
324
325 fp=fopen(fileName,"r");
326 if(!fp){return NULL;}
327 cnt=0;
328 while(fscanf(fp," %s ",temp)==1){cnt++;}
329 fclose(fp);
330
331 names=new char*[cnt];
332 if(!names){return NULL;}
333
334 fp=fopen(fileName,"r");
335 if(!fp){
336 delete[] names;
337 cnt=0;
338 return NULL;
339 }
340 cnt=0;
341 while(fscanf(fp," %s ",temp)==1){
342 names[cnt]=new char[strlen(temp)+1];
343 if(!names){
344 for(int j=0;j<cnt;j++){delete[] names[j];}
345 delete[] names;
346 cnt=0;
347 fclose(fp);
348 return NULL;
349 }
350 strcpy(names[cnt],temp);
351 cnt++;
352 }
353 fclose(fp);
354 return names;
355 }