2 ******************************************************************************
4 * Copyright (C) 2009-2010, 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"
21 #include <stdio.h> /* for fprintf */
22 #include <stdlib.h> /* for malloc */
24 #include "unicode/uclean.h"
30 #define DECLARE_PLUGIN(x) U_CAPI UPlugTokenReturn U_EXPORT2 x (UPlugData *data, UPlugReason reason, UErrorCode *status)
32 DECLARE_PLUGIN(myPlugin
);
33 DECLARE_PLUGIN(myPluginLow
);
34 DECLARE_PLUGIN(myPluginFailQuery
);
35 DECLARE_PLUGIN(myPluginFailToken
);
36 DECLARE_PLUGIN(myPluginBad
);
37 DECLARE_PLUGIN(myPluginHigh
);
38 DECLARE_PLUGIN(debugMemoryPlugin
);
41 * A simple, trivial plugin.
45 UPlugTokenReturn U_EXPORT2
myPlugin (
49 /* Just print this for debugging */
50 fprintf(stderr
,"MyPlugin: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
52 if(reason
==UPLUG_REASON_QUERY
) {
53 uplug_setPlugName(data
, "Just a Test High-Level Plugin"); /* This call is optional in response to UPLUG_REASON_QUERY, but is a good idea. */
54 uplug_setPlugLevel(data
, UPLUG_LEVEL_HIGH
); /* This call is Mandatory in response to UPLUG_REASON_QUERY */
57 return UPLUG_TOKEN
; /* This must always be returned, to indicate that the entrypoint was actually a plugin. */
62 UPlugTokenReturn U_EXPORT2
myPluginLow (
66 fprintf(stderr
,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
68 if(reason
==UPLUG_REASON_QUERY
) {
69 uplug_setPlugName(data
, "Low Plugin");
70 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
77 * Doesn't respond to QUERY properly.
80 UPlugTokenReturn U_EXPORT2
myPluginFailQuery (
84 fprintf(stderr
,"MyPluginFailQuery: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
86 /* Should respond to UPLUG_REASON_QUERY here. */
92 * Doesn't return the proper token.
95 UPlugTokenReturn U_EXPORT2
myPluginFailToken (
99 fprintf(stderr
,"MyPluginFailToken: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
101 if(reason
==UPLUG_REASON_QUERY
) {
102 uplug_setPlugName(data
, "myPluginFailToken Plugin");
103 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
106 return 0; /* Wrong. */
112 * Says it's low, but isn't.
115 UPlugTokenReturn U_EXPORT2
myPluginBad (
118 UErrorCode
*status
) {
119 fprintf(stderr
,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
121 if(reason
==UPLUG_REASON_QUERY
) {
122 uplug_setPlugName(data
, "Bad Plugin");
123 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
124 } else if(reason
== UPLUG_REASON_LOAD
) {
125 void *ctx
= uprv_malloc(12345);
127 uplug_setContext(data
, ctx
);
128 fprintf(stderr
,"I'm %p and I did a bad thing and malloced %p\n", (void*)data
, (void*)ctx
);
129 } else if(reason
== UPLUG_REASON_UNLOAD
) {
130 void * ctx
= uplug_getContext(data
);
140 UPlugTokenReturn U_EXPORT2
myPluginHigh (
143 UErrorCode
*status
) {
144 fprintf(stderr
,"MyPluginHigh: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
146 if(reason
==UPLUG_REASON_QUERY
) {
147 uplug_setPlugName(data
, "High Plugin");
148 uplug_setPlugLevel(data
, UPLUG_LEVEL_HIGH
);
155 /* Debug Memory Plugin (see hpmufn.c) */
156 static void * U_CALLCONV
myMemAlloc(const void *context
, size_t size
) {
157 void *retPtr
= (void *)malloc(size
);
158 (void)context
; /* unused */
159 fprintf(stderr
, "MEM: malloc(%d) = %p\n", (int32_t)size
, retPtr
);
163 static void U_CALLCONV
myMemFree(const void *context
, void *mem
) {
164 (void)context
; /* unused */
167 fprintf(stderr
, "MEM: free(%p)\n", mem
);
170 static void * U_CALLCONV
myMemRealloc(const void *context
, void *mem
, size_t size
) {
172 (void)context
; /* unused */
178 retPtr
= realloc(mem
, size
);
180 fprintf(stderr
, "MEM: realloc(%p, %d) = %p\n", mem
, (int32_t)size
, retPtr
);
185 UPlugTokenReturn U_EXPORT2
debugMemoryPlugin (
188 UErrorCode
*status
) {
189 fprintf(stderr
,"debugMemoryPlugin: data=%p, reason=%s, status=%s\n", (void*)data
, udbg_enumName(UDBG_UPlugReason
,(int32_t)reason
), u_errorName(*status
));
191 if(reason
==UPLUG_REASON_QUERY
) {
192 uplug_setPlugLevel(data
, UPLUG_LEVEL_LOW
);
193 uplug_setPlugName(data
, "Memory Plugin");
194 } else if(reason
==UPLUG_REASON_LOAD
) {
195 u_setMemoryFunctions(uplug_getContext(data
), &myMemAlloc
, &myMemRealloc
, &myMemFree
, status
);
196 fprintf(stderr
, "MEM: status now %s\n", u_errorName(*status
));
197 } else if(reason
==UPLUG_REASON_UNLOAD
) {
198 fprintf(stderr
, "MEM: not possible to unload this plugin (no way to reset memory functions)...\n");
199 uplug_setPlugNoUnload(data
, TRUE
);