]> git.sur5r.net Git - cc65/blob - src/cc65/scanstrbuf.c
82418f763d456b5852879e66df2d118a98dbba6d
[cc65] / src / cc65 / scanstrbuf.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 scanstrbuf.c                              */
4 /*                                                                           */
5 /*                     Small scanner for input from a StrBuf                 */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2002      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "chartype.h"
38
39 /* cc65 */
40 #include "error.h"
41 #include "hexval.h"
42 #include "ident.h"
43 #include "scanstrbuf.h"
44
45
46
47 /*****************************************************************************/
48 /*                               Helper functions                            */
49 /*****************************************************************************/
50
51
52
53 static char ParseChar (StrBuf* B)
54 /* Parse a character. Converts \n into EOL, etc. */
55 {
56     unsigned I;
57     int C;
58
59     /* Check for escape chars */
60     if ((C = SB_Get (B)) == '\\') {
61         switch (SB_Get (B)) {
62             case 'b':
63                 C = '\b';
64                 break;
65             case 'f':
66                 C = '\f';
67                 break;
68             case 'r':
69                 C = '\r';
70                 break;
71             case 'n':
72                 C = '\n';
73                 break;
74             case 't':
75                 C = '\t';
76                 break;
77             case '\"':
78                 C = '\"';
79                 break;
80             case '\'':
81                 C = '\'';
82                 break;
83             case '\\':
84                 C = '\\';
85                 break;
86             case 'x':
87             case 'X':
88                 /* Hex character constant */
89                 C = HexVal (SB_Get (B)) << 4;
90                 C |= HexVal (SB_Get (B));
91                 break;
92             case '0':
93                 /* Octal constant */
94                 C = 0;
95                 goto Octal;
96             case '1':
97                 /* Octal constant */
98                 C = 1;
99 Octal:          I = 0;
100                 while (SB_Peek (B) >= '0' && SB_Peek (B) <= '7' && I++ < 4) {
101                     C = (C << 3) | (SB_Get (B) - '0');
102                 }
103                 break;
104             default:
105                 Error ("Illegal character constant");
106                 C = ' ';
107                 break;
108         }
109     }
110
111     /* Return the character */
112     return C;
113 }
114
115
116
117
118
119
120
121 /*****************************************************************************/
122 /*                                   Code                                    */
123 /*****************************************************************************/
124
125
126
127 void SB_SkipWhite (StrBuf* B)
128 /* Skip whitespace in the string buffer */
129 {
130     while (IsBlank (SB_Peek (B))) {
131         SB_Skip (B);
132     }
133 }
134
135
136
137 int SB_GetSym (StrBuf* B, char* S)
138 /* Get a symbol from the string buffer. S must be able to hold MAX_IDENTLEN
139  * characters. Returns 1 if a symbol was found and 0 otherwise.
140  */
141 {
142     if (IsIdent (SB_Peek (B))) {
143         unsigned I = 0;
144         char C = SB_Peek (B);
145         do {
146             if (I < MAX_IDENTLEN) {
147                 ++I;
148                 *S++ = C;
149             }
150             SB_Skip (B);
151             C = SB_Peek (B);
152         } while (IsIdent (C) || IsDigit (C));
153         *S = '\0';
154         return 1;
155     } else {
156         return 0;
157     }
158 }
159
160
161
162 int SB_GetString (StrBuf* B, StrBuf* S)
163 /* Get a string from the string buffer. S will be initialized by the function
164  * and will return the correctly terminated string on return. The function
165  * returns 1 if a string was found and 0 otherwise.
166  */
167 {
168     char C;
169
170     /* Initialize S */
171     *S = AUTO_STRBUF_INITIALIZER;
172     if (SB_Peek (B) == '\"') {
173
174         /* String follows, be sure to concatenate strings */
175         while (SB_Peek (B) == '\"') {
176
177             /* Skip the quote char */
178             SB_Skip (B);
179
180             /* Read the actual string contents */
181             while ((C = SB_Peek (B)) != '\"') {
182                 if (C == '\0') {
183                     Error ("Unexpected end of string");
184                     break;
185                 }
186                 SB_AppendChar (S, ParseChar (B));
187             }
188
189             /* Skip the closing quote char if there was one */
190             SB_Skip (B);
191
192             /* Skip white space, read new input */
193             SB_SkipWhite (B);
194         }
195
196         /* Terminate the string */
197         SB_Terminate (S);
198
199         /* Success */
200         return 1;
201
202     } else {
203
204         /* Not a string */
205         SB_Terminate (S);
206         return 0;
207     }
208 }
209
210
211