home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK11 / MFC / SAMPLES / TUTORIAL / DMTEST.CP$ / dmtest
Encoding:
Text File  |  1992-01-10  |  7.7 KB  |  313 lines

  1. // dmtest.cpp : A program to test the CPerson and CPersonList classes.
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and Microsoft
  9. // QuickHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "person.h"
  14.  
  15. // Function prototypes.
  16. CPersonList* MakeDataBase();
  17. CFile* OpenForReading( const CString& rFileName );
  18. CFile* OpenForWriting( const CString& rFileName );
  19. CPersonList* ReadDataBase( CFile* pFile );
  20. BOOL WriteDataBase( CFile* pFile, CPersonList* pDataBase );
  21. void TestFindPerson( CPersonList* pDataBase );
  22. void ListDataBase( CPersonList* db );
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25.  
  26. void main()
  27. {
  28.     const char szFileName[] = "tutorial.dat";
  29.  
  30. #ifdef _DEBUG
  31.     // Prepare for display of search results
  32.     const int nDumpChildren = 1;
  33.     afxDump.SetDepth( nDumpChildren );
  34. #endif
  35.  
  36.     printf( "Create a person list and fill it with persons\n" );
  37.     CPersonList* pDataBase = MakeDataBase();
  38.  
  39.     printf( "Serialize the person list\n" );
  40.     CFile* pFile;  // Declare a file object
  41.  
  42.     TRY
  43.     {
  44.         // Could throw a file exception if can't open file
  45.         pFile = OpenForWriting( szFileName );
  46.  
  47.         // Could throw an archive exception if can't create
  48.         WriteDataBase( pFile, pDataBase );
  49.     }
  50.     CATCH( CFileException, theException )
  51.     {
  52.         printf( "Unable to open file for writing\n" );
  53.         exit( -1 );
  54.     }
  55.     AND_CATCH( CArchiveException, theException )
  56.     {
  57.         printf( "Unable to save the database\n" );
  58.         pFile->Close();  // Close up
  59.         delete pFile;
  60.         exit( -1 );
  61.     }
  62.     END_CATCH
  63.     
  64.     // No exceptions, so close up
  65.     pFile->Close();
  66.     delete pFile;
  67.  
  68.     ListDataBase( pDataBase );
  69.     
  70.     printf( "Delete the list and all its elements\n" );
  71.     pDataBase->DeleteAll();
  72.     ListDataBase( pDataBase );
  73.     delete pDataBase;
  74.     
  75.     printf( "Deserialize the data from disk into a new list\n" );
  76.     CPersonList* pDataBase2;   // Create a new, empty list
  77.  
  78.     TRY
  79.     {
  80.         // Could throw a file exception if can't open file
  81.         pFile = OpenForReading( szFileName );
  82.     
  83.         // Could throw an archive exception if can't create
  84.         pDataBase2 = ReadDataBase( pFile );
  85.     }
  86.     CATCH( CFileException, theException )
  87.     {
  88.         printf( "Unable to open file for reading database\n" );
  89.         exit( -1 );
  90.     }
  91.     AND_CATCH( CArchiveException, theException )
  92.     {
  93.         printf( "Unable to read the database\n" );
  94.         pFile->Close();  // Close up before exiting
  95.         delete pFile;
  96.         exit( -1 );
  97.     }
  98.     END_CATCH
  99.  
  100.     // No exceptions, so close up
  101.     pFile->Close();
  102.     delete pFile;
  103.  
  104.     ListDataBase( pDataBase2 );
  105.     
  106.     printf( "Test the FindPerson function\n" );
  107.     if ( pDataBase2 != NULL )
  108.     TestFindPerson( pDataBase2 );
  109.  
  110.     printf( "Delete the list and all its elements\n" );
  111.     pDataBase2->DeleteAll();
  112.     delete pDataBase2;
  113.  
  114.     TRACE( "End of program\n" );
  115. }
  116.  
  117. // MakeDataBase - Create a database and add some persons
  118. //
  119. CPersonList* MakeDataBase()
  120. {
  121.     TRACE( "  Make a new person list on the heap\n" );
  122.     CPersonList* pDataBase = new CPersonList;
  123.  
  124.     TRACE( "  Add several new persons to the list\n" );
  125.     CPerson* pNewPerson1 = new CPerson( "Smith", "Mary", "435-8159" );
  126.     pDataBase->AddHead( pNewPerson1 );
  127.  
  128.     CPerson* pNewPerson2 = new CPerson( "Smith", "Al", "435-4505" );
  129.     pDataBase->AddHead( pNewPerson2 );
  130.  
  131.     CPerson* pNewPerson3 = new CPerson( "Jones", "Steve", "344-9865" );
  132.     pDataBase->AddHead( pNewPerson3 );
  133.  
  134.     CPerson* pNewPerson4 = new CPerson( "Hart", "Mary", "287-0987" );
  135.     pDataBase->AddHead( pNewPerson4 );
  136.  
  137.     CPerson* pNewPerson5 = new CPerson( "Meyers", "Brian", "236-1234" );
  138.     pDataBase->AddHead( pNewPerson5 );
  139.  
  140.     TRACE( "  Return the completed database to main\n" );
  141.     return pDataBase;
  142. }
  143.  
  144. // OpenForReading - open a file for reading
  145. //
  146. CFile* OpenForReading( const CString& rFileName )
  147. {
  148.     CFile* pFile = new CFile;
  149.     CFileException* theException = new CFileException;
  150.     if ( !pFile->Open( rFileName, CFile::modeRead, theException ) )
  151.     {
  152.         delete pFile;
  153.         TRACE( "  Threw file exception in OpenForReading\n" );
  154.         THROW( theException );
  155.     }
  156.     delete theException;
  157.  
  158.     // Exit here if no exceptions
  159.     return pFile;
  160. }
  161.  
  162. // OpenForWriting - open a file for writing
  163. //
  164. CFile* OpenForWriting( const CString& rFileName )
  165. {
  166.     CFile* pFile = new CFile;
  167.     CFileStatus status;
  168.     UINT nAccess = CFile::modeWrite;
  169.  
  170.     // GetStatus will return TRUE if file exists,
  171.     // or FALSE if it doesn't exist
  172.     if ( !CFile::GetStatus( rFileName, status ) )
  173.         nAccess |= CFile::modeCreate;
  174.     
  175.     CFileException* theException = new CFileException;
  176.     if ( !pFile->Open( rFileName, nAccess, theException ) )
  177.     {
  178.         delete pFile;
  179.         TRACE( "  Threw a file exception in OpenForWriting\n" );
  180.         THROW( theException );
  181.     }
  182.     delete theException;
  183.     
  184.     // Exit here if no errors or exceptions
  185.     TRACE( "  Opened file for writing OK\n" );
  186.     return pFile;
  187. }
  188.  
  189. // ReadDataBase - read data into a person list
  190. //
  191. CPersonList* ReadDataBase( CFile* pFile )
  192. {
  193.     CPersonList* pNewDataBase = NULL;
  194.  
  195.     // Create an archive from pFile for reading
  196.     CArchive archive( pFile, CArchive::load );
  197.  
  198.     TRY
  199.     {
  200.         // and deserialize the new data base from the archive
  201.         archive >> pNewDataBase;
  202.     }
  203.     CATCH( CArchiveException, theException )
  204.     {
  205.         TRACE( "  Caught an archive exception in ReadDataBase\n" );
  206. #ifdef _DEBUG
  207.         theException->Dump( afxDump );
  208. #endif
  209.         archive.Close();
  210.     
  211.         // If we got part of the database then delete it so we don't
  212.         // have any Memory leaks
  213.         if ( pNewDataBase != NULL )
  214.         {
  215.             pNewDataBase->DeleteAll();
  216.             delete pNewDataBase;
  217.         }
  218.         THROW_LAST();
  219.     }
  220.     END_CATCH
  221.  
  222.     // Exit here if no errors or exceptions
  223.     archive.Close();
  224.     return pNewDataBase;
  225. }
  226.  
  227. // WriteDataBase - write data from a person list to disk
  228. //
  229. BOOL WriteDataBase( CFile* pFile, CPersonList* pDataBase )
  230. {
  231.     // Create an archive from pFile for writing
  232.     CArchive archive( pFile, CArchive::store );
  233.  
  234.     TRY
  235.     {
  236.         // and serialize the data base to the archive
  237.         archive << pDataBase;
  238.     }
  239.     CATCH( CArchiveException, theException )
  240.     {
  241.         TRACE( "  Caught an archive exception in WriteDataBase\n" );
  242. #ifdef _DEBUG
  243.         theException->Dump( afxDump );
  244. #endif
  245.         archive.Close();
  246.         THROW_LAST();
  247.     }
  248.     END_CATCH
  249.  
  250.     // Exit here if no errors or exceptions
  251.     archive.Close();
  252.     return TRUE;
  253. }
  254.  
  255. // TestFindPerson - test CPersonList::FindPerson
  256. //
  257. void TestFindPerson( CPersonList* pDataBase )
  258. {
  259.     printf( "  Looking for the name Banipuli\n" );
  260.     CPersonList* pFound = pDataBase->FindPerson( "Banipuli" );
  261.     if ( pFound->IsEmpty() )
  262.     {
  263.         printf( "  No matching persons\n" );
  264.     }
  265.     else
  266.     {
  267.         printf( "  Found matching persons\n" );
  268. #ifdef _DEBUG
  269.         pFound->Dump( afxDump );
  270. #endif
  271.     }
  272.     delete pFound;
  273.  
  274.     printf( "  Looking for the name Smith\n" );
  275.     pFound = pDataBase->FindPerson( "Smith" );
  276.     if ( pFound->IsEmpty() )
  277.     {
  278.         printf( "  No matching persons\n" );
  279.     }
  280.     else
  281.     {
  282.         printf( "  Found matching persons\n" );
  283. #ifdef _DEBUG
  284.         pFound->Dump( afxDump );
  285. #endif
  286.     }
  287.  
  288.     // Don't DeleteAll the found list since it
  289.     //  shares CPerson objects with dataBase
  290.     delete pFound; // Deletes only the list object
  291. }
  292.  
  293. void ListDataBase( CPersonList* db )
  294. {
  295.     CPerson* pCurrent;
  296.     POSITION pos;
  297.  
  298.     if ( db->GetCount() == 0 )
  299.         printf( "  List is Empty\n" );
  300.     else
  301.     {
  302.         printf( "  List contains:\n" );
  303.         pos = db->GetHeadPosition();
  304.         while ( pos != NULL )
  305.         {
  306.             pCurrent = (CPerson*)db->GetNext( pos );
  307.             printf( "\t%s, %s\t%s\n", (const char*)pCurrent->GetLastName(),
  308.                     (const char*)pCurrent->GetFirstName(),
  309.                     (const char*)pCurrent->GetPhoneNumber() );
  310.         }
  311.     }
  312. }
  313.