home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Product / Product.zip / sc621_3.zip / src / vmtbl.c < prev    next >
C/C++ Source or Header  |  1992-05-11  |  5KB  |  215 lines

  1. /*    SC    A Spreadsheet Calculator
  2.  *        Spreadsheet 'tbl' creation
  3.  *
  4.  *        original by James Gosling, September 1982
  5.  *        modifications by Mark Weiser and Bruce Israel,
  6.  *            University of Maryland
  7.  *
  8.  *              More mods Robert Bond, 12/86
  9.  *        More mods by Alan Silverstein, 3-4/88, see list of changes.
  10.  *        Currently supported by sequent!sawmill!buhrt (Jeff Buhrt)
  11.  *        $Revision: 6.21 $
  12.  *
  13.  */
  14. #ifdef PSC
  15. # include <stdio.h>
  16. #else /* PSC */
  17. # include <curses.h>
  18. #endif /* PSC */
  19.  
  20. #include "sc.h"
  21.  
  22. /*
  23.  * check to see if *rowp && *colp are currently allocated, if not expand the
  24.  * current size if we can.
  25.  */
  26. #ifndef PSC
  27. void
  28. checkbounds(rowp, colp)
  29. int    *rowp;
  30. int    *colp;
  31. {
  32.     if (*rowp < 0)
  33.         *rowp = 0;
  34.     else if (*rowp >= maxrows)
  35.     {    if (*colp >= maxcols)
  36.         {    if (!growtbl(GROWBOTH, *rowp, *colp))
  37.             {    *rowp = maxrows -1;
  38.                 *colp = maxcols -1;
  39.             }
  40.             return;
  41.         }
  42.         else
  43.         {    if (!growtbl(GROWROW, *rowp, 0))
  44.                 *rowp = maxrows-1;
  45.             return;
  46.         }
  47.     }
  48.     if (*colp < 0) 
  49.         *colp = 0;
  50.     else if (*colp >= maxcols)
  51.     {    if (!growtbl(GROWCOL, 0, *colp))
  52.             *colp = maxcols-1;
  53.     }
  54. }
  55. #endif /* !PSC */
  56.     
  57. /* scxrealloc will just scxmalloc if oldptr is == NULL */
  58. #define GROWALLOC(newptr, oldptr, nelem, type, msg) \
  59.     newptr = (type *)scxrealloc((char *)oldptr, \
  60.                      (unsigned)(nelem*sizeof(type))); \
  61.     if (newptr == (type *)NULL) \
  62.     {   error(msg); \
  63.         return(FALSE); \
  64.     } \
  65.     oldptr = newptr /* wait incase we can't alloc */
  66.  
  67. static    char    nolonger[] = "The table can't be any longer";
  68. static    char    nowider[] = "The table can't be any wider";
  69.  
  70. /*
  71.  * grow the main && auxiliary tables (reset maxrows/maxcols as needed)
  72.  * toprow &&/|| topcol tell us a better guess of how big to become.
  73.  * we return TRUE if we could grow, FALSE if not....
  74.  */
  75. int
  76. growtbl(rowcol, toprow, topcol)
  77. int    rowcol;
  78. int    toprow, topcol;
  79. {
  80.     struct ent ***tbl2;
  81.     struct ent ** nullit;
  82.     int    cnt;
  83.     int    *fwidth2;
  84.     int    *precision2;
  85.     int     *realfmt2;
  86.     char    *col_hidden2;
  87.     char    *row_hidden2;
  88.     int    newrows, newcols;
  89.     int    i;
  90.  
  91. #ifndef PSC
  92.     newrows = maxrows;
  93. #endif /* !PSC */
  94.  
  95.     newcols = maxcols;
  96.     if (rowcol == GROWNEW)
  97.     {
  98. #ifndef PSC
  99.         maxrows = toprow = 0;
  100.         /* when we first start up, fill the screen w/ cells */
  101.         {    int startval;
  102.             startval = LINES - RESROW;
  103.             newrows = startval > MINROWS ? startval : MINROWS;
  104.             startval = ((COLS) - RESCOL) / DEFWIDTH;
  105.             newcols = startval > MINCOLS ? startval : MINCOLS;
  106.         }
  107. #else
  108.         newcols = MINCOLS;
  109. #endif /* !PSC */
  110.         maxcols = topcol = 0;
  111.     }
  112. #ifndef PSC
  113.     /* set how much to grow */
  114.     if ((rowcol == GROWROW) || (rowcol == GROWBOTH))
  115.     {    if (toprow > maxrows)
  116.             newrows = GROWAMT + toprow;
  117.         else
  118.             newrows += GROWAMT;
  119.     }
  120. #endif /* !PSC */
  121.     if ((rowcol == GROWCOL) || (rowcol == GROWBOTH))
  122.     {    if ((rowcol == GROWCOL) && ((maxcols == ABSMAXCOLS) ||
  123.                     (topcol >= ABSMAXCOLS)))
  124.         {    error(nowider);
  125.             return(FALSE);
  126.         }
  127.  
  128.         if (topcol > maxcols)
  129.             newcols = GROWAMT + topcol;
  130.         else
  131.             newcols += GROWAMT;
  132.  
  133.         if (newcols > ABSMAXCOLS)
  134.             newcols = ABSMAXCOLS;
  135.     }
  136.  
  137. #ifndef PSC
  138.     if ((rowcol == GROWROW) || (rowcol == GROWBOTH) || (rowcol == GROWNEW))
  139.     {
  140.         struct ent *** lnullit;
  141.         int    lcnt;
  142.  
  143.         GROWALLOC(row_hidden2, row_hidden, newrows, char, nolonger);
  144.         memset(row_hidden+maxrows, 0, (newrows-maxrows)*sizeof(char));
  145.  
  146.         /*
  147.          * alloc tbl row pointers, per net.lang.c, calloc does not
  148.          * necessarily fill in NULL pointers
  149.          */
  150.         GROWALLOC(tbl2, tbl, newrows, struct ent **, nolonger);
  151.         for(lnullit = tbl+maxrows, lcnt = 0; lcnt < newrows-maxrows;
  152.                             lcnt++, lnullit++)
  153.             *lnullit = (struct ent **)NULL;
  154. /*        memset(tbl+maxrows, (char *)NULL, (newrows-maxrows)*(sizeof(struct ent **)));*/
  155.     }
  156. #endif /* !PSC */
  157.  
  158.     if ((rowcol == GROWCOL) || (rowcol == GROWBOTH) || (rowcol == GROWNEW))
  159.     {
  160.         GROWALLOC(fwidth2, fwidth, newcols, int, nowider);
  161.         GROWALLOC(precision2, precision, newcols, int, nowider);
  162.         GROWALLOC(realfmt2, realfmt, newcols, int, nowider);
  163. #ifdef PSC
  164.         memset(fwidth+maxcols, 0, (newcols-maxcols)*sizeof(int));
  165.         memset(precision+maxcols, 0, (newcols-maxcols)*sizeof(int));
  166.         memset(realfmt+maxcols, 0, (newcols-maxcols)*sizeof(int));
  167.     }
  168. #else
  169.         GROWALLOC(col_hidden2, col_hidden, newcols, char, nowider);
  170.         memset(col_hidden+maxcols, 0, (newcols-maxcols)*sizeof(char));
  171.         for (i = maxcols; i < newcols; i++) {
  172.             fwidth[i] = DEFWIDTH;
  173.             precision[i] = DEFPREC;
  174.             realfmt[i]= DEFREFMT;
  175.         }
  176.  
  177.         /* [re]alloc the space for each row */
  178.         for (i = 0; i < maxrows; i++)
  179.         {
  180.             if ((tbl[i] = (struct ent **)scxrealloc((char *)tbl[i],
  181.             (unsigned)(newcols * sizeof(struct ent **)))) == (struct ent **)0)
  182.             {    error(nowider);
  183.                 return(FALSE);
  184.             }
  185.         for(nullit = ATBL(tbl, i, maxcols), cnt = 0;
  186.                 cnt < newcols-maxcols; cnt++, nullit++)
  187.             *nullit = (struct ent *)NULL;
  188. /*        memset((char *)ATBL(tbl,i, maxcols), 0,
  189.                (newcols-maxcols)*sizeof(struct ent **));
  190. */               
  191.         }
  192.     }
  193.     else
  194.         i = maxrows;
  195.  
  196.     /* fill in the bottom of the table */
  197.     for (; i < newrows; i++)
  198.     {    if ((tbl[i] = (struct ent **)scxmalloc((unsigned)(newcols *
  199.                 sizeof(struct ent **)))) == (struct ent **)0)
  200.         {    error(nowider);
  201.             return(FALSE);
  202.         }
  203.         for(nullit = tbl[i], cnt = 0; cnt < newcols; cnt++, nullit++)
  204.             *nullit = (struct ent *)NULL;
  205. /*        memset((char *)tbl[i], 0, newcols*sizeof(struct ent **));*/
  206.     }
  207.  
  208.     FullUpdate++;
  209.     maxrows = newrows;
  210. #endif /* PSC */
  211.  
  212.     maxcols = newcols;
  213.     return(TRUE);
  214. }
  215.