home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / s1 / sss / !SSS / c / io < prev    next >
Text File  |  1991-07-30  |  5KB  |  212 lines

  1. /* input-output functions for SSS */
  2. /* Tue,30 Jul 1991 */
  3. /* Copyright C.T.Stretch 1991 */
  4.  
  5. #include "ssshdr.h"
  6.  
  7. BOOL io_save(char *fn,void *part)
  8. { int x,y,nc,nr;
  9.   int x0=0,x1=ncols,y0=0,y1=nrows;
  10.   entry *cb;
  11.   FILE *out=fopen(fn,"w");
  12.   if(!out) return FALSE;
  13.   if(part)
  14.   { x0=sx0;x1=sx1+1;y0=sy0;y1=sy1+1;
  15.   }
  16.   nc=x1-x0;nr=y1-y0;
  17.   fwrite(&nc,4,1,out);
  18.   fwrite(&nr,4,1,out);
  19.   fwrite(width+x0,4,nc,out);
  20.   for(y=y0;y<y1;y++) for(x=x0;x<x1;x++)
  21.   { cb=sheet[x+NCOLS*y];
  22.     if(cb)
  23.     { fputs(&(cb->c),out);putc('\n',out);
  24.       fwrite(cb,4,1,out);
  25.     }
  26.     else putc(0,out);
  27.   }
  28.   fclose(out);
  29.   sprintf(buf,"settype %s %X",fn,SSSTYPE);
  30.   system(buf);
  31.   strcpy(fname,fn);
  32.   return TRUE;
  33. }
  34.  
  35.  
  36. static void readentries(int nc,int nr,int xx,int y,FILE *file)
  37. { int x,c,f;
  38.   for(;nr>0;nr--,y++) for(x=xx;x<xx+nc;x++)
  39.   if((c=getc(file))!=0)
  40.   { ungetc(c,file);
  41.     fgets(buf,BUFLEN,file);
  42.     buf[strlen(buf)-1]=0;
  43.     fread(&f,4,1,file);
  44.     if((x<ncols)&&(y<nrows)) sheet_getentry(x,y,f);
  45.   }
  46. }
  47.  
  48. void io_merge()
  49. { FILE *file;
  50.   int nc,nr,w[NCOLS],x;
  51.   char *fn;
  52.   int ftype=xferrecv_checkinsert(&fn);
  53.   if(ftype!=SSSTYPE) return;
  54.   file=fopen(fn,"r");
  55.   if(!file) return;
  56.   fread(&nc,4,1,file);
  57.   fread(&nr,4,1,file);
  58.   if(nc>=NCOLS) nc=NCOLS;
  59.   fread(w,4,nc,file);
  60.   for(x=0;x<nc;x++)
  61.   { if(x+ex0>=ncols) break;
  62.     if(w[x]>width[ex0+x]) width[ex0+x]=w[x];
  63.   }
  64.   readentries(nc,nr,ex0,ey0,file);
  65.   xferrecv_insertfileok();
  66.   fclose(file);
  67.   if(autox) sheet_change();
  68. }
  69.  
  70.  
  71. void io_load(char* fn)
  72. { FILE *file;
  73.   int nr;
  74.   file=fopen(fn,"r");
  75.   if(!file) return;
  76.   fread(&ncols,4,1,file);
  77.   fread(&nr,4,1,file);
  78.   if(ncols>=NCOLS) werr(1,"Bad file");
  79.   sheet_clear(nr);
  80.   fread(width,4,ncols,file);
  81.   readentries(ncols,nr,0,0,file);
  82.   fclose(file);
  83.   strcpy(fname,fn);
  84.   if(autox) sheet_change();
  85. }
  86.  
  87.  
  88. static int form()
  89. { switch(buf[0])
  90.   { case '+':case '-':case '.':goto number;
  91.     default:if(isdigit(buf[0])) goto number;
  92.   }
  93.   return FSTRING;
  94.   number:if(strpbrk(buf,"eE")) return FEXP;
  95.   if(strchr(buf,'.')) return F2DP;
  96.   return FINT;
  97. }
  98.  
  99. static void readtext(char *fn)
  100. { FILE *file;
  101.   int c,nr,nc=10,x=0,y=0;
  102.   int p=0;
  103.   file=fopen(fn,"r");
  104.   if(!file) return;
  105.   for(nr=1;(c=fgetc(file))!=EOF;) if(c=='\n') nr++;
  106.   sheet_clear(nr);
  107.   rewind(file);
  108.   for(;;) switch(c=fgetc(file))
  109.   { case '\n':x=0;y++;
  110.     case ' ':case ',':case '\r':case '\t':
  111.              if(p)
  112.              { if((x<NCOLS)&&(y<nrows))
  113.                { buf[p]=0;
  114.                  sheet_getentry(x++,y,form());
  115.                if(x>nc) nc=x;
  116.                }
  117.                p=0;
  118.              }
  119.              break;
  120.      case EOF:goto eof;
  121.     default:buf[p++]=c;if(p>=BUFLEN) p=0;break;
  122.   }
  123.   eof:xferrecv_insertfileok();
  124.   fclose(file);
  125.   strcpy(fname,"Import");
  126.   ncols=nc;
  127.   if(autox) sheet_change();
  128. }
  129.  
  130. BOOL io_read(BOOL text)
  131. { FILE *file;
  132.   char *fn;
  133.   int nc,nr;
  134.   int ftype=xferrecv_checkinsert(&fn);
  135.   if(ftype==0xFFF) if(text) {readtext(fn);return TRUE;} else return FALSE;
  136.   if(ftype!=SSSTYPE) return FALSE;
  137.   file=fopen(fn,"r");
  138.   if(!file) return FALSE;
  139.   fread(&nc,4,1,file);
  140.   fread(&nr,4,1,file);
  141.   if(nc>=NCOLS) nc=NCOLS;
  142.   sheet_clear(nr);
  143.   ncols=nc;
  144.   fread(width,4,nc,file);
  145.   readentries(nc,nr,0,0,file);
  146.   xferrecv_insertfileok();
  147.   fclose(file);
  148.   strcpy(fname,fn);
  149.   if(autox) sheet_change();
  150.   return TRUE;
  151. }
  152.  
  153. static void prcell(FILE *file,int x,int y)
  154. { entry *ce=sheet[x+y*NCOLS];
  155.   int n=width[x]-1;
  156.   char num[32];
  157.   if(ce)
  158.   { if(ce->p) fprintf(file,"%-*.*s",n,n,"P???????");
  159.     else
  160.     { if(ce->a) fprintf(file,"%-*.*s",n,n,"A!!!!!!!");
  161.       else switch(ce->t)
  162.       { case FSTRING:
  163.         case FLONG:fprintf(file,"%-*.*s",n,n,&(ce->c));break;
  164.         case FINT:sprintf(num,"%.0f",ce->v);
  165.                   fprintf(file,"%-*.*s",n,n,num);break;
  166.         case F2DP:sprintf(num,"%.2f",ce->v);
  167.                   fprintf(file,"%-*.*s",n,n,num);break;
  168.         case F4DP:sprintf(num,"%.4f",ce->v);
  169.                   fprintf(file,"%-*.*s",n,n,num);break;
  170.         case FEXP:sprintf(num,"%.4e",ce->v);
  171.                   fprintf(file,"%-*.*s",n,n,num);break;
  172.       }
  173.     }
  174.   }
  175.   else fprintf(file,"%*.*s",n,n,"");
  176. }
  177.  
  178. BOOL io_pr(char *fn,void *v)
  179. { FILE *file;
  180.   int x,xx=0,y,pw,s,n=strlen(sep);
  181.   int x0=sx0;
  182.   BOOL rep;
  183.   v=v;
  184.   while(!width[x0]) x0++;
  185.   if(x0>sx1) return FALSE;
  186.   file=fopen(fn,"w");
  187.   if(!file) return FALSE;
  188.   rep=repfirst&x0<sx1;
  189.   pw=(rep)?prwidth-width[x0]-n+1:prwidth;
  190.   x=x0;if(rep)x++;
  191.   while(x<=sx1)
  192.   { for(y=sy0;y<=sy1;y++)
  193.     { xx=x;
  194.       if(rep){ prcell(file,x0,y);fputs(sep,file);}
  195.       while(!width[xx]) xx++;if(xx>sx1)break;
  196.       s=pw+width[xx]-1;prcell(file,xx++,y);
  197.       while(!width[xx]) xx++;
  198.       while(xx<=sx1&&s>width[xx]+n-1)
  199.       { fputs(sep,file);
  200.         s-=width[xx]+n-1;
  201.         prcell(file,xx++,y);
  202.         while(!width[xx]) xx++;
  203.       }
  204.       fputc('\n',file);
  205.     }
  206.     x=xx;
  207.     fputc('\n',file);
  208.   }
  209.   fclose(file);
  210.   return TRUE;
  211. }
  212.