home *** CD-ROM | disk | FTP | other *** search
/ messroms.de / 2007-01-13_www.messroms.de.zip / VZ200 / TOOLS / ZCCSRC.ZIP / scc / io.c < prev    next >
C/C++ Source or Header  |  2000-03-01  |  2KB  |  182 lines

  1. /*    File io.c: 2.1 (83/03/20,16:02:07) */
  2. /*% cc -O -c %
  3.  *
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include "defs.h"
  8. #include "data.h"
  9. #include "proto.h"
  10.  
  11. /*
  12.  *    open input file
  13.  */
  14. int openin(char *p)
  15. {
  16.     strcpy(fname, p);
  17.     fixname(fname);
  18.     if (!checkname(fname))
  19.         return (NO);
  20.     if ((input = fopen(fname, "r")) == NULL)
  21.     {
  22.         pl("Open failure\n");
  23.         return (NO);
  24.     }
  25.     kill();
  26.     return (YES);
  27. }
  28.  
  29. /*
  30.  *    open output file
  31.  */
  32. int openout(void)
  33. {
  34.     outfname(fname);
  35.     if ((output = fopen(fname, "w")) == NULL)
  36.     {
  37.         pl("Open failure");
  38.         return (NO);
  39.     }
  40.     kill();
  41.     return (YES);
  42. }
  43.  
  44. /*
  45.  *    change input filename to output filename
  46.  */
  47. void outfname(char *s)
  48. {
  49.     while (*s)
  50.         s++;
  51.     s[-1] = 'a';
  52.     *s++ = 's';
  53.     *s++ = 'm';
  54.     *s = '\0';
  55. }
  56.  
  57. /*
  58.  *    remove NL from filenames
  59.  */
  60. void fixname(char *s)
  61. {
  62.     while (*s && *s++ != EOL) ;
  63.     if (!*s)
  64.         return;
  65.     *(--s) = 0;
  66. }
  67.  
  68. /*
  69.  *    check that filename is "*.c" or "*.i"
  70.  */
  71. int checkname(char *s)
  72. {
  73.     while (*s)
  74.         s++;
  75.     if (*--s != 'c' && *s != 'i')
  76.         return (NO);
  77.     if (*--s != '.')
  78.         return (NO);
  79.     return (YES);
  80. }
  81.  
  82. void kill(void)
  83. {
  84.     lptr = 0;
  85.     line[lptr] = 0;
  86. }
  87.  
  88. void xinline(void)
  89. {
  90.     int k;
  91.     FILE *unit;
  92.  
  93.     for (;;)
  94.     {
  95.         if (feof(input))
  96.             return;
  97.         if ((unit = input2) == NULL)
  98.             unit = input;
  99.         kill();
  100.         while ((k = fgetc(unit)) != EOF)
  101.         {
  102.             if ((k == EOL) | (lptr >= LINEMAX))
  103.                 break;
  104.             line[lptr++] = k;
  105.         }
  106.         line[lptr] = 0;
  107.         if (k <= 0)
  108.             if (input2 != NULL)
  109.             {
  110.                 input2 = inclstk[--inclsp];
  111.                 fclose(unit);
  112.             }
  113.         if (lptr)
  114.         {
  115.             if ((ctext) & (cmode))
  116.             {
  117.                 comment();
  118.                 outstr(line);
  119.                 nl();
  120.             }
  121.             lptr = 0;
  122.             return;
  123.         }
  124.     }
  125. }
  126.  
  127. int inbyte(void)
  128. {
  129.     while (ch() == 0)
  130.     {
  131.         if (feof(input))
  132.             return (0);
  133.         preprocess();
  134.     }
  135.     return (gch());
  136. }
  137.  
  138. int inchar(void)
  139. {
  140.     if (ch() == 0)
  141.         xinline();
  142.     if (feof(input))
  143.         return (0);
  144.     return (gch());
  145. }
  146.  
  147. int gch(void)
  148. {
  149.     if (ch() == 0)
  150.         return (0);
  151.     else
  152.         return (line[lptr++] & 127);
  153. }
  154.  
  155. int nch(void)
  156. {
  157.     if (ch() == 0)
  158.         return (0);
  159.     else
  160.         return (line[lptr + 1] & 127);
  161. }
  162.  
  163. int ch(void)
  164. {
  165.     return (line[lptr] & 127);
  166. }
  167.  
  168. /*
  169.  *    print a carriage return and a string only to console
  170.  *
  171.  */
  172. void pl(char *str)
  173. {
  174.     int k;
  175.  
  176.     k = 0;
  177.     putchar(EOL);
  178.     while (str[k])
  179.         putchar(str[k++]);
  180. }
  181.  
  182.