home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gperf.lzh / GPERF / OPTIONS.H < prev    next >
C/C++ Source or Header  |  1993-07-30  |  10KB  |  259 lines

  1. /* This may look like C code, but it is really -*- C++ -*- */
  2.  
  3. /* Handles parsing the Options provided to the user.
  4.  
  5.    Copyright (C) 1989 Free Software Foundation, Inc.
  6.    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
  7.  
  8. This file is part of GNU GPERF.
  9.  
  10. GNU GPERF is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 1, or (at your option)
  13. any later version.
  14.  
  15. GNU GPERF is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. GNU General Public License for more details.
  19.  
  20. You should have received a copy of the GNU General Public License
  21. along with GNU GPERF; see the file COPYING.  If not, write to
  22. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  23.  
  24. /* This module provides a uniform interface to the various options available
  25.    to a user of the gperf hash function generator.  In addition to the
  26.    run-time options, found in the Option_Type below, there is also the
  27.    hash table Size and the Keys to be used in the hashing.
  28.    The overall design of this module was an experiment in using C++
  29.    classes as a mechanism to enhance centralization of option and
  30.    and error handling, which tend to get out of hand in a C program. */
  31.  
  32. #pragma once
  33. #include "std-err.h"
  34. #include "trace.h"
  35.  
  36. /* Enumerate the potential debugging Options. */
  37.  
  38. enum Option_Type
  39. {
  40.   DEBUG        = 01,            /* Enable debugging (prints diagnostics to Std_Err). */
  41.   ORDER        = 02,            /* Apply ordering heuristic to speed-up search time. */
  42.   ANSI         = 04,            /* Generate ANSI prototypes. */
  43.   ALLCHARS     = 010,           /* Use all characters in hash function. */
  44.   GNU          = 020,           /* Assume GNU extensions (primarily function inline). */
  45.   TYPE         = 040,           /* Handle user-defined type structured keyword input. */
  46.   RANDOM       = 0100,          /* Randomly initialize the associated values table. */
  47.   DEFAULTCHARS = 0200,          /* Make default char positions be 1,$ (end of keyword). */
  48.   SWITCH       = 0400,          /* Generate switch output to save space. */
  49.   POINTER      = 01000,         /* Have in_word_set function return pointer, not boolean. */
  50.   NOLENGTH     = 02000,         /* Don't include keyword length in hash computations. */
  51.   LENTABLE     = 04000,         /* Generate a length table for string comparison. */
  52.   DUP          = 010000,        /* Handle duplicate hash values for keywords. */
  53.   FAST         = 020000,        /* Generate the hash function ``fast.'' */
  54.   NOTYPE       = 040000,        /* Don't include user-defined type definition in output -- it's already defined elsewhere. */
  55.   COMP         = 0100000,       /* Generate strncmp rather than strcmp. */
  56.   GLOBAL       = 0200000,       /* Make the keyword table a global variable. */
  57.   CONST        = 0400000,       /* Make the generated tables readonly (const). */
  58.   CPLUSPLUS    = 01000000,      /* Generate C++ code. */
  59.   C            = 02000000,      /* Generate C code. */
  60.   ENUM           = 04000000,    /* Use enum for constants. */
  61. };
  62.  
  63. /* Define some useful constants (these don't really belong here, but I'm
  64.    not sure where else to put them!).  These should be consts, but g++
  65.    doesn't seem to do the right thing with them at the moment... ;-( */
  66.  
  67. enum 
  68. {
  69.   MAX_KEY_POS = 128 - 1,    /* Max size of each word's key set. */
  70.   WORD_START = 1,           /* Signals the start of a word. */
  71.   WORD_END = 0,             /* Signals the end of a word. */
  72.   EOS = MAX_KEY_POS,        /* Signals end of the key list. */
  73. };
  74.  
  75. /* Class manager for gperf program Options. */
  76.  
  77. class Options : private Std_Err
  78. {
  79. public:
  80.                       Options (void);
  81.                      ~Options (void);
  82.   int                 operator[] (Option_Type option);
  83.   void                operator() (int argc, char *argv[]);
  84.   void                operator= (enum Option_Type);
  85.   void                operator!= (enum Option_Type);
  86.   static void         print_options (void);
  87.   static void         set_asso_max (int r);
  88.   static int          get_asso_max (void);
  89.   static void         reset (void);
  90.   static int          get (void);
  91.   static int          get_iterations (void);
  92.   static int          get_max_keysig_size (void);
  93.   static void         set_keysig_size (int);
  94.   static int          get_jump (void);
  95.   static int          initial_value (void);
  96.   static int          get_total_switches (void);
  97.   static const char  *get_function_name (void);
  98.   static const char  *get_key_name (void);
  99.   static const char  *get_class_name (void);
  100.   static const char  *get_hash_name (void);
  101.   static const char  *get_delimiter (void);
  102.  
  103. private:
  104.   const int           DEFAULT_JUMP_VALUE = 5;             /* Size to jump on a collision. */
  105.   const char         *const DEFAULT_NAME = "in_word_set"; /* Default name for generated lookup function. */
  106.   const char         *const DEFAULT_KEY = "name";         /* Default name for the key component. */
  107.   const char         *const DEFAULT_CLASS_NAME = "Perfect_Hash"; /* Default name for the generated class. */
  108.   const char         *const DEFAULT_HASH_NAME = "hash";   /* Default name for generated hash function. */
  109.   const char         *const DEFAULT_DELIMITERS = ",\n";   /* Default delimiters that separate keywords from their attributes. */
  110.   static int          option_word;                        /* Holds the user-specified Options. */
  111.   static int          total_switches;                     /* Number of switch statements to generate. */     
  112.   static int          total_keysig_size;                 /* Total number of distinct key_positions. */
  113.   static int          size;                               /* Range of the hash table. */
  114.   static int          key_pos;                            /* Tracks current key position for Iterator. */
  115.   static int          jump;                               /* Jump length when trying alternative values. */
  116.   static int          initial_asso_value;                 /* Initial value for asso_values table. */
  117.   static int          argument_count;                     /* Records count of command-line arguments. */
  118.   static int          iterations;                         /* Amount to iterate when a collision occurs. */
  119.   static char       **argument_vector;                    /* Stores a pointer to command-line vector. */
  120.   static const char  *function_name;                      /* Names used for generated lookup function. */
  121.   static const char  *key_name;                           /* Name used for keyword key. */
  122.   static const char  *class_name;                         /* Name used for generated C++ class. */
  123.   static const char  *hash_name;                          /* Name used for generated hash function. */
  124.   static const char  *delimiters;                         /* Separates keywords from other attributes. */
  125.   static char         key_positions[MAX_KEY_POS];         /* Contains user-specified key choices. */
  126.   static int          key_sort (char *base, int len);     /* Sorts key positions in REVERSE order. */
  127.   static void         usage (void);                       /* Prints proper program usage. */
  128. };
  129.  
  130. /* Global option coordinator for the entire program. */
  131. extern Options option;       
  132.  
  133. #ifdef __OPTIMIZE__
  134.  
  135. inline int  
  136. Options::operator[] (Option_Type option) /* True if option enable, else false. */
  137.   T (Trace t ("Options::operator[]");)
  138.   return option_word & option;
  139. }
  140.  
  141. inline void
  142. Options::operator = (enum Option_Type opt) /* Enables option OPT. */
  143. {
  144.   option_word |= opt;
  145. }
  146.  
  147. inline void
  148. Options::operator != (enum Option_Type opt) /* Disables option OPT. */
  149. {
  150.   option_word &= ~opt;
  151. }
  152.  
  153. inline void 
  154. Options::reset (void) /* Initializes the key Iterator. */
  155.   T (Trace t ("Options::reset");)
  156.   key_pos = 0;
  157. }
  158.  
  159. inline int 
  160. Options::get (void) /* Returns current key_position and advanced index. */
  161.   T (Trace t ("Options::get");)
  162.   return key_positions[key_pos++];
  163. }
  164.  
  165. inline void 
  166. Options::set_asso_max (int r) /* Sets the size of the table size. */
  167.   T (Trace t ("Options::set_asso_max");)
  168.   size = r;
  169. }
  170.  
  171. inline int 
  172. Options::get_asso_max (void) /* Returns the size of the table size. */
  173.   T (Trace t ("Options::get_asso_max");)
  174.   return size;
  175. }
  176.  
  177. inline int 
  178. Options::get_max_keysig_size (void) /* Returns total distinct key positions. */
  179.   T (Trace t ("Options::get_max_keysig_size");)
  180.   return total_keysig_size;
  181. }
  182.  
  183. inline void
  184. Options::set_keysig_size (int size) /* Sets total distinct key positions. */
  185.   T (Trace t ("Options::set_keysig_size");)
  186.   total_keysig_size = size;
  187. }
  188.  
  189. inline int 
  190. Options::get_jump (void) /* Returns the jump value. */
  191.   T (Trace t ("Options::get_jump");)
  192.   return jump;
  193. }
  194.  
  195. inline const char *
  196. Options::get_function_name (void) /* Returns the generated function name. */
  197.   T (Trace t ("Options::get_function_name");)
  198.   return function_name;
  199. }
  200.  
  201. inline const char *
  202. Options::get_key_name (void) /* Returns the keyword key name. */
  203. {
  204.   T (Trace t ("Options::get_key_name");)
  205.   return key_name;
  206. }
  207.  
  208. inline const char *
  209. Options::get_hash_name (void) /* Returns the hash function name. */
  210. {
  211.   T (Trace t ("Options::get_hash_name");)
  212.   return hash_name;
  213. }
  214.  
  215. inline const char *
  216. Options::get_class_name (void) /* Returns the generated class name. */
  217. {
  218.   T (Trace t ("Options::get_class_name");)
  219.   return class_name;
  220. }
  221.  
  222. inline int 
  223. Options::initial_value (void) /* Returns the initial associated character value. */
  224.   T (Trace t ("Options::initial_value");)
  225.   return initial_asso_value;
  226. }
  227.  
  228. inline int 
  229. Options::get_iterations (void) /* Returns the iterations value. */
  230.   T (Trace t ("Options::get_iterations");)
  231.   return iterations;
  232. }
  233.  
  234. inline const char *
  235. Options::get_delimiter () /* Returns the string used to delimit keywords from other attributes. */
  236. {
  237.   T (Trace t ("Options::get_delimiter");)
  238.   return delimiters;
  239. }
  240.  
  241. inline int
  242. Options::get_total_switches () /* Gets the total number of switch statements to generate. */
  243. {
  244.   T (Trace t ("Options::get_total_switches");)
  245.   return total_switches;
  246. }
  247. #endif
  248.