2 * \brief The ANTLR3 C filestream is used when the source character stream
\r
3 * is a filesystem based input set and all the characters in the filestream
\r
4 * can be loaded at once into memory and away the lexer goes.
\r
6 * A number of initializers are provided in order that various character
\r
7 * sets can be supported from input files. The ANTLR3 C runtime expects
\r
8 * to deal with UTF32 characters only (the reasons for this are to
\r
9 * do with the simplification of C code when using this form of Unicode
\r
10 * encoding, though this is not a panacea. More information can be
\r
11 * found on this by consulting:
\r
12 * - http://www.unicode.org/versions/Unicode4.0.0/ch02.pdf#G11178
\r
13 * Where a well grounded discussion of the encoding formats available
\r
18 // [The "BSD licence"]
\r
19 // Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC
\r
20 // http://www.temporal-wave.com
\r
21 // http://www.linkedin.com/in/jimidle
\r
23 // All rights reserved.
\r
25 // Redistribution and use in source and binary forms, with or without
\r
26 // modification, are permitted provided that the following conditions
\r
28 // 1. Redistributions of source code must retain the above copyright
\r
29 // notice, this list of conditions and the following disclaimer.
\r
30 // 2. Redistributions in binary form must reproduce the above copyright
\r
31 // notice, this list of conditions and the following disclaimer in the
\r
32 // documentation and/or other materials provided with the distribution.
\r
33 // 3. The name of the author may not be used to endorse or promote products
\r
34 // derived from this software without specific prior written permission.
\r
36 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
\r
37 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
\r
38 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
\r
39 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
40 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
\r
41 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
\r
42 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
\r
43 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
\r
44 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
\r
45 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
50 /** \brief Use the contents of an operating system file as the input
\r
51 * for an input stream.
\r
53 * \param fileName Name of operating system file to read.
\r
55 * - Pointer to new input stream context upon success
\r
56 * - One of the ANTLR3_ERR_ defines on error.
\r
58 ANTLR3_API pANTLR3_INPUT_STREAM
\r
59 antlr3AsciiFileStreamNew(pANTLR3_UINT8 fileName)
\r
61 // Pointer to the input stream we are going to create
\r
63 pANTLR3_INPUT_STREAM input;
\r
64 ANTLR3_UINT32 status;
\r
66 if (fileName == NULL)
\r
71 // Allocate memory for the input stream structure
\r
73 input = (pANTLR3_INPUT_STREAM)
\r
74 ANTLR3_CALLOC(1, sizeof(ANTLR3_INPUT_STREAM));
\r
81 // Structure was allocated correctly, now we can read the file.
\r
83 status = antlr3readAscii(input, fileName);
\r
85 // Call the common 8 bit ASCII input stream handler
\r
86 // Initializer type thingy doobry function.
\r
88 antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);
\r
90 // Now we can set up the file name
\r
92 input->istream->streamName = input->strFactory->newStr(input->strFactory, fileName);
\r
93 input->fileName = input->istream->streamName;
\r
95 if (status != ANTLR3_SUCCESS)
\r
97 input->close(input);
\r
104 ANTLR3_API ANTLR3_UINT32
\r
105 antlr3readAscii(pANTLR3_INPUT_STREAM input, pANTLR3_UINT8 fileName)
\r
107 ANTLR3_FDSC infile;
\r
108 ANTLR3_UINT32 fSize;
\r
110 /* Open the OS file in read binary mode
\r
112 infile = antlr3Fopen(fileName, "rb");
\r
114 /* Check that it was there
\r
116 if (infile == NULL)
\r
118 return (ANTLR3_UINT32)ANTLR3_ERR_NOFILE;
\r
121 /* It was there, so we can read the bytes now
\r
123 fSize = antlr3Fsize(fileName); /* Size of input file */
\r
125 /* Allocate buffer for this input set
\r
127 input->data = ANTLR3_MALLOC((size_t)fSize);
\r
128 input->sizeBuf = fSize;
\r
130 if (input->data == NULL)
\r
132 return (ANTLR3_UINT32)ANTLR3_ERR_NOMEM;
\r
135 input->isAllocated = ANTLR3_TRUE;
\r
137 /* Now we read the file. Characters are not converted to
\r
138 * the internal ANTLR encoding until they are read from the buffer
\r
140 antlr3Fread(infile, fSize, input->data);
\r
142 /* And close the file handle
\r
144 antlr3Fclose(infile);
\r
146 return ANTLR3_SUCCESS;
\r
149 /** \brief Open an operating system file and return the descriptor
\r
150 * We just use the common open() and related functions here.
\r
151 * Later we might find better ways on systems
\r
152 * such as Windows and OpenVMS for instance. But the idea is to read the
\r
153 * while file at once anyway, so it may be irrelevant.
\r
155 ANTLR3_API ANTLR3_FDSC
\r
156 antlr3Fopen(pANTLR3_UINT8 filename, const char * mode)
\r
158 return (ANTLR3_FDSC)fopen((const char *)filename, mode);
\r
161 /** \brief Close an operating system file and free any handles
\r
165 antlr3Fclose(ANTLR3_FDSC fd)
\r
169 ANTLR3_API ANTLR3_UINT32
\r
170 antlr3Fsize(pANTLR3_UINT8 fileName)
\r
172 struct _stat statbuf;
\r
174 _stat((const char *)fileName, &statbuf);
\r
176 return (ANTLR3_UINT32)statbuf.st_size;
\r
179 ANTLR3_API ANTLR3_UINT32
\r
180 antlr3Fread(ANTLR3_FDSC fdsc, ANTLR3_UINT32 count, void * data)
\r
182 return (ANTLR3_UINT32)fread(data, (size_t)count, 1, fdsc);
\r