]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/cpp/DataBoardTest/libantlr3c-3.2/src/antlr3filestream.c
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / cpp / DataBoardTest / libantlr3c-3.2 / src / antlr3filestream.c
1 /** \file\r
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
5  *\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
14  * may be found.\r
15  *\r
16  */\r
17 \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
22 //\r
23 // All rights reserved.\r
24 //\r
25 // Redistribution and use in source and binary forms, with or without\r
26 // modification, are permitted provided that the following conditions\r
27 // are met:\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
35 //\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
46 \r
47 #include    <antlr3.h>\r
48 \r
49 \r
50 /** \brief Use the contents of an operating system file as the input\r
51  *         for an input stream.\r
52  *\r
53  * \param fileName Name of operating system file to read.\r
54  * \return\r
55  *      - Pointer to new input stream context upon success\r
56  *      - One of the ANTLR3_ERR_ defines on error.\r
57  */\r
58 ANTLR3_API pANTLR3_INPUT_STREAM\r
59 antlr3AsciiFileStreamNew(pANTLR3_UINT8 fileName)\r
60 {\r
61         // Pointer to the input stream we are going to create\r
62         //\r
63         pANTLR3_INPUT_STREAM    input;\r
64         ANTLR3_UINT32       status;\r
65 \r
66         if      (fileName == NULL)\r
67         {\r
68                 return NULL;\r
69         }\r
70 \r
71         // Allocate memory for the input stream structure\r
72         //\r
73         input   = (pANTLR3_INPUT_STREAM)\r
74                 ANTLR3_CALLOC(1, sizeof(ANTLR3_INPUT_STREAM));\r
75 \r
76         if      (input == NULL)\r
77         {\r
78                 return  NULL;\r
79         }\r
80 \r
81         // Structure was allocated correctly, now we can read the file.\r
82         //\r
83         status  = antlr3readAscii(input, fileName);\r
84 \r
85         // Call the common 8 bit ASCII input stream handler\r
86         // Initializer type thingy doobry function.\r
87         //\r
88         antlr3AsciiSetupStream(input, ANTLR3_CHARSTREAM);\r
89 \r
90         // Now we can set up the file name\r
91         //      \r
92         input->istream->streamName      = input->strFactory->newStr(input->strFactory, fileName);\r
93         input->fileName                         = input->istream->streamName;\r
94 \r
95         if      (status != ANTLR3_SUCCESS)\r
96         {\r
97                 input->close(input);\r
98                 return  NULL;\r
99         }\r
100 \r
101         return  input;\r
102 }\r
103 \r
104 ANTLR3_API ANTLR3_UINT32\r
105 antlr3readAscii(pANTLR3_INPUT_STREAM    input, pANTLR3_UINT8 fileName)\r
106 {\r
107         ANTLR3_FDSC                 infile;\r
108         ANTLR3_UINT32       fSize;\r
109 \r
110         /* Open the OS file in read binary mode\r
111         */\r
112         infile  = antlr3Fopen(fileName, "rb");\r
113 \r
114         /* Check that it was there\r
115         */\r
116         if      (infile == NULL)\r
117         {\r
118                 return  (ANTLR3_UINT32)ANTLR3_ERR_NOFILE;\r
119         }\r
120 \r
121         /* It was there, so we can read the bytes now\r
122         */\r
123         fSize   = antlr3Fsize(fileName);        /* Size of input file   */\r
124 \r
125         /* Allocate buffer for this input set   \r
126         */\r
127         input->data         = ANTLR3_MALLOC((size_t)fSize);\r
128         input->sizeBuf  = fSize;\r
129 \r
130         if      (input->data == NULL)\r
131         {\r
132                 return  (ANTLR3_UINT32)ANTLR3_ERR_NOMEM;\r
133         }\r
134 \r
135         input->isAllocated      = ANTLR3_TRUE;\r
136 \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
139         */\r
140         antlr3Fread(infile, fSize, input->data);\r
141 \r
142         /* And close the file handle\r
143         */\r
144         antlr3Fclose(infile);\r
145 \r
146         return  ANTLR3_SUCCESS;\r
147 }\r
148 \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
154  */\r
155 ANTLR3_API ANTLR3_FDSC\r
156 antlr3Fopen(pANTLR3_UINT8 filename, const char * mode)\r
157 {\r
158     return  (ANTLR3_FDSC)fopen((const char *)filename, mode);\r
159 }\r
160 \r
161 /** \brief Close an operating system file and free any handles\r
162  *  etc.\r
163  */\r
164 ANTLR3_API void\r
165 antlr3Fclose(ANTLR3_FDSC fd)\r
166 {\r
167     fclose(fd);\r
168 }\r
169 ANTLR3_API ANTLR3_UINT32\r
170 antlr3Fsize(pANTLR3_UINT8 fileName)\r
171 {   \r
172     struct _stat        statbuf;\r
173 \r
174     _stat((const char *)fileName, &statbuf);\r
175 \r
176     return (ANTLR3_UINT32)statbuf.st_size;\r
177 }\r
178 \r
179 ANTLR3_API ANTLR3_UINT32\r
180 antlr3Fread(ANTLR3_FDSC fdsc, ANTLR3_UINT32 count,  void * data)\r
181 {\r
182     return  (ANTLR3_UINT32)fread(data, (size_t)count, 1, fdsc);\r
183 }\r