00001
00002
00003 #include "Iguana/Framework/interface/IgArgsElement.h"
00004 #include "classlib/utils/DebugAids.h"
00005 #include <algorithm>
00006
00007
00008
00009
00010
00011
00012
00013
00014 IG_DEFINE_STATE_ELEMENT (IgArgsElement, "Services/Global/Args");
00015
00016
00017
00018
00019
00020 IgArgsElement::IgArgsElement (IgState *in, int argc, char **argv)
00021 : m_state (in),
00022 m_argc (argc),
00023 m_argv (argv)
00024 {
00025 ASSERT (argv);
00026 ASSERT (std::find (argv, argv+argc+1, (char *) 0) == argv+argc);
00027 m_state->put (s_key, this);
00028 }
00029
00030 int &
00031 IgArgsElement::args (void)
00032 { return m_argc; }
00033
00034 char *
00035 IgArgsElement::arg (int arg)
00036 { ASSERT (arg >= 0); ASSERT (arg < args ()); return argv ()[arg]; }
00037
00038 char **
00039 IgArgsElement::argv (void)
00040 { return m_argv; }
00041
00042 bool
00043 IgArgsElement::find (const std::vector<std::string> &arg, std::string &value,
00044 bool remove )
00045 {
00046 for(int i = 1; i < m_argc - 1; i++)
00047 {
00048 for(unsigned int j = 0; j < arg.size (); j++)
00049 {
00050 if (arg[j] == m_argv[i])
00051 {
00052 value = m_argv[i+1];
00053 if (remove)
00054 { m_argv[i][0] = '\0'; m_argv[i+1][0] = '\0';}
00055 return true;
00056 }
00057 }
00058 }
00059 return false;
00060 }
00061
00062 bool
00063 IgArgsElement::find (const std::string& arg, std::string &value,
00064 bool remove )
00065 {
00066 std::vector<std::string> argument;
00067 argument.push_back(arg);
00068 return find(argument, value, remove);
00069 }
00070
00071 bool
00072 IgArgsElement::find (const std::string& arg, int &value,
00073 bool remove )
00074 {
00075 std::vector<std::string> argument;
00076 argument.push_back (arg);
00077 std::string strValue;
00078 if (find (argument, strValue, remove))
00079 {
00080 value = atoi (strValue.c_str());
00081 return true;
00082 }
00083 return false;
00084 }
00085
00086 void
00087 IgArgsElement::remove (const std::vector<std::string> &arg,
00088 bool hasvalue )
00089 {
00090 int total = hasvalue ? m_argc - 1 : m_argc;
00091 for(int i = 1; i < total; i++)
00092 {
00093 for(unsigned int j = 0; j < arg.size (); j++)
00094 {
00095 if (arg[j] == m_argv[i])
00096 {
00097 m_argv[i][0]='\0';
00098 if (hasvalue)
00099 m_argv[i+1][0]='\0';
00100 return;
00101 }
00102 }
00103 }
00104 }
00105
00106 void
00107 IgArgsElement::remove (const std::string &arg,
00108 bool hasvalue )
00109 {
00110 std::vector<std::string> argument;
00111 argument.push_back (arg);
00112 remove (argument, hasvalue);
00113 }
00114
00115 bool
00116 IgArgsElement::exists (const std::vector<std::string> &arg,
00117 bool hasvalue )
00118 {
00119 int total = hasvalue ? m_argc - 1 : m_argc;
00120 for(int i = 1; i < total; i++)
00121 {
00122 for(unsigned int j = 0; j < arg.size (); j++)
00123 {
00124 if (arg[j] == m_argv[i])
00125 return true;
00126 }
00127 }
00128 return false;
00129 }
00130
00131 bool
00132 IgArgsElement::exists (const std::string &arg,
00133 bool hasvalue )
00134 {
00135 std::vector<std::string> argument;
00136 argument.push_back (arg);
00137 return exists (argument, hasvalue);
00138 }
00139