| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2008 by Eran Ifrah |
| 5 |
// file name : cpptoken.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 |
#include "cpptoken.h" |
| 26 |
#include <wx/crt.h> |
| 27 |
#include <map> |
| 28 |
|
| 29 |
CppToken::CppToken() { reset(); } |
| 30 |
|
| 31 |
CppToken::~CppToken() {} |
| 32 |
|
| 33 |
bool CppToken::operator<(const CppToken& rhs) const |
| 34 |
{ |
| 35 |
return this->getName() < rhs.getName(); |
| 36 |
} |
| 37 |
|
| 38 |
void CppToken::reset() |
| 39 |
{ |
| 40 |
name.clear(); |
| 41 |
offset = wxString::npos; |
| 42 |
m_id = wxNOT_FOUND; |
| 43 |
lineNumber = wxString::npos; |
| 44 |
filename.clear(); |
| 45 |
} |
| 46 |
|
| 47 |
void CppToken::append(wxChar ch) { name << ch; } |
| 48 |
|
| 49 |
void CppToken::print() { wxPrintf(wxT("%s | %ld\n"), name.c_str(), offset); } |
| 50 |
|
| 51 |
int CppToken::store(wxSQLite3Database* db, wxLongLong fileID) const |
| 52 |
{ |
| 53 |
try { |
| 54 |
|
| 55 |
wxSQLite3Statement st = db->PrepareStatement( |
| 56 |
"REPLACE INTO TOKENS_TABLE (ID, NAME, OFFSET, FILE_ID, LINE_NUMBER) VALUES(NULL, ?, ?, ?, ?)"); |
| 57 |
st.Bind(1, getName()); |
| 58 |
st.Bind(2, (int)getOffset()); |
| 59 |
st.Bind(3, fileID); |
| 60 |
st.Bind(4, (int)getLineNumber()); |
| 61 |
st.ExecuteUpdate(); |
| 62 |
return db->GetLastRowId().ToLong(); |
| 63 |
|
| 64 |
} catch(wxSQLite3Exception& e) { |
| 65 |
wxUnusedVar(e); |
| 66 |
} |
| 67 |
return wxNOT_FOUND; |
| 68 |
} |
| 69 |
|
| 70 |
CppToken::Vec_t CppToken::loadByNameAndFile(wxSQLite3Database* db, const wxString& name, wxLongLong fileID) |
| 71 |
{ |
| 72 |
CppToken::Vec_t matches; |
| 73 |
try { |
| 74 |
wxSQLite3Statement st = db->PrepareStatement("select * from TOKENS_TABLE where FILE_ID=? AND NAME=?"); |
| 75 |
st.Bind(1, fileID); |
| 76 |
st.Bind(2, name); |
| 77 |
wxSQLite3ResultSet res = st.ExecuteQuery(); |
| 78 |
while(res.NextRow()) { |
| 79 |
CppToken token(res); |
| 80 |
matches.push_back(token); |
| 81 |
} |
| 82 |
|
| 83 |
} catch(wxSQLite3Exception& e) { |
| 84 |
wxUnusedVar(e); |
| 85 |
} |
| 86 |
return matches; |
| 87 |
} |
| 88 |
|
| 89 |
CppToken::CppToken(wxSQLite3ResultSet& res) |
| 90 |
{ |
| 91 |
setId(res.GetInt(0)); |
| 92 |
setName(res.GetString(1)); |
| 93 |
setOffset(res.GetInt(2)); |
| 94 |
setLineNumber(res.GetInt(4)); |
| 95 |
} |
| 96 |
|
| 97 |
CppToken::Vec_t CppToken::loadByName(wxSQLite3Database* db, const wxString& name) |
| 98 |
{ |
| 99 |
CppToken::Vec_t matches; |
| 100 |
std::unordered_map<size_t, wxString> fileIdToFile; |
| 101 |
try { |
| 102 |
wxSQLite3Statement st = db->PrepareStatement("select * from TOKENS_TABLE where NAME=?"); |
| 103 |
st.Bind(1, name); |
| 104 |
wxSQLite3ResultSet res = st.ExecuteQuery(); |
| 105 |
while(res.NextRow()) { |
| 106 |
CppToken token(res); |
| 107 |
size_t fileID = res.GetInt64(3).ToLong(); |
| 108 |
if(fileIdToFile.count(fileID)) { |
| 109 |
token.setFilename(fileIdToFile.find(fileID)->second); |
| 110 |
} else { |
| 111 |
// load from the db |
| 112 |
wxSQLite3Statement st1 = db->PrepareStatement("SELECT FILE_NAME FROM FILES WHERE ID=? LIMIT 1"); |
| 113 |
st1.Bind(1, wxLongLong(fileID)); |
| 114 |
wxSQLite3ResultSet res1 = st1.ExecuteQuery(); |
| 115 |
if(res1.NextRow()) { |
| 116 |
token.setFilename(res1.GetString(0)); |
| 117 |
fileIdToFile.insert(std::make_pair(fileID, token.getFilename())); |
| 118 |
} |
| 119 |
} |
| 120 |
matches.push_back(token); |
| 121 |
} |
| 122 |
|
| 123 |
} catch(wxSQLite3Exception& e) { |
| 124 |
wxUnusedVar(e); |
| 125 |
} |
| 126 |
return matches; |
| 127 |
} |
| 128 |
|
| 129 |
//----------------------------------------------------------------- |
| 130 |
// CppTokensMap |
| 131 |
//----------------------------------------------------------------- |
| 132 |
CppTokensMap::CppTokensMap() {} |
| 133 |
|
| 134 |
CppTokensMap::~CppTokensMap() { clear(); } |
| 135 |
|
| 136 |
void CppTokensMap::addToken(const wxString& name, const CppToken::Vec_t& list) |
| 137 |
{ |
| 138 |
// try to locate an entry with this name |
| 139 |
std::unordered_map<wxString, std::vector<CppToken>*>::iterator iter = m_tokens.find(name); |
| 140 |
std::vector<CppToken>* tokensList(NULL); |
| 141 |
if(iter != m_tokens.end()) { |
| 142 |
tokensList = iter->second; |
| 143 |
} else { |
| 144 |
// create new list and add it to the map |
| 145 |
tokensList = new std::vector<CppToken>; |
| 146 |
m_tokens.insert(std::make_pair(name, tokensList)); |
| 147 |
} |
| 148 |
tokensList->insert(tokensList->end(), list.begin(), list.end()); |
| 149 |
} |
| 150 |
|
| 151 |
void CppTokensMap::addToken(const CppToken& token) |
| 152 |
{ |
| 153 |
// try to locate an entry with this name |
| 154 |
std::unordered_map<wxString, std::vector<CppToken>*>::iterator iter = m_tokens.find(token.getName()); |
| 155 |
std::vector<CppToken>* tokensList(NULL); |
| 156 |
if(iter != m_tokens.end()) { |
| 157 |
tokensList = iter->second; |
| 158 |
} else { |
| 159 |
// create new list and add it to the map |
| 160 |
tokensList = new std::vector<CppToken>; |
| 161 |
m_tokens[token.getName()] = tokensList; |
| 162 |
} |
| 163 |
tokensList->push_back(token); |
| 164 |
} |
| 165 |
|
| 166 |
bool CppTokensMap::contains(const wxString& name) |
| 167 |
{ |
| 168 |
std::unordered_map<wxString, std::vector<CppToken>*>::iterator iter = m_tokens.find(name); |
| 169 |
return iter != m_tokens.end(); |
| 170 |
} |
| 171 |
|
| 172 |
void CppTokensMap::findTokens(const wxString& name, std::vector<CppToken>& tokens) |
| 173 |
{ |
| 174 |
std::unordered_map<wxString, std::vector<CppToken>*>::iterator iter = m_tokens.find(name); |
| 175 |
// std::vector<CppToken> *tokensList(NULL); |
| 176 |
if(iter != m_tokens.end()) { tokens = *(iter->second); } |
| 177 |
} |
| 178 |
void CppTokensMap::clear() |
| 179 |
{ |
| 180 |
std::unordered_map<wxString, std::vector<CppToken>*>::iterator iter = m_tokens.begin(); |
| 181 |
for(; iter != m_tokens.end(); ++iter) { |
| 182 |
delete iter->second; |
| 183 |
} |
| 184 |
m_tokens.clear(); |
| 185 |
} |
| 186 |
|
| 187 |
bool CppTokensMap::is_empty() { return m_tokens.empty(); } |