CommandLineArguments.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include "CommandLineArguments.hpp"
00030 
00031 #include <cassert>
00032 #include <cstddef>
00033 
00034 CommandLineArguments::CommandLineArguments()
00035     : p_argc(NULL),
00036       p_argv(NULL)
00037 {
00038     // Make doubly sure there's only one instance
00039     assert(mpInstance == NULL);
00040 }
00041 
00042 CommandLineArguments* CommandLineArguments::Instance()
00043 {
00044     if (mpInstance == NULL)
00045     {
00046         mpInstance = new CommandLineArguments;
00047     }
00048     return mpInstance;
00049 }
00050 
00051 CommandLineArguments* CommandLineArguments::mpInstance = NULL;
00052 
00053 bool CommandLineArguments::OptionExists(std::string option)
00054 {
00055     int index = GetIndexForArgument(option);
00056     assert(index!=0);
00057 
00058     return (index > 0);
00059 }
00060 
00061 char* CommandLineArguments::GetValueCorrespondingToOption(std::string option, int valueNumber)
00062 {
00063     EXCEPT_IF_NOT(valueNumber>0);
00064     TestOptionFormat(option);
00065 
00066     int num_args = GetNumberOfArgumentsForOption(option);
00067     int index = GetIndexForArgument(option);
00068     EXCEPT_IF_NOT(index>0);
00069     EXCEPT_IF_NOT(num_args>=valueNumber);
00070     return (*p_argv)[index+valueNumber];
00071 }
00072 
00073 double CommandLineArguments::GetDoubleCorrespondingToOption(std::string option, int valueNumber)
00074 {
00075     char* val = GetValueCorrespondingToOption(option, valueNumber);
00076     return atof(val);
00077 }
00078 
00079 int CommandLineArguments::GetIntCorrespondingToOption(std::string option, int valueNumber)
00080 {
00081     char* val = GetValueCorrespondingToOption(option, valueNumber);
00082     return atoi(val);
00083 }
00084 
00085 unsigned CommandLineArguments::GetUnsignedCorrespondingToOption(std::string option, int valueNumber)
00086 {
00087     char* val = GetValueCorrespondingToOption(option, valueNumber);
00088     int i = atoi(val);
00089     if (i < 0)
00090     {
00091         EXCEPTION("Option is a negative number and cannot be converted to unsigned.");
00092     }
00093     return (unsigned)(i);
00094 }
00095 
00096 int CommandLineArguments::GetIndexForArgument(std::string argument)
00097 {
00098     TestOptionFormat(argument);
00099 
00100     for (int i=1; i<*p_argc; i++)
00101     {
00102         if (argument==std::string((*p_argv)[i]))
00103         {
00104             return i;
00105         }
00106     }
00107     return -1;
00108 }
00109 
00110 int CommandLineArguments::GetNumberOfArgumentsForOption(std::string option)
00111 {
00112     int start_idx = GetIndexForArgument(option);
00113     if (start_idx < 0)
00114     {
00115         EXCEPTION("Command line option '" + option + "' does not exist");
00116     }
00117 
00118     int end_idx = start_idx;
00119     for (int i=start_idx+1; i<*p_argc; i++)
00120     {
00121         std::string argument = std::string((*p_argv)[i]);
00122         if (argument.substr(0,1)=="-" && argument.substr(1,1).find_first_of("0123456789")==std::string::npos)
00123         {
00124             break;
00125         }
00126         end_idx = i;
00127     }
00128 
00129     if (end_idx == start_idx)
00130     {
00131         EXCEPTION("No value(s) given after command line option '" + option + "'");
00132     }
00133 
00134     return end_idx - start_idx;
00135 }
00136 
00137 std::string CommandLineArguments::GetStringCorrespondingToOption(std::string option, int valueNumber)
00138 {
00139     char* val = GetValueCorrespondingToOption(option, valueNumber);
00140     std::string string_arg(val);
00141     return string_arg;
00142 }
00143 
00144 std::vector<std::string> CommandLineArguments::GetStringsCorrespondingToOption(std::string option)
00145 {
00146     std::vector<std::string> strings;
00147     int num_args = GetNumberOfArgumentsForOption(option);
00148     for(int i=1; i<=num_args; ++i)
00149     {
00150         strings.push_back(GetStringCorrespondingToOption(option, i));
00151     }
00152     return strings;
00153 }
00154 
00155 std::vector<double> CommandLineArguments::GetDoublesCorrespondingToOption(std::string option)
00156 {
00157     std::vector<double> doubles;
00158     int num_args = GetNumberOfArgumentsForOption(option);
00159     for(int i=1; i<=num_args; ++i)
00160     {
00161         doubles.push_back(GetDoubleCorrespondingToOption(option, i));
00162     }
00163     return doubles;
00164 }
00165 
00166 std::vector<unsigned> CommandLineArguments::GetUnsignedsCorrespondingToOption(std::string option)
00167 {
00168     std::vector<unsigned> unsigneds;
00169     int num_args = GetNumberOfArgumentsForOption(option);
00170     for(int i=1; i<=num_args; ++i)
00171     {
00172         unsigneds.push_back(GetUnsignedCorrespondingToOption(option, i));
00173     }
00174     return unsigneds;
00175 }
00176 
00177 std::vector<int> CommandLineArguments::GetIntsCorrespondingToOption(std::string option)
00178 {
00179     std::vector<int> ints;
00180     int num_args = GetNumberOfArgumentsForOption(option);
00181     for(int i=1; i<=num_args; ++i)
00182     {
00183         ints.push_back(GetIntCorrespondingToOption(option, i));
00184     }
00185     return ints;
00186 }
00187 
00188 void CommandLineArguments::TestOptionFormat(std::string option)
00189 {
00190     if ( !( option.substr(0,1) == "-" && option.substr(1,1).find_first_of("0123456789")==std::string::npos ) )
00191     {
00192         EXCEPTION("A command line option must begin with '-' followed by a non-numeric character.");
00193     }
00194 }
00195 
Generated on Thu Dec 22 13:00:05 2011 for Chaste by  doxygen 1.6.3