michael@0: /* michael@0: ****************************************************************************** michael@0: * michael@0: * Copyright (C) 2009-2010, International Business Machines michael@0: * Corporation and others. All Rights Reserved. michael@0: * michael@0: ****************************************************************************** michael@0: * michael@0: * FILE NAME : testplug.c michael@0: * michael@0: * Date Name Description michael@0: * 10/29/2009 srl New. michael@0: ****************************************************************************** michael@0: * michael@0: * michael@0: * This file implements a number of example ICU plugins. michael@0: * michael@0: */ michael@0: michael@0: #include "unicode/icuplug.h" michael@0: #include /* for fprintf */ michael@0: #include /* for malloc */ michael@0: #include "udbgutil.h" michael@0: #include "unicode/uclean.h" michael@0: #include "cmemory.h" michael@0: michael@0: /** michael@0: * Prototypes michael@0: */ michael@0: #define DECLARE_PLUGIN(x) U_CAPI UPlugTokenReturn U_EXPORT2 x (UPlugData *data, UPlugReason reason, UErrorCode *status) michael@0: michael@0: DECLARE_PLUGIN(myPlugin); michael@0: DECLARE_PLUGIN(myPluginLow); michael@0: DECLARE_PLUGIN(myPluginFailQuery); michael@0: DECLARE_PLUGIN(myPluginFailToken); michael@0: DECLARE_PLUGIN(myPluginBad); michael@0: DECLARE_PLUGIN(myPluginHigh); michael@0: DECLARE_PLUGIN(debugMemoryPlugin); michael@0: michael@0: /** michael@0: * A simple, trivial plugin. michael@0: */ michael@0: michael@0: U_CAPI michael@0: UPlugTokenReturn U_EXPORT2 myPlugin ( michael@0: UPlugData *data, michael@0: UPlugReason reason, michael@0: UErrorCode *status) { michael@0: /* Just print this for debugging */ michael@0: fprintf(stderr,"MyPlugin: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); michael@0: michael@0: if(reason==UPLUG_REASON_QUERY) { michael@0: uplug_setPlugName(data, "Just a Test High-Level Plugin"); /* This call is optional in response to UPLUG_REASON_QUERY, but is a good idea. */ michael@0: uplug_setPlugLevel(data, UPLUG_LEVEL_HIGH); /* This call is Mandatory in response to UPLUG_REASON_QUERY */ michael@0: } michael@0: michael@0: return UPLUG_TOKEN; /* This must always be returned, to indicate that the entrypoint was actually a plugin. */ michael@0: } michael@0: michael@0: michael@0: U_CAPI michael@0: UPlugTokenReturn U_EXPORT2 myPluginLow ( michael@0: UPlugData *data, michael@0: UPlugReason reason, michael@0: UErrorCode *status) { michael@0: fprintf(stderr,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); michael@0: michael@0: if(reason==UPLUG_REASON_QUERY) { michael@0: uplug_setPlugName(data, "Low Plugin"); michael@0: uplug_setPlugLevel(data, UPLUG_LEVEL_LOW); michael@0: } michael@0: michael@0: return UPLUG_TOKEN; michael@0: } michael@0: michael@0: /** michael@0: * Doesn't respond to QUERY properly. michael@0: */ michael@0: U_CAPI michael@0: UPlugTokenReturn U_EXPORT2 myPluginFailQuery ( michael@0: UPlugData *data, michael@0: UPlugReason reason, michael@0: UErrorCode *status) { michael@0: fprintf(stderr,"MyPluginFailQuery: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); michael@0: michael@0: /* Should respond to UPLUG_REASON_QUERY here. */ michael@0: michael@0: return UPLUG_TOKEN; michael@0: } michael@0: michael@0: /** michael@0: * Doesn't return the proper token. michael@0: */ michael@0: U_CAPI michael@0: UPlugTokenReturn U_EXPORT2 myPluginFailToken ( michael@0: UPlugData *data, michael@0: UPlugReason reason, michael@0: UErrorCode *status) { michael@0: fprintf(stderr,"MyPluginFailToken: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); michael@0: michael@0: if(reason==UPLUG_REASON_QUERY) { michael@0: uplug_setPlugName(data, "myPluginFailToken Plugin"); michael@0: uplug_setPlugLevel(data, UPLUG_LEVEL_LOW); michael@0: } michael@0: michael@0: return 0; /* Wrong. */ michael@0: } michael@0: michael@0: michael@0: michael@0: /** michael@0: * Says it's low, but isn't. michael@0: */ michael@0: U_CAPI michael@0: UPlugTokenReturn U_EXPORT2 myPluginBad ( michael@0: UPlugData *data, michael@0: UPlugReason reason, michael@0: UErrorCode *status) { michael@0: fprintf(stderr,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); michael@0: michael@0: if(reason==UPLUG_REASON_QUERY) { michael@0: uplug_setPlugName(data, "Bad Plugin"); michael@0: uplug_setPlugLevel(data, UPLUG_LEVEL_LOW); michael@0: } else if(reason == UPLUG_REASON_LOAD) { michael@0: void *ctx = uprv_malloc(12345); michael@0: michael@0: uplug_setContext(data, ctx); michael@0: fprintf(stderr,"I'm %p and I did a bad thing and malloced %p\n", (void*)data, (void*)ctx); michael@0: } else if(reason == UPLUG_REASON_UNLOAD) { michael@0: void * ctx = uplug_getContext(data); michael@0: michael@0: uprv_free(ctx); michael@0: } michael@0: michael@0: michael@0: return UPLUG_TOKEN; michael@0: } michael@0: michael@0: U_CAPI michael@0: UPlugTokenReturn U_EXPORT2 myPluginHigh ( michael@0: UPlugData *data, michael@0: UPlugReason reason, michael@0: UErrorCode *status) { michael@0: fprintf(stderr,"MyPluginHigh: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); michael@0: michael@0: if(reason==UPLUG_REASON_QUERY) { michael@0: uplug_setPlugName(data, "High Plugin"); michael@0: uplug_setPlugLevel(data, UPLUG_LEVEL_HIGH); michael@0: } michael@0: michael@0: return UPLUG_TOKEN; michael@0: } michael@0: michael@0: michael@0: /* Debug Memory Plugin (see hpmufn.c) */ michael@0: static void * U_CALLCONV myMemAlloc(const void *context, size_t size) { michael@0: void *retPtr = (void *)malloc(size); michael@0: (void)context; /* unused */ michael@0: fprintf(stderr, "MEM: malloc(%d) = %p\n", (int32_t)size, retPtr); michael@0: return retPtr; michael@0: } michael@0: michael@0: static void U_CALLCONV myMemFree(const void *context, void *mem) { michael@0: (void)context; /* unused */ michael@0: michael@0: free(mem); michael@0: fprintf(stderr, "MEM: free(%p)\n", mem); michael@0: } michael@0: michael@0: static void * U_CALLCONV myMemRealloc(const void *context, void *mem, size_t size) { michael@0: void *retPtr; michael@0: (void)context; /* unused */ michael@0: michael@0: michael@0: if(mem==NULL) { michael@0: retPtr = NULL; michael@0: } else { michael@0: retPtr = realloc(mem, size); michael@0: } michael@0: fprintf(stderr, "MEM: realloc(%p, %d) = %p\n", mem, (int32_t)size, retPtr); michael@0: return retPtr; michael@0: } michael@0: michael@0: U_CAPI michael@0: UPlugTokenReturn U_EXPORT2 debugMemoryPlugin ( michael@0: UPlugData *data, michael@0: UPlugReason reason, michael@0: UErrorCode *status) { michael@0: fprintf(stderr,"debugMemoryPlugin: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); michael@0: michael@0: if(reason==UPLUG_REASON_QUERY) { michael@0: uplug_setPlugLevel(data, UPLUG_LEVEL_LOW); michael@0: uplug_setPlugName(data, "Memory Plugin"); michael@0: } else if(reason==UPLUG_REASON_LOAD) { michael@0: u_setMemoryFunctions(uplug_getContext(data), &myMemAlloc, &myMemRealloc, &myMemFree, status); michael@0: fprintf(stderr, "MEM: status now %s\n", u_errorName(*status)); michael@0: } else if(reason==UPLUG_REASON_UNLOAD) { michael@0: fprintf(stderr, "MEM: not possible to unload this plugin (no way to reset memory functions)...\n"); michael@0: uplug_setPlugNoUnload(data, TRUE); michael@0: } michael@0: michael@0: return UPLUG_TOKEN; michael@0: } michael@0: