aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/antlr3_cpp_runtime/include/antlr3filestream.inl
blob: b67804adf27ffe5fda5bd64f1f7bc87b7968361e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
namespace antlr3 {

template<class ImplTraits>
ANTLR_FDSC	FileUtils<ImplTraits>::AntlrFopen(const ANTLR_UINT8* filename, const char * mode)
{
	return  (ANTLR_FDSC)fopen((const char *)filename, mode);
}

template<class ImplTraits>
void	FileUtils<ImplTraits>::AntlrFclose	(ANTLR_FDSC fd)
{
	fclose(fd);
}

template<class ImplTraits>
ANTLR_UINT32	FileUtils<ImplTraits>::AntlrFsize(const ANTLR_UINT8* filename)
{
    struct _stat	statbuf;

    _stat((const char *)filename, &statbuf);

    return (ANTLR_UINT32)statbuf.st_size;
}

template<class ImplTraits>
ANTLR_UINT32	FileUtils<ImplTraits>::AntlrFread(ANTLR_FDSC fdsc, ANTLR_UINT32 count,  void* data)
{
	return  (ANTLR_UINT32)fread(data, (size_t)count, 1, fdsc);
}

template<class ImplTraits>
	template<typename InputStreamType>
ANTLR_UINT32	FileUtils<ImplTraits>::AntlrRead8Bit(InputStreamType* input, const ANTLR_UINT8* fileName)
{
	ANTLR_FDSC	    infile;
	ANTLR_UINT32	    fSize;

	/* Open the OS file in read binary mode
	*/
	infile  = FileUtils<ImplTraits>::AntlrFopen(fileName, "rb");

	/* Check that it was there
	*/
	if	(infile == NULL)
	{
		ParseFileAbsentException ex;
		throw ex;
	}

	/* It was there, so we can read the bytes now
	*/
	fSize   = FileUtils<ImplTraits>::AntlrFsize(fileName);	/* Size of input file	*/

	/* Allocate buffer for this input set   
	*/
	void* data = ImplTraits::AllocPolicyType::alloc(fSize);
	/* Now we read the file. Characters are not converted to
	* the internal ANTLR encoding until they are read from the buffer
	*/
	FileUtils<ImplTraits>::AntlrFread(infile, fSize, data );

	input->set_data( (unsigned char*) data );
	input->set_sizeBuf( fSize );

	input->set_isAllocated(true);

	/* And close the file handle
	*/
	FileUtils<ImplTraits>::AntlrFclose(infile);

	return  ANTLR_SUCCESS;
}

}