Chaste Release::3.1
|
00001 /* 00002 00003 Copyright (c) 2005-2012, University of Oxford. 00004 All rights reserved. 00005 00006 University of Oxford means the Chancellor, Masters and Scholars of the 00007 University of Oxford, having an administrative office at Wellington 00008 Square, Oxford OX1 2JD, UK. 00009 00010 This file is part of Chaste. 00011 00012 Redistribution and use in source and binary forms, with or without 00013 modification, are permitted provided that the following conditions are met: 00014 * Redistributions of source code must retain the above copyright notice, 00015 this list of conditions and the following disclaimer. 00016 * Redistributions in binary form must reproduce the above copyright notice, 00017 this list of conditions and the following disclaimer in the documentation 00018 and/or other materials provided with the distribution. 00019 * Neither the name of the University of Oxford nor the names of its 00020 contributors may be used to endorse or promote products derived from this 00021 software without specific prior written permission. 00022 00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00033 00034 */ 00035 00036 #include "CommandLineArguments.hpp" 00037 00038 #include <cassert> 00039 #include <cstddef> 00040 00041 CommandLineArguments::CommandLineArguments() 00042 : p_argc(NULL), 00043 p_argv(NULL) 00044 { 00045 // Make doubly sure there's only one instance 00046 assert(mpInstance == NULL); 00047 } 00048 00049 CommandLineArguments* CommandLineArguments::Instance() 00050 { 00051 if (mpInstance == NULL) 00052 { 00053 mpInstance = new CommandLineArguments; 00054 } 00055 return mpInstance; 00056 } 00057 00058 CommandLineArguments* CommandLineArguments::mpInstance = NULL; 00059 00060 bool CommandLineArguments::OptionExists(std::string option) 00061 { 00062 int index = GetIndexForArgument(option); 00063 assert(index!=0); 00064 00065 return (index > 0); 00066 } 00067 00068 char* CommandLineArguments::GetValueCorrespondingToOption(std::string option, int valueNumber) 00069 { 00070 EXCEPT_IF_NOT(valueNumber>0); 00071 TestOptionFormat(option); 00072 00073 int num_args = GetNumberOfArgumentsForOption(option); 00074 int index = GetIndexForArgument(option); 00075 EXCEPT_IF_NOT(index>0); 00076 EXCEPT_IF_NOT(num_args>=valueNumber); 00077 return (*p_argv)[index+valueNumber]; 00078 } 00079 00080 double CommandLineArguments::GetDoubleCorrespondingToOption(std::string option, int valueNumber) 00081 { 00082 char* val = GetValueCorrespondingToOption(option, valueNumber); 00083 return atof(val); 00084 } 00085 00086 int CommandLineArguments::GetIntCorrespondingToOption(std::string option, int valueNumber) 00087 { 00088 char* val = GetValueCorrespondingToOption(option, valueNumber); 00089 return atoi(val); 00090 } 00091 00092 unsigned CommandLineArguments::GetUnsignedCorrespondingToOption(std::string option, int valueNumber) 00093 { 00094 char* val = GetValueCorrespondingToOption(option, valueNumber); 00095 int i = atoi(val); 00096 if (i < 0) 00097 { 00098 EXCEPTION("Option is a negative number and cannot be converted to unsigned."); 00099 } 00100 return (unsigned)(i); 00101 } 00102 00103 int CommandLineArguments::GetIndexForArgument(std::string argument) 00104 { 00105 TestOptionFormat(argument); 00106 00107 for (int i=1; i<*p_argc; i++) 00108 { 00109 if (argument==std::string((*p_argv)[i])) 00110 { 00111 return i; 00112 } 00113 } 00114 return -1; 00115 } 00116 00117 int CommandLineArguments::GetNumberOfArgumentsForOption(std::string option) 00118 { 00119 int start_idx = GetIndexForArgument(option); 00120 if (start_idx < 0) 00121 { 00122 EXCEPTION("Command line option '" + option + "' does not exist"); 00123 } 00124 00125 int end_idx = start_idx; 00126 for (int i=start_idx+1; i<*p_argc; i++) 00127 { 00128 std::string argument = std::string((*p_argv)[i]); 00129 if (argument.substr(0,1)=="-" && argument.substr(1,1).find_first_of("0123456789")==std::string::npos) 00130 { 00131 break; 00132 } 00133 end_idx = i; 00134 } 00135 00136 if (end_idx == start_idx) 00137 { 00138 EXCEPTION("No value(s) given after command line option '" + option + "'"); 00139 } 00140 00141 return end_idx - start_idx; 00142 } 00143 00144 std::string CommandLineArguments::GetStringCorrespondingToOption(std::string option, int valueNumber) 00145 { 00146 char* val = GetValueCorrespondingToOption(option, valueNumber); 00147 std::string string_arg(val); 00148 return string_arg; 00149 } 00150 00151 std::vector<std::string> CommandLineArguments::GetStringsCorrespondingToOption(std::string option) 00152 { 00153 std::vector<std::string> strings; 00154 int num_args = GetNumberOfArgumentsForOption(option); 00155 for(int i=1; i<=num_args; ++i) 00156 { 00157 strings.push_back(GetStringCorrespondingToOption(option, i)); 00158 } 00159 return strings; 00160 } 00161 00162 std::vector<double> CommandLineArguments::GetDoublesCorrespondingToOption(std::string option) 00163 { 00164 std::vector<double> doubles; 00165 int num_args = GetNumberOfArgumentsForOption(option); 00166 for(int i=1; i<=num_args; ++i) 00167 { 00168 doubles.push_back(GetDoubleCorrespondingToOption(option, i)); 00169 } 00170 return doubles; 00171 } 00172 00173 std::vector<unsigned> CommandLineArguments::GetUnsignedsCorrespondingToOption(std::string option) 00174 { 00175 std::vector<unsigned> unsigneds; 00176 int num_args = GetNumberOfArgumentsForOption(option); 00177 for(int i=1; i<=num_args; ++i) 00178 { 00179 unsigneds.push_back(GetUnsignedCorrespondingToOption(option, i)); 00180 } 00181 return unsigneds; 00182 } 00183 00184 std::vector<int> CommandLineArguments::GetIntsCorrespondingToOption(std::string option) 00185 { 00186 std::vector<int> ints; 00187 int num_args = GetNumberOfArgumentsForOption(option); 00188 for(int i=1; i<=num_args; ++i) 00189 { 00190 ints.push_back(GetIntCorrespondingToOption(option, i)); 00191 } 00192 return ints; 00193 } 00194 00195 void CommandLineArguments::TestOptionFormat(std::string option) 00196 { 00197 if ( !( option.substr(0,1) == "-" && option.substr(1,1).find_first_of("0123456789")==std::string::npos ) ) 00198 { 00199 EXCEPTION("A command line option must begin with '-' followed by a non-numeric character."); 00200 } 00201 } 00202