home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 2001 Mobile
/
Chip_Mobile_2001.iso
/
palm
/
business
/
printcar
/
printcar.exe
/
src
/
AddressList.cc
next >
Wrap
C/C++ Source or Header
|
2000-06-10
|
4KB
|
176 lines
//
// $Id: AddressList.cc,v 1.5 2000/06/10 00:19:38 sergey Exp $
//
#include <Pilot.h>
#include "AddressList.h"
#include "Util/Assert.h"
// Constants
static const char* ADDRESS_DB_NAME = "AddressDB";
static const char* ADDRESS_FIRST_NAME_ENTRY = "firstName";
static const char* ADDRESS_LAST_NAME_ENTRY = "lastName";
static const char* ADDRESS_UNNAMED_TEXT = "-Unnamed-";
static const char* FIRST_LETTER_SEARCH_ENTRY_NAME = "lastName";
// construction
AddressList::AddressList():
_lastIndex(-1)
{}
// operations
const DB::AddressAppInfo& AddressList::appInfo() const
{
if (!ensureDatabaseOpened())
assertf(false, "Fail to read AppInfo block", 0);
return _appInfo;
}
int AddressList::recordCount() const
{
return ensureDatabaseOpened()? _database.recordCount() : 0;
}
const DB::AdresseRecord& AddressList::record(int index) const
{
if (!ensureRecordLoaded(index))
assertf(false, "Fail to read Address record %d", index);
return _currentRecord;
}
const char* AddressList::getDisplayName(int index, char* result, int maxResultSize) const
{
assert(result != 0);
result[0] = 0;
if (ensureRecordLoaded(index))
{
int off = 0;
off += copyString(_currentRecord.entry(ADDRESS_FIRST_NAME_ENTRY), result, maxResultSize);
const char* entry = _currentRecord.entry(ADDRESS_LAST_NAME_ENTRY);
if (entry != 0)
{
if (off > 0)
off += copyString(", ", result+off, maxResultSize-off);
off += copyString(entry, result+off, maxResultSize-off);
}
}
if (StrLen(result) == 0)
copyString(ADDRESS_UNNAMED_TEXT, result, maxResultSize);
return result;
}
int AddressList::findByFirstLetter(int startIndex, char letter) const
{
if (isFirstLetterMatch(startIndex+1, letter))
return startIndex+1;
for (int i = 0, count = recordCount(); i < count; ++i)
{
if (isFirstLetterMatch(i, letter))
return i;
}
return -1;
}
// attributes
bool AddressList::isRecord(int index) const
{
if (ensureDatabaseOpened())
{
if(_database.isRecord(index))
{
// Deleted records could remain in the database,
// we must ignore all deleted records.
return !_database.isDeletedRecord(index);
}
}
return false;
}
// implementation
bool AddressList::ensureDatabaseOpened() const
{
if (!_database.isOpened())
{
if (!_database.open(0, ADDRESS_DB_NAME, dmModeReadOnly))
return false;
if (!_database.readAppInfo(_appInfo))
return false;
}
return true;
}
bool AddressList::ensureRecordLoaded(int index) const
{
if (index == _lastIndex)
return true;
if (ensureDatabaseOpened())
{
if (_database.readRecord(index, _currentRecord))
{
_lastIndex = index;
return true;
}
}
_lastIndex = -1;
return false;
}
int AddressList::copyString(const char* src, char* dest, int maxDestSize) const
{
assert(dest != 0);
assert(maxDestSize > 0);
int len = 0;
if (src != 0)
{
len = StrLen(src);
if (len > maxDestSize-1)
len = maxDestSize-1;
MemMove(dest, src, len);
}
dest[len] = 0;
return len;
}
static bool compareLetters(char left, char right)
{
// TO DO: Probably must be done in international way
return left == right || Abs(right-left) == 'a'-'A';
}
bool AddressList::isFirstLetterMatch(int index, char letter) const
{
// The list could contain deleted records. Lets ignore them
if (isRecord(index))
{
const char* entry = record(index).entry(FIRST_LETTER_SEARCH_ENTRY_NAME);
if (entry != 0 && compareLetters(entry[0], letter))
return true;
}
return false;
}