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