1 #include "html.h"
2 #include "types.h"
3 #include "html_tag.h"
4 
trim(tstring & s)5 void litehtml::trim(tstring &s)
6 {
7 	tstring::size_type pos = s.find_first_not_of(_t(" \n\r\t"));
8 	if(pos != tstring::npos)
9 	{
10 	    s.erase(s.begin(), s.begin() + pos);
11 	}
12 	pos = s.find_last_not_of(_t(" \n\r\t"));
13 	if(pos != tstring::npos)
14 	{
15 	    s.erase(s.begin() + pos + 1, s.end());
16 	}
17 }
18 
lcase(tstring & s)19 void litehtml::lcase(tstring &s)
20 {
21 	for(tstring::iterator i = s.begin(); i != s.end(); i++)
22 	{
23 		(*i) = t_tolower(*i);
24 	}
25 }
26 
find_close_bracket(const tstring & s,tstring::size_type off,tchar_t open_b,tchar_t close_b)27 litehtml::tstring::size_type litehtml::find_close_bracket(const tstring &s, tstring::size_type off, tchar_t open_b, tchar_t close_b)
28 {
29 	int cnt = 0;
30 	for(tstring::size_type i = off; i < s.length(); i++)
31 	{
32 		if(s[i] == open_b)
33 		{
34 			cnt++;
35 		} else if(s[i] == close_b)
36 		{
37 			cnt--;
38 			if(!cnt)
39 			{
40 				return i;
41 			}
42 		}
43 	}
44 	return tstring::npos;
45 }
46 
value_index(const tstring & val,const tstring & strings,int defValue,tchar_t delim)47 int litehtml::value_index( const tstring& val, const tstring& strings, int defValue, tchar_t delim )
48 {
49 	if(val.empty() || strings.empty() || !delim)
50 	{
51 		return defValue;
52 	}
53 
54 	int idx = 0;
55 	tstring::size_type delim_start	= 0;
56 	tstring::size_type delim_end	= strings.find(delim, delim_start);
57 	tstring::size_type item_len		= 0;
58 	while(true)
59 	{
60 		if(delim_end == tstring::npos)
61 		{
62 			item_len = strings.length() - delim_start;
63 		} else
64 		{
65 			item_len = delim_end - delim_start;
66 		}
67 		if(item_len == val.length())
68 		{
69 			if(val == strings.substr(delim_start, item_len))
70 			{
71 				return idx;
72 			}
73 		}
74 		idx++;
75 		delim_start = delim_end;
76 		if(delim_start == tstring::npos) break;
77 		delim_start++;
78 		if(delim_start == strings.length()) break;
79 		delim_end = strings.find(delim, delim_start);
80 	}
81 	return defValue;
82 }
83 
value_in_list(const tstring & val,const tstring & strings,tchar_t delim)84 bool litehtml::value_in_list( const tstring& val, const tstring& strings, tchar_t delim )
85 {
86 	int idx = value_index(val, strings, -1, delim);
87 	if(idx >= 0)
88 	{
89 		return true;
90 	}
91 	return false;
92 }
93 
split_string(const tstring & str,string_vector & tokens,const tstring & delims,const tstring & delims_preserve,const tstring & quote)94 void litehtml::split_string(const tstring& str, string_vector& tokens, const tstring& delims, const tstring& delims_preserve, const tstring& quote)
95 {
96 	if(str.empty() || (delims.empty() && delims_preserve.empty()))
97 	{
98 		return;
99 	}
100 
101 	tstring all_delims = delims + delims_preserve + quote;
102 
103 	tstring::size_type token_start	= 0;
104 	tstring::size_type token_end	= str.find_first_of(all_delims, token_start);
105 	tstring::size_type token_len	= 0;
106 	tstring token;
107 	while(true)
108 	{
109 		while( token_end != tstring::npos && quote.find_first_of(str[token_end]) != tstring::npos )
110 		{
111 			if(str[token_end] == _t('('))
112 			{
113 				token_end = find_close_bracket(str, token_end, _t('('), _t(')'));
114 			} else if(str[token_end] == _t('['))
115 			{
116 				token_end = find_close_bracket(str, token_end, _t('['), _t(']'));
117 			} else if(str[token_end] == _t('{'))
118 			{
119 				token_end = find_close_bracket(str, token_end, _t('{'), _t('}'));
120 			} else
121 			{
122 				token_end = str.find_first_of(str[token_end], token_end + 1);
123 			}
124 			if(token_end != tstring::npos)
125 			{
126 				token_end = str.find_first_of(all_delims, token_end + 1);
127 			}
128 		}
129 
130 		if(token_end == tstring::npos)
131 		{
132 			token_len = tstring::npos;
133 		} else
134 		{
135 			token_len = token_end - token_start;
136 		}
137 
138 		token = str.substr(token_start, token_len);
139 		if(!token.empty())
140 		{
141 			tokens.push_back( token );
142 		}
143 		if(token_end != tstring::npos && !delims_preserve.empty() && delims_preserve.find_first_of(str[token_end]) != tstring::npos)
144 		{
145 			tokens.push_back( str.substr(token_end, 1) );
146 		}
147 
148 		token_start = token_end;
149 		if(token_start == tstring::npos) break;
150 		token_start++;
151 		if(token_start == str.length()) break;
152 		token_end = str.find_first_of(all_delims, token_start);
153 	}
154 }
155 
join_string(tstring & str,const string_vector & tokens,const tstring & delims)156 void litehtml::join_string(tstring& str, const string_vector& tokens, const tstring& delims)
157 {
158 	tstringstream ss;
159 	for(size_t i=0; i<tokens.size(); ++i)
160 	{
161 		if(i != 0)
162 		{
163 			ss << delims;
164 		}
165 		ss << tokens[i];
166 	}
167 
168 	str = ss.str();
169 }
170