Assignments Assignments

cmdLineParser.inl
Go to the documentation of this file.
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#include <iostream>
29#include <sstream>
30#include <fstream>
31#include <algorithm>
32#include <cassert>
33#include <string.h>
34#include <Util/exceptions.h>
35
36 namespace Util
37{
39 // CmdLineReadable //
41 inline CmdLineReadable::CmdLineReadable( const std::string &n ) : name(n) , set(false) {}
42
43 inline CmdLineReadable::~CmdLineReadable( void ){}
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 >
95 CmdLineParameters< Type >::~CmdLineParameters( void )
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}
Util::CmdLineParameterArray::read
int read(char **argv, int argc)
Definition cmdLineParser.inl:77
Util::CmdLineParameterArray::values
Type values[Dim]
Definition cmdLineParser.h:85
Util::CmdLineParameterArray::CmdLineParameterArray
CmdLineParameterArray(const std::string &name, const Type *v=NULL)
Definition cmdLineParser.inl:70
Util::CmdLineParameter::value
Type value
Definition cmdLineParser.h:66
Util::CmdLineParameter::CmdLineParameter
CmdLineParameter(const std::string &name)
Definition cmdLineParser.inl:50
Util::CmdLineParameter::read
int read(char **argv, int argc)
Definition cmdLineParser.inl:55
Util::CmdLineParameters::~CmdLineParameters
~CmdLineParameters(void)
Definition cmdLineParser.inl:95
Util::CmdLineParameters::read
int read(char **argv, int argc)
Definition cmdLineParser.inl:103
Util::CmdLineParameters::CmdLineParameters
CmdLineParameters(const std::string &name)
Definition cmdLineParser.inl:92
Util::CmdLineReadable
Definition cmdLineParser.h:41
Util::CmdLineReadable::set
bool set
Definition cmdLineParser.h:44
Util::CmdLineReadable::read
virtual int read(char **argv, int argc)
Definition cmdLineParser.inl:45
Util::CmdLineReadable::~CmdLineReadable
virtual ~CmdLineReadable(void)
Definition cmdLineParser.inl:43
Util::CmdLineReadable::CmdLineReadable
CmdLineReadable(const std::string &name)
Definition cmdLineParser.inl:41
exceptions.h
THROW
#define THROW(...)
Definition exceptions.h:151
WARN
#define WARN(...)
Definition exceptions.h:145
params
CmdLineReadable * params[]
Definition main1.cpp:37
Util
Definition algebra.h:7
Util::GetFileDirectory
std::string GetFileDirectory(const std::string &fileName)
Definition cmdLineParser.inl:183
Util::CmdLineParse
void CmdLineParse(int argc, char **argv, CmdLineReadable **params)
Definition cmdLineParser.inl:125
Util::ToLower
std::string ToLower(const std::string &str)
Definition cmdLineParser.inl:158
Util::FileSeparator
static const char FileSeparator
Definition cmdLineParser.h:120
Util::GetLocalFileName
std::string GetLocalFileName(const std::string &fileName)
Definition cmdLineParser.inl:175
Util::ToUpper
std::string ToUpper(const std::string &str)
Definition cmdLineParser.inl:149
Util::GetFileName
std::string GetFileName(const std::string &directory, const std::string &localFileName)
Definition cmdLineParser.inl:191
Util::GetFileExtension
std::string GetFileExtension(const std::string &fileName)
Definition cmdLineParser.inl:167
Util::ReadWords
std::vector< std::string > ReadWords(const std::string &fileName)
Definition cmdLineParser.inl:199