home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / todb101 / todb / lib / demo.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-02  |  8.7 KB  |  375 lines

  1. // ========================================================================
  2. //  TODB LIBRARY
  3. //    demo.cpp
  4. //
  5. //    Demonstration program
  6. //
  7. //    Version: see TODB.H file
  8. //
  9. //    Copyright 1993 Christian Thérien
  10. //    All rights reserved
  11. // ========================================================================
  12.  
  13. #include "demo.h"
  14. #include "schema.h"
  15. #include "schset.h"
  16. #include <string.h>
  17. #include <fstream.h>
  18. #include <stdlib.h>
  19.  
  20. void Load();
  21. void Clear();
  22. void Search();
  23. Boolean LoadBibNote( char * fn );
  24. Boolean DeleteBibNote( char * fn );
  25.  
  26. // key set database
  27. TODB * ksDB = NULL;
  28. // application framework
  29. TODBmgr * db = NULL;
  30.  
  31.  
  32. void main()
  33. {
  34.     Load();           // loads key set database
  35.     cout << "+++++++++++++ First search : " << endl << endl;
  36.     Search();
  37.     Clear();           // cleans up the database
  38.     cout << "+++++++++++++ Second search : " << endl;
  39.     Search();
  40. }
  41.  
  42. void Load()
  43. {
  44.     db = new TODBmgr;
  45.     db->DeleteDB( String( "DEMODB" ) );
  46.     if ( db->OpenNew( String( "DEMODB" ) ) == False ) {
  47.     delete db;
  48.     return;
  49.     }
  50.  
  51.     if ( db->isOpen() == True )
  52.     LoadBibNote( "DEMO.DAT" );
  53.  
  54.     delete db;
  55. }
  56.  
  57. void Clear()
  58. {
  59.     db = new TODBmgr;
  60.     if ( db->OpenExist( String( "DEMODB" ) ) == False ) {
  61.     delete db;
  62.     return;
  63.     }
  64.  
  65.     if ( db->isOpen() == True )
  66.     DeleteBibNote( "DEMO.DAT" );
  67.  
  68.     delete db;
  69. }
  70.  
  71. void Search()
  72. {
  73.     db = new TODBmgr;
  74.     if ( db->OpenExist( String( "DEMODB" ) ) == False ) {
  75.     delete db;
  76.     return;
  77.     }
  78.  
  79.     SearchSet s1( ksDB ), s2( ksDB ), s3( ksDB );
  80.  
  81.     cout << "=============" << endl;
  82.     s1.FindExact( BIBNOTE, AUTHOR, "MADSEN" );
  83.     cout << endl << "  Search of MADSEN (BIBNOTE, AUTHOR),        set length : " << s1.Card() << endl;
  84.  
  85.     cout << "=============" << endl;
  86.     s2.FindPattern( BIBNOTE, AUTHOR, "MA*" );
  87.     cout << endl << "  Search of MA*    (BIBNOTE, AUTHOR),        set length : " << s2.Card() << endl;
  88.  
  89.     cout << "=============" << endl;
  90.     s3 = s2 - s1;
  91.     cout << endl << "  MA* - MADSEN,                              set length : " << s3.Card() << endl;
  92.  
  93.     cout << "=============" << endl;
  94.     s1.FindAll( BIBNOTE, PUBLISHER );
  95.     cout << endl << "  Search all PUBLISHER (BIBNOTE, PUBLISHER), set length : " << s1.Card() << endl;
  96.  
  97.     cout << "=============" << endl;
  98.  
  99.     delete db;
  100. }
  101.  
  102. Boolean LoadBibNote( char * fn )
  103. {
  104.     char line[ 2048 ];
  105.     ifstream tagf;
  106.     char c;
  107.     BibNote * bibn = NULL;
  108.  
  109.     tagf.open( fn, ios::nocreate );
  110.     if ( !tagf ) return False;
  111.  
  112.     // Empty file?
  113.     tagf.get( c );
  114.     if ( tagf.eof() ) return False;
  115.     tagf.putback( c );
  116.  
  117.     tagf.getline( line, 2048, '\n' );
  118.     if ( strstr( line, "$KEY  : " ) == NULL ) return False;
  119.  
  120.     // Load Count class instance
  121.     Count cnt;
  122.  
  123.     // create the first BibNote
  124.     bibn = new BibNote( cnt.IncCNote() );
  125.  
  126.     while ( !tagf.eof() ) {
  127.     tagf.getline( line, 2048, '\n' );
  128.     if ( strstr( line, "$KEY  : " ) != NULL ) {
  129.         cout << "Loading BibNote no. " << cnt.CNote() << endl;
  130.         bibn->ChangeIdx();
  131.         delete bibn;
  132.         // Next BibNote
  133.         bibn = new BibNote( cnt.IncCNote() );
  134.     }
  135.     if ( strstr( line, "AUTHOR: ") != NULL) {
  136.         bibn->author += line + 8;
  137.         bibn->author += " ";
  138.     }
  139.     if ( strstr( line, "SHELFM: ") != NULL) {
  140.         bibn->shelf_mark += line + 8;
  141.         bibn->shelf_mark += " ";
  142.     }
  143.     if ( strstr( line, "DATE  : ") != NULL) {
  144.         bibn -> date += line + 8;
  145.         bibn -> date += " ";
  146.     }
  147.     if ( strstr( line, "KEYWDS: ") != NULL) {
  148.         bibn->keywords += line + 8;
  149.         bibn->keywords += " ";
  150.     }
  151.     if ( strstr( line, "PUBL  : ") != NULL) {
  152.         bibn->publisher += line + 8;
  153.         bibn->publisher += " ";
  154.     }
  155.     if ( strstr( line, "ISSN  : ") != NULL) {
  156.         bibn->issn += line + 8;
  157.         bibn->issn += " ";
  158.     }
  159.     if ( strstr( line, "ISBN  : ") != NULL) {
  160.         bibn->isbn += line + 8;
  161.         bibn->isbn += " ";
  162.     }
  163.     if ( strstr( line, "PUBLPL: ") != NULL) {
  164.         bibn -> publishing_place += line + 8;
  165.         bibn -> publishing_place += " ";
  166.     }
  167.     if ( strstr( line, "PAGING: ") != NULL) {
  168.         bibn->paging += line + 8;
  169.         bibn->paging += " ";
  170.     }
  171.     if ( strstr( line, "ARTSUB: ") != NULL) {
  172.         bibn->article_subtitle += line + 8;
  173.         bibn->article_subtitle += " ";
  174.     }
  175.     if ( strstr( line, "ARTIT : ") != NULL) {
  176.         bibn->article_title += line + 8;
  177.         bibn->article_title += " ";
  178.     }
  179.     if ( strstr( line, "TITLE : ") != NULL) {
  180.         bibn->title += line + 8;
  181.         bibn->title += " ";
  182.     }
  183.     if ( strstr( line, "SUBTIT: ") != NULL) {
  184.         bibn->subtitle += line + 8;
  185.         bibn->subtitle += " ";
  186.     }
  187.     }
  188.     cout << "Loading BibNote no. " << cnt.CNote() << endl;
  189.     bibn->ChangeIdx();
  190.     delete bibn;
  191.  
  192.     tagf.close();
  193.     return True;
  194. }
  195.  
  196. Boolean DeleteBibNote( char * fn )
  197. {
  198.     char line[ 2048 ];
  199.     ifstream tagf;
  200.     char c;
  201.     BibNote * bibn = NULL;
  202.  
  203.     tagf.open( fn, ios::nocreate );
  204.     if ( !tagf ) return False;
  205.  
  206.     // Empty file?
  207.     tagf.get( c );
  208.     if ( tagf.eof() ) return False;
  209.     tagf.putback( c );
  210.  
  211.     tagf.getline( line, 2048, '\n' );
  212.     if ( strstr( line, "$KEY  : " ) == NULL ) return False;
  213.  
  214.     KeyType key = 1;
  215.  
  216.     bibn = new BibNote( key );
  217.  
  218.     while ( !tagf.eof() ) {
  219.     tagf.getline( line, 2048, '\n' );
  220.     if ( strstr( line, "$KEY  : " ) != NULL ) {
  221.         cout << "Deleting BibNote no. " << key << endl;
  222.         bibn->Delete();
  223.         delete bibn;
  224.         bibn = new BibNote( ++key );
  225.     }
  226.     if ( strstr( line, "AUTHOR: ") != NULL) {
  227.         bibn->author += line + 8;
  228.         bibn->author += " ";
  229.     }
  230.     if ( strstr( line, "SHELFM: ") != NULL) {
  231.         bibn->shelf_mark += line + 8;
  232.         bibn->shelf_mark += " ";
  233.     }
  234.     if ( strstr( line, "DATE  : ") != NULL) {
  235.         bibn -> date += line + 8;
  236.         bibn -> date += " ";
  237.     }
  238.     if ( strstr( line, "KEYWDS: ") != NULL) {
  239.         bibn->keywords += line + 8;
  240.         bibn->keywords += " ";
  241.     }
  242.     if ( strstr( line, "PUBL  : ") != NULL) {
  243.         bibn->publisher += line + 8;
  244.         bibn->publisher += " ";
  245.     }
  246.     if ( strstr( line, "ISSN  : ") != NULL) {
  247.         bibn->issn += line + 8;
  248.         bibn->issn += " ";
  249.     }
  250.     if ( strstr( line, "ISBN  : ") != NULL) {
  251.         bibn->isbn += line + 8;
  252.         bibn->isbn += " ";
  253.     }
  254.     if ( strstr( line, "PUBLPL: ") != NULL) {
  255.         bibn -> publishing_place += line + 8;
  256.         bibn -> publishing_place += " ";
  257.     }
  258.     if ( strstr( line, "PAGING: ") != NULL) {
  259.         bibn->paging += line + 8;
  260.         bibn->paging += " ";
  261.     }
  262.     if ( strstr( line, "ARTSUB: ") != NULL) {
  263.         bibn->article_subtitle += line + 8;
  264.         bibn->article_subtitle += " ";
  265.     }
  266.     if ( strstr( line, "ARTIT : ") != NULL) {
  267.         bibn->article_title += line + 8;
  268.         bibn->article_title += " ";
  269.     }
  270.     if ( strstr( line, "TITLE : ") != NULL) {
  271.         bibn->title += line + 8;
  272.         bibn->title += " ";
  273.     }
  274.     if ( strstr( line, "SUBTIT: ") != NULL) {
  275.         bibn->subtitle += line + 8;
  276.         bibn->subtitle += " ";
  277.     }
  278.     }
  279.     cout << "Deleting BibNote no. " << key << endl;
  280.     bibn->Delete();
  281.     delete bibn;
  282.  
  283.     tagf.close();
  284.     return True;
  285. }
  286.  
  287. //-------------------------------------------------------------------------
  288. // TODBmgr class
  289. //-------------------------------------------------------------------------
  290.  
  291. TODBmgr::TODBmgr()
  292. {
  293.     open_ = False;
  294. }
  295.  
  296. TODBmgr::~TODBmgr()
  297. {
  298.     if ( open_ == True ) Close();
  299. }
  300.  
  301. // Open an empty database
  302. Boolean TODBmgr::OpenNew( String & fname )
  303. {
  304.     open_ = False;
  305.  
  306.     ksDB = new TODB;
  307.     if ( ksDB->Open( fname + String( ".KEY" ),
  308.              fname + String( ".IDX" ),
  309.              IDX_CREAT | IDX_APPEND ) == False )
  310.     return False;
  311.  
  312.     Count * cnt = new Count( 0 );
  313.     cnt->SetVersion( String( "MyVersion: 1.0" ) );
  314.     cnt->SetDirty();
  315.     cnt->SetCNote( 0 );
  316.     cnt->AddObject();
  317.     delete cnt;
  318.  
  319.     return ( open_ = True );
  320. }
  321.  
  322. // if exist, open the database
  323. Boolean TODBmgr::OpenExist( String & fname )
  324. {
  325.     ksDB = new TODB;
  326.     if ( ksDB->Open( fname + String( ".KEY" ),
  327.              fname + String( ".IDX" ),
  328.              IDX_EXIST ) == False )
  329.     return ( open_ = False );
  330.  
  331.     // check the dirty bit value
  332.     Count * cnt = new Count;
  333.     if ( cnt->Dirty() == True ) {
  334.     delete cnt;
  335.     delete ksDB;
  336.     FileError();
  337.     }
  338.     else {
  339.     delete cnt;
  340.     return ( open_ = True );
  341.     }
  342.  
  343.     return ( open_ = False );
  344. }
  345.  
  346. Boolean TODBmgr::isOpen()
  347. {
  348.     return open_;
  349. }
  350.  
  351. Boolean TODBmgr::Close()
  352. {
  353.     if ( open_ == True ) {
  354.     Count * cnt = new Count;
  355.     cnt->ResetDirty();
  356.     delete cnt;
  357.     delete ksDB;
  358.     open_ = False;
  359.     return True;
  360.     }
  361.     return False;
  362. }
  363.  
  364. void TODBmgr::DeleteDB( String & fname )
  365. {
  366.     remove( fname + String( ".KEY" ) );
  367.     remove( fname + String( ".IDX" ) );
  368. }
  369.  
  370. void TODBmgr::FileError()
  371. {
  372.     cerr << "\n FATAL ERROR - TODBmgr: dirty bit is set, file .KEY corrupted";
  373.     exit( EXIT_FAILURE );
  374. }
  375.