antlr3filestream.inl 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. namespace antlr3 {
  2. template<class ImplTraits>
  3. ANTLR_FDSC FileUtils<ImplTraits>::AntlrFopen(const ANTLR_UINT8* filename, const char * mode)
  4. {
  5. return (ANTLR_FDSC)fopen((const char *)filename, mode);
  6. }
  7. template<class ImplTraits>
  8. void FileUtils<ImplTraits>::AntlrFclose (ANTLR_FDSC fd)
  9. {
  10. fclose(fd);
  11. }
  12. template<class ImplTraits>
  13. ANTLR_UINT32 FileUtils<ImplTraits>::AntlrFsize(const ANTLR_UINT8* filename)
  14. {
  15. struct _stat statbuf;
  16. _stat((const char *)filename, &statbuf);
  17. return (ANTLR_UINT32)statbuf.st_size;
  18. }
  19. template<class ImplTraits>
  20. ANTLR_UINT32 FileUtils<ImplTraits>::AntlrFread(ANTLR_FDSC fdsc, ANTLR_UINT32 count, void* data)
  21. {
  22. return (ANTLR_UINT32)fread(data, (size_t)count, 1, fdsc);
  23. }
  24. template<class ImplTraits>
  25. template<typename InputStreamType>
  26. ANTLR_UINT32 FileUtils<ImplTraits>::AntlrRead8Bit(InputStreamType* input, const ANTLR_UINT8* fileName)
  27. {
  28. ANTLR_FDSC infile;
  29. ANTLR_UINT32 fSize;
  30. /* Open the OS file in read binary mode
  31. */
  32. infile = FileUtils<ImplTraits>::AntlrFopen(fileName, "rb");
  33. /* Check that it was there
  34. */
  35. if (infile == NULL)
  36. {
  37. ParseFileAbsentException ex;
  38. throw ex;
  39. }
  40. /* It was there, so we can read the bytes now
  41. */
  42. fSize = FileUtils<ImplTraits>::AntlrFsize(fileName); /* Size of input file */
  43. /* Allocate buffer for this input set
  44. */
  45. void* data = ImplTraits::AllocPolicyType::alloc(fSize);
  46. /* Now we read the file. Characters are not converted to
  47. * the internal ANTLR encoding until they are read from the buffer
  48. */
  49. FileUtils<ImplTraits>::AntlrFread(infile, fSize, data );
  50. input->set_data( (unsigned char*) data );
  51. input->set_sizeBuf( fSize );
  52. input->set_isAllocated(true);
  53. /* And close the file handle
  54. */
  55. FileUtils<ImplTraits>::AntlrFclose(infile);
  56. return ANTLR_SUCCESS;
  57. }
  58. }