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     assert(option.substr(0,1)=="-");
00056     int index = GetIndexForArgument(option);
00057     assert(index!=0);
00058     return (index>0);
00059 }
00060 
00061 char* CommandLineArguments::GetValueCorrespondingToOption(std::string option)
00062 {
00063     assert(option.substr(0,1)=="-");
00064     int index = GetIndexForArgument(option);
00065     assert(index!=0);
00066     if(index<0)
00067     {
00068         EXCEPTION("Command line option '" + option + "' does not exist");
00069     }
00070     if(index+1==*p_argc)
00071     {
00072         EXCEPTION("No value given after command line option '" + option + "'");
00073     }
00074     return (*p_argv)[index+1];
00075 }
00076 
00077 double CommandLineArguments::GetDoubleCorrespondingToOption(std::string option)
00078 {
00079     char* val = GetValueCorrespondingToOption(option);
00080     return atof(val);
00081 }
00082 
00083 int CommandLineArguments::GetIntCorrespondingToOption(std::string option)
00084 {
00085     char* val = GetValueCorrespondingToOption(option);
00086     return atoi(val);
00087 }
00088 
00089 unsigned CommandLineArguments::GetUnsignedCorrespondingToOption(std::string option)
00090 {
00091     char* val = GetValueCorrespondingToOption(option);
00092     int i = atoi(val);
00093     if (i<0)
00094     {
00095         EXCEPTION("Option is a negative number and cannot be converted to unsigned.");
00096     }
00097     return (unsigned)(i);
00098 }
00099 
00100 int CommandLineArguments::GetIndexForArgument(std::string argument)
00101 {
00102     assert(argument.substr(0,1)=="-");
00103 
00104     for(int i=1; i<*p_argc; i++)
00105     {
00106         if(argument==std::string((*p_argv)[i]))
00107         {
00108             return i;
00109         }
00110     }
00111     return -1;
00112 }
00113 
00114 
00115 
00116 

Generated on Mon Apr 18 11:35:28 2011 for Chaste by  doxygen 1.5.5