Menu

[r7]: / Plugin / cppwordscanner.cpp  Maximize  Restore  History

Download this file

216 lines (188 with data), 5.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : cppwordscanner.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include <wx/ffile.h>
#include "globals.h"
#include <wx/tokenzr.h>
#include "cppwordscanner.h"
#include "stringaccessor.h"
//#include "tokendb.h"
enum {
STATE_NORMAL = 0,
STATE_C_COMMENT,
STATE_CPP_COMMENT,
STATE_DQ_STRING,
STATE_SINGLE_STRING,
STATE_PRE_PROCESSING
};
CppWordScanner::CppWordScanner(const wxString &file_name)
: m_filename(file_name)
{
wxString key_words =
wxT("auto break case char const continue default define defined do double elif else endif enum error extern float"
"for goto if ifdef ifndef include int line long pragma register return short signed sizeof static struct switch"
"typedef undef union unsigned void volatile while class namespace delete friend inline new operator overload"
"protected private public this virtual template typename dynamic_cast static_cast const_cast reinterpret_cast"
"using throw catch size_t");
//add this items into map
m_arr = wxStringTokenize(key_words, wxT(" "));
m_arr.Sort();
#ifdef __WXGTK__
wxFFile thefile(file_name, wxT("rb"));
wxFileOffset size = thefile.Length();
wxString fileData;
fileData.Alloc(size);
thefile.ReadAll( &m_text );
#else
ReadFileWithConversion(file_name, m_text);
#endif
}
CppWordScanner::~CppWordScanner()
{
// m_db = NULL;
}
void CppWordScanner::FindAll(CppTokensMap &l)
{
doFind(wxEmptyString, l);
// if (m_db) {
// m_db->BeginTransaction();
// m_db->DeleteByFile(m_filename);
// CppTokenList::iterator iter = l.begin();
// for (; iter != l.end(); iter++) {
// m_db->Store( (*iter) );
// }
// m_db->Commit();
// }
}
void CppWordScanner::Match(const wxString& word, CppTokensMap& l)
{
doFind(word, l);
// if (m_db) {
// m_db->BeginTransaction();
// m_db->DeleteByFile(m_filename);
// CppTokenList::iterator iter = l.begin();
// for (; iter != l.end(); iter++) {
// m_db->Store( (*iter) );
// }
// m_db->Commit();
// }
}
void CppWordScanner::doFind(const wxString& filter, CppTokensMap& l)
{
int state(STATE_NORMAL);
// bool sol(true);
StringAccessor accessor(m_text);
CppToken token;
for (size_t i=0; i<m_text.size(); i++) {
char ch = accessor.safeAt(i);
switch (state) {
case STATE_NORMAL:
if (accessor.match("#", i)) {
if ( i == 0 || // satrt of document
accessor.match("\n", i-1)) { // we are at start of line
state = STATE_PRE_PROCESSING;
}
} else if (accessor.match("//", i)) {
// C++ comment, advance i
state = STATE_CPP_COMMENT;
i++;
} else if (accessor.match("/*", i)) {
// C comment
state = STATE_C_COMMENT;
i++;
} else if (accessor.match("'", i)) {
// single quoted string
state = STATE_SINGLE_STRING;
} else if (accessor.match("\"", i)) {
// dbouble quoted string
state = STATE_DQ_STRING;
} else if ( accessor.isWordChar(ch) ) {
// is valid C++ word?
token.append( ch );
if (token.getOffset() == wxString::npos) {
token.setOffset( i );
}
} else {
if (token.getName().empty() == false) {
if ((int)token.getName().GetChar(0) >= 48 && (int)token.getName().GetChar(0) <= 57) {
token.reset();
} else {
//dont add C++ key words
if (m_arr.Index(token.getName()) == wxNOT_FOUND) {
// Ok, we are not a number!
// filter out non matching words
if (filter.empty() || filter == token.getName()) {
token.setFilename(m_filename);
l.addToken(token);
}
}
token.reset();
}
}
}
break;
case STATE_PRE_PROCESSING:
//skip pre processor lines
if ( accessor.match("\n", i) && !accessor.match("\\", i-1) ) {
// no wrap
state = STATE_NORMAL;
}
break;
case STATE_C_COMMENT:
if ( accessor.match("*/", i)) {
state = STATE_NORMAL;
i++;
}
break;
case STATE_CPP_COMMENT:
if ( accessor.match("\n", i)) {
state = STATE_NORMAL;
}
break;
case STATE_DQ_STRING:
if (accessor.match("\\\"", i)) {
//escaped string
i++;
} else if(accessor.match("\\", i)){
i++;
} else if (accessor.match("\"", i)) {
state = STATE_NORMAL;
}
break;
case STATE_SINGLE_STRING:
if (accessor.match("\\'", i)) {
//escaped single string
i++;
} else if (accessor.match("'", i)) {
state = STATE_NORMAL;
}
break;
}
}
}
//void CppWordScanner::SetDatabase(TokenDb* db)
//{
// m_db = db;
//}
//
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.