home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 100-199 / ff119.lzh / MicroEMACS / src / src.zoo / fileio.c < prev    next >
C/C++ Source or Header  |  1987-12-09  |  5KB  |  231 lines

  1. /*
  2.  * The routines in this file read and write ASCII files from the disk. All of
  3.  * the knowledge about files are here.
  4.  */
  5.  
  6. #include        <stdio.h>
  7. #include    "estruct.h"
  8. #include        "edef.h"
  9.  
  10. FILE    *ffp;        /* File pointer, all functions. */
  11. int eofflag;        /* end-of-file flag */
  12.  
  13. /*
  14.  * Open a file for reading.
  15.  */
  16. ffropen(fn)
  17. char    *fn;
  18. {
  19.         if ((ffp=fopen(fn, "r")) == NULL)
  20.                 return (FIOFNF);
  21.     eofflag = FALSE;
  22.         return (FIOSUC);
  23. }
  24.  
  25. /*
  26.  * Open a file for writing. Return TRUE if all is well, and FALSE on error
  27.  * (cannot create).
  28.  */
  29. ffwopen(fn)
  30. char    *fn;
  31. {
  32. #if     VMS
  33.         register int    fd;
  34.  
  35.         if ((fd=creat(fn, 0666, "rfm=var", "rat=cr")) < 0
  36.         || (ffp=fdopen(fd, "w")) == NULL) {
  37. #else
  38.         if ((ffp=fopen(fn, "w")) == NULL) {
  39. #endif
  40.                 mlwrite("Cannot open file for writing");
  41.                 return (FIOERR);
  42.         }
  43.         return (FIOSUC);
  44. }
  45.  
  46. /*
  47.  * Close a file. Should look at the status in all systems.
  48.  */
  49. ffclose()
  50. {
  51.     /* free this since we do not need it anymore */
  52.     if (fline) {
  53.         free(fline);
  54.         fline = NULL;
  55.     }
  56.  
  57. #if    MSDOS & CTRLZ
  58.     fputc(26, ffp);        /* add a ^Z at the end of the file */
  59. #endif
  60.     
  61. #if     V7 | USG | BSD | (MSDOS & (LATTICE | MSC | TURBO)) | (ST520 & MWC)
  62.         if (fclose(ffp) != FALSE) {
  63.                 mlwrite("Error closing file");
  64.                 return(FIOERR);
  65.         }
  66.         return(FIOSUC);
  67. #else
  68.         fclose(ffp);
  69.         return (FIOSUC);
  70. #endif
  71. }
  72.  
  73. /*
  74.  * Write a line to the already opened file. The "buf" points to the buffer,
  75.  * and the "nbuf" is its length, less the free newline. Return the status.
  76.  * Check only at the newline.
  77.  */
  78. ffputline(buf, nbuf)
  79. char    buf[];
  80. {
  81.         register int    i;
  82. #if    CRYPT
  83.     char c;        /* character to translate */
  84.  
  85.     if (cryptflag) {
  86.             for (i = 0; i < nbuf; ++i) {
  87.             c = buf[i] & 0xff;
  88.             crypt(&c, 1);
  89.             fputc(c, ffp);
  90.         }
  91.     } else
  92.             for (i = 0; i < nbuf; ++i)
  93.                     fputc(buf[i]&0xFF, ffp);
  94. #else
  95.         for (i = 0; i < nbuf; ++i)
  96.                 fputc(buf[i]&0xFF, ffp);
  97. #endif
  98.  
  99. #if    ST520 & ADDCR
  100.         fputc('\r', ffp);
  101. #endif        
  102.         fputc('\n', ffp);
  103.  
  104.         if (ferror(ffp)) {
  105.                 mlwrite("Write I/O error");
  106.                 return (FIOERR);
  107.         }
  108.  
  109.         return (FIOSUC);
  110. }
  111.  
  112. /*
  113.  * Read a line from a file, and store the bytes in the supplied buffer. The
  114.  * "nbuf" is the length of the buffer. Complain about long lines and lines
  115.  * at the end of the file that don't have a newline present. Check for I/O
  116.  * errors too. Return status.
  117.  */
  118. ffgetline()
  119.  
  120. {
  121.         register int c;        /* current character read */
  122.         register int i;        /* current index into fline */
  123.     register char *tmpline;    /* temp storage for expanding line */
  124.  
  125.     /* if we are at the end...return it */
  126.     if (eofflag)
  127.         return(FIOEOF);
  128.  
  129.     /* dump fline if it ended up too big */
  130.     if (flen > NSTRING) {
  131.         free(fline);
  132.         fline = NULL;
  133.     }
  134.  
  135.     /* if we don't have an fline, allocate one */
  136.     if (fline == NULL)
  137.         if ((fline = malloc(flen = NSTRING)) == NULL)
  138.             return(FIOMEM);
  139.  
  140.     /* read the line in */
  141.         i = 0;
  142.         while ((c = fgetc(ffp)) != EOF && c != '\n') {
  143.                 fline[i++] = c;
  144.         /* if it's longer, get more room */
  145.                 if (i >= flen) {
  146.                     if ((tmpline = malloc(flen+NSTRING)) == NULL)
  147.                         return(FIOMEM);
  148.                     strncpy(tmpline, fline, flen);
  149.                     flen += NSTRING;
  150.                     free(fline);
  151.                     fline = tmpline;
  152.                 }
  153.         }
  154.  
  155. #if    ST520
  156.     if(fline[i-1] == '\r')
  157.         i--;
  158. #endif
  159.  
  160.     /* test for any errors that may have occured */
  161.         if (c == EOF) {
  162.                 if (ferror(ffp)) {
  163.                         mlwrite("File read error");
  164.                         return(FIOERR);
  165.                 }
  166.  
  167.                 if (i != 0)
  168.             eofflag = TRUE;
  169.         else
  170.             return(FIOEOF);
  171.         }
  172.  
  173.     /* terminate and decrypt the string */
  174.         fline[i] = 0;
  175. #if    CRYPT
  176.     if (cryptflag)
  177.         crypt(fline, strlen(fline));
  178. #endif
  179.         return(FIOSUC);
  180. }
  181.  
  182. int fexist(fname)    /* does <fname> exist on disk? */
  183.  
  184. char *fname;        /* file to check for existance */
  185.  
  186. {
  187.     FILE *fp;
  188.  
  189.     /* try to open the file for reading */
  190.     fp = fopen(fname, "r");
  191.  
  192.     /* if it fails, just return false! */
  193.     if (fp == NULL)
  194.         return(FALSE);
  195.  
  196.     /* otherwise, close it and report true */
  197.     fclose(fp);
  198.     return(TRUE);
  199. }
  200.  
  201. #if    AZTEC & MSDOS
  202. #undef    fgetc
  203. /*    a1getc:        Get an ascii char from the file input stream
  204.             but DO NOT strip the high bit
  205. */
  206.  
  207. int a1getc(fp)
  208.  
  209. FILE *fp;
  210.  
  211. {
  212.     int c;        /* translated character */
  213.  
  214.     c = getc(fp);    /* get the character */
  215.  
  216.     /* if its a <LF> char, throw it out  */
  217.     while (c == 10)
  218.         c = getc(fp);
  219.  
  220.     /* if its a <RETURN> char, change it to a LF */
  221.     if (c == '\r')
  222.         c = '\n';
  223.  
  224.     /* if its a ^Z, its an EOF */
  225.     if (c == 26)
  226.         c = EOF;
  227.  
  228.     return(c);
  229. }
  230. #endif
  231.