//---------------------------------------------------------------------------
#pragma hdrstop
//#define _DEBUG_
#include "PhoneBook.h"
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
PhoneBook *myPhoneBook;
PhoneBook::~PhoneBook()
{
file.close();
file.clear();
}
void PhoneBook::initPhoneBook()
{
file.close();
file.clear();
if(Form1->SaveDialog1->Execute())
{
filename=Form1->SaveDialog1->FileName;
#ifdef _DEBUG_
ShowMessage(filename);
#endif
file.open(filename.c_str(),ios::in|ios::out|ios::trunc|ios::binary);
if(file.is_open())
{
Form1->StatusBar1->Panels->Items[0]->Text=ExtractFileName(filename);
newContact();
iCurrentNo=0;
ShowMessage(filename+" 文件创建成功.");
}
else
ShowMessage("文件创建失败.");
}
}
void PhoneBook::openPhoneBook()
{
file.close();
file.clear();
if(Form1->OpenDialog1->Execute())
{
filename=Form1->OpenDialog1->FileName;
file.open(filename.c_str(),ios::in|ios::out|ios::binary);
if(file.is_open())
{
Form1->StatusBar1->Panels->Items[0]->Text=ExtractFileName(filename);
newContact();
iCurrentNo=0;
ShowMessage(filename+" 文件打开成功.");
firstContact();
}
else
ShowMessage("文件打开失败.");
}
}
void PhoneBook::newContact()
{
Form1->edtName->Text="";
Form1->edtName->SetFocus();
Form1->edtAge->Text="";
Form1->edtTel->Text="";
Form1->mmoCompany->Lines->Text="";
memset(&aContact,0,sizeof(Contact));
Form1->bbSave->Enabled=true;
Form1->bbModify->Enabled=false;
Form1->StatusBar1->Panels->Items[1]->Text="新记录";
}
void PhoneBook::saveContact()
{
if(!file.is_open())
{
ShowMessage("请先新建电话簿,或打开电话簿.");
}
else
{
if(getContactFromInput())
{
file.seekp(0,ios_base::end);
#ifdef _DEBUG_
ShowMessage("文件当前位置:"+IntToStr(file.tellp()));
#endif
// file.clear();
file.write(reinterpret_cast<char *>(&aContact),sizeof(Contact));
if(file.fail())
{
ShowMessage("保存记录失败.");
file.clear();
}
else
{
ShowMessage("保存记录成功.");
file.flush();
Form1->bbSave->Enabled=false;
lastContact();
}
}
}
}
bool PhoneBook::getContactFromInput()
{
int age;
memset(&aContact,0,sizeof(Contact));
if(Form1->edtName->Text==""||Form1->edtName->Text.Length()>MaxNameLength)
{
ShowMessage("姓名为空或者姓名太长,请重新输入.");
return false;
}
if(Form1->radMale->Checked==false&&Form1->radFemale->Checked==false)
{
ShowMessage("请选择性别.");
return false;
}
if(Form1->edtAge->Text==""||(age=Form1->edtAge->Text.ToInt())<0||age>150)
{
ShowMessage("年龄为空或者年龄有误,请重新输入.");
return false;
}
if(Form1->edtTel->Text==""||Form1->edtTel->Text.Length()>MaxTelLength)
{
ShowMessage("电话为空或者电话太长,请重新输入.");
return false;
}
if(Form1->mmoCompany->Lines->Text.Length()>MaxCompanyLength)
{
ShowMessage("公司名字太长,请重新输入.");
return false;
}
strcpy(aContact.name,Form1->edtName->Text.c_str());
if(Form1->radMale->Checked==true)
strcpy(aContact.sex,"男");
else if(Form1->radFemale->Checked==true)
strcpy(aContact.sex,"女");
aContact.age=Form1->edtAge->Text.ToInt();
strcpy(aContact.tel,Form1->edtTel->Text.c_str());
strcpy(aContact.company,Form1->mmoCompany->Lines->Text.c_str());
return true;
}
void PhoneBook::modifyContact()
{
if(!file.is_open())
{
ShowMessage("请先新建电话簿,或打开电话簿.");
}
else if(iCurrentNo<1)
ShowMessage("请先定位记录.");
else
{
if(getContactFromInput())
{
file.seekp((iCurrentNo-1)*sizeof(Contact),ios_base::beg);
file.write(reinterpret_cast<char *>(&aContact),sizeof(Contact));
if(file.fail())
{
ShowMessage("修改记录失败.");
file.clear();
}
else
{
ShowMessage("修改记录成功.");
file.flush();
}
}
}
}
void PhoneBook::firstContact()
{
if(!file.is_open())
{
ShowMessage("请先新建电话簿,或打开电话簿.");
}
else
{
file.seekg(0,ios_base::end);
streampos pos=file.tellg();
if((unsigned)pos<sizeof(Contact))
{
ShowMessage("电话簿无电话记录,请返回.");
return;
}
else
{
file.seekg(0,ios_base::beg);
file.read(reinterpret_cast<char *>(&aContact),sizeof(Contact));
if(file.fail())
{
ShowMessage("读取记录失败.");
file.clear();
return;
}
displayContact();
iCurrentNo=1;
Form1->StatusBar1->Panels->Items[1]->Text="当前记录:"+IntToStr(iCurrentNo);
}
}
}
void PhoneBook::displayContact() const
{
Form1->edtName->Text=AnsiString(aContact.name);
if(AnsiString(aContact.sex)=="男")
Form1->radMale->Checked=true;
else if(AnsiString(aContact.sex)=="女")
Form1->radFemale->Checked=true;
Form1->edtAge->Text=IntToStr(aContact.age);
Form1->edtTel->Text=AnsiString(aContact.tel);
Form1->mmoCompany->Lines->Text=AnsiString(aContact.company);
Form1->bbSave->Enabled=false;
Form1->bbModify->Enabled=true;
}
void PhoneBook::prevContact()
{
if(!file.is_open())
{
ShowMessage("请先新建电话簿,或打开电话簿.");
return;
}
file.seekg(0,ios_base::end);
streampos pos=file.tellg();
if((unsigned)pos<sizeof(Contact))
{
ShowMessage("电话簿无电话记录,请返回.");
return;
}
else if(iCurrentNo<2)
ShowMessage("无上一条,请返回.");
else
{
file.seekg((iCurrentNo-2)*sizeof(Contact),ios_base::beg);
file.read(reinterpret_cast<char *>(&aContact),sizeof(Contact));
if(file.fail())
{
ShowMessage("读取记录失败.");
file.clear();
return;
}
displayContact();
iCurrentNo--;
Form1->StatusBar1->Panels->Items[1]->Text="当前记录:"+IntToStr(iCurrentNo);
}
}
void PhoneBook::nextContact()
{
if(!file.is_open())
{
ShowMessage("请先新建电话簿,或打开电话簿.");
}
else
{
file.seekg(0,ios_base::end);
streampos pos=file.tellg();
if((unsigned)pos<sizeof(Contact))
{
ShowMessage("电话簿无电话记录,请返回.");
return;
}
else if((unsigned)pos<(iCurrentNo+1)*sizeof(Contact))
ShowMessage("无下一条,请返回.");
else
{
file.seekg(iCurrentNo*sizeof(Contact),ios_base::beg);
file.read(reinterpret_cast<char *>(&aContact),sizeof(Contact));
if(file.fail())
{
ShowMessage("读取记录失败.");
file.clear();
return;
}
displayContact();
iCurrentNo++;
Form1->StatusBar1->Panels->Items[1]->Text="当前记录:"+IntToStr(iCurrentNo);
}
}
}
void PhoneBook::lastContact()
{
if(!file.