home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 May / Pcwk5b98.iso / Borland / Cplus45 / BC45 / LOOKUP.PAK / LOOKUP.CPP next >
C/C++ Source or Header  |  1995-08-29  |  2KB  |  74 lines

  1. /*------------------------------------------------------------------------*/
  2. /*                                                                        */
  3. /*  LOOKUP.CPP                                                            */
  4. /*                                                                        */
  5. /*  Copyright (c) 1991, 1993 Borland International                        */
  6. /*  All Rights Reserved.                                                  */
  7. /*                                                                        */
  8. /*  Hash table example file                                               */
  9. /*                                                                        */
  10. /*------------------------------------------------------------------------*/
  11.  
  12. #if !defined( __CSTRING_H )
  13. #include <cstring.h>
  14. #endif  // __CSTRING_H
  15.  
  16. #if !defined( __ASSOC_H )
  17. #include "classlib\assoc.h"
  18. #endif  // __ASSOC_H
  19.  
  20. #if !defined( __DICT_H )
  21. #include "classlib\dict.h"
  22. #endif  // __DICT_H
  23.  
  24. #ifndef __IOSTREAM_H
  25. #include <iostream.h>    
  26. #endif
  27.  
  28. static char *Entries[] =
  29.     {
  30.     "string",           "manipulates character data",
  31.      "TDate",            "manipulates dates",
  32.      "TTime",            "manipulates times",
  33.      "VectorImp",        "implements a zero-based vector",
  34.      "CVectorImp",       "implements a zero-based counted vector",
  35.      "SVectorImp",       "implements a zero-based sorted vector",
  36.      };
  37.  
  38. #define ArraySize(n) (sizeof(n)/sizeof(*n))
  39.  
  40. class HashString : public string {
  41.   public:
  42.      HashString() : string() {}
  43.      HashString(const char* s) : string(s) {}
  44.      unsigned HashValue() const { return hash(); }
  45. };
  46.                          
  47. typedef TDDAssociation<HashString,HashString> ClassData;
  48. typedef TDictionaryAsHashTable<ClassData> Dictionary;
  49.  
  50. int main( int argc, char *argv[] )
  51. {
  52.      if( argc != 2 )
  53.           {
  54.           cerr << "Usage:  lookup classname\n";
  55.           return 1;
  56.           }
  57.  
  58.      Dictionary ClassDefinitions;
  59.      string::set_case_sensitive(0);
  60.  
  61.      for( int i = 0; i < ArraySize(Entries); i+=2 )
  62.           ClassDefinitions.Add( ClassData( Entries[i], Entries[i+1] ) );
  63.  
  64.      ClassData *definition =
  65.           ClassDefinitions.Find( ClassData( argv[1], (char *)0 ) );
  66.      if( definition == 0 )
  67.           cout << "A definition for " << argv[1]
  68.                  << " was not found in the dictionary.\n";
  69.      else
  70.           cout << definition->Key() << " : " << definition->Value() << endl;
  71.  
  72.      return 0;
  73. }
  74.