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