2 ******************************************************************************
4 * Copyright (C) 2009-2015, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 ******************************************************************************
9 * FILE NAME : testplug.c
11 * Date Name Description
13 ******************************************************************************
16 * This file implements a number of example ICU plugins.
20 #include "unicode/icuplug.h"
22 #if UCONFIG_ENABLE_PLUGINS
23 /* This file isn't usually compiled except on Windows. Guard it. */
25 #include <stdio.h> /* for fprintf */
26 #include <stdlib.h> /* for malloc */
28 #include "unicode/uclean.h"
34 #define DECLARE_PLUGIN(x) U_CAPI UPlugTokenReturn U_EXPORT2 x (UPlugData *data, UPlugReason reason, UErrorCode *status)
36 DECLARE_PLUGIN(myPlugin
);
37 DECLARE_PLUGIN(myPluginLow
);
38 DECLARE_PLUGIN(myPluginFailQuery
);
39 DECLARE_PLUGIN(myPluginFailToken
);
40 DECLARE_PLUGIN(myPluginBad
);
41 DECLARE_PLUGIN(myPluginHigh
);
42 DECLARE_PLUGIN(debugMemoryPlugin
);
45 * A simple, trivial plugin.
49 UPlugTokenReturn U_EXPORT2
myPlugin (
53 /* Just print this for debugging */
54 fprintf(stderr
,"MyPlugin: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
56 if(reason
==UPLUG_REASON_QUERY
) {
57 uplug_setPlugName(data
, "Just a Test High-Level Plugin"); /* This call is optional in response to UPLUG_REASON_QUERY, but is a good idea. */
58 uplug_setPlugLevel(data
, UPLUG_LEVEL_HIGH
); /* This call is Mandatory in response to UPLUG_REASON_QUERY */
61 return UPLUG_TOKEN
; /* This must always be returned, to indicate that the entrypoint was actually a plugin. */
66 UPlugTokenReturn U_EXPORT2
myPluginLow (
70 fprintf(stderr
,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
72 if(reason
==UPLUG_REASON_QUERY
) {
73 uplug_setPlugName(data
, "Low Plugin");
74 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
81 * Doesn't respond to QUERY properly.
84 UPlugTokenReturn U_EXPORT2
myPluginFailQuery (
88 fprintf(stderr
,"MyPluginFailQuery: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
90 /* Should respond to UPLUG_REASON_QUERY here. */
96 * Doesn't return the proper token.
99 UPlugTokenReturn U_EXPORT2
myPluginFailToken (
102 UErrorCode
*status
) {
103 fprintf(stderr
,"MyPluginFailToken: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
105 if(reason
==UPLUG_REASON_QUERY
) {
106 uplug_setPlugName(data
, "myPluginFailToken Plugin");
107 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
110 return 0; /* Wrong. */
116 * Says it's low, but isn't.
119 UPlugTokenReturn U_EXPORT2
myPluginBad (
122 UErrorCode
*status
) {
123 fprintf(stderr
,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
125 if(reason
==UPLUG_REASON_QUERY
) {
126 uplug_setPlugName(data
, "Bad Plugin");
127 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
128 } else if(reason
== UPLUG_REASON_LOAD
) {
129 void *ctx
= uprv_malloc(12345);
131 uplug_setContext(data
, ctx
);
132 fprintf(stderr
,"I'm %p and I did a bad thing and malloced %p\n", (void*)data
, (void*)ctx
);
133 } else if(reason
== UPLUG_REASON_UNLOAD
) {
134 void * ctx
= uplug_getContext(data
);
144 UPlugTokenReturn U_EXPORT2
myPluginHigh (
147 UErrorCode
*status
) {
148 fprintf(stderr
,"MyPluginHigh: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
150 if(reason
==UPLUG_REASON_QUERY
) {
151 uplug_setPlugName(data
, "High Plugin");
152 uplug_setPlugLevel(data
, UPLUG_LEVEL_HIGH
);
159 /* Debug Memory Plugin (see hpmufn.c) */
160 static void * U_CALLCONV
myMemAlloc(const void *context
, size_t size
) {
161 void *retPtr
= (void *)malloc(size
);
162 (void)context
; /* unused */
163 fprintf(stderr
, "MEM: malloc(%d) = %p\n", (int32_t)size
, retPtr
);
167 static void U_CALLCONV
myMemFree(const void *context
, void *mem
) {
168 (void)context
; /* unused */
171 fprintf(stderr
, "MEM: free(%p)\n", mem
);
174 static void * U_CALLCONV
myMemRealloc(const void *context
, void *mem
, size_t size
) {
176 (void)context
; /* unused */
182 retPtr
= realloc(mem
, size
);
184 fprintf(stderr
, "MEM: realloc(%p, %d) = %p\n", mem
, (int32_t)size
, retPtr
);
189 UPlugTokenReturn U_EXPORT2
debugMemoryPlugin (
192 UErrorCode
*status
) {
193 fprintf(stderr
,"debugMemoryPlugin: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
195 if(reason
==UPLUG_REASON_QUERY
) {
196 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
197 uplug_setPlugName(data
, "Memory Plugin");
198 } else if(reason
==UPLUG_REASON_LOAD
) {
199 u_setMemoryFunctions(uplug_getContext(data
), &myMemAlloc
, &myMemRealloc
, &myMemFree
, status
);
200 fprintf(stderr
, "MEM: status now %s\n", u_errorName(*status
));
201 } else if(reason
==UPLUG_REASON_UNLOAD
) {
202 fprintf(stderr
, "MEM: not possible to unload this plugin (no way to reset memory functions)...\n");
203 uplug_setPlugNoUnload(data
, TRUE
);