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