-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateData.cpp
143 lines (116 loc) · 3.88 KB
/
CreateData.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
// CreateData.cpp : implementation file
//
#include "stdafx.h"
#include "resource.h"
#include "CreateData.h"
#include "ErrorString.h"
#include "Seeker.h"
#include "RegVars.h"
#define NUM_RECORDS_PER_BLOCK 1000
// CCreateData dialog
IMPLEMENT_DYNAMIC(CCreateData, CDialog)
CCreateData::CCreateData(CWnd* pParent /*=NULL*/)
: CDialog(CCreateData::IDD, pParent)
{
}
CCreateData::~CCreateData()
{
}
void CCreateData::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SPIN_RECORDS, c_SpinRecords);
DDX_Control(pDX, IDC_PROGRESS, c_Progress);
}
BEGIN_MESSAGE_MAP(CCreateData, CDialog)
ON_BN_CLICKED(IDC_CREATE_DATA, OnBnClickedCreateData)
END_MESSAGE_MAP()
// CCreateData message handlers
void CCreateData::OnBnClickedCreateData()
{
CString filter;
filter.LoadString(IDS_DATABASE_FILTER);
CFileDialog dlg(FALSE, _T(".dat"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, filter);
if(dlg.DoModal() != IDOK)
return; // not found
CString filename = dlg.GetPathName();
filename.Trim();
if(filename.IsEmpty())
return;
CStdioFile f;
if(!f.Open(filename, CFile::modeWrite | CFile::modeCreate))
{ /* failed to open */
DWORD err = ::GetLastError();
CString msg = ErrorString(err);
msg += _T("\r\n");
msg += filename;
AfxMessageBox(msg, MB_ICONERROR | MB_OK);
return;
} /* failed to open */
int n = c_SpinRecords.GetPos32();
Seeker seekinfo(f.GetFilePath());
c_Progress.SetRange32(0, n);
c_Progress.SetPos(0);
c_Progress.ShowWindow(SW_SHOW);
c_Progress.SetStep(1);
CString length;
length.Format(_T("%06d"), n * NUM_RECORDS_PER_BLOCK);
length += _T("\r\n");
f.WriteString(length); // first 6 bytes are length, plus CRLF
for(int i = 0; i < n; i++)
{ /* create file */
AddBlock(f, i, &seekinfo);
c_Progress.StepIt();
c_Progress.UpdateData();
} /* create file */
c_Progress.StepIt();
c_Progress.ShowWindow(SW_HIDE);
f.Close();
RegistryString database(IDS_REGISTRY_DATABASE);
database.value = filename;
database.store();
}
/****************************************************************************
* CCreateData::OnInitDialog
* Result: BOOL
* TRUE, always
* Effect:
* Initializes the dialog
****************************************************************************/
BOOL CCreateData::OnInitDialog()
{
CDialog::OnInitDialog();
c_SpinRecords.SetRange32(1, 100);
c_SpinRecords.SetPos(10);
c_Progress.ShowWindow(SW_HIDE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
/****************************************************************************
* CCreateData::AddBlock
* Inputs:
* CStdioFile & f: File
* int n: Block number
* Result: void
*
* Effect:
* Writes a block of the file. Each block has NUM_RECORDS_PER_BLOCK records
****************************************************************************/
void CCreateData::AddBlock(CStdioFile & f, int n, Seeker * seekinfo)
{
// Each record is of the form
// nnnnnnnn nnnnnnnn ... nnnnnnn
for(int i = 0; i < NUM_RECORDS_PER_BLOCK; i++)
{ /* write block */
int recno = n * NUM_RECORDS_PER_BLOCK + i;
CString s;
s.Format(_T("%08d "), recno);
CString data;
while((DWORD)data.GetLength() < seekinfo->GetBytesPerRecord())
{ /* make block */
data += s;
} /* make block */
f.WriteString(data.Left(seekinfo->GetBytesPerRecord() - 1));
f.WriteString(_T("*"));
} /* write block */
} // CCreateData::AddBlock