home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / MacWT 0.9 / wt Source / table.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-10  |  1.9 KB  |  82 lines  |  [TEXT/CWIE]

  1. /*
  2. **  MacWT -- a 3d game engine for the Macintosh
  3. **  © 1995, Bill Hayden and Nikol Software
  4. **  Free for non-commercial use - address questions to the e-mail address below
  5. **
  6. **  Mail:           afn28988@freenet.ufl.edu (Bill Hayden)
  7. **    MacWT FTP site: ftp.circa.ufl.edu/pub/software/ufmug/mirrors/LocalSW/Hayden/
  8. **  WWW Page:       http://grove.ufl.edu:80/~nikolsw
  9. **
  10. **    All of the above addresses are due to changes sometime in 1996, so stay tuned
  11. **
  12. **  based on wt, by Chris Laurel
  13. **
  14. **  This program is distributed in the hope that it will be useful,
  15. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. */
  18.  
  19.  
  20. #include <string.h>
  21. #include "wt.h"
  22. #include "error.h"
  23. #include "wtmem.h"
  24. #include "table.h"
  25.  
  26.  
  27. #define STARTING_TABLE_SIZE  16
  28.  
  29.  
  30. /* For a dynamic table, pass 0 for the table_size; otherwise the table size
  31. **   will be fixed at table_size entries;
  32. */
  33. Table *new_table(size_t entry_size, short table_size)
  34. {
  35.     Table *t;
  36.      
  37.     t = (Table *)wtmalloc(sizeof(Table));
  38.     t->entry_size = entry_size;
  39.     t->current_entries = 0;
  40.     if (table_size != 0)
  41.         {
  42.         t->fixed_size = TRUE;
  43.         t->max_entries = table_size;
  44.         t->table = wtmalloc(entry_size * table_size);
  45.         }
  46.     else
  47.         {
  48.         t->fixed_size = FALSE;
  49.         t->max_entries = 0;
  50.         t->table = NULL;
  51.         }
  52.  
  53.     return t;
  54. }
  55.  
  56.  
  57. /* Add an entry to a table and return its index. */
  58. short add_table_entry(Table *t, void *entry)
  59. {
  60.     if (t->current_entries >= t->max_entries)
  61.         {
  62.         if (t->fixed_size)
  63.             fatal_error("Fixed size table full.");
  64.  
  65.         if (t->max_entries == 0)
  66.             {
  67.             t->max_entries = STARTING_TABLE_SIZE;
  68.             t->table = wtmalloc(t->max_entries * t->entry_size);
  69.             }
  70.         else
  71.             {
  72.             t->max_entries = (t->current_entries * 3) / 2;
  73.             t->table = wtrealloc(t->table, t->max_entries * t->entry_size);
  74.             }
  75.         }
  76.  
  77.     memcpy((char *) t->table + t->entry_size * t->current_entries, entry, t->entry_size);
  78.  
  79.     return t->current_entries++;
  80. }
  81.  
  82.