home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / CMDS / memacs400_src.lzh / MEMACS400 / SRC / fileio.c < prev    next >
Text File  |  1996-04-25  |  6KB  |  299 lines

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