home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / EDITOR / MG2A_SRC.ZIP / SYS / ATARI / FILEIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-23  |  5.5 KB  |  242 lines

  1. /* fileio.c -- Atari ST file system interface for MG
  2.  *
  3.  * author :  Sandra Loosemore (from an earlier version by dec-rex!conroy)
  4.  * date   :  24 Oct 1987
  5.  * changes:  Marion Hakanson -- Jan 1988
  6.  *
  7.  */
  8. #include    "..\..\def.h"
  9.  
  10.  
  11. /* Adjustname regularizes a filename.  This fills in the current drive
  12.  *    and directory onto the front if they are not already provided, and
  13.  *    also lowercases it so a case-sensitive string compare can be used
  14.  *    on filenames.
  15.  * This doesn't do the right things with collapsing "..\" and ".\" -- it
  16.  *      doesn't check for them being embedded in pathnames, only at the
  17.  *    front.  Sigh.
  18.  */
  19.  
  20. static char adbuffer[128];
  21.  
  22. char* adjustname (fname)
  23.     char *fname;
  24. {   int i, j, k;
  25.     char pathbuf[128];
  26.  
  27.     if (fname[1] == ':')  {
  28.         j = 2;
  29.         adbuffer[0] = TOLOWER(fname[0]);
  30.     }
  31.     else  {
  32.         adbuffer[0] = (char)Dgetdrv() + 'a';
  33.         j = 0;
  34.         }
  35.     adbuffer[1] = ':';
  36.     i = 2;
  37.     if (fname[j] != '\\')  {
  38.         Dgetpath(pathbuf, (int)(adbuffer[0] - 'a' + 1));
  39.         for (k=0; pathbuf[k] != '\0'; k++)  {
  40.         adbuffer[i] = TOLOWER(pathbuf[k]);
  41.         i++;
  42.         }
  43.         adbuffer[i] = '\\';
  44.     i++;
  45.         }
  46.     if (fname[j] == '.')  {
  47.         if (fname[j+1] == '.')  {
  48.             i = i - 2;
  49.             while (adbuffer[i] != '\\')  i--;
  50.             j = j + 2;
  51.             }
  52.         else  {
  53.             j = j + 1;
  54.             i = i - 1;
  55.         }
  56.         }
  57.     while (fname[j] != '\0')  {
  58.         adbuffer[i] = TOLOWER(fname[j]);
  59.         i++;
  60.         j++;
  61.         }
  62.     adbuffer[i] = '\0';
  63.     return(adbuffer);
  64.     }
  65.  
  66.  
  67. /* Define some things used later on */
  68.  
  69. static int handle;
  70. #define RBUFSIZE 512
  71. static char rbuffer[RBUFSIZE];
  72. static int rbufmax, rbufnext, rbufcr;
  73. static int rbufeof;
  74.  
  75.  
  76. /* Open and close files.  Store the file handle in "handle".  We'll use 
  77.  *    it later in the routines that read and write to the file.
  78.  */
  79.  
  80.  
  81. ffropen (fn)
  82.     char *fn;
  83. {   handle = Fopen(fn, 0);
  84.     if (handle < 0)
  85.         return(FIOFNF);
  86.     else  {
  87.         rbufmax = 0;
  88.         rbufnext = 0;
  89.     rbufcr = FALSE;
  90.     rbufeof = FALSE;
  91.         return(FIOSUC);
  92.     }
  93.     }
  94.  
  95.  
  96. ffwopen (fn)
  97.     char *fn;
  98. {   (VOID) Fdelete(fn);
  99.     handle = Fcreate(fn, 0);
  100.     if (handle < 0)  {
  101.     ewprintf("Open of %s failed with error code %d.", fn, handle);
  102.     return (FIOERR);
  103.     }
  104.     else
  105.     return (FIOSUC);
  106.     }
  107.  
  108. ffclose ()
  109. {   (VOID) Fclose(handle);
  110.     return (FIOSUC);
  111.     }
  112.  
  113.  
  114. /* Write an entire buffer to the already open file.  The last line of 
  115.  *     the buffer does not have an implied newline.
  116.  */
  117.  
  118. static char *crlf = "\r\n";
  119.  
  120. ffputbuf (buf)
  121.     BUFFER *buf;
  122. {   LINE *line, *last;
  123.  
  124.     last = buf->b_linep;       /* Header line is empty */
  125.     line = last->l_fp;         /* The first real line */
  126.  
  127.     while (line->l_fp != last)  {
  128.         if ((Fwrite (handle, (long)line->l_used, line->l_text) < 0) ||
  129.         (Fwrite (handle, 2l, crlf) < 0))  {
  130.         ewprintf("File write error");
  131.         return (FIOERR);
  132.                 }
  133.         line = line->l_fp;
  134.         }
  135.     if (line->l_used > 0)  {
  136.         if (Fwrite (handle, (long)line->l_used, line->l_text) < 0)  {
  137.         ewprintf("File write error");
  138.         return (FIOERR);
  139.                 }
  140.         }
  141.     return(FIOSUC);
  142.     }
  143.  
  144.  
  145. /* Read a line from the file up to nbuf long.  Set *nbytes to the actual
  146.  *    count read (excluding the end-of-line marker).  Returns:
  147.  *        FIOSUC if all is well.
  148.  *        FIOLONG if no newline was found yet.
  149.  *        FIOEOF on end-of-file.
  150.  *        FIOERR if any random errors happened.
  151.  * The GemDos routine Fread does not break on end-of-line; it will read
  152.  *    however many characters you tell it to.  So we do a little buffering.
  153.  *    Remember that crlf is the line terminator.  Cr's and lf's that appear
  154.  *    by themselves are passed on.
  155.  */
  156.  
  157.  
  158. ffgetline (buf, nbuf, nbytes)
  159.     char buf[];
  160.     int nbuf;
  161.     int *nbytes;
  162. {   register int i;
  163.     register char ch;
  164.  
  165.     for (i=0; i<nbuf; i++)  {
  166.         if (rbufmax == rbufnext)  {
  167.         if (rbufeof || 
  168.               ((rbufmax = Fread(handle, (long)RBUFSIZE, rbuffer)) == 0))  {
  169.                 (*nbytes) = i;
  170.                 return (FIOEOF);
  171.                 }
  172.             else if (rbufmax < 0)  {
  173.              ewprintf("File read error");
  174.             return (FIOERR);
  175.                 }
  176.         else  {
  177.         if (rbufmax < RBUFSIZE)  rbufeof = TRUE;
  178.                 rbufnext = 0;
  179.         }
  180.             }
  181.         ch = rbuffer[rbufnext];
  182.         rbufnext++;
  183.         if (rbufcr && (ch == '\n'))  {
  184.         rbufcr = FALSE;
  185.         (*nbytes) = i-1;
  186.             return (FIOSUC);
  187.             }
  188.         else  {
  189.         buf[i] = ch;
  190.         rbufcr = (ch == '\r');
  191.             }
  192.     }
  193.     return (FIOLONG);
  194.     }
  195.  
  196.  
  197. #ifndef NO_BACKUP
  198.  
  199. /*
  200.  * Finish this routine when you decide
  201.  * what the right thing to do when renaming a
  202.  * file for backup purposes.
  203.  */
  204.  
  205. fbackupfile(fname)
  206. char    *fname;
  207. {
  208.     return (TRUE);
  209. }
  210.  
  211. #endif
  212.  
  213.  
  214.  
  215. #ifndef NO_STARTUP
  216.  
  217. /* Look for a startup file as MG.INI in the current directory, then for
  218.  *    the file specified by the environment variable MGINIT.
  219.  */
  220.  
  221. char* startupfile (ignore)
  222.     char *ignore;
  223. {   int handle;
  224.     char *foo;
  225.     extern char *getenv();
  226.  
  227.     if ((handle = Fopen ("MG.INI", 0)) >= 0)  {
  228.         (VOID) Fclose (handle);
  229.         return("MG.INI");
  230.         }
  231.     else if (((foo = getenv("MGINIT")) != NULL) &&
  232.              ((handle = Fopen (foo, 0)) >= 0))  {
  233.         (VOID) Fclose (handle);
  234.         return(foo);
  235.         }
  236.     else
  237.         return(NULL);
  238.     }
  239.  
  240. #endif
  241.  
  242.