home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / UE311C.ZIP / FILEIO.C < prev    next >
C/C++ Source or Header  |  1991-08-18  |  6KB  |  264 lines

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