-
Notifications
You must be signed in to change notification settings - Fork 4
/
apputils.cpp
306 lines (270 loc) · 7.2 KB
/
apputils.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
#include "stdafx.h"
#include "resource.h"
#include "res1.h"
#include "utils.h"
#include "apputils.h"
#include "ModelessDialog.h"
extern RECT dialogRect;
namespace AU {
// a getopt implementation
static int xgetopt(
CSimpleArray<CString>& argv,
const TCHAR *ospec,
int& argp,
const TCHAR *&state,
const TCHAR *&arg)
{
const TCHAR *cp;
TCHAR opt;
if (!state || !state[0]) { // look a the next arg
if (argp>=argv.GetSize() || argv[argp][0]!='-') // no more options
return 0;
if (!argv[argp][1]) // a lone '-', treat as an end of list
return 0;
if (argv[argp][1]=='-') { // '--', ignore rest of text and stop
++argp;
return 0;
}
state=(const TCHAR *)argv[argp]+1;
++argp;
}
// we are in a middle of an arg
opt=(unsigned)*state++;
bool found = false;
for (cp=ospec;*cp;++cp) {
if (*cp==opt)
{
found = true;
break;
}
if (cp[1]==':')
++cp;
}
if(!found)
{
U::MessageBox(MB_OK|MB_ICONERROR, IDS_ERRMSGBOX_CAPTION, IDS_INVALID_CML_MSG, opt);
return -1; // error
}
if (cp[1]==':') { // option requires an argument
if (*state) { // use rest of string
arg=state;
state=NULL;
return opt;
}
// use next arg if available
if (argp<argv.GetSize()) {
arg=argv[argp];
++argp;
return opt;
}
// barf about missing args
U::MessageBox(MB_OK|MB_ICONERROR, IDS_ERRMSGBOX_CAPTION, IDS_CML_ARGS_MSG, opt);
return -1;
}
// just return current option
return opt;
}
CmdLineArgs _ARGS;
bool ParseCmdLineArgs() {
const TCHAR *arg,*state=NULL;
int argp=0;
int ch;
for (;;) {
switch ((ch=xgetopt(_ARGV,_T("d"),argp,state,arg))) {
case 0: // end of options
while (argp--)
_ARGV.RemoveAt(0);
return true;
case _T('d'):
_ARGS.start_in_desc_mode=true;
break;
case -1: // error
return false;
// just ignore options for now :)
}
}
}
// an input box
class CInputBox: public CDialogImpl<CInputBox>,
public CWinDataExchange<CInputBox>
{
public:
enum { IDD=IDD_INPUTBOX };
CString m_text;
const wchar_t *m_title;
const wchar_t *m_prompt;
CInputBox(const wchar_t *title,const wchar_t *prompt) : m_title(title), m_prompt(prompt) { }
BEGIN_DDX_MAP(CInputBox)
DDX_TEXT(IDC_INPUT,m_text)
END_DDX_MAP()
BEGIN_MSG_MAP(CInputBox)
COMMAND_ID_HANDLER(IDYES, OnYes)
COMMAND_ID_HANDLER(IDNO, OnCancel)
COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
END_MSG_MAP()
LRESULT OnInitDialog(UINT, WPARAM, LPARAM, BOOL&) {
DoDataExchange(FALSE); // Populate the controls
SetDlgItemText(IDC_PROMPT,m_prompt);
SetWindowText(m_title);
if (dialogRect.left != -1)
::SetWindowPos(m_hWnd, HWND_TOPMOST, dialogRect.left, dialogRect.top, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW);
return 0;
}
BOOL EndDialog(int nRetCode)
{
::GetWindowRect(m_hWnd, &dialogRect);
return ::EndDialog(m_hWnd, nRetCode);
}
LRESULT OnYes(WORD, WORD wID, HWND, BOOL&) {
DoDataExchange(TRUE); // Populate the data members
EndDialog(wID);
return 0L;
}
LRESULT OnCancel(WORD, WORD wID, HWND, BOOL&) {
EndDialog(wID);
return 0L;
}
};
int InputBox(CString& result, const wchar_t *title, const wchar_t *prompt) {
CInputBox dlg(title,prompt);
dlg.m_text=result;
int dlgResult = dlg.DoModal();
if (dlgResult == IDYES) result=dlg.m_text;
return dlgResult;
}
// html
CString GetAttrCS(MSHTML::IHTMLElement *elem,const wchar_t *attr) {
if (!elem) return L"";
_variant_t va(elem->getAttribute(attr,2));
if (V_VT(&va)!=VT_BSTR)
return CString();
return V_BSTR(&va);
}
_bstr_t GetAttrB(MSHTML::IHTMLElement *elem,const wchar_t *attr) {
if (!elem) return L"";
_variant_t va(elem->getAttribute(attr,2));
if (V_VT(&va)!=VT_BSTR)
return _bstr_t();
return _bstr_t(va.Detach().bstrVal,false);
}
char *ToUtf8(const CString& s,int& patlen) {
DWORD len=::WideCharToMultiByte(CP_UTF8,0,
s,s.GetLength(),
NULL,0,NULL,NULL);
char *tmp=(char *)malloc(len+1);
if (tmp) {
::WideCharToMultiByte(CP_UTF8,0,
s,s.GetLength(),
tmp,len,NULL,NULL);
tmp[len]='\0';
}
patlen=len;
return tmp;
}
#ifdef USE_PCRE
// Regexp constructor
IMatchCollection* IRegExp2::Execute (CString sourceString)
{
#define OVECCOUNT 300
pcre *re;
const char *error;
bool is_error = true;
int options;
int erroffset;
int ovector[OVECCOUNT];
int subject_length;
int rc, offset, char_offset;
IMatchCollection* matches;
// fix for issue #145
char dst[0xFFFF];
matches = new IMatchCollection();
options = IgnoreCase?PCRE_CASELESS:0;
options |= PCRE_UTF8;
// convert pattern to UTF-8
CT2A pat(m_pattern, CP_UTF8);
re = pcre_compile(
pat, /* the pattern */
options, /* default options */
&error, /* for error message */
&erroffset, /* for error offset */
NULL); /* use default character tables */
if (re)
{
is_error = false;
CT2A subj(sourceString, CP_UTF8);
subject_length = strlen(subj);
offset = char_offset = 0;
do
{
rc = pcre_exec(
re, /* the compiled pattern */
NULL, /* no extra data - we didn't study the pattern */
subj, /* the subject string */
subject_length, /* the length of the subject */
offset, /* start at offset 0 in the subject */
0, /* default options */
ovector, /* output vector for substring information */
OVECCOUNT); /* number of elements in the output vector */
if (rc > 0)
{
// add match
char *substring_start = subj + ovector[0];
int substring_length = ovector[1] - ovector[0];
if (substring_length < sizeof(dst))
{
// convert substring to Unicode
strncpy(dst, substring_start, substring_length);
dst[substring_length] = '\0';
// calculate character position
while (offset < ovector[0])
{
offset += UTF8_CHAR_LEN(subj[offset]);
char_offset++;
}
CString str = CString(CA2T(dst, CP_UTF8));
IMatch2* item = new IMatch2(str, char_offset);
// add submatches (including match)
for (int i=1; i<rc; i++)
{
substring_start = subj + ovector[i*2];
substring_length = ovector[i*2+1] - ovector[i*2];
if (substring_length < sizeof(dst))
{
// convert substring to Unicode
strncpy(dst, substring_start, substring_length);
dst[substring_length] = '\0';
item->AddSubMatch(CString(CA2T(dst, CP_UTF8)));
}
}
matches->AddItem(item);
char_offset += str.GetLength();
offset = ovector[1];
// empty line
if (ovector[0] == ovector[1])
{
offset++;
char_offset++;
}
}
}
} while (rc > 0);
pcre_free(re); /* Release memory used for the compiled pattern */
}
if (is_error)
{
// Raise COM-compatible exception
ICreateErrorInfoPtr cerrinf = 0;
if (::CreateErrorInfo(&cerrinf) == S_OK)
{
cerrinf->SetDescription(CString(error).AllocSysString());
cerrinf->SetSource(L"Perl Compatible Regular Expressions");
cerrinf->SetGUID(GUID_NULL);
IErrorInfoPtr ei = cerrinf;
throw _com_error(MK_E_SYNTAX, ei, true);
}
}
return matches;
}
#endif
}