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