--- /dev/null
+/** \file\r
+ * \brief The ANTLR3 C filestream is used when the source character stream\r
+ * is a filesystem based input set and all the characters in the filestream\r
+ * can be loaded at once into memory and away the lexer goes.\r
+ *\r
+ * A number of initializers are provided in order that various character\r
+ * sets can be supported from input files. The ANTLR3 C runtime expects\r
+ * to deal with UTF32 characters only (the reasons for this are to\r
+ * do with the simplification of C code when using this form of Unicode \r
+ * encoding, though this is not a panacea. More information can be\r
+ * found on this by consulting: \r
+ * - http://www.unicode.org/versions/Unicode4.0.0/ch02.pdf#G11178\r
+ * Where a well grounded discussion of the encoding formats available\r
+ * may be found.\r
+ *\r
+ */\r
+\r
+// [The "BSD licence"]\r
+// Copyright (c) 2005-2009 Jim Idle, Temporal Wave LLC\r
+// http://www.temporal-wave.com\r
+// http://www.linkedin.com/in/jimidle\r
+//\r
+// All rights reserved.\r
+//\r
+// Redistribution and use in source and binary forms, with or without\r
+// modification, are permitted provided that the following conditions\r
+// are met:\r
+// 1. Redistributions of source code must retain the above copyright\r
+// notice, this list of conditions and the following disclaimer.\r
+// 2. Redistributions in binary form must reproduce the above copyright\r
+// notice, this list of conditions and the following disclaimer in the\r
+// documentation and/or other materials provided with the distribution.\r
+// 3. The name of the author may not be used to endorse or promote products\r
+// derived from this software without specific prior written permission.\r
+//\r
+// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\r
+// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\r
+// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\r
+// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
+// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\r
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\r
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\r
+// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
+\r
+#include <antlr3.h>\r
+\r
+\r
+/** \brief Use the contents of an operating system file as the input\r
+ * for an input stream.\r
+ *\r
+ * \param fileName Name of operating system file to read.\r
+ * \return\r
+ * - Pointer to new input stream context upon success\r
+ * - One of the ANTLR3_ERR_ defines on error.\r
+ */\r
+ANTLR3_API pANTLR3_INPUT_STREAM\r
+antlr3AsciiFileStreamNew(pANTLR3_UINT8 fileName)\r
+{\r
+ // Pointer to the input stream we are going to create\r
+ //\r
+ pANTLR3_INPUT_STREAM input;\r
+ ANTLR3_UINT32 status;\r
+\r
+ if (fileName == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ // Allocate memory for the input stream structure\r
+ //\r
+ input = (pANTLR3_INPUT_STREAM)\r
+ ANTLR3_CALLOC(1, sizeof(ANTLR3_INPUT_STREAM));\r
+\r
+ if (input == NULL)\r
+ {\r
+ return NULL;\r
+ }\r
+\r
+ // Structure was allocated correctly, now we can read the file.\r
+ //\r
+ status = antlr3readAscii(input, fileName);\r
+\r
+ // Call the common 8 bit ASCII input stream handler\r
+ // Initializer type thingy doobry function.\r
+ //\r
+ antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);\r
+\r
+ // Now we can set up the file name\r
+ // \r
+ input->istream->streamName = input->strFactory->newStr(input->strFactory, fileName);\r
+ input->fileName = input->istream->streamName;\r
+\r
+ if (status != ANTLR3_SUCCESS)\r
+ {\r
+ input->close(input);\r
+ return NULL;\r
+ }\r
+\r
+ return input;\r
+}\r
+\r
+ANTLR3_API ANTLR3_UINT32\r
+antlr3readAscii(pANTLR3_INPUT_STREAM input, pANTLR3_UINT8 fileName)\r
+{\r
+ ANTLR3_FDSC infile;\r
+ ANTLR3_UINT32 fSize;\r
+\r
+ /* Open the OS file in read binary mode\r
+ */\r
+ infile = antlr3Fopen(fileName, "rb");\r
+\r
+ /* Check that it was there\r
+ */\r
+ if (infile == NULL)\r
+ {\r
+ return (ANTLR3_UINT32)ANTLR3_ERR_NOFILE;\r
+ }\r
+\r
+ /* It was there, so we can read the bytes now\r
+ */\r
+ fSize = antlr3Fsize(fileName); /* Size of input file */\r
+\r
+ /* Allocate buffer for this input set \r
+ */\r
+ input->data = ANTLR3_MALLOC((size_t)fSize);\r
+ input->sizeBuf = fSize;\r
+\r
+ if (input->data == NULL)\r
+ {\r
+ return (ANTLR3_UINT32)ANTLR3_ERR_NOMEM;\r
+ }\r
+\r
+ input->isAllocated = ANTLR3_TRUE;\r
+\r
+ /* Now we read the file. Characters are not converted to\r
+ * the internal ANTLR encoding until they are read from the buffer\r
+ */\r
+ antlr3Fread(infile, fSize, input->data);\r
+\r
+ /* And close the file handle\r
+ */\r
+ antlr3Fclose(infile);\r
+\r
+ return ANTLR3_SUCCESS;\r
+}\r
+\r
+/** \brief Open an operating system file and return the descriptor\r
+ * We just use the common open() and related functions here. \r
+ * Later we might find better ways on systems\r
+ * such as Windows and OpenVMS for instance. But the idea is to read the \r
+ * while file at once anyway, so it may be irrelevant.\r
+ */\r
+ANTLR3_API ANTLR3_FDSC\r
+antlr3Fopen(pANTLR3_UINT8 filename, const char * mode)\r
+{\r
+ return (ANTLR3_FDSC)fopen((const char *)filename, mode);\r
+}\r
+\r
+/** \brief Close an operating system file and free any handles\r
+ * etc.\r
+ */\r
+ANTLR3_API void\r
+antlr3Fclose(ANTLR3_FDSC fd)\r
+{\r
+ fclose(fd);\r
+}\r
+ANTLR3_API ANTLR3_UINT32\r
+antlr3Fsize(pANTLR3_UINT8 fileName)\r
+{ \r
+ struct _stat statbuf;\r
+\r
+ _stat((const char *)fileName, &statbuf);\r
+\r
+ return (ANTLR3_UINT32)statbuf.st_size;\r
+}\r
+\r
+ANTLR3_API ANTLR3_UINT32\r
+antlr3Fread(ANTLR3_FDSC fdsc, ANTLR3_UINT32 count, void * data)\r
+{\r
+ return (ANTLR3_UINT32)fread(data, (size_t)count, 1, fdsc);\r
+}\r