37
37
%{
38
38
#include <stdio.h>
39
39
#include <stdlib.h>
40
+ #include <assert.h>
40
41
#include "config.h"
41
42
42
43
m4_ifdef(`VARIANT_REENTRANT', `#define VARIANT_REENTRANT 1', `')
44
+ m4_ifdef(`VARIANT_CPLUSPLUS', `#define VARIANT_CPLUSPLUS 1', `')
45
+
46
+ #if VARIANT_CPLUSPLUS
47
+ #define out(str) (*yyout << str)
48
+ #else
49
+ #define out(str) (fprintf(yyout, str))
50
+ #endif
43
51
44
52
%}
45
53
@@ -48,16 +56,20 @@ m4_ifdef(`VARIANT_REENTRANT', `#define VARIANT_REENTRANT 1', `')
48
56
%option warn
49
57
50
58
m4_ifdef(`VARIANT_REENTRANT', `%option reentrant', `')
51
-
59
+ m4_ifdef(`VARIANT_CPLUSPLUS', `%option c++')
52
60
53
61
%%
54
62
55
- [A-Z������������������������������] { fprintf(yyout, "U"); }
56
- [a-z��������������������������������] { fprintf(yyout, "L"); }
57
- [0-9] { fprintf(yyout, "N"); }
63
+ [A-Z������������������������������] { out( "U"); }
64
+ [a-z��������������������������������] { out( "L"); }
65
+ [0-9] { out( "N"); }
58
66
59
67
%%
60
68
69
+ #if VARIANT_CPLUSPLUS
70
+ class TestFlexLexer: public yyFlexLexer {
71
+ public:
72
+ #endif
61
73
/*
62
74
* The function provided by scanner to handle encodings. It gets set of incoming
63
75
* bytes and convert into set of characters in internal representation - in the
@@ -76,12 +88,18 @@ m4_ifdef(`VARIANT_REENTRANT', `%option reentrant', `')
76
88
* RETURNS: number of characters that has been written into "target" buffer.
77
89
* Must not be greater than value of "target_length" parameter.
78
90
*/
91
+ #if VARIANT_CPLUSPLUS
92
+ size_t yycharset_handler(char *charset, char* source, size_t source_bytes,
93
+ YY_CHAR* target, size_t target_length, size_t* converted_bytes)
94
+ #else
79
95
size_t charset_handler(char *charset, char* source, size_t source_bytes,
80
96
YY_CHAR* target, size_t target_length, size_t* converted_bytes
81
97
#if VARIANT_REENTRANT
82
98
, yyscan_t yyscanner
83
99
#endif
84
- ) {
100
+ )
101
+ #endif
102
+ {
85
103
/* conversion from CP850 to ISO-8859-1. Unrepresentable values are set to -1 */
86
104
static int conversion_table_cp850[256] = {
87
105
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
@@ -150,6 +168,10 @@ size_t charset_handler(char *charset, char* source, size_t source_bytes,
150
168
return source_bytes;
151
169
}
152
170
171
+ #if VARIANT_CPLUSPLUS
172
+ };
173
+ #endif
174
+
153
175
int main (int argc, char *argv[])
154
176
{
155
177
if(argc < 2) {
@@ -158,15 +180,24 @@ int main (int argc, char *argv[])
158
180
}
159
181
char *charset = argv[1];
160
182
161
- #if VARIANT_REENTRANT
183
+ #if VARIANT_CPLUSPLUS
184
+ TestFlexLexer lexer;
185
+ lexer.set_charset(charset);
186
+ assert(strcmp(lexer.get_charset(), charset)==0);
187
+ lexer.yylex();
188
+ #elif VARIANT_REENTRANT
162
189
yyscan_t lexer;
163
190
164
191
yylex_init(&lexer);
165
192
166
193
yyset_in(stdin, lexer);
167
194
yyset_out(stdout, lexer);
195
+
168
196
yyset_charset(charset, lexer);
197
+ assert(strcmp(yyget_charset(lexer), charset)==0);
198
+
169
199
yyset_charset_handler(charset_handler, lexer);
200
+ assert(yyget_charset_handler(lexer) == charset_handler);
170
201
171
202
yylex( lexer );
172
203
0 commit comments