~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/pbms/src/cslib/CSTokenStream.cc

Added the PBMS daemon plugin.

(Augen zu und durch!)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 2008 PrimeBase Technologies GmbH, Germany
 
2
 *
 
3
 * PrimeBase Media Stream for MySQL
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 2 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
18
 *
 
19
 * Original author: Paul McCullagh (H&G2JCtL)
 
20
 * Continued development: Barry Leslie
 
21
 *
 
22
 * 2007-06-08
 
23
 *
 
24
 * CORE SYSTEM:
 
25
 * A token stream. The tokens are identified in the same
 
26
 * manner as the UNIX/DOS command line.
 
27
 *
 
28
 * This basically means that all tokens must be sepparated
 
29
 * by at least one space.
 
30
 *
 
31
 * EOL and EOF are also tokens.
 
32
 *
 
33
 */
 
34
 
 
35
#include "CSConfig.h"
 
36
 
 
37
#include "CSGlobal.h"
 
38
#include "CSTokenStream.h"
 
39
 
 
40
const char *CSToken::singletons = "=[]!#$^&*()|;<>?~";
 
41
 
 
42
CSTokenStream::~CSTokenStream()
 
43
{
 
44
        enter_();
 
45
        if (iStream)
 
46
                iStream->release();
 
47
        exit_();
 
48
}
 
49
 
 
50
void CSTokenStream::open(CSInputStream *stream, u_int line)
 
51
{
 
52
        enter_();
 
53
        if (iStream)
 
54
                iStream->release();
 
55
        iStream = stream;
 
56
        iLine = line;
 
57
        iChar = -2;
 
58
        exit_();
 
59
}
 
60
 
 
61
void CSTokenStream::close()
 
62
{
 
63
        enter_();
 
64
        iStream->close();
 
65
        exit_();
 
66
}
 
67
        
 
68
void CSTokenStream::nextChar()
 
69
{
 
70
        enter_();
 
71
        iChar = iStream->read();
 
72
        exit_();
 
73
}
 
74
 
 
75
CSTokenStream *CSTokenStream::newTokenStream(CSInputStream *stream, u_int line)
 
76
{
 
77
        return UXTokenStream::newTokenStream(stream, line);
 
78
}
 
79
 
 
80
CSToken *UXTokenStream::nextToken()
 
81
{
 
82
        char    quote;
 
83
        const char      *ptr;
 
84
 
 
85
        enter_();
 
86
        /* Initialize the current character: */
 
87
        if (iChar == -2)
 
88
                nextChar();
 
89
 
 
90
        /* Ignore space: */
 
91
        while (iChar == ' ' || iChar == '\t')
 
92
                nextChar();
 
93
 
 
94
        /* Handle the singletons: */
 
95
        if ((ptr = strchr(CSToken::singletons, iChar))) {
 
96
                int ch = iChar;
 
97
 
 
98
                iChar = -2;
 
99
                return_(newToken((int) (ptr - CSToken::singletons), ch));
 
100
        }
 
101
 
 
102
        /* Handle EOF and EOL: */
 
103
        switch (iChar) {
 
104
                case -1:
 
105
                        return_(newToken(CS_TKN_EOS, "[EOF]"));
 
106
                case '\r':
 
107
                        nextChar();
 
108
                        if (iChar == '\n') {
 
109
                                iChar = -2;
 
110
                                if (iLine) iLine++;
 
111
                                return_(newToken(CS_TKN_EOL, "\r\n"));
 
112
                        }
 
113
                        if (iLine) iLine++;
 
114
                        return_(newToken(CS_TKN_EOL, "\r"));
 
115
                case '\n':
 
116
                        iChar = -2;
 
117
                        if (iLine) iLine++;
 
118
                        return_(newToken(CS_TKN_EOL, "\n"));
 
119
        }
 
120
 
 
121
        /* Now we have some text
 
122
         * The text ends with the first space, EOF, EOL
 
123
         * or a singleton.
 
124
         */
 
125
        CSStringBuffer *text;
 
126
        
 
127
        new_(text, CSStringBuffer());
 
128
        push_(text);
 
129
 
 
130
        for (;;) {
 
131
                switch (iChar) {
 
132
                        case '`':
 
133
                                // ` are included in the string to indicate it should be executed
 
134
                                text->append((char) iChar);
 
135
                        case '"':
 
136
                        case '\'':
 
137
                                /* Quoted text: */
 
138
                                quote = iChar;
 
139
                                nextChar();
 
140
                                while (iChar != quote && iChar != -1 && iChar != '\r' && iChar != '\n') {
 
141
                                        text->append((char) iChar);
 
142
                                        nextChar();
 
143
                                }
 
144
                                if (iChar == quote) {
 
145
                                        if (quote == '`')
 
146
                                                text->append((char) quote);
 
147
                                        nextChar();
 
148
                                }
 
149
                                break;
 
150
                        case '\\':
 
151
                                /* Escaped character: */
 
152
                                nextChar();
 
153
                                if (iChar != -1) {
 
154
                                        text->append((char) iChar);
 
155
                                        nextChar();
 
156
                                }
 
157
                                break;
 
158
                        case -1:
 
159
                        case ' ':
 
160
                        case '\t':
 
161
                        case '\r':
 
162
                        case '\n':
 
163
                                /* These characters terminated the text: */
 
164
                                goto exit_loop;
 
165
                        default:
 
166
                                /* Singletons terminate the text as well, unless they
 
167
                                 * were escaped or in quotes.
 
168
                                 */
 
169
                                if ((ptr = strchr(CSToken::singletons, iChar)))
 
170
                                        goto exit_loop;
 
171
                                text->append((char) iChar);
 
172
                                nextChar();
 
173
                }
 
174
        }
 
175
        exit_loop:
 
176
        
 
177
        char *str = text->take();
 
178
        release_(text);
 
179
        return_(newToken(CS_TKN_TEXT, str));
 
180
}
 
181
 
 
182
CSToken *UXTokenStream::newToken(int type, const char *text)
 
183
{
 
184
        CSToken *tk;
 
185
 
 
186
        new_(tk, UXToken(type, text));
 
187
        return tk;
 
188
}
 
189
 
 
190
CSToken *UXTokenStream::newToken(int type, char ch)
 
191
{
 
192
        CSToken *tk;
 
193
 
 
194
        new_(tk, UXToken(type, ch));
 
195
        return tk;
 
196
}
 
197
 
 
198
CSTokenStream *UXTokenStream::newTokenStream(CSInputStream *stream, u_int line)
 
199
{
 
200
        CSTokenStream *s;
 
201
        enter_();
 
202
        push_(stream);
 
203
        new_(s, UXTokenStream());
 
204
        pop_(stream);
 
205
        s->open(stream, line);
 
206
        return_(s);
 
207
}
 
208