Debian Codegraph @

CodeLite/StringUtils.cpp

raw
1 #include "StringUtils.h"
2
3 #include <vector>
4 #include <wx/tokenzr.h>
5
6 namespace
7 {
8 bool is_env_variable(const wxString& str, wxString* env_name)
9 {
10 if(str.empty() || str[0] != '$') {
11 return false;
12 }
13 env_name->reserve(str.length());
14
15 // start from 1 to skip the prefix $
16 for(size_t i = 1; i < str.length(); ++i) {
17 wxChar ch = str[i];
18 if(ch == '(' || ch == ')' || ch == '{' || ch == '}')
19 continue;
20 env_name->Append(ch);
21 }
22 return true;
23 }
24
25 /**
26 * @brief expand an environment variable. use `env_map` to resolve any variables
27 * accepting value in the form of (one example):
28 * $PATH;C:\bin;$(LD_LIBRARY_PATH);${PYTHONPATH}
29 */
30 wxString expand_env_variable(const wxString& value, const wxEnvVariableHashMap& env_map)
31 {
32 // split the value into its parts
33 wxArrayString parts = wxStringTokenize(value, wxPATH_SEP, wxTOKEN_STRTOK);
34 wxString resolved;
35 for(const wxString& part : parts) {
36 wxArrayString resovled_array;
37 wxString env_name;
38 if(is_env_variable(part, &env_name)) {
39 // try the environment variables first
40 if(env_map.find(env_name) != env_map.end()) {
41 resolved << env_map.find(env_name)->second;
42 }
43 } else {
44 // literal
45 resolved << part;
46 }
47 resolved << wxPATH_SEP;
48 }
49 if(!resolved.empty()) {
50 resolved.RemoveLast();
51 }
52 return resolved;
53 }
54
55 clEnvList_t split_env_string(const wxString& env_str)
56 {
57 clEnvList_t result;
58 wxArrayString lines = ::wxStringTokenize(env_str, "\r\n", wxTOKEN_STRTOK);
59 for(wxString& line : lines) {
60 wxString key = line.BeforeFirst('=');
61 wxString value = line.AfterFirst('=');
62 if(key.empty()) {
63 continue;
64 }
65 result.push_back({ key, value });
66 }
67 return result;
68 }
69
70 } // namespace
71 std::string StringUtils::ToStdString(const wxString& str)
72 {
73 const char* data = str.mb_str(wxConvUTF8).data();
74 if(!data) {
75 data = str.To8BitData();
76 }
77
78 std::string res;
79 if(!data) {
80 return res;
81 }
82 res = data;
83 return res;
84 }
85
86 #define BUFF_STATE_NORMAL 0
87 #define BUFF_STATE_IN_ESC 1
88 #define BUFF_STATE_IN_OSC 2
89
90 // see : https://en.wikipedia.org/wiki/ANSI_escape_code#Escape_sequences
91 void StringUtils::StripTerminalColouring(const std::string& buffer, std::string& modbuffer)
92 {
93 modbuffer.reserve(buffer.length());
94 short state = BUFF_STATE_NORMAL;
95 for(const char& ch : buffer) {
96 switch(state) {
97 case BUFF_STATE_NORMAL:
98 if(ch == 0x1B) { // found ESC char
99 state = BUFF_STATE_IN_ESC;
100 } else {
101 modbuffer += ch;
102 }
103 break;
104 case BUFF_STATE_IN_ESC:
105 switch(ch) {
106 case 'm':
107 case 'K':
108 case 'G':
109 case 'J':
110 case 'H':
111 case 'X':
112 case 'B':
113 case 'C':
114 case 'D':
115 case 'd':
116 state = BUFF_STATE_NORMAL;
117 break;
118 case ']':
119 // Operating System Command
120 state = BUFF_STATE_IN_OSC;
121 break;
122 default:
123 break;
124 }
125 break;
126 case BUFF_STATE_IN_OSC:
127 if(ch == '\a') {
128 // bell, leave the current state
129 state = BUFF_STATE_NORMAL;
130 }
131 break;
132 }
133 }
134 modbuffer.shrink_to_fit();
135 }
136
137 void StringUtils::StripTerminalColouring(const wxString& buffer, wxString& modbuffer)
138 {
139 std::string source = ToStdString(buffer);
140 std::string output;
141 StripTerminalColouring(source, output);
142 if(!output.empty()) {
143 modbuffer = wxString(output.c_str(), wxConvUTF8);
144 if(modbuffer.IsEmpty()) {
145 modbuffer = wxString::From8BitData(output.c_str());
146 }
147 } else {
148 modbuffer.Clear();
149 }
150 }
151
152 void StringUtils::DisableMarkdownStyling(wxString& buffer)
153 {
154 buffer.Replace("\\", "\\\\");
155 buffer.Replace("#", "\\#");
156 buffer.Replace("-", "\\-");
157 buffer.Replace("=", "\\=");
158 buffer.Replace("*", "\\*");
159 buffer.Replace("~", "\\~");
160 buffer.Replace("`", "\\`");
161 }
162
163 #define ARGV_STATE_NORMAL 0
164 #define ARGV_STATE_DQUOTE 1
165 #define ARGV_STATE_SQUOTE 2
166 #define ARGV_STATE_ESCAPE 3
167 #define ARGV_STATE_BACKTICK 4 // `
168 #define ARGV_STATE_DOLLAR 5 //$
169 #define ARGV_STATE_PAREN 6 // (
170
171 #define PUSH_CURTOKEN() \
172 { \
173 if(!curstr.empty()) { \
174 A.push_back(curstr); \
175 curstr.clear(); \
176 } \
177 }
178
179 namespace
180 {
181 int get_current_state(const std::vector<int>& states)
182 {
183 if(states.empty()) {
184 return ARGV_STATE_NORMAL;
185 }
186 return states[0];
187 }
188
189 int get_prev_state(const std::vector<int>& states)
190 {
191 if(states.size() < 2) {
192 return ARGV_STATE_NORMAL;
193 }
194 return states[1];
195 }
196
197 void push_state(std::vector<int>& states, int state) { states.insert(states.begin(), state); }
198 void pop_state(std::vector<int>& states) { states.erase(states.begin()); }
199 } // namespace
200
201 char** StringUtils::BuildArgv(const wxString& str, int& argc)
202 {
203 std::vector<wxString> A;
204 int dollar_paren_depth = 0;
205 std::vector<int> states = { ARGV_STATE_NORMAL };
206 wxString curstr;
207 for(wxChar ch : str) {
208 switch(get_current_state(states)) {
209 case ARGV_STATE_NORMAL: {
210 switch(ch) {
211 case '$':
212 curstr << ch;
213 push_state(states, ARGV_STATE_DOLLAR);
214 break;
215 case ' ':
216 case '\t':
217 case ';':
218 PUSH_CURTOKEN();
219 break;
220 case '\'':
221 push_state(states, ARGV_STATE_SQUOTE);
222 curstr << ch;
223 break;
224 case '"':
225 push_state(states, ARGV_STATE_DQUOTE);
226 curstr << ch;
227 break;
228 case '`':
229 push_state(states, ARGV_STATE_BACKTICK);
230 curstr << ch;
231 break;
232 case '\\':
233 push_state(states, ARGV_STATE_ESCAPE);
234 curstr << ch;
235 break;
236 default:
237 curstr << ch;
238 break;
239 }
240 } break;
241 case ARGV_STATE_DOLLAR: {
242 switch(ch) {
243 case '(':
244 curstr << ch;
245 push_state(states, ARGV_STATE_PAREN);
246 dollar_paren_depth++; // makes it 1
247 break;
248 case ';':
249 case ' ':
250 case '\t':
251 PUSH_CURTOKEN();
252 pop_state(states);
253 break;
254 default:
255 curstr << ch;
256 pop_state(states);
257 break;
258 }
259 } break;
260 case ARGV_STATE_PAREN: {
261 switch(ch) {
262 case '(':
263 curstr << ch;
264 dollar_paren_depth++; // increase teh depth
265 break;
266 case ')':
267 curstr << ch;
268 dollar_paren_depth--; // reduce the depth
269 // if the depth reached 0, we should leave this state
270 if(dollar_paren_depth == 0) {
271 // leave the state
272 pop_state(states);
273 }
274 break;
275 default:
276 curstr << ch;
277 break;
278 }
279 } break;
280 case ARGV_STATE_ESCAPE: {
281 if(get_prev_state(states) == ARGV_STATE_NORMAL) {
282 curstr << ch;
283 pop_state(states);
284 break;
285 } else if(get_prev_state(states) == ARGV_STATE_DQUOTE) {
286 switch(ch) {
287 case '"':
288 curstr << "\"";
289 pop_state(states);
290 break;
291 default:
292 curstr << "\\" << ch;
293 pop_state(states);
294 break;
295 }
296 } else if(get_prev_state(states) == ARGV_STATE_BACKTICK) {
297 switch(ch) {
298 case '`':
299 curstr << "`";
300 pop_state(states);
301 break;
302 default:
303 curstr << "\\" << ch;
304 pop_state(states);
305 break;
306 }
307 } else { // single quote
308 switch(ch) {
309 case '\'':
310 curstr << "'";
311 pop_state(states);
312 break;
313 default:
314 curstr << "\\" << ch;
315 pop_state(states);
316 break;
317 }
318 }
319 } break;
320 case ARGV_STATE_DQUOTE: {
321 switch(ch) {
322 case '\\':
323 push_state(states, ARGV_STATE_ESCAPE);
324 break;
325 case '"':
326 curstr << ch;
327 pop_state(states);
328 break;
329 default:
330 curstr << ch;
331 break;
332 }
333 } break;
334 case ARGV_STATE_SQUOTE: {
335 switch(ch) {
336 case '\\':
337 push_state(states, ARGV_STATE_ESCAPE);
338 break;
339 case '\'':
340 curstr << ch;
341 pop_state(states);
342 break;
343 default:
344 curstr << ch;
345 break;
346 }
347 } break;
348 case ARGV_STATE_BACKTICK: {
349 switch(ch) {
350 case '\\':
351 push_state(states, ARGV_STATE_ESCAPE);
352 break;
353 case '`':
354 curstr << ch;
355 pop_state(states);
356 break;
357 default:
358 curstr << ch;
359 break;
360 }
361 } break;
362 }
363 }
364
365 if(!curstr.IsEmpty()) {
366 A.push_back(curstr);
367 }
368
369 if(A.empty()) {
370 return nullptr;
371 }
372
373 char** argv = new char*[A.size() + 1];
374 argv[A.size()] = NULL;
375 for(size_t i = 0; i < A.size(); ++i) {
376 argv[i] = strdup(A[i].mb_str(wxConvUTF8).data());
377 }
378 argc = (int)A.size();
379 return argv;
380 }
381
382 void StringUtils::FreeArgv(char** argv, int argc)
383 {
384 for(int i = 0; i < argc; ++i) {
385 free(argv[i]);
386 }
387 delete[] argv;
388 }
389
390 wxArrayString StringUtils::BuildArgv(const wxString& str)
391 {
392 int argc = 0;
393 char** argv = BuildArgv(str, argc);
394 wxArrayString arrArgv;
395 for(int i = 0; i < argc; ++i) {
396 arrArgv.Add(argv[i]);
397 }
398 FreeArgv(argv, argc);
399
400 for(wxString& s : arrArgv) {
401 if((s.length() > 1) && s.StartsWith("\"") && s.EndsWith("\"")) {
402 s.RemoveLast().Remove(0, 1);
403 }
404 }
405 return arrArgv;
406 }
407
408 clEnvList_t StringUtils::BuildEnvFromString(const wxString& envstr) { return split_env_string(envstr); }
409
410 clEnvList_t StringUtils::ResolveEnvList(const clEnvList_t& env_list)
411 {
412 wxEnvVariableHashMap current_env;
413 ::wxGetEnvMap(&current_env);
414
415 clEnvList_t result;
416 result.reserve(env_list.size());
417
418 for(auto& p : env_list) {
419 wxString key = p.first;
420 wxString value = p.second;
421 value = expand_env_variable(value, current_env);
422 current_env.erase(key);
423 current_env.insert({ key, value });
424 result.push_back({ key, value });
425 }
426 return result;
427 }
428
429 clEnvList_t StringUtils::ResolveEnvList(const wxString& envstr)
430 {
431 auto source_list = BuildEnvFromString(envstr);
432 return ResolveEnvList(source_list);
433 }
434
435 wxArrayString StringUtils::BuildCommandArrayFromString(const wxString& command)
436 {
437 wxArrayString lines = ::wxStringTokenize(command, "\n", wxTOKEN_STRTOK);
438
439 wxArrayString command_array;
440 command_array.reserve(lines.size());
441
442 for(auto& line : lines) {
443 line.Trim().Trim(false);
444 if(line.StartsWith("#") || line.IsEmpty()) {
445 continue;
446 }
447
448 line = line.BeforeFirst('#'); // remove trailing comment
449
450 int count = 0;
451 auto argv = BuildArgv(line, count);
452 for(int i = 0; i < count; ++i) {
453 command_array.push_back(argv[i]);
454 }
455 StringUtils::FreeArgv(argv, count);
456 }
457 return command_array;
458 }
459
460 wxString StringUtils::BuildCommandStringFromArray(const wxArrayString& command_arr, size_t flags)
461 {
462 wxString command;
463
464 bool span_multiple_lines = !(flags & ONE_LINER);
465 bool include_comment_block = (flags & WITH_COMMENT_PREFIX);
466
467 if(span_multiple_lines && include_comment_block) {
468 command << "# Command to execute:\n";
469 command << "\n";
470 }
471
472 const wxString SPACE = span_multiple_lines ? " " : " ";
473 const wxString COMMAND_SEPARATOR = span_multiple_lines ? "\n" : " ";
474
475 for(size_t i = 0; i < command_arr.size(); ++i) {
476 if(i > 0) {
477 command << SPACE;
478 }
479 command << command_arr[i] << COMMAND_SEPARATOR;
480 }
481 return command;
482 }