Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgCmdLineOpt.cpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file SgCmdLineOpt.cpp
00003     See SgCmdLineOpt.h
00004 */
00005 //----------------------------------------------------------------------------
00006 
00007 #include "SgSystem.h"
00008 #include "SgCmdLineOpt.h"
00009 
00010 #include <algorithm>
00011 #include <sstream>
00012 #include "SgDebug.h"
00013 #include "SgException.h"
00014 
00015 using namespace std;
00016 
00017 //----------------------------------------------------------------------------
00018 
00019 SgCmdLineOpt::SgCmdLineOpt()
00020 {
00021 }
00022 
00023 bool SgCmdLineOpt::Contains(const char* option) const
00024 {
00025     return (m_map.find(option) != m_map.end());
00026 }
00027 
00028 const vector<string>& SgCmdLineOpt::GetArguments() const
00029 {
00030     return m_args;
00031 }
00032 
00033 double SgCmdLineOpt::GetDouble(const char* option) const
00034 {
00035     return GetDouble(option, 0.0);
00036 }
00037 
00038 double SgCmdLineOpt::GetDouble(const char* option, double defaultValue) const
00039 {
00040     map<string, string>::const_iterator it = m_map.find(option);
00041     if (it == m_map.end())
00042         return defaultValue;
00043     string s = it->second;
00044     istringstream in(s);
00045     double value;
00046     in >> value;
00047     if (! in)
00048         throw SgException(string("Option ") + option + " needs float value");
00049     return value;
00050 }
00051 
00052 
00053 int SgCmdLineOpt::GetInteger(const char* option) const
00054 {
00055     return GetInteger(option, 0);
00056 }
00057 
00058 int SgCmdLineOpt::GetInteger(const char* option, int defaultValue) const
00059 {
00060     map<string, string>::const_iterator it = m_map.find(option);
00061     if (it == m_map.end())
00062         return defaultValue;
00063     string s = it->second;
00064     istringstream in(s);
00065     int value;
00066     in >> value;
00067     if (! in)
00068         throw SgException(string("Option ") + option
00069                           + " needs integer value");
00070     return value;
00071 }
00072 
00073 string SgCmdLineOpt::GetString(const char* option) const
00074 {
00075     return GetString(option, "");
00076 }
00077 
00078 string SgCmdLineOpt::GetString(const char* option,
00079                                const string& defaultValue) const
00080 {
00081     map<string, string>::const_iterator it = m_map.find(option);
00082     if (it == m_map.end())
00083         return defaultValue;
00084     return it->second;
00085 }
00086 
00087 void SgCmdLineOpt::Parse(int argc, char* argv[],
00088                          const vector<std::string>& specs)
00089 {
00090     m_args.clear();
00091     m_map.clear();
00092     int n = 1;
00093     bool endOfOptions = false;
00094     while (n < argc)
00095     {
00096         string s = argv[n];
00097         ++n;
00098         if (! endOfOptions && s.size() > 0 && s[0] == '-')
00099         {
00100             if (s == "--")
00101             {
00102                 endOfOptions = true;
00103                 continue;
00104             }
00105             s = s.substr(1);
00106             bool needsArg = false;
00107             vector<string>::const_iterator spec;
00108             spec = find(specs.begin(), specs.end(), s);
00109             if (spec == specs.end())
00110             {
00111                 spec = find(specs.begin(), specs.end(), s + ":");
00112                 if (spec == specs.end())
00113                     throw SgException("Unknown option -" + s);
00114                 needsArg = true;
00115             }
00116             string value;
00117             if (needsArg)
00118             {
00119                 if (n >= argc)
00120                     throw SgException("Option -" + s + " needs value");
00121                 value = argv[n];
00122                 ++n;
00123                 if (value.size() > 0 && value[0] == '-')
00124                     throw SgException("Option -" + s + " needs value");
00125             }
00126             m_map.insert(pair<string, string>(s, value));
00127         }
00128         else
00129             m_args.push_back(s);
00130     }
00131 }
00132 
00133 void SgCmdLineOpt::Parse(int argc, const char* argv[],
00134                          const vector<std::string>& specs)
00135 {
00136     Parse(argc, const_cast<char**>(argv), specs);
00137 }
00138 
00139 //----------------------------------------------------------------------------
00140 


17 Jun 2010 Doxygen 1.4.7