Sample program

The following code listing is the entire sample from which the previous examples were taken.

      // Copyright " 1994 Taligent, Inc. All Rights Reserved.
      // $Revision: 1.2 $
      #ifndef __DATAMANAGEMENT__
      #include <DataManagement.h>
      #endif
      #ifndef __COLLECTIONS__
      #include <Collections.h>
      #endif
      #ifndef __RUNTIME__
      #include <RunTime.h>
      #endif
      #ifndef __STORAGE__
      #include <Storage.h>
      #endif
      #ifndef Taligent_STANDARDTEXT
      #include <StandardText.h>
      #endif
      #ifndef _FILESYSTEMINTERFACE_
      #include <FileSystemInterface.h>
      #endif
      
      void main()
      {   
      TDirectory theTaligentRoot;
          TStandardText myTestDictionaries("TestDictionaries");
          TStandardText theDictionaryName("TestA");
      
          TRY
          {
              theTaligentRoot = TPathName::GetTaligentRoot();
          }
          CATCH (TStandardException)
          {
          qprintf("The Taligent Root directory does not exist, exiting now! \n");
              return;
          } ENDTRY;
      
          // ok, we have a directory up to TaligentRoot
          // see if my testDictionary subdirectory exists
      
          TPropertyQuery  query = TFileSystemEntity::kName == myTestDictionaries;
          TFileSystemEntity myTestEntity;
          TRY
          {
              myTestEntity = theTaligentRoot.LookUp (query);
          }
          CATCH(TStandardException)
          {
              TRY
              {
                  theTaligentRoot.CreateDirectory(myTestDictionaries);
              }
              CATCH (TStandardException)
              {
                  qprintf("The directory specified could not be created, exiting now! \n");
                  return;
              } ENDTRY;
              // directory created, lookup again to verify?
              TRY
              {
                  myTestEntity = theTaligentRoot.LookUp (query);
              }
              CATCH (TStandardException)
              {
                  qprintf("The directory specified was not be created\n");
                  return;
              } ENDTRY;
          }
          ENDTRY;
      
          // now TestDictionaries exists as a subdirectory of TaligentRoot
          // this will serve as the directory for some dictionaries
          // myTestEntity should be the TDirectory to $TaligentRoot/TestDictionaries
      
      
          TDirectory theDirectory = myTestEntity;
          try
          {
          dict = new TDiskDictionary(theDirectory, theDictionaryName);
          }
          catch (TStandardException)
          {
          qprintf("dict failed -- BytesAllocated = %d diff = %d\n", myafter, mydiffer);
                  qprintf("The Dictionary threw an exception, exiting now! \n");
                  return;
          }
          
                      
          for (long i=1, j=i+1; i <= 10; i++, j++) {
              TCollectibleLong key(i);
              TCollectibleLong val(j);
              dict->Add(key,val); 
          }
      
          dict->Commit();
          
          // example of using TDiskDictionary::Count
          unsigned long myCount = dict->Count();
          
          // example using TDiskDictionaryIterator
          TDiskDictionaryIterator* myIterator = new TDiskDictionaryIterator(dict);
          
          for (i=0; i=myCount; i++ ) {
              if (i==0)
                  myAssoc = myIterator->First();
              else
                  myAssoc = myIterator->Next();
              if (myAssoc)
                  delete myAssoc;
              else
                  {
                  qprintf("No Assoc exists for i = %d  ",i);
                  if (i == myCount)
                      qprintf("This is expected.\n");
                  else
                      qprintf("This is NOT expected!\n");
                  break;
                  }
      
          }
          delete myIterator;
          
          // example of using TDiskDictionary::SetDestroyFileOnDelete
          dict->SetDestroyFileOnDelete(FALSE);    // leave files intact when dict deleted
          delete dict;
      
          qprintf("Pass A completed.\n");
          
          TDiskDictionary*  dict2 = new TDiskDictionary(myDict);
      
          for (i=11, j=i+1; i <= 20; i++, j++) {
              TCollectibleLong key(i);
              TCollectibleLong val(j);
              dict->Add(key,val); 
          }
      
          dict2->Commit();
      
          // example of using TDiskDictionary::SetDestroyFileOnDelete
          dict2->SetDestroyFileOnDelete(FALSE);   // leave files intact when dict deleted
          delete dict2;
      
          qprintf("Pass B completed.\n");
      
          TDiskDictionary*  dict3 = new TDiskDictionary(myDict);
          
          qprintf("The existence of dict3 is %d\n", dict3->DidExistAlready());
      
          // example of using TDiskDictionary::Count
          qprintf("The number or records in dict3 is %d\n", dict3->Count());
      
          for (i = 1; i <= 20; i++) {
              TCollectibleLong key(i);
              TCollectibleLong *tmpKey;
              if ((tmpKey=(TCollectibleLong*)dict3->Member (key)) != NIL) { // using Member
                  TCollectibleLong *val = (TCollectibleLong *)dict3->Retrieve (key);
                  qprintf("The value retrieved for key \"%d\" = %d\n", key.GetValue(), 
                                                                          val->GetValue());
                  delete val;
                  delete tmpKey;
                  
                  // example of using TDiskDictionary::Remove
                  dict3->Remove(key); 
              }
          }
          
          // example of using TDiskDictionary::SetDestroyFileOnDelete
          dict3->SetDestroyFileOnDelete(TRUE);    // remove the files when dict is deleted
          delete dict3;
      
           qprintf("Done.\n");
      }

[Contents] [Previous] [Next]
Click the icon to mail questions or corrections about this material to Taligent personnel.
Copyright©1995 Taligent,Inc. All rights reserved.

Generated with WebMaker