This repository has been archived by the owner on Feb 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathflagcb.c
231 lines (194 loc) · 7.72 KB
/
flagcb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "unicode/utypes.h"
#include "unicode/ucnv.h"
#include "flagcb.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define DEBUG_TMI 0 /* set to 1 for Too Much Information (TMI) */
U_CAPI ToUFLAGContext* U_EXPORT2 flagCB_toU_openContext()
{
ToUFLAGContext *ctx;
ctx = (ToUFLAGContext*) malloc(sizeof(ToUFLAGContext));
ctx->subCallback = NULL;
ctx->subContext = NULL;
ctx->flag = FALSE;
return ctx;
}
U_CAPI FromUFLAGContext* U_EXPORT2 flagCB_fromU_openContext()
{
FromUFLAGContext *ctx;
ctx = (FromUFLAGContext*) malloc(sizeof(FromUFLAGContext));
ctx->subCallback = NULL;
ctx->subContext = NULL;
ctx->flag = FALSE;
return ctx;
}
U_CAPI void U_EXPORT2 flagCB_toU(
const void *context,
UConverterToUnicodeArgs *toUArgs,
const char* codeUnits,
int32_t length,
UConverterCallbackReason reason,
UErrorCode * err)
{
/* First step - based on the reason code, take action */
if ((UCNV_UNASSIGNED == reason) ||
(UCNV_ILLEGAL == reason) ||
(UCNV_IRREGULAR == reason)
)
((ToUFLAGContext*) context)->flag = TRUE;
if (UCNV_CLONE == reason)
{
/* The following is the recommended way to implement UCNV_CLONE
in a callback. */
UConverterToUCallback saveCallback;
const void *saveContext;
ToUFLAGContext *old, *cloned;
UErrorCode subErr = U_ZERO_ERROR;
#if DEBUG_TMI
printf("*** FLAGCB: cloning %p ***\n", context);
#endif
old = (ToUFLAGContext*) context;
cloned = flagCB_toU_openContext();
memcpy(cloned, old, sizeof(ToUFLAGContext));
#if DEBUG_TMI
printf("%p: my subcb=%p:%p\n", old, old->subCallback,
old->subContext);
printf("%p: cloned subcb=%p:%p\n", cloned, cloned->subCallback,
cloned->subContext);
#endif
/* We need to get the sub CB to handle cloning,
* so we have to set up the following, temporarily:
*
* - Set the callback+context to the sub of this (flag) cb
* - preserve the current cb+context, it could be anything
*
* Before:
* CNV -> FLAG -> subcb -> ...
*
* After:
* CNV -> subcb -> ...
*
* The chain to 'something' on is saved, and will be restored
* at the end of this block.
*
*/
ucnv_setToUCallBack(toUArgs->converter,
cloned->subCallback,
cloned->subContext,
&saveCallback,
&saveContext,
&subErr);
if( cloned->subCallback != NULL )
/* Now, call the sub callback if present */
cloned->subCallback(cloned->subContext, toUArgs, codeUnits,
length, reason, err);
ucnv_setToUCallBack(toUArgs->converter,
saveCallback, /* Us */
cloned, /* new context */
&cloned->subCallback, /* IMPORTANT! Accept any change in CB or context */
&cloned->subContext,
&subErr);
if (U_FAILURE(subErr))
*err = subErr;
}
/* process other reasons here if need be */
/* Always call the subCallback if present */
if (((ToUFLAGContext*) context)->subCallback != NULL && reason != UCNV_CLONE)
((ToUFLAGContext*) context)->subCallback( ((ToUFLAGContext*) context)->subContext,
toUArgs,
codeUnits,
length,
reason,
err
);
/* cleanup - free the memory AFTER calling the sub CB */
if (reason == UCNV_CLOSE)
free((void*) context);
}
// from Unicode callback
U_CAPI void U_EXPORT2 flagCB_fromU(
const void *context,
UConverterFromUnicodeArgs *fromUArgs,
const UChar* codeUnits,
int32_t length,
UChar32 codePoint,
UConverterCallbackReason reason,
UErrorCode * err)
{
/* First step - based on the reason code, take action */
if ((UCNV_UNASSIGNED == reason) ||
(UCNV_ILLEGAL == reason) ||
(UCNV_IRREGULAR == reason)
)
((FromUFLAGContext*) context)->flag = TRUE;
if (reason == UCNV_CLONE)
{
/* The following is the recommended way to implement UCNV_CLONE
in a callback. */
UConverterFromUCallback saveCallback;
const void *saveContext;
FromUFLAGContext *old, *cloned;
UErrorCode subErr = U_ZERO_ERROR;
#if DEBUG_TMI
printf("*** FLAGCB: cloning %p ***\n", context);
#endif
old = (FromUFLAGContext*) context;
cloned = flagCB_fromU_openContext();
memcpy(cloned, old, sizeof(FromUFLAGContext));
#if DEBUG_TMI
printf("%p: my subcb=%p:%p\n", old, old->subCallback,
old->subContext);
printf("%p: cloned subcb=%p:%p\n", cloned, cloned->subCallback,
cloned->subContext);
#endif
/* We need to get the sub CB to handle cloning,
* so we have to set up the following, temporarily:
*
* - Set the callback+context to the sub of this (flag) cb
* - preserve the current cb+context, it could be anything
*
* Before:
* CNV -> FLAG -> subcb -> ...
*
* After:
* CNV -> subcb -> ...
*
* The chain from 'something' on is saved, and will be restored
* at the end of this block.
*
*/
ucnv_setFromUCallBack(fromUArgs->converter,
cloned->subCallback,
cloned->subContext,
&saveCallback,
&saveContext,
&subErr);
if ( cloned->subCallback != NULL )
/* Now, call the sub callback if present */
cloned->subCallback(cloned->subContext, fromUArgs, codeUnits,
length, codePoint, reason, err);
ucnv_setFromUCallBack(fromUArgs->converter,
saveCallback, /* Us */
cloned, /* new context */
&cloned->subCallback, /* IMPORTANT! Accept any change in CB or context */
&cloned->subContext,
&subErr);
if (U_FAILURE(subErr))
*err = subErr;
}
/* process other reasons here if need be */
/* Always call the subCallback if present */
if (((FromUFLAGContext*) context)->subCallback != NULL && reason != UCNV_CLONE)
((FromUFLAGContext*) context)->subCallback( ((FromUFLAGContext*) context)->subContext,
fromUArgs,
codeUnits,
length,
codePoint,
reason,
err
);
/* cleanup - free the memory AFTER calling the sub CB */
if (reason == UCNV_CLOSE)
free((void*) context);
}