home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 498a.lha / SC_v6.7 / pvmtbl.c < prev    next >
C/C++ Source or Header  |  1991-04-08  |  5KB  |  199 lines

  1. #define PSC
  2. #ifdef PSC
  3. # include <stdio.h>
  4. # include "sc.h"
  5. # ifndef FALSE
  6. #  define    FALSE    0
  7. #  define    TRUE    1
  8. # endif /* !FALSE */
  9. # undef    error
  10. # define error(msg)    fprintf(stderr, msg);
  11. #else /* PSC */
  12. # include <curses.h>
  13. # include "sc.h"
  14. #endif /* PSC */
  15.  
  16. extern    char    *malloc();
  17. extern    char    *realloc();
  18.  
  19. #if defined(BSD42) || defined(BSD43)
  20. #define    memcpy(dest, source, len)    bcopy(source, dest, (unsigned int)len);
  21. #define    memset(dest, zero, len)        bzero((dest), (unsigned int)(len));
  22. #endif
  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.  
  60. #define GROWALLOC(newptr, oldptr, nelem, type, msg) \
  61.     if (oldptr == (type *)NULL) \
  62.         newptr = (type *)malloc((unsigned)(nelem*sizeof(type))); \
  63.     else \
  64.         newptr = (type *)realloc((char *)oldptr, \
  65.                      (unsigned)(nelem*sizeof(type))); \
  66.     if (newptr == (type *)NULL) \
  67.     {   error(msg); \
  68.         return(FALSE); \
  69.     } \
  70.     oldptr = newptr /* wait incase we can't alloc */
  71.  
  72. static    char    nolonger[] = "The table can't be any longer";
  73. static    char    nowider[] = "The table can't be any wider";
  74.  
  75. /*
  76.  * grow the main && auxiliary tables (reset maxrows/maxcols as needed)
  77.  * toprow &&/|| topcol tell us a better guess of how big to become.
  78.  * we return TRUE if we could grow, FALSE if not....
  79.  */
  80. int
  81. growtbl(rowcol, toprow, topcol)
  82. int    rowcol;
  83. int    toprow, topcol;
  84. {
  85.     struct ent ***tbl2;
  86.     int    *fwidth2;
  87.     int    *precision2;
  88.     char    *col_hidden2;
  89.     char    *row_hidden2;
  90.     int    newrows, newcols;
  91.     int    i;
  92.  
  93. #ifndef PSC
  94.     newrows = maxrows;
  95. #endif /* !PSC */
  96.  
  97.     newcols = maxcols;
  98.     if (rowcol == GROWNEW)
  99.     {
  100. #ifndef PSC
  101.         maxrows = toprow = 0;
  102.         /* when we first start up, fill the screen w/ cells */
  103.         {    int startval;
  104.             startval = LINES - RESROW;
  105.             newrows = startval > MINROWS ? startval : MINROWS;
  106.             startval = ((COLS) - RESCOL) / DEFWIDTH;
  107.             newcols = startval > MINCOLS ? startval : MINCOLS;
  108.         }
  109. #else
  110.         newcols = MINCOLS;
  111. #endif /* !PSC */
  112.         maxcols = topcol = 0;
  113.     }
  114. #ifndef PSC
  115.     /* set how much to grow */
  116.     if ((rowcol == GROWROW) || (rowcol == GROWBOTH))
  117.     {    if (toprow > maxrows)
  118.             newrows = GROWAMT + toprow;
  119.         else
  120.             newrows += GROWAMT;
  121.     }
  122. #endif /* !PSC */
  123.     if ((rowcol == GROWCOL) || (rowcol == GROWBOTH))
  124.     {    if ((rowcol == GROWCOL) && ((maxcols == ABSMAXCOLS) ||
  125.                     (topcol >= ABSMAXCOLS)))
  126.         {    error(nowider);
  127.             return(FALSE);
  128.         }
  129.  
  130.         if (topcol > maxcols)
  131.             newcols = GROWAMT + topcol;
  132.         else
  133.             newcols += GROWAMT;
  134.  
  135.         if (newcols > ABSMAXCOLS)
  136.             newcols = ABSMAXCOLS;
  137.     }
  138.  
  139. #ifndef PSC
  140.     if ((rowcol == GROWROW) || (rowcol == GROWBOTH) || (rowcol == GROWNEW))
  141.     {
  142.         GROWALLOC(row_hidden2, row_hidden, newrows, char, nolonger);
  143.         memset(row_hidden+maxrows, 0, (newrows-maxrows)*sizeof(char));
  144.  
  145.         /* alloc tbl row pointers */
  146.         GROWALLOC(tbl2, tbl, newrows, struct ent **, nolonger);
  147.         memset(tbl+maxrows, 0, (newrows-maxrows)*(sizeof(struct ent **)));
  148.     }
  149. #endif /* !PSC */
  150.  
  151.     if ((rowcol == GROWCOL) || (rowcol == GROWBOTH) || (rowcol == GROWNEW))
  152.     {
  153.         GROWALLOC(fwidth2, fwidth, newcols, int, nowider);
  154.         GROWALLOC(precision2, precision, newcols, int, nowider);
  155. #ifdef PSC
  156.         memset(fwidth+maxcols, 0, (newcols-maxcols)*sizeof(int));
  157.         memset(precision+maxcols, 0, (newcols-maxcols)*sizeof(int));
  158.     }
  159. #else
  160.         GROWALLOC(col_hidden2, col_hidden, newcols, char, nowider);
  161.         memset(col_hidden+maxcols, 0, (newcols-maxcols)*sizeof(char));
  162.         for (i = maxcols; i < newcols; i++) {
  163.             fwidth[i] = DEFWIDTH;
  164.             precision[i] = DEFPREC;
  165.         }
  166.  
  167.         /* [re]alloc the space for each row */
  168.         for (i = 0; i < maxrows; i++)
  169.         {
  170.             if ((tbl[i] = (struct ent **)realloc((char *)tbl[i],
  171.             (unsigned)(newcols * sizeof(struct ent **)))) == (struct ent **)0)
  172.             {    error(nowider);
  173.                 return(FALSE);
  174.             }
  175.             memset((char *)ATBL(tbl,i, maxcols), 0,
  176.                (newcols-maxcols)*sizeof(struct ent **));
  177.         }
  178.     }
  179.     else
  180.         i = maxrows;
  181.  
  182.     /* fill in the bottom of the table */
  183.     for (; i < newrows; i++)
  184.     {    if ((tbl[i] = (struct ent **)malloc((unsigned)(newcols *
  185.                 sizeof(struct ent **)))) == (struct ent **)0)
  186.         {    error(nowider);
  187.             return(FALSE);
  188.         }
  189.         memset((char *)tbl[i], 0, newcols*sizeof(struct ent **));
  190.     }
  191.  
  192.     FullUpdate++;
  193.     maxrows = newrows;
  194. #endif /* PSC */
  195.  
  196.     maxcols = newcols;
  197.     return(TRUE);
  198. }
  199.