My Project
CommandLineParser.hpp
1 #pragma once
2 //-----------------------------------------------------------------------------
3 // Class: CommandLineParser
4 // Authors: LiXizhi
5 // Emails: LiXizhi@yeah.net
6 // Company: ParaEngine Co.
7 // Date: 2010.4.25
8 // Desc: Header only helper class. It can be used for applications who do not link with the core ParaEngine plugin.
9 //-----------------------------------------------------------------------------
10 #include <string>
11 #include <map>
12 
13 namespace ParaEngine
14 {
25  {
26  public:
27  CCommandLineParser(const char* lpCmdLine = NULL)
28  {
29  SetCommandLine(lpCmdLine);
30  }
31 
32 
34  const char* GetCommandLine()
35  {
36  return m_sAppCmdLine.c_str();
37  }
38 
44  const char* GetValue(const char* pParam, const char* defaultValue)
45  {
46  std::map<string, string>::iterator it = m_commandParams.find(pParam);
47  if(it!= m_commandParams.end())
48  {
49  return (*it).second.c_str();
50  }
51  if(defaultValue!=NULL)
52  {
53  m_commandParams[pParam] = defaultValue;
54  return m_commandParams[pParam].c_str();
55  }
56  return NULL;
57  }
58 
63  void SetCommandLine(const char* pCommandLine)
64  {
65  if(pCommandLine)
66  m_sAppCmdLine = pCommandLine;
67  else
68  m_sAppCmdLine.clear();
69 
71 
72  int nPos = 0;
73  int nBegin = 0;
74  while((nPos =(int) m_sAppCmdLine.find('=', nBegin))!=string::npos)
75  {
76  int i = nPos-1;
77  int nFromPos = (int)m_sAppCmdLine.find('\"', nBegin+1);
78  if(nFromPos!=(int)string::npos)
79  {
80  int nToPos = (int)m_sAppCmdLine.find('\"', nFromPos+1);
81  if(nToPos!=(int)string::npos)
82  {
83  if(nPos>nBegin)
84  {
85  // remove heading and trailing spaces
86  for(;(m_sAppCmdLine[nBegin] == ' '|| m_sAppCmdLine[nBegin] == '\t' || m_sAppCmdLine[nBegin] == '\n' || m_sAppCmdLine[nBegin] == '\r'); nBegin++)
87  ;
88  for(;nPos>=1 && (m_sAppCmdLine[nPos-1] == ' '|| m_sAppCmdLine[nPos-1] == '\t' || m_sAppCmdLine[nPos-1] == '\n' || m_sAppCmdLine[nPos-1] == '\r'); nPos--)
89  ;
90  if(nPos>nBegin)
91  {
92  m_commandParams[m_sAppCmdLine.substr(nBegin, nPos-nBegin)] = m_sAppCmdLine.substr(nFromPos+1, nToPos - nFromPos-1);
93  nBegin = nToPos+1;
94  continue;
95  }
96  }
97  }
98  }
99  nBegin = nPos+1;
100  }
101  }
102 
103  private:
107  std::string m_sAppCmdLine;
108 
110  std::map<std::string, std::string> m_commandParams;
111  };
112 
113 }
void SetCommandLine(const char *pCommandLine)
set string specifying the command line for the application, excluding the program name...
Definition: CommandLineParser.hpp:63
const char * GetCommandLine()
get string specifying the command line for the application, excluding the program name...
Definition: CommandLineParser.hpp:34
different physics engine has different winding order.
Definition: EventBinding.h:32
const char * GetValue(const char *pParam, const char *defaultValue)
return a specified parameter value in the command line of the application.
Definition: CommandLineParser.hpp:44
command line parameter parser e.g.
Definition: CommandLineParser.hpp:24