Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / nel / src / net / varpath.cpp
blob857af5c64adb1e131de33bf5ecfcec55dff0a6f9
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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/>.
17 #include "stdnet.h"
19 #include "nel/misc/types_nl.h"
21 #include "nel/misc/debug.h"
22 #include "nel/misc/config_file.h"
23 #include "nel/misc/displayer.h"
24 #include "nel/misc/log.h"
26 #include "nel/net/varpath.h"
30 // Namespaces
33 using namespace std;
34 using namespace NLMISC;
38 // Variables
44 // Functions
47 /**
49 * VarPath ::= [bloc '.']* bloc
50 * bloc ::= '[' [VarPath ',']* VarPath ']' | name
51 * name ::= [ascii]* ['*']
55 /*bool CVarPath::getDest (uint level, vector<string> &dest)
57 return true;
58 }*/
60 string CVarPath::getToken ()
62 string res;
64 if (TokenPos >= RawVarPath.size())
65 return res;
67 res += RawVarPath[TokenPos];
69 switch (RawVarPath[TokenPos++])
71 case '.': case '*': case '[': case ']': case ',': case '=': case ' ':
72 break;
73 default :
74 while (TokenPos < RawVarPath.size() && RawVarPath[TokenPos] != '.' && RawVarPath[TokenPos] != '[' && RawVarPath[TokenPos] != ']' && RawVarPath[TokenPos] != ',' && RawVarPath[TokenPos] != '=' && RawVarPath[TokenPos] != ' ')
76 res += RawVarPath[TokenPos++];
78 break;
80 return res;
84 void CVarPath::decode ()
86 vector<string> dest;
87 TokenPos = 0;
88 Destination.clear ();
90 string val = getToken ();
92 if (val.empty())
93 return;
95 if (val == "[" )
97 for(;;)
99 uint osbnb = 0;
100 string d;
101 for(;;)
103 val = getToken ();
104 if (val == "[")
105 osbnb++;
107 // end of token
108 if (val.empty())
110 nlwarning ("VP: Bad VarPath '%s', suppose it s an empty varpath", RawVarPath.c_str());
111 Destination.clear ();
112 return;
115 if (osbnb == 0 && (val == "," || val == "]"))
116 break;
118 if (val == "]")
119 osbnb--;
121 d += val;
123 dest.push_back (d);
124 if (val == "]")
125 break;
128 else if (val != "." && val != "," && val != "]")
130 dest.push_back (val);
132 else
134 nlwarning ("VP: Malformated VarPath '%s' before position %d", RawVarPath.c_str (), TokenPos);
135 return;
138 // must be a . or end of string
139 val = getToken ();
140 if (val == " ")
142 // special case, there s a space that means that everything after is not really part of the varpath.
143 Destination.push_back (make_pair(RawVarPath, string("")));
144 return;
146 else if (val != "." && !val.empty() && val != "=")
148 nlwarning ("VP: Malformated VarPath '%s' before position %d", RawVarPath.c_str (), TokenPos);
149 return;
152 for (uint i = 0; i < dest.size(); ++i)
154 string srv, var;
155 string::size_type pos;
157 if ((pos = dest[i].find ('.')) != string::npos)
159 srv = dest[i].substr(0, pos);
160 var = dest[i].substr(pos+1);
161 if (TokenPos < RawVarPath.size())
162 var += val + RawVarPath.substr (TokenPos);
164 else
166 srv = dest[i];
167 if (val == "=")
169 srv += val + RawVarPath.substr (TokenPos);
170 var.clear();
172 else
173 var = RawVarPath.substr (TokenPos);
176 Destination.push_back (make_pair(srv, var));
179 //display ();
182 bool CVarPath::isFinal ()
184 if(Destination.empty()) return true;
185 if(Destination[0].second.empty()) return true;
186 return false;
189 void CVarPath::display ()
191 nlinfo ("VP: VarPath dest = %d", Destination.size ());
192 for (uint i = 0; i < Destination.size (); i++)
194 nlinfo ("VP: > '%s' '%s'", Destination[i].first.c_str(), Destination[i].second.c_str());
198 NLMISC_CATEGORISED_COMMAND(nel, varPath, "Test a varpath (for debug purpose)", "<rawvarpath>")
200 nlunreferenced(rawCommandString);
201 nlunreferenced(quiet);
202 nlunreferenced(human);
204 if(args.size() != 1) return false;
206 CVarPath vp (args[0]);
208 log.displayNL ("VarPath contains %d destination", vp.Destination.size ());
209 for (uint i = 0; i < vp.Destination.size (); i++)
211 log.displayNL ("Dest '%s' value '%s'", vp.Destination[i].first.c_str(), vp.Destination[i].second.c_str());
213 log.displayNL ("End of varpath");
215 return true;