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/algo.h"
31 bool testWildCard(const std::string
&strIn
, const std::string
&wildCard
)
33 return testWildCard(strIn
.c_str(), wildCard
.c_str());
37 // ***************************************************************************
38 bool testWildCard(const char *strIn
, const char *wildCard
)
40 // run the 2 string in //el
41 while(*wildCard
!=0 && *strIn
!=0)
43 // if same char, continue.
49 // if wildCard is ?, continue
50 else if(*wildCard
=='?')
55 // if wildcard is *, recurs check.
56 else if(*wildCard
=='*')
62 // else must check next strings.
65 // build the wilcard token. eg from "*pipo?", take "pipo"
67 while(*wildCard
!='*' && *wildCard
!='?' && *wildCard
!=0)
72 // if token size is empty, error
76 // in strIn, search all the occurence of token. For each solution, recurs test.
78 string::size_type pos
= sCopy
.find(token
, 0);
79 while(pos
!=string::npos
)
81 // do a testWildCard test on the remaining string/wildCard
82 if( testWildCard(strIn
+pos
+token
.size(), wildCard
) )
85 // fails=> test with another occurence of token in the string.
86 pos
= sCopy
.find(token
, pos
+1);
89 // if all failed, fail
98 // If quit here because end Of 2 strs, OK.
99 if(*wildCard
==0 && *strIn
==0)
101 // if quit here because wildCard=="*" and s="", OK too.
102 if(*strIn
==0 && wildCard
[0]=='*' && wildCard
[1]==0)
107 It may be wildCard="?aez" and s="" => error
108 It may be wildCard="" and s="aer" => error
114 // ***************************************************************************
115 void splitString(const std::string
&str
, const std::string
&separator
, std::vector
<std::string
> &retList
)
117 string::size_type pos
=0;
118 string::size_type newPos
=0;
120 while( (newPos
= str
.find(separator
,pos
)) != string::npos
)
122 // if not empty sub str. (skip repetition of separator )
124 retList
.push_back(str
.substr(pos
, newPos
-pos
));
126 pos
= newPos
+separator
.size();
128 // copy the last substr
130 retList
.push_back(str
.substr(pos
, str
.size()-pos
));
134 // ***************************************************************************
135 void splitUCString(const ucstring
&ucstr
, const ucstring
&separator
, std::vector
<ucstring
> &retList
)
137 ucstring::size_type pos
=0;
138 ucstring::size_type newPos
=0;
140 while( (newPos
= ucstr
.find(separator
,pos
)) != ucstring::npos
)
142 // if not empty sub str. (skip repetition of separator )
144 retList
.push_back(ucstr
.substr(pos
, newPos
-pos
));
146 pos
= newPos
+separator
.size();
148 // copy the last substr
149 if( pos
<ucstr
.size() )
150 retList
.push_back(ucstr
.substr(pos
, ucstr
.size()-pos
));
153 // ***************************************************************************
155 void drawFullLine (float x0
, float y0
, float x1
, float y1
, std::vector
<std::pair
<sint
, sint
> > &result
)
159 float dx
= (float) fabs (x0
-x1
);
160 float dy
= (float) fabs (y0
-y1
);
161 if ((dx
== 0) && (dy
== 0))
162 result
.push_back (pair
<sint
, sint
> ((sint
)floor (x0
), (sint
)floor (y0
)));
176 float deltaX
= x1
- x0
;
177 const float deltaY
= (y1
-y0
)/deltaX
;
179 // Current integer pixel
180 sint currentX
= (sint
)floor (x0
);
181 sint currentY
= (sint
)floor (y0
);
186 sint previousY
= currentY
;
192 y0
+= deltaX
* deltaY
;
199 if (currentY
<=previousY
)
203 result
.push_back (pair
<sint
, sint
> (currentX
, previousY
));
206 while (currentY
<=previousY
);
212 result
.push_back (pair
<sint
, sint
> (currentX
, previousY
));
215 while (currentY
>=previousY
);
235 float deltaY
= y1
- y0
;
236 const float deltaX
= (x1
-x0
)/deltaY
;
238 // Current integer pixel
239 sint currentY
= (sint
)floor (y0
);
240 sint currentX
= (sint
)floor (x0
);
245 sint previousX
= currentX
;
251 x0
+= deltaY
* deltaX
;
258 if (currentX
<=previousX
)
262 result
.push_back (pair
<sint
, sint
> (previousX
, currentY
));
265 while (currentX
<=previousX
);
271 result
.push_back (pair
<sint
, sint
> (previousX
, currentY
));
274 while (currentX
>=previousX
);
283 // ***************************************************************************
285 void drawLine (float x0
, float y0
, float x1
, float y1
, vector
<pair
<sint
, sint
> > &result
)
287 float dx
= (float)(floor(x1
+0.5) - floor(x0
+0.5));
288 float dy
= (float)(floor(y1
+0.5) - floor(y0
+0.5));
293 sint d
= (sint
)std::max(fabs(dx
), fabs(dy
));
294 float maxd
= (float)(std::max(fabs(rdx
), fabs(rdy
)));
301 result
.push_back(make_pair
<sint
,sint
>((sint
)floor(x0
+0.5), (sint
)floor(y0
+0.5)));
308 // ***************************************************************************