home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / printcar / printcar.exe / src / AddressList.cc next >
C/C++ Source or Header  |  2000-06-10  |  4KB  |  176 lines

  1. //
  2. //  $Id: AddressList.cc,v 1.5 2000/06/10 00:19:38 sergey Exp $
  3. //
  4.  
  5. #include <Pilot.h>
  6. #include "AddressList.h"
  7. #include "Util/Assert.h"
  8.  
  9.  
  10. // Constants
  11.  
  12. static const char*  ADDRESS_DB_NAME                 = "AddressDB";
  13. static const char*  ADDRESS_FIRST_NAME_ENTRY        = "firstName";
  14. static const char*  ADDRESS_LAST_NAME_ENTRY         = "lastName";
  15. static const char*  ADDRESS_UNNAMED_TEXT            = "-Unnamed-";
  16.  
  17. static const char*  FIRST_LETTER_SEARCH_ENTRY_NAME  = "lastName";
  18.  
  19. // construction
  20.  
  21. AddressList::AddressList():
  22.     _lastIndex(-1)
  23. {}
  24.  
  25. // operations
  26.  
  27. const DB::AddressAppInfo& AddressList::appInfo() const
  28. {
  29.     if (!ensureDatabaseOpened())
  30.         assertf(false, "Fail to read AppInfo block", 0);
  31.  
  32.     return _appInfo;
  33. }
  34.  
  35. int AddressList::recordCount() const
  36. {
  37.     return ensureDatabaseOpened()? _database.recordCount() : 0;
  38. }
  39.  
  40. const DB::AdresseRecord& AddressList::record(int index) const
  41. {
  42.     if (!ensureRecordLoaded(index))
  43.         assertf(false, "Fail to read Address record %d", index);
  44.  
  45.     return _currentRecord;
  46. }
  47.  
  48. const char* AddressList::getDisplayName(int index, char* result, int maxResultSize) const
  49. {
  50.     assert(result != 0);
  51.     result[0] = 0;
  52.  
  53.     if (ensureRecordLoaded(index))
  54.     {
  55.         int off = 0;
  56.         off += copyString(_currentRecord.entry(ADDRESS_FIRST_NAME_ENTRY), result, maxResultSize);
  57.  
  58.         const char* entry = _currentRecord.entry(ADDRESS_LAST_NAME_ENTRY);
  59.         if (entry != 0)
  60.         {
  61.             if (off > 0)
  62.                 off += copyString(", ", result+off, maxResultSize-off);
  63.  
  64.             off += copyString(entry, result+off, maxResultSize-off);
  65.         }
  66.     }
  67.  
  68.     if (StrLen(result) == 0)
  69.         copyString(ADDRESS_UNNAMED_TEXT, result, maxResultSize);
  70.  
  71.     return result;
  72. }
  73.  
  74. int AddressList::findByFirstLetter(int startIndex, char letter) const
  75. {
  76.     if (isFirstLetterMatch(startIndex+1, letter))
  77.         return startIndex+1;
  78.  
  79.     for (int i = 0, count = recordCount(); i < count; ++i)
  80.     {
  81.         if (isFirstLetterMatch(i, letter))
  82.             return i;
  83.     }
  84.  
  85.     return -1;
  86. }
  87.  
  88. // attributes
  89.  
  90. bool AddressList::isRecord(int index) const
  91. {
  92.     if (ensureDatabaseOpened())
  93.     {
  94.         if(_database.isRecord(index))
  95.         {
  96.             // Deleted records could remain in the database,
  97.             // we must ignore all deleted records.
  98.             return !_database.isDeletedRecord(index);
  99.         }
  100.     }
  101.     return false;
  102. }
  103.  
  104. // implementation
  105.  
  106. bool AddressList::ensureDatabaseOpened() const
  107. {
  108.     if (!_database.isOpened())
  109.     {
  110.         if (!_database.open(0, ADDRESS_DB_NAME, dmModeReadOnly))
  111.             return false;
  112.  
  113.         if (!_database.readAppInfo(_appInfo))
  114.             return false;
  115.     }
  116.     return true;
  117. }
  118.  
  119. bool AddressList::ensureRecordLoaded(int index) const
  120. {
  121.     if (index == _lastIndex)
  122.         return true;
  123.  
  124.     if (ensureDatabaseOpened())
  125.     {
  126.         if (_database.readRecord(index, _currentRecord))
  127.         {
  128.             _lastIndex = index;
  129.             return true;
  130.         }
  131.     }
  132.  
  133.     _lastIndex = -1;
  134.     return false;
  135. }
  136.  
  137. int AddressList::copyString(const char* src, char* dest, int maxDestSize) const
  138. {
  139.     assert(dest != 0);
  140.     assert(maxDestSize > 0);
  141.  
  142.     int len = 0;
  143.     if (src != 0)
  144.     {
  145.         len = StrLen(src);
  146.         if (len > maxDestSize-1)
  147.             len = maxDestSize-1;
  148.  
  149.         MemMove(dest, src, len);
  150.     }
  151.  
  152.     dest[len] = 0;
  153.     return len;
  154. }
  155.  
  156. static bool compareLetters(char left, char right)
  157. {
  158.     // TO DO: Probably must be done in international way
  159.  
  160.     return left == right || Abs(right-left) == 'a'-'A';
  161. }
  162.  
  163. bool AddressList::isFirstLetterMatch(int index, char letter) const
  164. {
  165.     // The list could contain deleted records. Lets ignore them
  166.     if (isRecord(index))
  167.     {
  168.         const char* entry = record(index).entry(FIRST_LETTER_SEARCH_ENTRY_NAME);
  169.  
  170.         if (entry != 0 && compareLetters(entry[0], letter))
  171.             return true;
  172.     }
  173.  
  174.     return false;
  175. }
  176.