forked from morbac/xmltools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToolsConvert.cpp
105 lines (81 loc) · 2.66 KB
/
ToolsConvert.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
#include "StdAfx.h"
#include "XMLTools.h"
#include "nppHelpers.h"
#include "Report.h"
void strreplaceall(std::string& str, const char* find, const char* replacement) {
auto curpos = str.size();
auto findSize = strlen(find);
while (curpos != std::string::npos && (curpos = str.rfind(find, curpos)) != std::string::npos) {
if (curpos != std::string::npos) {
str.replace(curpos, findSize, replacement);
}
--curpos;
}
}
void sciConvertText2XML(ScintillaDoc &doc) {
auto seltext = doc.GetSelectedText();
if (!seltext) {
Report::_printf_err(L"Please select text to transform before you call the function.");
return;
}
std::string str(seltext.text);
seltext.FreeMemory();
if (xmltoolsoptions.convertApos) {
strreplaceall(str, "'", "'");
}
if (xmltoolsoptions.convertQuote) {
strreplaceall(str, """, "\"");
}
if (xmltoolsoptions.convertLt) {
strreplaceall(str, "<", "<");
}
if (xmltoolsoptions.convertGt) {
strreplaceall(str, ">", ">");
}
// & needs to be the last one
if (xmltoolsoptions.convertAmp) {
strreplaceall(str, "&", "&");
}
// Replace the selection with new string
doc.ReplaceSelection(str.c_str());
// Defines selection without scrolling
doc.SetCurrentPosition(seltext.selstart);
doc.SetAnchor(seltext.selstart + str.length());
}
void nppConvertText2XML() {
nppDocumentCommand(L"convertText2XML", sciConvertText2XML);
}
///////////////////////////////////////////////////////////////////////////////
void sciConvertXML2Text(ScintillaDoc &doc) {
auto seltext = doc.GetSelectedText();
if (!seltext) {
Report::_printf_err(L"Please select text to transform before you call the function.");
return;
}
std::string str(seltext.text);
seltext.FreeMemory();
// & needs to be the first one
if (xmltoolsoptions.convertAmp) {
strreplaceall(str, "&", "&");
}
if (xmltoolsoptions.convertApos) {
strreplaceall(str, "'", "'");
}
if (xmltoolsoptions.convertQuote) {
strreplaceall(str, "\"", """);
}
if (xmltoolsoptions.convertLt) {
strreplaceall(str, "<", "<");
}
if (xmltoolsoptions.convertGt) {
strreplaceall(str, ">", ">");
}
// Replace the selection with new string
doc.ReplaceSelection(str.c_str());
// Defines selection without scrolling
doc.SetCurrentPosition(seltext.selstart);
doc.SetAnchor(seltext.selstart + str.length());
}
void nppConvertXML2Text() {
nppDocumentCommand(L"convertXML2Text", sciConvertXML2Text);
}