/
HexFileList.cpp
594 lines (481 loc) · 17.6 KB
/
HexFileList.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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// HexFileList: override of MFC CRecentFileList class
//
// Copyright (c) 2011 by Andrew W. Phillips.
//
// This file is distributed under the MIT license, which basically says
// you can do what you want with it but I take no responsibility for bugs.
// See http://www.opensource.org/licenses/mit-license.php for full details.
//
#include "stdafx.h"
#include <sys/stat.h> // Use stat() for file size by name
#include <imagehlp.h> // For ::MakeSureDirectoryPathExists()
#include "HexEdit.h"
#include "MainFrm.h"
#include "HexFileList.h"
#include "HexEditView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CHexEditApp theApp;
// The following are not in a public header
extern BOOL AFXAPI AfxFullPath(LPTSTR lpszPathOut, LPCTSTR lpszFileIn);
extern BOOL AFXAPI AfxComparePath(LPCTSTR lpszPath1, LPCTSTR lpszPath2);
CHexFileList::CHexFileList(UINT nStart, LPCTSTR lpszSection, int nSize, int nMaxDispLen /*= AFX_ABBREV_FILENAME_LEN*/)
: CRecentFileList(nStart, lpszSection, _T("File%d"), nSize, nMaxDispLen)
{
// Set up the default values
// Note: the default scheme should always be empty in which case a file extension
// scheme is searched for then a scheme based on current char set is used
// Also note that some values are set in the default string here just to show the fields
// and are overwritten with the current default value from the registry below.
default_data_ = "1|-30000|-30000|-30000|-30000|0|0|0|0||||0|16|4|0|Courier|16|Terminal|18|default";
SetDefaults();
ASSERT(AfxGetMainWnd() != NULL);
// Get user's Application Data folder
if (!::GetDataPath(filename_))
return;
filename_ += CString(lpszSection); // append file name
// Create HexEdit folder within that
if (!MakeSureDirectoryPathExists(filename_))
{
filename_.Empty();
return;
}
size_t capacity = 256; // Initial vector capacities
#ifdef _DEBUG
capacity = 1; // Force reallocation sooner in debug to catch bad iterator bugs
#else
// Work out roughly how many recent files we have from the recent file list's file size
struct _stat stat;
if (::_stat(filename_, &stat) != -1)
capacity = stat.st_size / 100; // Most lines of the text file are longer than 100 chars
#endif
name_.reserve(capacity);
hash_.reserve(capacity);
opened_.reserve(capacity);
data_.reserve(capacity);
ver_ = -1;
}
void CHexFileList::SetDefaults()
{
if (theApp.open_max_)
SetDV(CMD, int(SW_SHOWMAXIMIZED));
else
SetDV(CMD, int(SW_SHOW));
if (theApp.open_plf_ != NULL)
{
SetDV(FONT, theApp.open_plf_->lfFaceName);
SetDV(HEIGHT, theApp.open_plf_->lfHeight);
}
if (theApp.open_oem_plf_ != NULL)
{
SetDV(OEMFONT, theApp.open_oem_plf_->lfFaceName);
SetDV(OEMHEIGHT, theApp.open_oem_plf_->lfHeight);
}
SetDV(COLUMNS, theApp.open_rowsize_);
SetDV(GROUPING, theApp.open_group_by_);
SetDV(OFFSET, theApp.open_offset_);
SetDV(VERT_BUFFER_ZONE, theApp.open_vertbuffer_);
ASSERT(theApp.open_disp_state_ != -1); // Ensures we have read default values
SetDV(DISPLAY, theApp.open_disp_state_);
SetDV(DFFDVIEW, theApp.dffdview_);
SetDV(AERIALVIEW, theApp.aerialview_);
SetDV(AERIALDISPLAY, theApp.aerial_disp_state_);
SetDV(SCHEME, theApp.open_scheme_name_);
}
#ifdef _DEBUG
// Make sure the first entries in our vector match the filenames in the MFC string array
bool CHexFileList::Check()
{
for (int ii = 0; ii < m_nSize-1 && int(name_.size())-ii-1 > 0; ++ii)
if (!AfxComparePath(m_arrNames[ii], name_[name_.size()-ii-1]))
{
TRACE2("Recent File List ERROR: <%s> does not match <%s>", m_arrNames[ii], name_[name_.size()-ii-1]);
return false;
}
return true;
}
#endif
void CHexFileList::ChangeSize(int nSize)
{
// Recreate the base class names array used to update menus
delete[] m_arrNames;
m_arrNames = new CString[nSize];
m_nSize = nSize;
// Fill in all the entries
for (int iMRU = 0; iMRU < m_nSize && iMRU < name_.size(); iMRU++)
{
m_arrNames[iMRU] = name_[name_.size()-iMRU-1];
}
ASSERT(Check());
}
void CHexFileList::Add(LPCTSTR lpszPathName)
{
ASSERT(lpszPathName != NULL);
ASSERT(AfxIsValidString(lpszPathName));
// Get the fully qualified path name
TCHAR szTemp[_MAX_PATH];
AfxFullPath(szTemp, lpszPathName);
ASSERT(hash_.size() == name_.size());
ASSERT(opened_.size() == name_.size());
ASSERT(data_.size() == name_.size());
CString saved_data;
// Find and remove any existing entry(s) for this file
// Note: we use an index rather than iterator since an iterator is invalid after erase() is called
for (int ii = name_.size()-1; ii >= 0; ii--)
{
// if (_stricmp(name_[ii], szTemp) == 0)
if (AfxComparePath(name_[ii], szTemp)) // Handle DBCS case-insensitive comparisons etc
{
// Remove the existing entry for the file
name_.erase(name_.begin() + ii);
hash_.erase(hash_.begin() + ii);
opened_.erase(opened_.begin() + ii);
saved_data = data_[ii]; // save data_ for when file read (below)
data_.erase(data_.begin() + ii);
}
}
// Add to the end of the vectors (most recent files at end)
CString ss(szTemp);
name_.push_back(ss);
ss.MakeUpper();
hash_.push_back(str_hash(ss));
opened_.push_back(time(NULL));
data_.push_back(saved_data);
ASSERT(hash_.size() == name_.size());
ASSERT(opened_.size() == name_.size());
ASSERT(data_.size() == name_.size());
// Let base class also keep its silly little list
CRecentFileList::Add(lpszPathName);
((CMainFrame *)AfxGetMainWnd())->UpdateExplorer(szTemp); // Let explorer update (last opened time)
ASSERT(Check());
}
void CHexFileList::ClearAll()
{
name_.clear();
hash_.clear();
opened_.clear();
data_.clear();
// Remove from the base class list starting at the end
for (int ii = m_nSize; ii > 0; ii--)
CRecentFileList::Remove(ii-1);
((CMainFrame *)AfxGetMainWnd())->UpdateExplorer(); // some of the files may have been in current explorer folder
ASSERT(Check());
}
void CHexFileList::Remove(int nIndex)
{
ASSERT(nIndex < name_.size());
ASSERT(hash_.size() == name_.size());
ASSERT(opened_.size() == name_.size());
ASSERT(data_.size() == name_.size());
((CMainFrame *)AfxGetMainWnd())->UpdateExplorer(name_[nIndex]); // Let explorer update (last opened time is now gone)
name_ .erase(name_ .begin() + (name_ .size() - nIndex - 1));
hash_ .erase(hash_ .begin() + (hash_ .size() - nIndex - 1));
opened_.erase(opened_.begin() + (opened_.size() - nIndex - 1));
data_ .erase(data_ .begin() + (data_ .size() - nIndex - 1));
ASSERT(hash_.size() == name_.size());
ASSERT(opened_.size() == name_.size());
ASSERT(data_.size() == name_.size());
// Let base class also update its list
if (nIndex < m_nSize)
CRecentFileList::Remove(nIndex);
// Fill in empty entry created at end of base class list
for (int iMRU = nIndex; iMRU < m_nSize; iMRU++)
{
if (m_arrNames[iMRU].IsEmpty() && iMRU < name_.size())
m_arrNames[iMRU] = name_[name_.size()-iMRU-1];
}
ASSERT(Check());
}
bool CHexFileList::ReadFile()
{
CStdioFile ff; // Text file we are reading from
CFileException fe; // Stores file exception info
CString strLine; // One line read in from the file
// Open the file
if (!ff.Open(filename_, CFile::modeRead|CFile::shareDenyWrite|CFile::typeText, &fe))
return false;
// Read all the recent file names (and associated data) from the file
ver_ = -1;
for (int line_no = 0; ff.ReadString(strLine); ++line_no)
{
// Ignore comment lines
if (strLine[0] == ';')
{
// Get file version from 1st comment line
if (line_no == 0 && _strnicmp(strLine, "; version ", 10) == 0)
ver_ = atoi((const char *)strLine + 10);
continue;
}
CString ss;
// Get the file's name
AfxExtractSubString(ss, strLine, 0, '|');
if (ss.IsEmpty()) continue;
name_.push_back(ss);
ss.MakeUpper();
hash_.push_back(str_hash(ss));
// Get the last opened date
AfxExtractSubString(ss, strLine, 1, '|');
time_t tt = strtol(ss, NULL, 10);
opened_.push_back(tt);
// Get the data string
int curr; // Where in the input string the extra data is stored
ss.Empty(); // Default data
if (ver_ > 3)
{
// Skip 1st three vertical bars (|)
if ((curr = strLine.Find('|')) != -1 && (curr = strLine.Find('|', curr+1)) != -1 && (curr = strLine.Find('|', curr+1)) != -1)
ss = strLine.Mid(curr+1);
}
else
{
// Skip 1st two vertical bars (|)
if ((curr = strLine.Find('|')) != -1 && (curr = strLine.Find('|', curr+1)) != -1)
ss = strLine.Mid(curr+1);
}
data_.push_back(ss);
}
ff.Close();
int max_keep = 20000;
if (name_.size() > max_keep)
{
CString ss;
ss.Format("Truncated recent file list to most recent %d files", max_keep);
name_.erase(name_.begin(), name_.begin() + (name_.size() - max_keep));
hash_.erase(hash_.begin(), hash_.begin() + (hash_.size() - max_keep));
opened_.erase(opened_.begin(), opened_.begin() + (opened_.size() - max_keep));
data_.erase(data_.begin(), data_.begin() + (data_.size() - max_keep));
}
ASSERT(hash_.size() == name_.size());
ASSERT(opened_.size() == name_.size());
ASSERT(data_.size() == name_.size());
return true;
}
void CHexFileList::ReadList()
{
ClearAll();
if (ReadFile())
{
// Update m_arrNames to match
for (int ii = 0; ii < name_.size() && ii < m_nSize; ++ii)
m_arrNames[ii] = name_[name_.size()-ii-1];
}
else
{
// Call base class to get file names from the registry
CRecentFileList::ReadList();
// Get other parameters using old registry entries (for backward compatibility)
int ii;
CString fnum;
struct
{
union
{
DWORD disp_state_;
struct display_bits display_;
};
} tt;
// Read options for each file of the MRU list
for (ii = m_nSize - 1; ii >= 0; ii--)
{
if (m_arrNames[ii].IsEmpty())
continue;
CString ss;
ss = m_arrNames[ii];
name_.push_back(ss);
ss.MakeUpper();
hash_.push_back(str_hash(ss));
opened_.push_back(time(NULL));
data_.push_back("");
fnum.Format("File%d", ii+1);
SetData(name_.size()-1, CMD, theApp.GetProfileInt(fnum, "WindowState", SW_SHOWNORMAL));
SetData(name_.size()-1, TOP, (int)theApp.GetProfileInt(fnum, "WindowTop", -30000));
SetData(name_.size()-1, LEFT, (int)theApp.GetProfileInt(fnum, "WindowLeft", -30000));
SetData(name_.size()-1, BOTTOM, (int)theApp.GetProfileInt(fnum, "WindowBottom", -30000));
SetData(name_.size()-1, RIGHT, (int)theApp.GetProfileInt(fnum, "WindowRight", -30000));
SetData(name_.size()-1, COLUMNS, __min(CHexEditView::max_buf, __max(4, theApp.GetProfileInt(fnum, "Columns", theApp.open_rowsize_))));
SetData(name_.size()-1, GROUPING,__max(2, theApp.GetProfileInt(fnum, "Grouping", theApp.open_group_by_)));
SetData(name_.size()-1, OFFSET, __min(atoi(GetData(name_.size()-1, COLUMNS))-1, theApp.GetProfileInt(fnum, "Offset", theApp.open_offset_)));
ss = theApp.GetProfileString(fnum, "Scheme");
ss.Replace("|", "_"); // A scheme name may no longer contain a vertical bar (|)
SetData(name_.size()-1, SCHEME, ss);
ss = theApp.GetProfileString(fnum, "Font");
CString strTemp;
AfxExtractSubString(strTemp, ss, 0, ',');
SetData(name_.size()-1, FONT, strTemp);
AfxExtractSubString(strTemp, ss, 1, ',');
SetData(name_.size()-1, HEIGHT, strTemp);
ss = theApp.GetProfileString(fnum, "OemFont");
AfxExtractSubString(strTemp, ss, 0, ',');
SetData(name_.size()-1, OEMFONT, strTemp);
AfxExtractSubString(strTemp, ss, 1, ',');
SetData(name_.size()-1, OEMHEIGHT, strTemp);
// Read the option values, defaulting to the global (theApp.open_*) values
if ((tt.disp_state_ = (int)theApp.GetProfileInt(fnum, "DisplayState", -1)) == -1)
if (!tt.display_.hex_area && !tt.display_.char_area)
tt.display_.hex_area = TRUE;
if (!tt.display_.hex_area)
tt.display_.edit_char = TRUE;
else if (!tt.display_.char_area)
tt.display_.edit_char = FALSE;
if (tt.display_.control > 2)
tt.display_.control = 0;
SetData(name_.size()-1, DISPLAY, tt.disp_state_);
SetData(name_.size()-1, DOC_FLAGS, theApp.GetProfileInt(fnum, "KeepTimes", 0));
SetData(name_.size()-1, FORMAT, theApp.GetProfileString(fnum, "FormatFile"));
SetData(name_.size()-1, SELSTART, theApp.GetProfileString(fnum, "SelStart64"));
SetData(name_.size()-1, SELEND, theApp.GetProfileString(fnum, "SelEnd64"));
SetData(name_.size()-1, POS, theApp.GetProfileString(fnum, "Pos64"));
SetData(name_.size()-1, MARK, theApp.GetProfileString(fnum, "Mark64"));
SetData(name_.size()-1, HIGHLIGHTS, theApp.GetProfileString(fnum, "Highlights"));
}
// Now delete the recent file list key (and old one) and all files
theApp.WriteProfileString(_T("RecentFiles"), NULL, NULL);
theApp.WriteProfileString(_T("Recent File List"), NULL, NULL);
for (ii = 0; ii < 16; ++ii) // There may be up to 16 entries (some old unused ones)
{
fnum.Format("File%d", ii+1);
theApp.WriteProfileString(fnum, NULL, NULL); // Delete this recent file entry
}
}
((CMainFrame *)AfxGetMainWnd())->UpdateExplorer(); // make sure our explorer window shows info we have just loaded
}
bool CHexFileList::WriteFile()
{
CStdioFile ff; // Text file we are reading from
CFileException fe; // Stores file exception info
CString strLine; // One line read in from the file
// Open the file
if (!ff.Open(filename_, CFile::modeCreate|CFile::modeWrite|CFile::shareExclusive|CFile::typeText, &fe))
return false;
ff.WriteString("; version 3\n");
for (int ii = 0; ii < name_.size(); ++ii)
{
CString ss;
ss.Format("|%ld|", opened_[ii]);
ff.WriteString(name_[ii] + ss + data_[ii] + '\n');
}
ff.Close();
return true;
}
void CHexFileList::WriteList()
{
WriteFile();
}
BOOL CHexFileList::GetDisplayName(CString& strName, int nIndex,
LPCTSTR lpszCurDir, int nCurDir, BOOL bAtLeastName) const
{
ASSERT(m_arrNames != NULL);
ASSERT(nIndex < m_nSize);
if (!::IsDevice(m_arrNames[nIndex]))
return CRecentFileList::GetDisplayName(strName, nIndex, lpszCurDir, nCurDir, bAtLeastName);
strName = ::DeviceName(m_arrNames[nIndex]);
if (strName.IsEmpty())
return FALSE;
return TRUE;
}
#include "timer.h"
// GetIndex returns index into list given a file name or -1 if not found.
// Note: We use a hash of the filename to speed up searching. (We could
// have done even better by creating an index or something, but that was deemed
// too hard for the benfit - maybe one day.) Testing shows this speeds up
// searches a lot but only slows down file read (at startup) a tiny bit.
int CHexFileList::GetIndex(LPCTSTR lpszPathName) const
{
ASSERT(lpszPathName != NULL);
ASSERT(AfxIsValidString(lpszPathName));
CString ss(lpszPathName);
ss.MakeUpper();
unsigned long hash = str_hash(ss);
for (int ii = 0; ii < name_.size(); ++ii)
{
// if (AfxComparePath(name_[ii], lpszPathName))
if (hash == hash_[ii] && AfxComparePath(name_[ii], lpszPathName))
return ii;
}
return -1;
}
bool CHexFileList::SetDV(param_num param, __int64 vv)
{
char buf[32];
sprintf(buf, "%I64d", vv);
return SetDV(param, buf);
}
// Set the default value for a parameter
bool CHexFileList::SetDV(param_num param, LPCTSTR value)
{
ASSERT(strchr(value, '|') == NULL); // Value string should not contain separator chars
// First make sure there are enough separator chars that we can use the sub-string
int count, curr = -1;
for (count = 0; (curr = default_data_.Find('|', curr+1)) != -1; ++count)
;
if (count < param)
default_data_ += CString('|', param - count); // Add more param separator chars
curr = -1;
for (int ii = 0; ii < param; ++ii)
curr = default_data_.Find('|', curr+1);
int next = default_data_.Find('|', curr+1);
if (next == -1) next = default_data_.GetLength();
default_data_ = default_data_.Left(curr+1) + value + default_data_.Mid(next);
return true;
}
// Get the default value for a parameter
CString CHexFileList::GetDV(param_num param) const
{
CString retval;
AfxExtractSubString(retval, default_data_, param, '|');
return retval;
}
bool CHexFileList::SetData(int index, param_num param, __int64 vv)
{
char buf[32];
sprintf(buf, "%I64d", vv);
return SetData(index, param, buf);
}
bool CHexFileList::SetData(int index, param_num param, LPCTSTR vv)
{
CString value(vv);
ASSERT(index > -1);
#if 0 // the following is brain-dead since if the default setting is changed then the used value becomes wrong
// If we are setting to the default value make it blank
if (GetDV(param) == value)
{
CString ss;
AfxExtractSubString(ss, data_[index], param, '|');
if (ss.IsEmpty())
return true; // Current is empty so leave it that way to indicate d.v.
else
value.Empty();
}
#endif
ASSERT(index < name_.size());
ASSERT(strchr(value, '|') == NULL); // Value string should not contain separator chars
value.Replace("|", "!");
// First make sure there are enough separator chars that we can use the sub-string
int count, curr = -1;
for (count = 0; (curr = data_[index].Find('|', curr+1)) != -1; ++count)
;
if (count < param)
data_[index] += CString('|', param - count); // Add more param separator chars
curr = -1;
for (int ii = 0; ii < param; ++ii)
curr = data_[index].Find('|', curr+1);
int next = data_[index].Find('|', curr+1);
if (next == -1) next = data_[index].GetLength();
data_[index] = data_[index].Left(curr+1) + value + data_[index].Mid(next);
return true;
}
// Get current setting for a file or the default settings if not yet set
// This now handles an index of -1 to get the default setting
CString CHexFileList::GetData(int index, param_num param) const
{
ASSERT(index < int(name_.size()));
CString retval;
if (index > - 1)
AfxExtractSubString(retval, data_[index], param, '|');
if (retval.IsEmpty())
AfxExtractSubString(retval, default_data_, param, '|');
return retval;
}