-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathREBuilder.cpp
More file actions
169 lines (147 loc) · 5.14 KB
/
Copy pathREBuilder.cpp
File metadata and controls
169 lines (147 loc) · 5.14 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
#include "StdAfx.h"
#include "RESearch.h"
// Start, length
typedef map<int, int> part_map;
tstring MatchingRE(const tstring &strSource)
{
if (CRegExp::Match(strSource, _T("^\\d+$"), 0, NULL)) return _T("\\d+");
if (CRegExp::Match(strSource, _T("^\\s+$"), 0, NULL)) return _T("\\s+");
if (CRegExp::Match(strSource, _T("^\\w+$"), 0, NULL)) return _T("\\w+");
if (CRegExp::Match(strSource, _T("^\\S+$"), 0, NULL)) return _T("\\S+");
return _T(".+");
}
tstring BuildRE(const tstring &strSource, const part_map &mapParts)
{
tstring strResult;
int nLeftOff = 0;
for (part_map::const_iterator it = mapParts.begin(); it != mapParts.end(); it++) {
tstring strStart = strSource.substr(nLeftOff, it->first-nLeftOff);
CSO::QuoteRegExpString(strStart);
strResult += strStart;
tstring strPart = strSource.substr(it->first, it->second);
tstring strRE = _T("(") + MatchingRE(strPart) + _T(")");
strResult += strRE;
nLeftOff = it->first + it->second;
}
tstring strEnd = strSource.substr(nLeftOff);
CSO::QuoteRegExpString(strEnd);
strResult += strEnd;
return strResult;
}
struct sREData {
part_map mapParts;
int nCurrentPart;
};
void UpdateStrings(CFarDialog *pDlg, sREData *pData)
{
tstring strSource = pDlg->GetDlgItemText(2);
tstring strRE = BuildRE(strSource, pData->mapParts);
pDlg->SendDlgMessage(DM_SETTEXTPTR, 4, (LONG_PTR)strRE.data());
pcre *re;
if (!PreparePattern(&re, NULL, strRE, ECaseSensitive)) return;
TREParameters ThisREParam;
ThisREParam.AddSource(strSource.c_str(), strSource.length());
ThisREParam.AddRE(re);
if (pcre_exec(re, NULL, strSource.c_str(), strSource.length(), 0, 0, ThisREParam.Match(), ThisREParam.Count()) < 0) return;
tstring strReplace = pDlg->GetDlgItemText(6);
tstring strResult = CSO::CreateReplaceString(strReplace.c_str(), _T("\n"), NULL, ThisREParam);
pDlg->SendDlgMessage(DM_SETTEXTPTR, 8, (LONG_PTR)strResult.data());
}
LONG_PTR WINAPI REBuilderDialogProc(CFarDialog *pDlg, int nMsg, int nParam1, LONG_PTR lParam2)
{
sREData *pData = (sREData *)pDlg->SendDlgMessage(DM_GETDLGDATA, 0, 0);
switch (nMsg) {
case DN_INITDIALOG:
pDlg->SendDlgMessage(DM_SETDLGDATA, 0, lParam2);
break;
case DN_DRAWDLGITEM:
if (nParam1 == 2) {
EditorSelect Select;
#ifdef FAR3
Select.StructSize = sizeof(EditorSelect);
#endif
pDlg->SendDlgMessage(DM_GETSELECTION, 2, (LONG_PTR)&Select);
pData->mapParts.erase(pData->nCurrentPart);
if ((Select.BlockType == BTYPE_STREAM) && (Select.BlockWidth > 0)) {
int BlockEndPos = Select.BlockStartPos + Select.BlockWidth;
bool bNewBlock = true;
for (part_map::const_iterator it = pData->mapParts.begin(); bNewBlock && (it != pData->mapParts.end()); it++) {
if (Select.BlockStartPos < it->first) {
bNewBlock = BlockEndPos <= it->first;
} else if (Select.BlockStartPos < it->first+it->second) {
bNewBlock = false;
}
}
if (bNewBlock) {
pData->nCurrentPart = Select.BlockStartPos;
pData->mapParts[pData->nCurrentPart] = Select.BlockWidth;
}
} else {
pData->nCurrentPart = -1;
}
UpdateStrings(pDlg, pData);
}
break;
#ifdef FAR3
case DN_CONTROLINPUT:
if (pDlg->GetFocus() != 2) break;
INPUT_RECORD *record = (INPUT_RECORD *)lParam2;
if ((record->EventType == KEY_EVENT) && (record->Event.KeyEvent.bKeyDown)) {
if (record->Event.KeyEvent.wVirtualKeyCode == VK_RETURN) {
pData->nCurrentPart = -1;
UpdateStrings(pDlg, pData);
return true;
}
if (record->Event.KeyEvent.wVirtualKeyCode == VK_ESCAPE) {
pData->nCurrentPart = -1;
pData->mapParts.clear();
UpdateStrings(pDlg, pData);
return true;
}
}
break;
#else
case DN_KEY:
if ((nParam1 == 2) && (lParam2 == KEY_ENTER)) {
pData->nCurrentPart = -1;
UpdateStrings(pDlg, pData);
return true;
}
if ((nParam1 == 2) && (lParam2 == KEY_ESC)) {
pData->nCurrentPart = -1;
pData->mapParts.clear();
UpdateStrings(pDlg, pData);
return true;
}
break;
#endif
}
return pDlg->DefDlgProc(nMsg, nParam1, lParam2);
}
bool RunREBuilder(tstring &strSearch, tstring &strReplace)
{
tstring strSource = strSearch, strRE, strResult;
sREData Data;
Data.nCurrentPart = -1;
CFarDialog Dialog(76, 15, _T("REBuilder"));
Dialog.SetWindowProc(REBuilderDialogProc, (LONG_PTR)&Data);
Dialog.AddFrame(MREBuilder);
Dialog.Add(new CFarTextItem(5, 2, 0, MRBSourceText));
Dialog.Add(new CFarEditItem(5, 3, 70, DIF_HISTORY,_T("SearchText"), strSource));
Dialog.Add(new CFarTextItem(5, 4, 0, MRBResultRE));
Dialog.Add(new CFarEditItem(5, 5, 70, DIF_HISTORY|DIF_READONLY,_T("RESearch.RBResultRE"), strRE));
Dialog.Add(new CFarTextItem(5, 6, 0, MRBReplaceText));
Dialog.Add(new CFarEditItem(5, 7, 70, DIF_HISTORY,_T("ReplaceText"), strReplace));
Dialog.Add(new CFarTextItem(5, 8, 0, MRBResultText));
Dialog.Add(new CFarEditItem(5, 9, 70, DIF_HISTORY|DIF_READONLY,_T("RESearch.RBResultText"), strResult));
Dialog.AddButtons(MOk, MCancel);
do {
switch (Dialog.Display(1, -2)) {
case 0:
strSearch = strRE;
return true;
case -1:
return false;
}
} while (true);
}