stream.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* This file is part of the software similarity tester SIM.
  2. Written by Dick Grune, Vrije Universiteit, Amsterdam.
  3. $Id: stream.c,v 2.7 2001/11/08 12:30:32 dick Exp $
  4. */
  5. #include <stdio.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include "system.par"
  9. #include "token.h"
  10. #include "lex.h"
  11. #include "lang.h"
  12. #include "stream.h"
  13. static FILE *fopen_regular_file(const char *fname);
  14. int
  15. OpenStream(const char *fname) {
  16. int ok;
  17. lex_nl_cnt = 1;
  18. lex_tk_cnt = 0;
  19. lex_non_ascii_cnt = 0;
  20. /* start the lex machine */
  21. yyin = fopen_regular_file(fname);
  22. ok = (yyin != 0);
  23. if (!ok) {
  24. /* fake a stream, to simplify the rest of the program */
  25. yyin = fopen(NULLFILE, "r");
  26. }
  27. yystart();
  28. return ok;
  29. }
  30. static FILE *fopen_regular_file(const char *fname) {
  31. struct stat buf;
  32. if (stat(fname, &buf) != 0) return 0;
  33. if ((buf.st_mode & S_IFMT) != S_IFREG) return 0;
  34. return fopen(fname, "r");
  35. }
  36. int
  37. NextStreamTokenObtained(void) {
  38. return yylex();
  39. }
  40. void
  41. CloseStream(void) {
  42. if (yyin) {
  43. fclose(yyin);
  44. yyin = 0;
  45. }
  46. }