Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CmdLineArgs.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2009 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "precompiled.h"
19 #include "CmdLineArgs.h"
20 
21 #include "lib/sysdep/sysdep.h"
22 
23 CmdLineArgs::CmdLineArgs(int argc, const char* argv[])
24 {
25  if (argc >= 1)
26  {
27  std::string arg0(argv[0]);
28  // avoid OsPath complaining about mixing both types of separators,
29  // which happens when running in the VC2010 debugger
30  std::replace(arg0.begin(), arg0.end(), '/', SYS_DIR_SEP);
31  m_Arg0 = arg0;
32  }
33 
34  for (int i = 1; i < argc; ++i)
35  {
36  // Only accept arguments that start with '-'
37  if (argv[i][0] != '-')
38  continue;
39 
40  CStr name, value;
41 
42  // Check for "-arg=value"
43  const char* eq = strchr(argv[i], '=');
44  if (eq)
45  {
46  name = CStr(argv[i]+1, eq-argv[i]-1);
47  value = CStr(eq+1);
48  }
49  else
50  {
51  name = CStr(argv[i]+1);
52  }
53 
54  m_Args.push_back(make_pair(name, value));
55  }
56 }
57 
58 template<typename T>
60 {
61  T x;
62  first_equals(const T& x) : x(x) {}
63  template<typename S> bool operator()(const S& v) { return v.first == x; }
64 };
65 
66 bool CmdLineArgs::Has(const char* name) const
67 {
68  return find_if(m_Args.begin(), m_Args.end(), first_equals<CStr>(name)) != m_Args.end();
69 }
70 
71 CStr CmdLineArgs::Get(const char* name) const
72 {
73  ArgsT::const_iterator it = find_if(m_Args.begin(), m_Args.end(), first_equals<CStr>(name));
74  if (it != m_Args.end())
75  return it->second;
76  else
77  return "";
78 }
79 
80 std::vector<CStr> CmdLineArgs::GetMultiple(const char* name) const
81 {
82  std::vector<CStr> values;
83  ArgsT::const_iterator it = m_Args.begin();
84  while (1)
85  {
86  it = find_if(it, m_Args.end(), first_equals<CStr>(name));
87  if (it == m_Args.end())
88  break;
89  values.push_back(it->second);
90  ++it; // start searching from the next one in the next iteration
91  }
92  return values;
93 }
94 
96 {
97  return m_Arg0;
98 }
CStr Get(const char *name) const
Get the value of the named parameter.
Definition: CmdLineArgs.cpp:71
OsPath GetArg0() const
Get the value of argv[0], which is typically meant to be the name/path of the program (but the actual...
Definition: CmdLineArgs.cpp:95
std::vector< CStr > GetMultiple(const char *name) const
Get all the values given to the named parameter.
Definition: CmdLineArgs.cpp:80
#define SYS_DIR_SEP
directory separation character
Definition: sysdep.h:196
bool Has(const char *name) const
Test whether the given name was specified, as either -name or -name=value
Definition: CmdLineArgs.cpp:66
Definition: path.h:75
first_equals(const T &x)
Definition: CmdLineArgs.cpp:62
bool operator()(const S &v)
Definition: CmdLineArgs.cpp:63
#define T(string_literal)
Definition: secure_crt.cpp:70
ArgsT m_Args
Definition: CmdLineArgs.h:66
OsPath m_Arg0
Definition: CmdLineArgs.h:67