home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / libs / gle / util / manip / cell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-11-29  |  7.2 KB  |  333 lines

  1. #include "all.h"
  2. #include "edt.h"
  3. #ifdef __TURBOC__
  4. #define DASHCHAR 249
  5. #include <alloc.h>
  6. #include "bios.h"
  7. #include "conio.h"
  8. #include "dir.h"
  9. #else
  10. #define far
  11. #define huge
  12. #define farcalloc calloc
  13. #define DASHCHAR '.'
  14. #include "vaxconio.h"
  15. #endif
  16.  
  17. #define true (!false)
  18. #define false 0
  19. extern int changed;
  20. extern int data_x;
  21. extern int data_y;
  22. extern int max_x,max_y;
  23. int usefloat;
  24.  
  25. #ifdef __TURBOC__
  26. double MISS=1.7e300;
  27. #else
  28. double MISS=1e10;
  29. #endif
  30. char huge **d_strings;
  31. double huge *d_numbers;
  32. float huge *f_numbers;
  33. #define cellindex(x,y) (((x)-1)+((y)-1)* ((long) data_x))
  34. #define oldindex(x,y) (((x)-1)+((y)-1)* ((long) odx))
  35. extern char strmiss[];
  36.  
  37. set_usefloat()
  38. {
  39.     usefloat = true;
  40. #ifdef __TURBOC__
  41.     MISS = 3e38;
  42. #else
  43.     MISS = 3.3e10;
  44. #endif
  45. }
  46. data_expand(int x, int y)
  47. {
  48.     int  datx,daty,mx,my;
  49.     /*fner("Expand %d %d,  max %d %d, data %d %d ",x,y,max_x,max_y
  50.         ,data_x,data_y); scr_getch();*/
  51.     mx = max_x; my = max_y;
  52.     datx = data_x; daty = data_y;
  53.     if (x>mx) mx = x;
  54.     if (y>my) my = y;
  55.     if (mx>my) {
  56.         datx = mx * 2;
  57.         daty = my;
  58.     } else {
  59.         datx = mx;
  60.         daty = my * 2;
  61.     }
  62.     xdata_setsize(datx,daty,mx,my);
  63. }
  64. clear_data()
  65. {
  66.     long i,j,k;
  67.     if (usefloat) {
  68.       if (f_numbers!=NULL) farfree(f_numbers);
  69.       f_numbers = NULL;
  70.     } else {
  71.       for (i=1;i<=max_x;i++) for (j=1;j<=max_y;j++) {
  72.         k = cellindex(i,j);
  73.         if (d_strings[k]!=NULL) farfree(d_strings[k]);
  74.       }
  75.       if (d_numbers!=NULL) farfree(d_numbers);
  76.       if (d_strings!=NULL) farfree(d_strings);
  77.       d_numbers = NULL; d_strings = NULL;
  78.     }
  79.     max_x = 0;
  80.     max_y = 0;
  81.     data_x = 0;
  82.     data_y = 0;
  83.     refresh();
  84. }
  85. set_size(int x, int y)
  86. {
  87.     long i,j,k;
  88.     for (i=1;i<=max_x;i++) for (j=1;j<=max_y;j++) {
  89.       if (i>x || j>y) {
  90.         k = cellindex(i,j);
  91.         if (usefloat) f_numbers[k] = MISS;
  92.         else {
  93.             d_numbers[k] = MISS;
  94.             if (d_strings[k]!=NULL) {farfree(d_strings[k]); d_strings[k]=NULL;}
  95.         }
  96.       }
  97.     }
  98.     data_setsize(x,y);
  99.     max_x = x;
  100.     max_y = y;
  101.     refresh();
  102. }
  103. data_setsize(int x, int y)
  104. {
  105.     xdata_setsize(x,y,max_x,max_y);
  106. }
  107. xdata_setsize(int x, int y,int mx,int my)
  108. {
  109.     /* x,y = new size of array,  mx, my = new used part of array */
  110.     char huge **ds;
  111.     int  ox,oy,odx;
  112.     long k;
  113.     long i,j;
  114.     double huge *dn;
  115.     float huge *fn;
  116.     if (x<=data_x && y<=data_y) return;
  117.     ox = max_x; oy = max_y; odx = data_x;
  118.     max_x = mx;
  119.     max_y = my;
  120.     data_x = x;
  121.     data_y = y;
  122.     if (usefloat) {
  123.       fn = farcalloc(((long) data_x)*data_y, sizeof(float));
  124.       if (fn==NULL) {fner("memory %d %d %ld\n",data_x,data_y,((long) data_x)*data_y);}
  125.       if (fn==NULL) gle_abort("Unable to allocate more storage\n");
  126.       for (k=0;k<data_x*data_y;k++) fn[k] = MISS;
  127.       if (f_numbers!=NULL) {
  128.         for (i=1;i<=ox;i++) for (j=1;j<=oy;j++) {
  129.             fn[cellindex(i,j)] = f_numbers[oldindex(i,j)];
  130.         }
  131.         farfree(f_numbers);
  132.       }
  133.       f_numbers = fn;
  134.       top_line();
  135.       return;
  136.     }
  137.     /*fner("expand old %d %d, odx=(%d)  new %d %d \n"
  138.         ,ox,oy,odx,data_x,data_y); scr_getch();*/
  139.     dn = farcalloc(((long) data_x)*data_y, sizeof(double));
  140.     ds = farcalloc(((long) data_x)*data_y, sizeof(char *));
  141. /* printf("New dimensions %d %d  %d %d  \n",data_x,data_y,max_x,max_y);*/
  142.     if (dn==NULL || ds==NULL) gle_abort("Unable to allocate more storage\n");
  143.     for (k=0;k<data_x*data_y;k++) dn[k] = MISS;
  144.     if (d_numbers!=NULL) {
  145.         for (i=1;i<=ox;i++) for (j=1;j<=oy;j++) {
  146.             /*fner("copy i=%ld j=%ld   (%ld)  <-- (%ld) \n"
  147.                 ,i,j,cellindex(i,j),oldindex(i,j)); scr_getch();*/
  148.             dn[cellindex(i,j)] = d_numbers[oldindex(i,j)];
  149.             ds[cellindex(i,j)] = d_strings[oldindex(i,j)];
  150.         }
  151.         farfree(d_numbers); farfree(d_strings);
  152.     }
  153.     d_numbers = dn;
  154.     d_strings = ds;
  155.     top_line();
  156. }
  157. set_scell(int x, int y, char *s)
  158. {
  159.     long j;
  160.     if (x>data_x || y>data_y) data_expand(x,y);
  161.     if (x>max_x) max_x = x;
  162.     if (y>max_y) max_y = y;
  163.     j = cellindex(x,y);
  164.     /* fner("scell %d %d %ld \n",x,y,j); scr_getch(); */
  165.     if (usefloat) {f_numbers[j] = 0; return;}
  166.     if (d_strings[j]!=NULL) free(d_strings[j]);
  167.     d_strings[j] = (char *) strdup(s);
  168.     d_numbers[j] = 0;
  169.     changed = true;
  170. }
  171. set_cell(int x, int y, double d)
  172. {
  173.     char *s;
  174.     if (x>data_x || y>data_y) data_expand(x,y);
  175.     if (x>max_x) max_x = x;
  176.     if (y>max_y) max_y = y;
  177.     if (usefloat) {f_numbers[cellindex(x,y)] = d; return;}
  178.     d_numbers[cellindex(x,y)] = d;
  179.     s = d_strings[cellindex(x,y)];
  180.     if (s!=NULL) {free(s); d_strings[cellindex(x,y)] = NULL;}
  181.     changed = true;
  182. }
  183. cell_greater(int x1,int y1, int x2, int y2)
  184. {
  185.     long j1,j2;
  186.     if (x1>max_x || y1>max_y) return false;
  187.     if (x2>max_x || y2>max_y) return true;
  188.     j1 = cellindex(x1,y1);
  189.     j2 = cellindex(x2,y2);
  190.     if (!usefloat) {
  191.     if (d_strings[j1]!=NULL && d_strings[j2]!=NULL) {
  192.         if (strcmp(d_strings[j1],d_strings[j2])>0)
  193.             return true;
  194.         else
  195.             return false;
  196.     }
  197.     }
  198.     if (vcell(x1,y1)>vcell(x2,y2)) return true;
  199.     return false;
  200. }
  201. char *scell(int x, int y)
  202. {
  203.     long j;
  204.     static char buff[80];
  205.     if (x>max_x || y>max_y) return strmiss;
  206.     j = cellindex(x,y);
  207.     if (usefloat) {
  208.         if (f_numbers[j]>=MISS) return strmiss;
  209.         sprintf(buff,"%g",f_numbers[j]);
  210.         return buff;
  211.     }
  212.     if (d_strings[j]!=NULL) {
  213.         return d_strings[j];
  214.     }
  215.     if (d_numbers[j]>=MISS) return strmiss;
  216.     sprintf(buff,"%g",d_numbers[j]);
  217.     return buff;
  218. }
  219. double vcell(int x, int y)
  220. {
  221.     long j;
  222.     if (x>max_x || y>max_y) return 0;
  223.     j = cellindex(x,y);
  224.     if (usefloat) {
  225.         if (f_numbers[j]>=MISS) return 0;
  226.         return f_numbers[j];
  227.     }
  228.     if (d_strings[j]!=NULL) return 0;
  229.     if (d_numbers[j]>=MISS) return 0;
  230.     return d_numbers[j];
  231. }
  232.  
  233. get_cell(int x, int y, double *v)
  234. {
  235.     long j;
  236.     *v = 0;
  237.     if (x>max_x || y>max_y) return;
  238.     j = cellindex(x,y);
  239.     if (usefloat) {
  240.         if (f_numbers[j]>=MISS) return ;
  241.         *v =  f_numbers[j];
  242.         return;
  243.     }
  244.     if (d_strings[j]!=NULL) return;
  245.     if (d_numbers[j]>=MISS) return;
  246.     *v = d_numbers[j];
  247. }
  248.  
  249. copy_cell(int x, int y, int x2, int y2)
  250. {
  251.     long j;
  252.     if (x>max_x || y>max_y) {set_cell(x2,y2,MISS); return;}
  253.     j = cellindex(x,y);
  254.     if (usefloat) {set_cell(x2,y2,f_numbers[j]); return;}
  255.     if (d_strings[j]!=NULL) {set_scell(x2,y2,d_strings[j]); return;}
  256.     set_cell(x2,y2,d_numbers[j]);
  257. }
  258.  
  259. swap_cell(int x, int y, int x2, int y2)
  260. {
  261.     char *s;
  262.     double v;
  263.     long j,j2;
  264.     if (x>data_x || y>data_y) data_expand(x,y);
  265.     if (x2>data_x || y2>data_y) data_expand(x2,y2);
  266.     if (x>max_x) max_x = x;
  267.     if (y>max_y) max_y = y;
  268.     if (x2>max_x) max_x = x2;
  269.     if (y2>max_y) max_y = y2;
  270.  
  271.     j = cellindex(x,y);
  272.     j2 = cellindex(x2,y2);
  273.     if (usefloat) {
  274.         v = f_numbers[j];
  275.         f_numbers[j] = f_numbers[j2];
  276.         f_numbers[j2] = v;
  277.         return;
  278.     }
  279.     v = d_numbers[j];
  280.     s = d_strings[j];
  281.     d_numbers[j] = d_numbers[j2];
  282.     d_strings[j] = d_strings[j2];
  283.     d_numbers[j2] = v;
  284.     d_strings[j2] = s;
  285. }
  286. clear_cell(int x, int y)
  287. {
  288.     long j;
  289.     if (x>max_x || y>max_y) {return;}
  290.     j = cellindex(x,y);
  291.     if (usefloat) {f_numbers[j] = MISS; return;}
  292.     if (d_strings[j]!=NULL) {free(d_strings[j]); d_strings[j] = NULL;}
  293.     d_numbers[j] = MISS;
  294. }
  295. get_cellboth(int x, int y, double *v, char **s)
  296. {
  297.     long j;
  298.     *s = NULL;
  299.     *v = 0;
  300.     if (x>max_x || y>max_y) {*s = strmiss; return;}
  301.     j = cellindex(x,y);
  302.     if (usefloat) {
  303.         if (f_numbers[j]>=MISS) {*s = strmiss; return;}
  304.         *v = f_numbers[j];
  305.         *s = NULL;
  306.         return;
  307.     }
  308.     if (d_strings[j]!=NULL) {*s = d_strings[j]; return;}
  309.     if (d_numbers[j]>=MISS) {*s = strmiss; return;}
  310.     *v = d_numbers[j];
  311. }
  312. trim_data()
  313. {
  314.     long i,j,k,mx,my;
  315.     mx = my = 0;
  316.     for (i=1;i<=max_x;i++) for (j=1;j<=max_y;j++) {
  317.        k = cellindex(i,j);
  318.       if (usefloat) {
  319.         if (f_numbers[k]!=MISS) {
  320.             if (i>mx) mx = i;
  321.             if (j>my) my = j;
  322.         }
  323.       } else {
  324.         if (d_numbers[k]!=MISS) {
  325.             if (i>mx) mx = i;
  326.             if (j>my) my = j;
  327.         }
  328.       }
  329.     }
  330.     max_x = mx;
  331.     max_y = my;
  332. }
  333.