Debian Codegraph @

CodeLite/clcommandlineparser.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : clcommandlineparser.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "clcommandlineparser.h"
27
28 clCommandLineParser::clCommandLineParser(const wxString& str, size_t flags)
29 : m_commandline(str)
30 , m_flags(flags)
31 {
32 DoParse();
33 }
34
35 clCommandLineParser::~clCommandLineParser() {}
36
37 #define STATE_NORMAL 0
38 #define STATE_IN_SINGLE_QUOTES 1
39 #define STATE_IN_DOUBLE_QUOTES 2
40
41 #define PUSH_TOKEN() \
42 if(!curtoken.IsEmpty()) { m_tokens.Add(curtoken); } \
43 curtoken.Clear();
44
45 #define HANDLE_NEWLINE() \
46 if(!(m_flags & kIgnoreNewLines)) { curtoken << ch; }
47
48 void clCommandLineParser::DoParse()
49 {
50 wxString tmpstr = m_commandline;
51 wxString curtoken;
52
53 int state = STATE_NORMAL;
54 for(size_t i = 0; i < tmpstr.length(); ++i) {
55 wxChar ch = tmpstr.at(i);
56 switch(state) {
57 case STATE_NORMAL: {
58 switch(ch) {
59 case '\n':
60 case '\r':
61 HANDLE_NEWLINE();
62 break;
63 case ' ':
64 case '\t':
65 // whitespace, start new token
66 PUSH_TOKEN();
67 break;
68 case '"':
69 PUSH_TOKEN();
70 state = STATE_IN_DOUBLE_QUOTES;
71 break;
72 case '\'':
73 PUSH_TOKEN();
74 state = STATE_IN_SINGLE_QUOTES;
75 break;
76 default: {
77 // Normal
78 curtoken << ch;
79 break;
80 }
81 }
82 break;
83 }
84 case STATE_IN_DOUBLE_QUOTES:
85 if(ch == '"') {
86 PUSH_TOKEN();
87 state = STATE_NORMAL;
88 } else {
89 curtoken << ch;
90 }
91 break;
92 case STATE_IN_SINGLE_QUOTES:
93 if(ch == '\'') {
94 PUSH_TOKEN();
95 state = STATE_NORMAL;
96 } else {
97 curtoken << ch;
98 }
99 break;
100 }
101 }
102 PUSH_TOKEN();
103 }