1 /*
2  * This file is part of CSSTidy.
3  *
4  * CSSTidy 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  * CSSTidy 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 CSSTidy; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18 
19 #include "csspp_globals.hpp"
20 #include <cstring>
21 
escaped(const string & istring,const int pos)22 bool escaped(const string &istring, const int pos)
23 {
24 	return !(s_at(istring,pos-1) != '\\' || escaped(istring,pos-1));
25 }
26 
27 // Save replacement for .at()
s_at(const string & istring,const int pos)28 char s_at(const string &istring, const int pos)
29 {
30 	if(pos > (istring.length()-1) && pos < 0)
31 	{
32 		return 0;
33 	}
34 	else
35 	{
36 		return istring[pos];
37 	}
38 }
39 
explode(const string e,string s,const bool check)40 vector<string> explode(const string e,string s, const bool check)
41 {
42 	vector<string> ret;
43 	int iPos = s.find(e, 0);
44 	int iPit = e.length();
45 
46 	while(iPos > -1)
47 	{
48 		if(iPos != 0 || check)
49 		{
50 			ret.push_back(s.substr(0,iPos));
51 		}
52 		s.erase(0,iPos+iPit);
53 		iPos = s.find(e, 0);
54 	}
55 
56  	if(s != "" || check)
57  	{
58 		ret.push_back(s);
59 	}
60 	return ret;
61 }
62 
implode(const string e,const vector<string> s)63 string implode(const string e,const vector<string> s)
64 {
65 	string ret;
66 	for(int i = 0; i < s.size(); i++)
67 	{
68 		ret += s[i];
69 		if(i != (s.size()-1)) ret += e;
70 	}
71 	return ret;
72 }
73 
round(const float & number,const int num_digits)74 float round(const float &number, const int num_digits)
75 {
76     float doComplete5i, doComplete5(number * powf(10.0f, (float) (num_digits + 1)));
77 
78     if(number < 0.0f)
79         doComplete5 -= 5.0f;
80     else
81         doComplete5 += 5.0f;
82 
83     doComplete5 /= 10.0f;
84     modff(doComplete5, &doComplete5i);
85 
86     return doComplete5i / powf(10.0f, (float) num_digits);
87 }
88 
89 
str_replace(const string find,const string replace,string str)90 string str_replace(const string find, const string replace, string str)
91 {
92     int len = find.length();
93     int replace_len = replace.length();
94     int pos = str.find(find);
95 
96     while(pos != string::npos)
97 	{
98         str.replace(pos, len, replace);
99         pos = str.find(find, pos + replace_len);
100     }
101     return str;
102 }
103 
str_replace(const vector<string> & find,const string replace,string str)104 string str_replace(const vector<string>& find, const string replace, string str)
105 {
106 	int replace_len = replace.length();
107 
108 	for(int i = 0; i < find.size(); ++i)
109 	{
110 	    int len = find[i].length();
111 	    int pos = str.find(find[i]);
112 
113 	    while(pos != string::npos)
114 		{
115 	        str.replace(pos, len, replace);
116 	        pos = str.find(find[i], pos + replace_len);
117 	    }
118 	}
119     return str;
120 }
121 
122 
in_char_arr(const char * haystack,const char needle)123 bool in_char_arr(const char* haystack, const char needle)
124 {
125 	for(int i = 0; i < strlen(haystack); ++i)
126 	{
127 		if(haystack[i] == needle)
128 		{
129 			return true;
130 		}
131 	}
132 	return false;
133 }
134 
in_str_array(const string & haystack,const char needle)135 bool in_str_array(const string& haystack, const char needle)
136 {
137 	return (haystack.find_first_of(needle,0) != string::npos);
138 }
139 
in_str_array(const vector<string> & haystack,const string needle)140 bool in_str_array(const vector<string>& haystack, const string needle)
141 {
142 	for(int i = 0; i < haystack.size(); ++i)
143 	{
144 		if(haystack[i] == needle)
145 		{
146 			return true;
147 		}
148 	}
149 	return false;
150 }
151 
htmlspecialchars(string istring,int quotes)152 string htmlspecialchars(string istring, int quotes)
153 {
154 	istring = str_replace("&","&amp;",istring);
155 	istring = str_replace("<","&lt;",istring);
156 	istring = str_replace(">","&gt;",istring);
157 	if(quotes > 0) istring = str_replace("\"","&quot;",istring);
158 	if(quotes > 1) istring = str_replace("'","&#039;",istring);
159 	return istring;
160 }
161 
max(const int i1,const int i2)162 int max(const int i1, const int i2)
163 {
164 	if(i1 > i2)
165 	{
166 		return i1;
167 	}
168 	else
169 	{
170 		return i2;
171 	}
172 }
173 
ctype_space(const char c)174 bool ctype_space(const char c)
175 {
176 	return (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11);
177 }
178 
ctype_digit(const char c)179 bool ctype_digit(const char c)
180 {
181 	return (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9');
182 }
183 
unserialise_sa(const string istring)184 vector<string> unserialise_sa(const string istring)
185 {
186 	int strlen = istring.length();
187 	int strpos = 0;
188 	vector<string> ret;
189 
190 	while(strlen > 0)
191 	{
192 		string digit_tmp = "";
193 		for(int i = strpos; ctype_digit(s_at(istring,i)); i++)
194 		{
195 			digit_tmp += istring[i];
196 			--strlen; ++strpos;
197 		}
198 		// :
199 		--strlen; ++strpos;
200 
201 		int next_length = static_cast<int>(str2f(digit_tmp));
202 		next_length += strpos;
203 
204 		string string_tmp = "";
205 		for(int i = strpos; (i<istring.length() && i < next_length); i++)
206 		{
207 			string_tmp += istring[i];
208 			--strlen; ++strpos;
209 		}
210 		ret.push_back(string_tmp);
211 	}
212 	return ret;
213 }
214 
serialise_sa(const string istring)215 string serialise_sa(const string istring)
216 {
217 	return f2str(istring.length()) + ":" + istring;
218 }
219 
ctype_xdigit(char c)220 bool ctype_xdigit(char c)
221 {
222 	c = chartolower(c);
223 	return (ctype_digit(c) || c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f');
224 }
225 
ctype_alpha(char c)226 bool ctype_alpha(char c)
227 {
228 	c = chartolower(c);
229 	return (c == 'a' || c == 'b' || c == 'c' || c == 'd' || c == 'e' || c == 'f' || c == 'g' || c == 'h' || c == 'i' || c == 'j' ||
230 	   c == 'k' || c == 'l' || c == 'm' || c == 'n' || c == 'o' || c == 'p' || c == 'q' || c == 'r' || c == 's' || c == 't' ||
231 	   c == 'u' || c == 'v' || c == 'w' || c == 'x' || c == 'y' || c == 'z');
232 }
233