home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / mutt / me2s_pl7.zoo / mu_edit2 / util / dtable.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-05  |  4.5 KB  |  138 lines

  1. static char rcsid[] = "$Id: dtable.c,v 1.1 1992/09/06 19:31:32 mike Exp $";
  2.  
  3. /* $Log: dtable.c,v $
  4.  * Revision 1.1  1992/09/06  19:31:32  mike
  5.  * Initial revision
  6.  *
  7.  */
  8.  
  9. /* dtable.c : dynamic tables
  10.  * xpand_dTable:  Make sure a dTable can hold n more items.
  11.  *   Input:
  12.  *     dtable:         A pointer to a dTable.
  13.  *     n:         Number of items that will be added to the table after
  14.  *               this call.
  15.  *     initial_size: Number of cells to initialize table to.
  16.  *     step:         Grow table by multiples of step cells.
  17.  *   Output:
  18.  *     TRUE:  table has room for n items.
  19.  *     FALSE: Not enough memory - table is junk. !!! not in ansi C
  20.  *   How to use:
  21.  *     #include "dtable.h"
  22.  *     typedef struct { ... } Blob;
  23.  *     * IF dTable is global (or static):
  24.  *       declare_and_init_dTable(foobar,Blob);
  25.  *     * IF dTable is a typdef'ed global:
  26.  *       typedef declare_dTable_of(Blob) FooBar;
  27.  *       FooBar foobar = initial_dTable_data(foobar);
  28.  *     * IF dTable is automatic (local):
  29.  *       declare_dTable_of(Blob) foobar;
  30.  *         INIT_dTable(&foobar); or initialize_dTable(&foobar,sizeof(Blob));
  31.  *     xpand_dTable(&foobar, n, initial_size, step);
  32.  *     foobar.table[j] = a_Blob;    -- do this n times 
  33.  *   Notes:
  34.  *     Make sure the dTable is initialized!  See "How to use" above.
  35.  *     If you want to reuse the table: reset_dTable(dtable);
  36.  *     sizeof_dTable(dtable) is the number of items in the table (not the
  37.  *       max).
  38.  *     To release the dtable: free_dTable(dtable).  If you are going to
  39.  *       reuse the table header, do an INIT_dTable(dtable) after the
  40.  *       free.
  41.  *     extern declare_dTable_of(Blob) foobar; is legal.
  42.  *     typedef declare_dTable_of(Blob) foobar; is legal.
  43.  *     If initial_size is not big enough to hold the first n items, the
  44.  *       table is created n+step big.
  45.  *   Warning:
  46.  *     Table may be moved during xpand_dTable().  So don't expect
  47.  *       ptr = foo->table; xpand_dTable(foo,...) to work.  Always set ptr
  48.  *       after the xpand.
  49.  * Craig Durland 6/89
  50.  */
  51.  
  52. /* Copyright 1989, 1990 Craig Durland
  53.  *   Distributed under the terms of the GNU General Public License.
  54.  *   Distributed "as is", without warranties of any kind, but comments,
  55.  *     suggestions and bug reports are welcome.
  56.  */
  57.  
  58. #include "const.h"
  59. #include "dtable.h"
  60.  
  61. void initialize_dTable(dtable,blob_size) dTable *dtable;
  62. {
  63.   dtable->max_items = dtable->num_items = 0;
  64.   dtable->blob_size = blob_size;
  65.   dtable->table = NULL;        /* for debugging purposes */
  66. }
  67.  
  68. void free_dTable(dtable) dTable *dtable;
  69.     { if (dtable->max_items) free((char *)dtable->table); }
  70.  
  71. extern char *malloc(), *realloc();
  72.  
  73. xpand_dTable(dtable, n, initial_size, step)  dTable *dtable;
  74. {
  75.   register int max_items, num_items, a, blob_size;
  76.   register char *ptr;
  77.  
  78.   max_items = dtable->max_items;
  79.   num_items = (dtable->num_items += n);
  80.  
  81.     /* check to see if already have enough room for n more items */
  82.   if (num_items <= max_items) return TRUE;
  83.   blob_size = dtable->blob_size;
  84.   if (max_items == 0)                /* table not allocated yet */
  85.   {
  86.     if ((a = initial_size) < num_items)    /* initial size ain't big enough */
  87.     a = num_items +step;
  88.     ptr = malloc(a*blob_size);
  89.   }
  90.   else                        /* table full, make bigger */
  91.   {
  92.     a = (num_items -max_items +step -1)/step;
  93.     a = max_items +a*step;
  94.     ptr = realloc(dtable->table,a*blob_size);
  95.     /*!!! in ANSI C, realloc may fail but table is still OK */
  96.   }
  97.  
  98.   dtable->max_items = a;
  99.   if ((dtable->table = ptr) == NULL)    /* out of memory => table is mush */
  100.     { initialize_dTable(dtable,blob_size); return FALSE; }
  101.  
  102.   return TRUE;
  103. }
  104.  
  105. #ifdef TEST
  106. /* ******************************************************************** */
  107. /* *************** TEST *********************************************** */
  108. /* ******************************************************************** */
  109.  
  110. typedef struct { char *name; int token; } Blob;
  111.  
  112. declare_and_init_dTable(foobar,Blob);
  113.  
  114. main()
  115. {
  116.   char zik[100], *name, *savestr();
  117.   int n, s, j;
  118.   
  119.   for (j = 0; 1; j++)
  120.   {
  121.     printf("name: ");  gets(zik);
  122. if (*zik=='q') break;
  123.     name = savestr(zik);
  124.     printf("token: "); gets(zik); n = atoi(zik);
  125.     s = xpand_dTable(&foobar, 1, 3, 2);
  126.     foobar.table[j].name = name;
  127.     foobar.table[j].token = n;
  128.     printf("%d | num_items = %d, max_items = %d: %d %s\n",
  129.     s, 
  130.     sizeof_dTable(&foobar),
  131.     foobar.max_items,
  132.     foobar.table[j].token,foobar.table[j].name);
  133.   }
  134.   for (n=0; n<sizeof_dTable(&foobar); n++) 
  135.     printf("%d: %d %s\n",n, foobar.table[n].token,foobar.table[n].name);
  136. }
  137. #endif
  138.