1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ******************************************************************************
6 * Copyright (C) 2009-2015, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 ******************************************************************************
11 * FILE NAME : testplug.c
13 * Date Name Description
15 ******************************************************************************
18 * This file implements a number of example ICU plugins.
22 #include "unicode/icuplug.h"
24 #if UCONFIG_ENABLE_PLUGINS
25 /* This file isn't usually compiled except on Windows. Guard it. */
27 #include <stdio.h> /* for fprintf */
28 #include <stdlib.h> /* for malloc */
30 #include "unicode/uclean.h"
36 #define DECLARE_PLUGIN(x) U_CAPI UPlugTokenReturn U_EXPORT2 x (UPlugData *data, UPlugReason reason, UErrorCode *status)
38 DECLARE_PLUGIN(myPlugin
);
39 DECLARE_PLUGIN(myPluginLow
);
40 DECLARE_PLUGIN(myPluginFailQuery
);
41 DECLARE_PLUGIN(myPluginFailToken
);
42 DECLARE_PLUGIN(myPluginBad
);
43 DECLARE_PLUGIN(myPluginHigh
);
44 DECLARE_PLUGIN(debugMemoryPlugin
);
47 * A simple, trivial plugin.
51 UPlugTokenReturn U_EXPORT2
myPlugin (
55 /* Just print this for debugging */
56 fprintf(stderr
,"MyPlugin: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
58 if(reason
==UPLUG_REASON_QUERY
) {
59 uplug_setPlugName(data
, "Just a Test High-Level Plugin"); /* This call is optional in response to UPLUG_REASON_QUERY, but is a good idea. */
60 uplug_setPlugLevel(data
, UPLUG_LEVEL_HIGH
); /* This call is Mandatory in response to UPLUG_REASON_QUERY */
63 return UPLUG_TOKEN
; /* This must always be returned, to indicate that the entrypoint was actually a plugin. */
68 UPlugTokenReturn U_EXPORT2
myPluginLow (
72 fprintf(stderr
,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
74 if(reason
==UPLUG_REASON_QUERY
) {
75 uplug_setPlugName(data
, "Low Plugin");
76 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
83 * Doesn't respond to QUERY properly.
86 UPlugTokenReturn U_EXPORT2
myPluginFailQuery (
90 fprintf(stderr
,"MyPluginFailQuery: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
92 /* Should respond to UPLUG_REASON_QUERY here. */
98 * Doesn't return the proper token.
101 UPlugTokenReturn U_EXPORT2
myPluginFailToken (
104 UErrorCode
*status
) {
105 fprintf(stderr
,"MyPluginFailToken: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
107 if(reason
==UPLUG_REASON_QUERY
) {
108 uplug_setPlugName(data
, "myPluginFailToken Plugin");
109 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
112 return 0; /* Wrong. */
118 * Says it's low, but isn't.
121 UPlugTokenReturn U_EXPORT2
myPluginBad (
124 UErrorCode
*status
) {
125 fprintf(stderr
,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
127 if(reason
==UPLUG_REASON_QUERY
) {
128 uplug_setPlugName(data
, "Bad Plugin");
129 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
130 } else if(reason
== UPLUG_REASON_LOAD
) {
131 void *ctx
= uprv_malloc(12345);
133 uplug_setContext(data
, ctx
);
134 fprintf(stderr
,"I'm %p and I did a bad thing and malloced %p\n", (void*)data
, (void*)ctx
);
135 } else if(reason
== UPLUG_REASON_UNLOAD
) {
136 void * ctx
= uplug_getContext(data
);
146 UPlugTokenReturn U_EXPORT2
myPluginHigh (
149 UErrorCode
*status
) {
150 fprintf(stderr
,"MyPluginHigh: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
152 if(reason
==UPLUG_REASON_QUERY
) {
153 uplug_setPlugName(data
, "High Plugin");
154 uplug_setPlugLevel(data
, UPLUG_LEVEL_HIGH
);
161 /* Debug Memory Plugin (see hpmufn.c) */
162 static void * U_CALLCONV
myMemAlloc(const void *context
, size_t size
) {
163 void *retPtr
= (void *)malloc(size
);
164 (void)context
; /* unused */
165 fprintf(stderr
, "MEM: malloc(%d) = %p\n", (int32_t)size
, retPtr
);
169 static void U_CALLCONV
myMemFree(const void *context
, void *mem
) {
170 (void)context
; /* unused */
173 fprintf(stderr
, "MEM: free(%p)\n", mem
);
176 static void * U_CALLCONV
myMemRealloc(const void *context
, void *mem
, size_t size
) {
178 (void)context
; /* unused */
184 retPtr
= realloc(mem
, size
);
186 fprintf(stderr
, "MEM: realloc(%p, %d) = %p\n", mem
, (int32_t)size
, retPtr
);
191 UPlugTokenReturn U_EXPORT2
debugMemoryPlugin (
194 UErrorCode
*status
) {
195 fprintf(stderr
,"debugMemoryPlugin: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
197 if(reason
==UPLUG_REASON_QUERY
) {
198 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
199 uplug_setPlugName(data
, "Memory Plugin");
200 } else if(reason
==UPLUG_REASON_LOAD
) {
201 u_setMemoryFunctions(uplug_getContext(data
), &myMemAlloc
, &myMemRealloc
, &myMemFree
, status
);
202 fprintf(stderr
, "MEM: status now %s\n", u_errorName(*status
));
203 } else if(reason
==UPLUG_REASON_UNLOAD
) {
204 fprintf(stderr
, "MEM: not possible to unload this plugin (no way to reset memory functions)...\n");
205 uplug_setPlugNoUnload(data
, TRUE
);