-
Notifications
You must be signed in to change notification settings - Fork 12
/
Snippets.cpp
326 lines (275 loc) · 9.14 KB
/
Snippets.cpp
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/////////////////////////////////////////////////////////////////////////////
// //
// NppSnippets - Code Snippets plugin for Notepad++ //
// Copyright (C) 2010-2011 Frank Fesevur //
// //
// 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. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program; if not, write to the Free Software //
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. //
// //
/////////////////////////////////////////////////////////////////////////////
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "NPP/PluginInterface.h"
#include "NppSnippets.h"
#include "Snippets.h"
/////////////////////////////////////////////////////////////////////////////
//
void SnippetBase::WSetName(LPCWCH txt)
{
if (_Name != NULL)
free(_Name);
_Name = (txt == NULL ? NULL : wcsdup(txt));
}
/////////////////////////////////////////////////////////////////////////////
//
Snippet::Snippet()
{
_SnippetID = 0;
_LibraryID = 0;
_Name = NULL;
_BeforeSelection = NULL;
_AfterSelection = NULL;
_ReplaceSelection = false;
_NewDocument = false;
_NewDocumentLang = L_EXTERNAL;
_Sort = 0;
}
Snippet::Snippet(SqliteStatement* stmt)
{
_Name = NULL;
_BeforeSelection = NULL;
_AfterSelection = NULL;
Set(stmt);
}
///////////////////////////////////////////////////////////////////////////
// Copy-Constructor.
Snippet::Snippet(const Snippet& copy)
{
_SnippetID = 0;
_Name = NULL;
_BeforeSelection = NULL;
_AfterSelection = NULL;
CopyValues(copy);
}
Snippet::~Snippet()
{
if (_Name != NULL)
free(_Name);
if (_BeforeSelection != NULL)
free(_BeforeSelection);
if (_AfterSelection != NULL)
free(_AfterSelection);
}
///////////////////////////////////////////////////////////////////////////
// Overloads of the '=' operator
Snippet& Snippet::operator=(const Snippet& copy)
{
CopyValues(copy);
return(*this);
}
Snippet& Snippet::operator=(SqliteStatement* stmt)
{
Set(stmt);
return(*this);
}
/////////////////////////////////////////////////////////////////////////////
//
void Snippet::CopyValues(const Snippet& copy)
{
//_SnippetID = copy._SnippetID;
_LibraryID = copy._LibraryID;
WSetName(copy._Name);
WSetBeforeSelection(copy._BeforeSelection);
WSetAfterSelection(copy._AfterSelection);
_ReplaceSelection = copy._ReplaceSelection;
_NewDocument = copy._NewDocument;
_NewDocumentLang = copy._NewDocumentLang;
_Sort = copy._Sort;
}
/////////////////////////////////////////////////////////////////////////////
//
void Snippet::WSetBeforeSelection(LPCWCH txt)
{
if (_BeforeSelection != NULL)
free(_BeforeSelection);
_BeforeSelection = (txt == NULL ? NULL : wcsdup(txt));
}
void Snippet::WSetAfterSelection(LPCWCH txt)
{
if (_AfterSelection != NULL)
free(_AfterSelection);
_AfterSelection = (txt == NULL ? NULL : wcsdup(txt));
}
void Snippet::SetBeforeSelection(LPCSTR txt)
{
if (txt == NULL)
{
WSetBeforeSelection(NULL);
return;
}
size_t len = strlen(txt);
if (len == 0)
{
WSetBeforeSelection(NULL);
return;
}
// Convert from UTF-8 to WCHAR and store
size_t sizeInChars = MultiByteToWideChar(CP_UTF8, 0, txt, (int) (len + 1), NULL, 0);
WCHAR* wBuffer = (WCHAR*) malloc(sizeInChars * sizeof(WCHAR));
ZeroMemory(wBuffer, sizeInChars * sizeof(WCHAR));
MultiByteToWideChar(CP_UTF8, 0, txt, (int) (len + 1), wBuffer, (int)sizeInChars);
WSetBeforeSelection(wBuffer);
free(wBuffer);
}
///////////////////////////////////////////////////////////////////////////////
// Convert the Unicode-string to an Ansi-string
LPSTR Snippet::Unicode2Ansi(LPCWSTR wszStr)
{
size_t len = wcslen(wszStr);
size_t size = sizeof(char) * (len + 3);
char* buffer = (char*) malloc(size);
ZeroMemory(buffer, size);
if (wszStr != NULL)
WideCharToMultiByte(CP_ACP, 0, wszStr, -1, buffer, (int) len, NULL, NULL);
return buffer;
}
/////////////////////////////////////////////////////////////////////////////
//
void Snippet::Set(SqliteStatement* stmt)
{
_SnippetID = stmt->GetIntColumn("SnippetID");
_LibraryID = stmt->GetIntColumn("LibraryID");
WSetName(stmt->GetWTextColumn("Name").c_str());
WSetBeforeSelection(stmt->GetWTextColumn("BeforeSelection").c_str());
WSetAfterSelection(stmt->GetWTextColumn("AfterSelection").c_str());
SetReplaceSelection(stmt->GetIntColumn("ReplaceSelection"));
SetNewDocument(stmt->GetIntColumn("NewDocument"));
SetSort(stmt->GetIntColumn("Sort"));
// Get the language for a new document
_NewDocumentLang = (LangType) stmt->GetIntColumn("NewDocumentLang");
if (_NewDocumentLang == 0)
{
//if (sqlite3_column_type(stmt, cNames["NewDocumentLang"]) == SQLITE_NULL)
//{
// _NewDocumentLang = L_EXTERNAL;
//}
}
}
/////////////////////////////////////////////////////////////////////////////
// Save the snippet to the database
void Snippet::SaveToDB(bool autoOpen)
{
// Try to open the database
g_db->Open();
// Saving a new record or updating an existing?
SqliteStatement stmt(g_db);
bool adding = false;
if (_SnippetID == 0)
{
adding = true;
// Determine the last used SnippetID
SqliteStatement stmt2(g_db, "SELECT MAX(SnippetID) AS MaxID FROM Snippets");
stmt2.GetNextRecord();
_SnippetID = stmt2.GetIntColumn("MaxID") + 1;
stmt2.Finalize();
// Prepare the statement
stmt.Prepare("INSERT INTO Snippets(SnippetID, LibraryID, Name, BeforeSelection, AfterSelection, ReplaceSelection, NewDocument, NewDocumentLang, Sort) VALUES (@id, @libid, @name, @before, @after, @replace, @newdoc, @newdoclang, @sort)");
}
else
{
// Prepare the statement
stmt.Prepare("UPDATE Snippets SET Name = @name, BeforeSelection = @before, AfterSelection = @after, ReplaceSelection = @replace, NewDocument = @newdoc, NewDocumentLang = @newdoclang, Sort = @sort WHERE SnippetID = @id");
}
// Bind the values to the parameters
stmt.Bind("@id", _SnippetID);
if (adding)
stmt.Bind("@libid", _LibraryID);
stmt.Bind("@name", _Name);
stmt.Bind("@before", _BeforeSelection);
stmt.Bind("@after", _AfterSelection);
stmt.Bind("@replace", GetReplaceSelectionInt());
stmt.Bind("@newdoc", GetNewDocumentInt());
stmt.Bind("@newdoclang", _NewDocumentLang, _NewDocumentLang == L_EXTERNAL);
stmt.Bind("@sort", _Sort, _Sort == 0);
stmt.SaveRecord();
stmt.Finalize();
if (autoOpen)
g_db->Close();
}
/////////////////////////////////////////////////////////////////////////////
// Delete the snippet from the database
void Snippet::DeleteFromDB()
{
// Try to open the database
g_db->Open();
SqliteStatement stmt(g_db, "DELETE FROM Snippets WHERE SnippetID = @id");
stmt.Bind("@id", _SnippetID);
stmt.SaveRecord();
stmt.Finalize();
g_db->Close();
}
/////////////////////////////////////////////////////////////////////////////
// Guess the name of the snippet based upon the context of _BeforeSelection
void Snippet::GuessName()
{
// If the name already set, do nothing
if (_Name != NULL)
return;
// Is there a text to analyse?
if (_BeforeSelection == NULL)
return;
// First, try to find the first line containing text and use that as Name
bool foundChar = false;
WCHAR* p = _BeforeSelection;
WCHAR* start = _BeforeSelection;
while (*p)
{
// Did we run into a space character?
if (isspace(*p))
{
// Did we already find any normal character
if (foundChar)
{
// Did we run into the end of the line
if (*p == '\r' || *p == '\n')
break;
}
}
else
{
// Is the first normal character, store it's position
if (!foundChar)
{
start = p;
foundChar = true;
}
}
p++;
}
// If we didn't find any normal character, we're done
if (!foundChar)
return;
// Now see if this text is not too long and therefore becomes impractible
size_t len = ((size_t) p - (size_t) start) / sizeof(WCHAR);
if (len > 50)
len = 50;
// Finally we can set the Name
size_t size = sizeof(WCHAR) * (len + 3);
WCHAR* tmp = (WCHAR*) malloc(size);
ZeroMemory(tmp, size);
memcpy(tmp, start, sizeof(WCHAR) * len);
WSetName(tmp);
free(tmp);
}