home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Mobile / Chip_Mobile_2001.iso / palm / business / printcar / printcar.exe / src / CardList.cc < prev    next >
C/C++ Source or Header  |  2000-06-04  |  1KB  |  68 lines

  1. //
  2. //  $Id: CardList.cc,v 1.2 2000/06/04 00:50:46 sergey Exp $
  3. //
  4.  
  5. #include <Pilot.h>
  6. #include "CardList.h"
  7. #include "Util/Assert.h"
  8.  
  9.  
  10. // Constants
  11.  
  12. static const char*  CARD_DB_NAME = "PrintCardDB";
  13.  
  14. // construction
  15.  
  16. CardList::CardList():
  17.     _recordCount(0),
  18.     _lastIndex(-1)
  19. {}
  20.  
  21. // operations
  22.  
  23. int CardList::recordCount() const
  24. {
  25.     return ensureDatabaseOpened()? _recordCount : 0;
  26. }
  27.  
  28. const CardRecord& CardList::record(int index) const
  29. {
  30.     if (!ensureRecordLoaded(index))
  31.         assertf(false, "Fail to read Card record %d", index);
  32.  
  33.     return _currentRecord;
  34. }
  35.  
  36. // implementation
  37.  
  38. bool CardList::ensureDatabaseOpened() const
  39. {
  40.     if (!_database.isOpened())
  41.     {
  42.         if (!_database.open(0, CARD_DB_NAME, dmModeReadOnly))
  43.             return false;
  44.  
  45.          // store the amount of Card records in the database
  46.         _recordCount = CardRecord::countRecords(_database);
  47.     }
  48.     return true;
  49. }
  50.  
  51. bool CardList::ensureRecordLoaded(int index) const
  52. {
  53.     if (index == _lastIndex)
  54.         return true;
  55.  
  56.     if (ensureDatabaseOpened())
  57.     {
  58.         if (_currentRecord.load(index, _database))
  59.         {
  60.             _lastIndex = index;
  61.             return true;
  62.         }
  63.     }
  64.  
  65.     _lastIndex = -1;
  66.     return false;
  67. }
  68.