1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program 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 Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "nel/misc/sstring.h"
20 #include "nel/net/module_common.h"
23 using namespace NLMISC
;
28 TParsedCommandLine::TParsedCommandLine(const TParsedCommandLine
& copy
)
29 :ParamName(copy
.ParamName
), ParamValue(copy
.ParamValue
)
34 uint first
= 0, last
= (uint
)copy
.SubParams
.size();
35 SubParams
.resize( last
);
36 for (; first
!= last
; ++first
)
38 // calls recursively copy constructor
39 SubParams
[first
] = new TParsedCommandLine(*copy
.SubParams
[first
]);
44 TParsedCommandLine::~TParsedCommandLine()
49 void TParsedCommandLine::clear()
51 for (std::vector
<TParsedCommandLine
*>::iterator it
=SubParams
.begin(); it
!=SubParams
.end(); ++it
)
60 bool TParsedCommandLine::parseParamList(const std::string
&rawParamString
)
65 return _parseParamList(rawParamString
);
68 std::string
TParsedCommandLine::toString() const
73 if (!ParamValue
.empty())
75 ret
+= " = "+ParamValue
;
78 if (!SubParams
.empty())
82 for (uint i
=0; i
<SubParams
.size(); ++i
)
86 ret
+= SubParams
[i
]->toString();
97 bool TParsedCommandLine::_parseParamList(const std::string
&rawParamString
)
99 CSString
parsedString(rawParamString
);
101 for (CSString part
= parsedString
.strtok(" \t", true, false);
103 part
= parsedString
.strtok(" \t", true, false))
107 // this is a sub parameter list
108 if (SubParams
.empty() || SubParams
.back()->ParamName
.empty())
110 nlwarning("While parsing param string '%s', missing param header", rawParamString
.c_str());
113 if (!SubParams
.back()->ParamValue
.empty())
115 nlwarning("While parsing param string '%s', Invalid sub param header '%s' for sub part '%s', must not define value",
116 rawParamString
.c_str(),
117 SubParams
.back()->ParamName
.c_str(),
123 if (part
[part
.size()-1] != ')')
125 nlwarning("While parsing param string '%s', Invalid sub param value '%s' missing closing ')'",
126 rawParamString
.c_str(),
132 part
= part
.stripBlockDelimiters();
134 if (!SubParams
.back()->_parseParamList(part
))
136 nlwarning("Error parsing sub param list for header '%s' in '%s'",
137 SubParams
.back()->ParamName
.c_str(),
138 rawParamString
.c_str());
142 else if (part
[part
.size()-1] == ')')
144 nlwarning("While parsing param string '%s', Invalid param value '%s' : missing openning '('",
145 rawParamString
.c_str(),
150 else if (part
[0] == '\"')
152 // this is a quoted parameter value
153 if (SubParams
.empty() || !SubParams
.back()->ParamValue
.empty())
155 nlwarning("While parsing param string '%s', param '%s' already have the value '%s'",
156 rawParamString
.c_str(),
157 SubParams
.back()->ParamName
.c_str(),
158 SubParams
.back()->ParamValue
.c_str());
161 SubParams
.back()->ParamValue
= part
.unquote();
165 // this is a simple param
166 CSString name
= part
.splitTo('=', true, true);
169 nlwarning("Can't find param name for value '%s' in the param string '%s'",
171 rawParamString
.c_str());
174 CSString value
= part
.strtok("=");
176 SubParams
.push_back( new TParsedCommandLine() );
177 SubParams
.back()->ParamName
= name
;
178 SubParams
.back()->ParamValue
= value
;
185 const TParsedCommandLine
*TParsedCommandLine::getParam(const std::string
&name
) const
187 vector
<string
> parts
;
188 NLMISC::explode(name
, string("."), parts
);
190 return _getParam(parts
.begin(), parts
.end());
193 void TParsedCommandLine::setParam(const std::string
&name
, const std::string
&value
)
195 vector
<string
> parts
;
196 NLMISC::explode(name
, string("."), parts
);
200 // at least one part in the name
201 // check if sub ojbcct exist
202 TParsedCommandLine
*sub
= _getParam(parts
.begin(), (parts
.begin()+1));
205 TParsedCommandLine
* newElem
= new TParsedCommandLine();
206 newElem
->ParamName
= parts
[0];
207 SubParams
.push_back(newElem
);
208 sub
= SubParams
.back();
213 // name is more deep, need to resurse
214 parts
.erase(parts
.begin());
216 join(parts
, ".", subName
);
217 sub
->setParam(subName
, value
);
221 // last level, set the value
222 sub
->ParamValue
= value
;
227 const TParsedCommandLine
*TParsedCommandLine::_getParam(std::vector
<std::string
>::iterator it
, std::vector
<std::string
>::iterator end
) const
229 return const_cast<TParsedCommandLine
&>(*this)._getParam(it
, end
);
232 TParsedCommandLine
*TParsedCommandLine::_getParam(std::vector
<std::string
>::iterator it
, std::vector
<std::string
>::iterator end
)
236 // end of recursion, we found the searched param
240 // look for sub param
241 for (uint i
=0; i
<SubParams
.size(); ++i
)
243 if (SubParams
[i
]->ParamName
== *it
)
244 return SubParams
[i
]->_getParam(++it
, end
);
247 // parameter not found
252 } // namespace NLMISC