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