home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / a / as1_23 / source / c / input < prev    next >
Encoding:
Text File  |  1993-12-28  |  2.5 KB  |  168 lines

  1.  
  2. /*
  3.  *  input.c
  4.  * Copyright © 1992 Niklas Röjemo
  5.  */ 
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10. #include "error.h"
  11. #include "global.h"
  12. #include "input.h"
  13. #include "riscos.h"
  14.  
  15. #define MAX_LINE 4096
  16.  
  17. extern int gcc_comment;
  18.  
  19. FILE *asmfile;
  20. static char buff[MAX_LINE];
  21. static char *pos,*mark;
  22.  
  23. int inputLineNo;
  24.  
  25. char inputLook(void)
  26. {
  27.   return *pos;
  28. }
  29.  
  30. int inputComment(void)
  31. {
  32.   int c;
  33.   return 0==(c=*pos) || c == ';' || (gcc_comment && c == '@');
  34. }
  35.  
  36. char inputLookN(int n)  /* Unsafe */
  37. {
  38.   return pos[n];
  39. }
  40.  
  41. char inputGet(void)
  42. {
  43.   if(*pos) return *pos++;
  44.   else     return *pos;
  45. }
  46. void inputUnGet(char c)
  47. {
  48.   if(pos>buff && pos[-1] == c)
  49.     pos--;
  50.   else
  51.     if(*pos || c) error(ErrorSerious,FALSE,"Internal inputUnGet: Illegal character.");
  52. }
  53.  
  54. void inputSkip(void)
  55. {
  56.   if(*pos) pos++;
  57. }
  58.  
  59. void inputSkipN(int n)
  60. {
  61.   while(*pos && n--) pos++;
  62.   if(n>-1)
  63.     error(ErrorSerious,FALSE,"Internal inputSkipN: Trying to skip more characters than are availible.");
  64. }
  65.  
  66. char *inputRest(void)
  67. {
  68.   char *t = pos;
  69.   while(*pos) pos++;
  70.   return t;
  71. }
  72. char *inputLine(void)
  73. {
  74.   return buff;
  75. }
  76.  
  77. char inputSkipLook(void)
  78. {
  79.   if(*pos) return *++pos;
  80.   else     return *pos;
  81. }
  82.  
  83. void skipblanks(void)
  84. {
  85.   char c;
  86.   while((c=*pos)!=0 && isspace(c))
  87.     pos++;
  88. }
  89.  
  90. void skiprest(void)
  91. {
  92.   buff[0] = 0;
  93.   pos = mark = &buff[0];
  94. }
  95.  
  96. void inputMark(void)
  97. {
  98.   mark = pos;
  99. }
  100.  
  101. void inputRollback(void)
  102. {
  103.   pos = mark;
  104. }
  105.  
  106. void inputInit(char *infile)
  107. {
  108.   if(infile && strcmp(infile,"-")) {
  109.     if((asmfile = fopen(toriscos(infile,"s",'s'),"r")) == NULL) {
  110.       errorLine(0,ErrorAbort,FALSE,"As can't read %s.",infile);
  111.     }
  112.   } else
  113.     asmfile = stdin;
  114.  
  115.   inputLineNo = 0;
  116.   skiprest();
  117. }
  118.  
  119. void inputFinish(void)
  120. {
  121.   if(asmfile != stdin)
  122.     fclose(asmfile);
  123. }
  124.  
  125. BOOL inputNextLine(void)
  126. {
  127.   int l;
  128.   inputLineNo++;
  129.   if(fgets(buff,MAX_LINE,asmfile)==NULL) {
  130.     inputLineNo = -1;
  131.     return FALSE;
  132.   }
  133.   l = strlen(buff);
  134.   if(l) {
  135.     if(buff[l-1] != '\n') {
  136.       if((l=getc(asmfile))!= EOF && l!='\n') {
  137.         error(ErrorSerious,TRUE,"Line truncated");
  138.         while((l=getc(asmfile))!= EOF && l!='\n')
  139.            ;
  140.       }
  141.     } else
  142.       buff[l-1] = 0;
  143.   }
  144.   pos = &buff[0];
  145.   return TRUE;
  146. }
  147.  
  148. char *inputSymbol(int *ilen, char del)
  149. {
  150.   char *res = pos;
  151.   char c;
  152.   if(del) {
  153.     while((c=*pos)!=0 && c != del) {
  154.       pos++;
  155.       if(c == '\\' && *pos)
  156.         pos++;
  157.     }
  158.   } else {
  159.     while((c=*pos)!=0 && (isalnum(c) || c == '_')) {
  160.       pos++;
  161.       if(c == '\\' && *pos)
  162.         pos++;
  163.     }
  164.   }
  165.   *ilen = pos-res;
  166.   return res;
  167. }
  168.