-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPersonFile.cpp
172 lines (154 loc) · 4.01 KB
/
PersonFile.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
#include <iostream>
#include <string>
#include <cstring>
#include "Person.h"
#include "PersonFile.h"
using namespace std;
string SSNHyphens(const string &);
string SSNNoHyphens(const string &);
string ZipHyphens(const string &);
string ZipNoHyphens(const string &);
const int RECORDSIZE = 111;
PersonFile::PersonFile() : _recordSize(RECORDSIZE)
{
const char PERSONFILE[] = "person.db";
int initZero;
initZero = 0;
fstream person1(PERSONFILE, ios::in | ios::out | ios::binary);
if(!person1.is_open())
{
ofstream person2(PERSONFILE);
person2.seekp(0);
person2.write((char *) &initZero, sizeof(initZero));
person2.close();
}
else
person1.close();
_personFile.open(PERSONFILE, ios::in | ios::out | ios::binary);
if(!_personFile)
cerr << "Error opening file " << PERSONFILE << endl;
else
{
_personFile.seekg(0);
_personFile.read((char*)&_numberPersons, sizeof(_numberPersons));
}
}
PersonFile::~PersonFile()
{ _personFile.close(); }
void PersonFile::SortBySSN()
{
bool sorted;
char input1[RECORDSIZE];
char input2[RECORDSIZE];
int pass;
int i;
pass = 1;
sorted = false;
while(!sorted)
{
sorted = true;
for(i=1; i <= _numberPersons - pass; ++i)
{
_personFile.seekg(i * _recordSize);
_personFile.read(input1, _recordSize);
_personFile.seekg((i + 1) * _recordSize);
_personFile.read(input2, _recordSize);
if(strncmp(input1, input2, 9) < 0)
continue;
_personFile.seekp(i * _recordSize);
_personFile.write(input2, _recordSize);
_personFile.seekp((i + 1) * _recordSize);
_personFile.write(input1, _recordSize);
sorted = false;
}
++pass;
}
}
Person PersonFile::SearchBySSN(const string & aSSN)
{
int first;
int last;
int midpoint;
int icompare;
char dataRecord[RECORDSIZE];
string ssnSearch;
Person foundPerson;
foundPerson.SetFound(false);
ssnSearch = SSNNoHyphens(aSSN);
first=1;
last=_numberPersons;
while(true)
{
if(first > last) return foundPerson;
midpoint=(first+last)/2;
_personFile.seekg(midpoint*RECORDSIZE);
_personFile.read(dataRecord,RECORDSIZE);
icompare = strncmp(ssnSearch.c_str(),dataRecord,9);
if(icompare < 0) last=midpoint-1;
if(icompare > 0) first=midpoint+1;
if(icompare == 0)
{
_currentRecordNumber=midpoint+1;
foundPerson.MakePerson(dataRecord);
if(foundPerson.IsDeleted())
foundPerson.SetFound(false);
return foundPerson;
}
}
}
Person PersonFile::SearchByOLN(const string & aOLN)
{
int icompare;
char dataRecord[RECORDSIZE];
string olnSearch;
Person foundPerson;
foundPerson.SetFound(false);
olnSearch = aOLN;
for(int i = 1; i <= _numberPersons; ++i)
{
_personFile.seekg(i*RECORDSIZE);
_personFile.read(dataRecord,RECORDSIZE);
icompare = strncmp(olnSearch.c_str(),(dataRecord+9),olnSearch.length());
if(icompare == 0)
{
_currentRecordNumber= i+1;
foundPerson.MakePerson(dataRecord);
if(foundPerson.IsDeleted())
foundPerson.SetFound(false);
return foundPerson;
}
}
return foundPerson;
}
Person PersonFile::SearchByRecordNumber(int inRecNum)
{
Person foundPerson;
char dataRecord[RECORDSIZE];
_currentRecordNumber = inRecNum+1;
if(inRecNum > 0 && inRecNum <= _numberPersons)
{
_personFile.seekg(inRecNum*RECORDSIZE);
_personFile.read(dataRecord,RECORDSIZE);
foundPerson.MakePerson(dataRecord);
if(foundPerson.IsDeleted())
foundPerson.SetFound(false);
return foundPerson;
}
else
return foundPerson;
}
void PersonFile::UpdatePerson(Person & aPerson)
{
string record = aPerson.UnMakePerson();
_personFile.seekp((_currentRecordNumber-1)*RECORDSIZE);
_personFile.write(record.c_str(), RECORDSIZE);
}
void PersonFile::AddPerson(Person & aPerson)
{
++_numberPersons;
_currentRecordNumber = _numberPersons+1;
_personFile.seekp(0);
_personFile.write((char*)&_numberPersons, sizeof(_numberPersons));
UpdatePerson(aPerson);
SortBySSN();
}