My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#include "ide.h"

void Ide::SerializeFf(Stream& s) {
s % ff.find;
ff.find.SerializeList(s);
s % ff.wholeword % ff.ignorecase % ff.wildcards;
s % ff.replace;
ff.replace.SerializeList(s);
}

FileSel& sSD()
{
static bool b;
static FileSel fs;
if(!b) {
fs.AllFilesType();
b = true;
}
return fs;
}

void Ide::SerializeFindInFiles(Stream& s) {
int version = 2;
s / version;
s % ff.files;
ff.files.SerializeList(s);
s % ff.folder;
ff.folder.SerializeList(s);
if(version >= 2)
{
s % ff.replace;
ff.replace.SerializeList(s);
s % ff.style;
}
if(version >= 1)
s % sSD();
}

void SearchForFiles(Vector<String>& files, String dir, String mask, Progress& pi) {
FindFile ff(AppendFileName(dir, "*.*"));
while(ff) {
if(ff.IsFolder() && *ff.GetName() != '.')
SearchForFiles(files, AppendFileName(dir, ff.GetName()), mask, pi);
else
if(ff.IsFile() && PatternMatchMulti(mask, ff.GetName())) {
if(pi.StepCanceled()) return;
files.Add(AppendFileName(dir, ff.GetName()));
}
ff.Next();
}
}

enum {
WILDANY = 16,
WILDONE,
WILDSPACE,
WILDNUMBER,
WILDID,
};

bool Match(const char *f, const char *s, bool we, bool ignorecase) {
while(*f) {
if(*f == WILDANY) {
f++;
for(;;) {
if(Match(f, s, we, ignorecase))
return true;
if(!*s++) break;
}
return false;
}
else
if(*f == WILDONE) {
if(!*s++) return false;
}
else
if(*f == WILDSPACE) {
if(*s != ' ' && *s != '\t') return false;
s++;
while(*s == ' ' || *s == '\t')
s++;
}
else
if(*f == WILDNUMBER) {
if(*s < '0' || *s > '9') return false;
s++;
while(*s >= '0' && *s <= '9')
s++;
}
else
if(*f == WILDID) {
if(!iscib(*s)) return false;
s++;
while(iscid(*s)) s++;
}
else {
if(ignorecase ? ToUpper(*s) != ToUpper(*f) : *s != *f) return false;
s++;
}
f++;
}
return we && iscid(*s) ? false : true;
}

bool Ide::SearchInFile(const String& fn, const String& pattern, bool wholeword, bool ignorecase,
int& n) {
FileIn in(fn);
if(!in) return true;
int ln = 1;
bool wb = wholeword ? iscid(*pattern) : false;
bool we = wholeword ? iscid(*pattern.Last()) : false;
int infile = 0;
while(!in.IsEof()) {
String line = in.GetLine();
bool bw = true;
for(const char *s = line; *s; s++) {
if(bw && Match(pattern, s, we, ignorecase)) {
console2 << fn << Format("(%d):", ln) << line << "\n";
console2.Sync();
infile++;
n++;
break;
}
if(wb) bw = !iscid(*s);
}
ln++;
}

in.Close();
int ffs = ~ff.style;
if(infile && ffs != STYLE_NO_REPLACE)
{
EditFile(fn);
bool doit = true;
if(ffs == STYLE_CONFIRM_REPLACE)
{
editor.SetCursor(0);
editor.Find(false, true);
switch(PromptYesNoCancel(NFormat("Replace %d lines in [* \1%s\1]?", infile, fn)))
{
case 1: break;
case 0: doit = false; break;
case -1: return false;
}
}
if(doit)
{
editor.SelectAll();
editor.BlockReplace();
SaveFile();
console2 << NFormat("%s: %d replacements made\n", fn, infile);
console2.Sync();
}
}

return true;
}

static bool RawFileMatch(const char *pattern, const char *file, const char *& endptr)
{
for(char c; (c = *pattern++) != 0;)
if(c == '*')
{
do
if(RawFileMatch(pattern, file, endptr))
return true;
while(*file++);
endptr = pattern;
return false;
}
else if(c == '?')
{
if(*file++ == 0)
{
endptr = pattern - 1;
return false;
}
}
else if(ToLower(c) != ToLower(*file++))
{
endptr = pattern - 1;
return false;
}
endptr = file;
return true;
}

int CharFilterFindFileMask(int c)
{
return ToUpper(ToAscii(c));
}

void Ide::FindFileName() {
const Workspace& wspc = IdeWorkspace();

WithFindFileLayout<TopWindow> ffdlg;
CtrlLayoutOKCancel(ffdlg, "Find file");
ffdlg.list.AutoHideSb();
ffdlg.list.AddColumn("Package");
ffdlg.list.AddColumn("File");
ffdlg.list.WhenLeftDouble = ffdlg.Acceptor(IDOK);
ffdlg.mask.NullText("Search");
ffdlg.mask.SetText(find_file_search_string);
ffdlg.mask.SelectAll();
ffdlg.mask.SetFilter(CharFilterFindFileMask);
ffdlg.mask <<= ffdlg.Breaker(IDYES);
for(;;) {
ffdlg.list.Clear();
String mask = ~ffdlg.mask;
for(int p = 0; p < wspc.GetCount(); p++) {
String packname = wspc[p];
const Package& pack = wspc.GetPackage(p);
for(int f = 0; f < pack.file.GetCount(); f++) {
String fn = pack.file[f];
if(ToUpper(packname).Find(mask) >= 0 || ToUpper(fn).Find(mask) >= 0)
ffdlg.list.Add(packname, pack.file[f]);
}
}
if(ffdlg.list.GetCount() > 0)
ffdlg.list.SetCursor(0);
switch(ffdlg.Run()) {
case IDCANCEL:
return;
case IDOK:
if(ffdlg.list.IsCursor()) {
find_file_search_string = ffdlg.mask;
EditFile(SourcePath(ffdlg.list.Get(0), ffdlg.list.Get(1)));
return;
}
break;
case IDYES:
break;
}
}
}

void Ide::FindInFiles(bool replace) {
StringStream ss;
editor.SerializeFind(ss);
ss.Open(ss.GetResult());
SerializeFf(ss);
if(String(ff.folder).IsEmpty())
ff.folder <<= GetUppDir();
ff.style <<= STYLE_NO_REPLACE;
ff.Sync();
ff.itext = editor.GetI();
ff.Setup(replace);

int c = ff.Execute();
ss.Create();
SerializeFf(ss);
ss.Open(ss.GetResult());
editor.SerializeFind(ss);
if(c == IDOK && !String(ff.find).IsEmpty()) {
Renumber();
ff.find.AddHistory();
ff.files.AddHistory();
ff.folder.AddHistory();
ff.replace.AddHistory();

Progress pi("Found %d files to search.");
Vector<String> files;
SearchForFiles(files, NormalizePath((String)ff.folder, GetUppDir()), ~ff.files, pi);
if(!pi.Canceled()) {
String pattern;
if(ff.wildcards) {
String q = ~ff.find;
for(const char *s = q; *s; s++)
if(*s == '\\') {
s++;
if(*s == '\0') break;
q.Cat(*s);
}
else
switch(*s) {
case '*': pattern.Cat(WILDANY); break;
case '?': pattern.Cat(WILDONE); break;
case '%': pattern.Cat(WILDSPACE); break;
case '#': pattern.Cat(WILDNUMBER); break;
case '$': pattern.Cat(WILDID); break;
default: pattern.Cat(*s);
}
}
else
pattern = ~ff.find;
pi.SetTotal(files.GetCount());
ShowConsole2();
console2.Clear();
pi.SetPos(0);
int n = 0;
for(int i = 0; i < files.GetCount(); i++) {
pi.SetText(files[i]);
if(pi.StepCanceled()) break;
if(!SearchInFile(files[i], pattern, ff.wholeword, ff.ignorecase, n))
break;
}
console2 << Format("%d occurrence(s) have been found.\n", n);
}
}
SetErrorEditor();
}

void Ide::FindString(bool back)
{
if(!editor.FindString(back))
BeepMuteExclamation();
}

void Ide::TranslateString()
{
int l, h;
if(editor.GetSelection(l, h)) {
editor.Insert(l, "t_(");
editor.Insert(h + 3, ")");
editor.SetCursor(h + 4);
FindString(false);
}
}

void Ide::InsertWildcard(int c) {
iwc = c;
}

void Ide::FindWildcard() {
int l, h;
ff.find.GetSelection(l, h);
iwc = 0;
FindWildcardMenu(THISBACK(InsertWildcard), ff.find.GetPushScreenRect().TopRight(), false);
if(iwc) {
ff.wildcards = true;
ff.find.SetFocus();
ff.find.SetSelection(l, h);
ff.find.RemoveSelection();
ff.find.Insert(iwc);
}
}

void Ide::FindSetStdDir(String n)
{
ff.folder <<= n;
}

void Ide::FindStdDir()
{
String n = GetFileFolder(editfile);
MenuBar menu;
if(!IsNull(n))
menu.Add(n, THISBACK1(FindSetStdDir, n));
Vector<String> d = GetUppDirs();
for(int i = 0; i < d.GetCount(); i++)
menu.Add(d[i], THISBACK1(FindSetStdDir, d[i]));
menu.Execute(&ff.folder, ff.folder.GetPushScreenRect().BottomLeft());
}

void Ide::FindFolder()
{
if(!sSD().ExecuteSelectDir()) return;
ff.folder <<= ~sSD();
}

void Ide::ConstructFindInFiles() {
ff.find.AddButton().SetMonoImage(CtrlImg::smallright()).Tip("Wildcard") <<= THISBACK(FindWildcard);
static const char *defs = "*.cpp *.h *.hpp *.c *.m *.C *.M *.cxx *.cc *.mm *.MM *.icpp *.sch *.lay *.rc";
ff.files <<= String(defs);
ff.files.AddList((String)defs);
ff.files.AddList((String)"*.txt");
ff.files.AddList((String)"*.*");
ff.folder.AddButton().SetMonoImage(CtrlImg::smalldown()).Tip("Related folders") <<= THISBACK(FindStdDir);
ff.folder.AddButton().SetMonoImage(CtrlImg::smallright()).Tip("Select folder") <<= THISBACK(FindFolder);
editor.PutI(ff.find);
editor.PutI(ff.replace);
CtrlLayoutOKCancel(ff, "Find In Files");
}

void FindInFilesDlg::Sync()
{
replace.Enable((int)~style);
}

FindInFilesDlg::FindInFilesDlg()
{
style <<= THISBACK(Sync);
}

void FindInFilesDlg::Setup(bool replacing)
{
Title(replacing ? "Find and replace in files" : "Find in files");
replace_lbl.Show(replacing);
style.Show(replacing);
replace_lbl2.Show(replacing);
replace.Show(replacing);
Size sz = GetLayoutSize();
if(!replacing)
sz.cy -= replace.GetRect().bottom - folder.GetRect().bottom;
Rect r = GetRect();
r.SetSize(sz);
SetRect(r);
}

bool FindInFilesDlg::Key(dword key, int count)
{
if(key == K_CTRL_I) {
if(find.HasFocus()) {
find <<= itext;
return true;
}
if(replace.HasFocus()) {
replace <<= itext;
return true;
}
}
return TopWindow::Key(key, count);
}

Change log

r4457 by cxl on Jan 21, 2012   Diff
*uppsrc: Fixed many GCC warnings
Go to: 
Project members, sign in to write a code review

Older revisions

r3950 by cxl on Oct 3, 2011   Diff
ide: Mac OSX11 patches (thanks
arilect)
r3837 by cxl on Sep 8, 2011   Diff
ide: Find file now remembers last file
searched (thanks
r3222 by cxl on Feb 18, 2011   Diff
cs-cz translation updated
All revisions of this file

File info

Size: 9030 bytes, 413 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting