Assignments
Assignments
cmdLineParser.inl
Go to the documentation of this file.
1/* -*- C++ -*-
2Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
8Redistributions of source code must retain the above copyright notice, this list of
9conditions and the following disclaimer. Redistributions in binary form must reproduce
10the above copyright notice, this list of conditions and the following disclaimer
11in the documentation and/or other materials provided with the distribution.
12
13Neither the name of the Johns Hopkins University nor the names of its contributors
14may be used to endorse or promote products derived from this software without specific
15prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
18EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES
19OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
22TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26DAMAGE.
27*/
28#include <iostream>
29#include <sstream>
30#include <fstream>
31#include <algorithm>
32#include <cassert>
33#include <string.h>
34#include <Util/exceptions.h>
35
36namespace Util
37{
39 // CmdLineReadable //
41 inline CmdLineReadable::CmdLineReadable( const std::string &n ) : name(n) , set(false) {}
42
44
45 inline int CmdLineReadable::read( char ** , int ){ set = true ; return 0; }
46
48 // CmdLineParameter //
50 template< class Type > CmdLineParameter< Type >::CmdLineParameter( const std::string &name ) : CmdLineReadable(name) { value = Type(); }
51
52 template< class Type > CmdLineParameter< Type >::CmdLineParameter( const std::string &name , Type v ) : CmdLineReadable(name) , value(v) {}
53
54 template< class Type >
55 int CmdLineParameter< Type >::read( char **argv , int argc )
56 {
57 if( argc>0 )
58 {
59 std::stringstream( argv[0] ) >> value;
60 set = true;
61 return 1;
62 }
63 else return 0;
64 }
65
67 // CmdLineParameterArray //
69 template< class Type , int Dim >
70 CmdLineParameterArray< Type , Dim >::CmdLineParameterArray( const std::string &name , const Type* v ) : CmdLineReadable(name)
71 {
72 if( v ) for( int i=0 ; i<Dim ; i++ ) values[i] = v[i];
73 else for( int i=0 ; i<Dim ; i++ ) values[i] = Type();
74 }
75
76 template< class Type , int Dim >
77 int CmdLineParameterArray< Type , Dim >::read( char **argv , int argc )
78 {
79 if( argc>=Dim )
80 {
81 for( int i=0 ; i<Dim ; i++ ) std::stringstream( argv[i] ) >> values[i];
82 set = true;
83 return Dim;
84 }
85 else return 0;
86 }
87
89 // CmdLineParameters //
91 template< class Type >
92 CmdLineParameters< Type >::CmdLineParameters( const std::string &name ) : CmdLineReadable(name) , values(NULL) , count(0) { }
93
94 template< class Type >
96 {
97 if( values ) delete[] values;
98 values = NULL;
99 count = 0;
100 }
101
102 template< class Type >
103 int CmdLineParameters< Type >::read( char **argv , int argc )
104 {
105 if( values ) delete[] values;
106 values = NULL;
107
108 if( argc>0 )
109 {
110 count = atoi(argv[0]);
111 if( count <= 0 || argc <= count ) return 1;
112 values = new Type[count];
113 if( !values ) return 0;
114 for( int i=0 ; i<count ; i++ ) std::stringstream( argv[i+1] ) >> values[i];
115 set = true;
116 return count+1;
117 }
118 else return 0;
119 }
120
122 // Helper functions //
124
125 inline void CmdLineParse( int argc , char **argv , CmdLineReadable** params )
126 {
127 while( argc>0 )
128 {
129 if( argv[0][0]=='-' && argv[0][1]=='-' )
130 {
131 CmdLineReadable* readable=NULL;
132 for( int i=0 ; params[i]!=NULL && readable==NULL ; i++ ) if( params[i]->name==argv[0]+2 ) readable = params[i];
133 if( readable )
134 {
135 int j = readable->read( argv+1 , argc-1 );
136 argv += j , argc -= j;
137 }
138 else
139 {
140 WARN( "Invalid option: %s" , argv[0] );
141 for( int i=0 ; params[i]!=NULL ; i++ ) std::cerr << "\t--" << params[i]->name << std::endl;
142 }
143 }
144 else WARN( "Parameter name should be of the form --<name>: %s" , argv[0] );
145 ++argv , --argc;
146 }
147 }
148
149 inline std::string ToUpper( const std::string &str )
150 {
151 auto _ToUpper = []( char c ){ return c>='a' && c<='z' ? c+'A'-'a' : c; };
152 std::string upper;
153 upper.resize( str.size() );
154 std::transform( str.begin() , str.end() , upper.begin() , _ToUpper );
155 return upper;
156 }
157
158 inline std::string ToLower( const std::string &str )
159 {
160 auto _ToLower = []( char c ){ return c>='A' && c<='Z' ? c+'a'-'A' : c; };
161 std::string lower;
162 lower.resize( str.size() );
163 std::transform( str.begin() , str.end() , lower.begin() , _ToLower );
164 return lower;
165 }
166
167 inline std::string GetFileExtension( const std::string &fileName )
168 {
169 std::string ext;
170 std::stringstream stream( fileName );
171 while( std::getline( stream , ext , '.' ) ) ;
172 return ext;
173 }
174
175 inline std::string GetLocalFileName( const std::string &fileName )
176 {
177 size_t idx = -1;
178 for( int i=(int)fileName.size()-1 ; i>=0 ; i-- ) if( fileName[i]==FileSeparator ){ idx = i ; break; }
179 if( idx==-1 ) return fileName;
180 else return fileName.substr( idx+1 , fileName.size() );
181 };
182
183 inline std::string GetFileDirectory( const std::string &fileName )
184 {
185 size_t idx = -1;
186 for( int i=(int)fileName.size()-1 ; i>=0 ; i-- ) if( fileName[i]==FileSeparator ){ idx = i ; break; }
187 if( idx==-1 ) return "." + std::string( 1 , FileSeparator );
188 else return fileName.substr( 0 , idx+1 );
189 };
190
191 inline std::string GetFileName( const std::string &directory , const std::string &localFileName )
192 {
193 if( !directory.size() ) return localFileName;
194 else if( localFileName[0]==FileSeparator ) return localFileName;
195 else if( directory.back()==FileSeparator ) return directory + localFileName;
196 else return directory + FileSeparator + localFileName;
197 };
198
199 inline std::vector< std::string > ReadWords( const std::string &fileName )
200 {
201 std::ifstream istream;
202 istream.open( fileName );
203 if( !istream ) THROW( "Failed to open file for reading: " , fileName );
204 std::vector< std::string > words;
205 std::string word;
206 while( istream >> word ) words.push_back( word );
207 return words;
208 }
209}
int read(char **argv, int argc)
Definition cmdLineParser.inl:77
Type values[Dim]
Definition cmdLineParser.h:85
CmdLineParameterArray(const std::string &name, const Type *v=NULL)
Definition cmdLineParser.inl:70
Type value
Definition cmdLineParser.h:66
CmdLineParameter(const std::string &name)
Definition cmdLineParser.inl:50
int read(char **argv, int argc)
Definition cmdLineParser.inl:55
~CmdLineParameters(void)
Definition cmdLineParser.inl:95
int read(char **argv, int argc)
Definition cmdLineParser.inl:103
CmdLineParameters(const std::string &name)
Definition cmdLineParser.inl:92
Definition cmdLineParser.h:41
bool set
Definition cmdLineParser.h:44
virtual int read(char **argv, int argc)
Definition cmdLineParser.inl:45
virtual ~CmdLineReadable(void)
Definition cmdLineParser.inl:43
CmdLineReadable(const std::string &name)
Definition cmdLineParser.inl:41
#define THROW(...)
Definition exceptions.h:151
#define WARN(...)
Definition exceptions.h:145
CmdLineReadable * params[]
Definition main1.cpp:37
Definition algebra.h:7
std::string GetFileDirectory(const std::string &fileName)
Definition cmdLineParser.inl:183
void CmdLineParse(int argc, char **argv, CmdLineReadable **params)
Definition cmdLineParser.inl:125
std::string ToLower(const std::string &str)
Definition cmdLineParser.inl:158
static const char FileSeparator
Definition cmdLineParser.h:120
std::string GetLocalFileName(const std::string &fileName)
Definition cmdLineParser.inl:175
std::string ToUpper(const std::string &str)
Definition cmdLineParser.inl:149
std::string GetFileName(const std::string &directory, const std::string &localFileName)
Definition cmdLineParser.inl:191
std::string GetFileExtension(const std::string &fileName)
Definition cmdLineParser.inl:167
std::vector< std::string > ReadWords(const std::string &fileName)
Definition cmdLineParser.inl:199