-
Notifications
You must be signed in to change notification settings - Fork 0
/
3eExplorerDoc.cpp
347 lines (279 loc) · 8.13 KB
/
3eExplorerDoc.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
// 3eExplorerDoc.cpp : implementation of the CMy3eExplorerDoc class
//
#include "stdafx.h"
#include "3eExplorer.h"
#include "3eExplorerDoc.h"
#include "DocumentStatus.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
CCHS::CCHS(unsigned int _c, unsigned int _h, unsigned int _s)
: c(_c), h(_h), s(_s)
{
}
/////////////////////////////////////////////////////////////////////////////
// CMy3eExplorerDoc
IMPLEMENT_DYNCREATE(CMy3eExplorerDoc, CDocument)
BEGIN_MESSAGE_MAP(CMy3eExplorerDoc, CDocument)
//{{AFX_MSG_MAP(CMy3eExplorerDoc)
ON_COMMAND(ID_FILE_FILESTATUS, OnFileFilestatus)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy3eExplorerDoc construction/destruction
CMy3eExplorerDoc::CMy3eExplorerDoc()
: m_pFile(NULL)
, m_pHdfHeader(NULL)
{
// TODO: add one-time construction code here
}
CMy3eExplorerDoc::~CMy3eExplorerDoc()
{
}
BOOL CMy3eExplorerDoc::OnNewDocument()
{
//if (!CDocument::OnNewDocument())
// return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return FALSE;
}
/////////////////////////////////////////////////////////////////////////////
// CMy3eExplorerDoc serialization
void CMy3eExplorerDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CMy3eExplorerDoc diagnostics
#ifdef _DEBUG
void CMy3eExplorerDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CMy3eExplorerDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMy3eExplorerDoc commands
BOOL CMy3eExplorerDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
// we want to override the default stuff, as we want to keep the file open and cache it as we go
// so we'll do pretty much the default handling
if (IsModified())
TRACE0("Warning: OnOpenDocument replaces an unsaved document.\n");
CFileException fe;
CFile* pFile = GetFile(lpszPathName,
CFile::modeRead|CFile::shareDenyWrite|CFile::typeBinary, &fe);
if (pFile == NULL)
{
ReportSaveLoadException(lpszPathName, &fe,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
return FALSE;
}
DeleteContents();
SetModifiedFlag(); // dirty during de-serialize
// store the filename
std::string filename = lpszPathName;
std::string::size_type pos = filename.find_last_of("\\/");
if (std::string::npos == pos)
pos = 0;
else
pos++;
m_sFileName = filename.substr(pos);
// store the file pointer
m_pFile = pFile;
ASSERT (m_pHdfHeader == NULL);
// read in a block of the image and verify it
try
{
char first_block[0x216];
UINT read = pFile->Read(first_block, 0x216);
m_pHdfHeader = new CHdfHeader(this, first_block, read, m_sFileName.c_str());
}
catch (CHdfHeader::EHdfInvalid &e)
{
::AfxMessageBox(e.what(), MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
catch (CHdfHeader::EHdfUnsupported &e)
{
::AfxMessageBox(e.what(), MB_ICONINFORMATION | MB_OK);
return FALSE;
}
catch (CFileException *e)
{
ReportSaveLoadException(lpszPathName, e, FALSE, IDS_FILE_ACCESS_ERROR);
return FALSE;
}
SetModifiedFlag(FALSE); // start off with unmodified
return TRUE;
}
void CMy3eExplorerDoc::DeleteContents()
{
// Here we need to release the file and delete any allocated memory
if (NULL != m_pFile)
{
m_pFile->Close();
ReleaseFile(m_pFile, TRUE);
m_pFile = NULL;
}
// step through the cache and free up memory
for (TCacheIterator it = m_sector_cache.begin(); it != m_sector_cache.end(); it++)
{
delete [] (*it).second;
}
m_sector_cache.clear();
// now delete the object tree we have created.
if (NULL != m_pHdfHeader)
{
delete m_pHdfHeader;
m_pHdfHeader = NULL;
}
CDocument::DeleteContents();
}
BOOL CMy3eExplorerDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
//return CDocument::OnSaveDocument(lpszPathName);
// DON'T SAVE!
return 0;
}
unsigned int CMy3eExplorerDoc::ReadSector(const unsigned int _zz, char ** _buff)
{
ASSERT (m_pFile != NULL);
ASSERT (m_pHdfHeader != NULL);
unsigned int sector_size = m_pHdfHeader->getSectorSize();
// first see if this sector is in our cache...
TCacheIterator it = m_sector_cache.find(_zz);
if (it == m_sector_cache.end())
{
// we could be reading a word and using a byte, you know...
unsigned int read_size = sector_size;
if (m_pHdfHeader->UsingHalfWord())
read_size *= 2;
// need to allocate another sector in memory
char * temp_buffer = new char[read_size];
// read from the file into the buffer
LONG byte_offset = m_pHdfHeader->getOffset() + (_zz * read_size);
try
{
m_pFile->Seek(byte_offset, CFile::begin);
if (read_size != m_pFile->Read(temp_buffer, read_size))
{
// couldn't read everything - throw an exception
std::ostringstream except;
except << "Error reading sector " << _zz << ". File may be truncated";
throw EInvalidSector(except.str());
}
}
catch (CFileException *e)
{
// for now just rethrow
throw e;
}
if (m_pHdfHeader->UsingHalfWord())
{
char * copy_sector = new char[sector_size];
// now copy every other byte
for (int ccount = 0; ccount < sector_size; ccount++)
{
copy_sector[ccount] = temp_buffer[2 * ccount];
}
m_sector_cache[_zz] = copy_sector;
delete [] temp_buffer;
}
else
{
m_sector_cache[_zz] = temp_buffer;
}
it = m_sector_cache.find(_zz);
ASSERT (it != m_sector_cache.end());
}
// just for fun, see what happens if we try to set the status...
*_buff = (*it).second;
return sector_size;
}
unsigned int CMy3eExplorerDoc::ReadSector(CCHS _chs, char ** _buff)
{
// work out the sector number and call the other method
ASSERT (m_pHdfHeader != NULL);
unsigned int cc = m_pHdfHeader->getCylinders();
unsigned int hh = m_pHdfHeader->getHeads();
unsigned int ss = m_pHdfHeader->getSectors();
if (_chs.c > m_pHdfHeader->getCylinders())
{
std::ostringstream except;
except << "Cylinder count error : " << _chs.c << " > " << m_pHdfHeader->getCylinders();
throw EInvalidSector(except.str());
}
if (_chs.h > m_pHdfHeader->getHeads())
{
std::ostringstream except;
except << "Head count error : " << _chs.h << " > " << m_pHdfHeader->getHeads();
throw EInvalidSector(except.str());
}
if (_chs.s > m_pHdfHeader->getSectors())
{
std::ostringstream except;
except << "Sector count error : " << _chs.s << " > " << m_pHdfHeader->getSectors();
throw EInvalidSector(except.str());
}
// see if we're doing a cache of a head a time...
if (theApp.HeadCache())
{
// do a precache of all the sectors in this head
unsigned int presec = (_chs.h * ss) + (_chs.c * hh);
unsigned int presec_max = (presec + ss);
for (; presec < presec_max; presec++)
{
ReadSector(presec, _buff);
}
}
unsigned int sector = (_chs.s) + (_chs.h * ss) + (_chs.c * (hh * ss));
m_cur_chs = _chs;
return ReadSector(sector, _buff);
}
unsigned int CMy3eExplorerDoc::ReadNextSector(char ** _buff)
{
if (++m_cur_chs.s >= m_pHdfHeader->getSectors())
{
m_cur_chs.s = 0;
if (++m_cur_chs.h >= m_pHdfHeader->getHeads())
{
m_cur_chs.h = 0;
if (++m_cur_chs.c >= m_pHdfHeader->getCylinders())
{
m_cur_chs.c = 0;
}
}
}
return ReadSector(m_cur_chs, _buff);
}
void CMy3eExplorerDoc::OnFileFilestatus()
{
// here we want to open the dialog that gives us some status information about this document
CDocumentStatus docStatus;
docStatus.SetExplorerDoc(this);
docStatus.DoModal();
}
void CMy3eExplorerDoc::FlushCache()
{
// step through the cache and free up memory
for (TCacheIterator it = m_sector_cache.begin(); it != m_sector_cache.end(); it++)
{
delete [] (*it).second;
}
m_sector_cache.clear();
}