Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* |
michael@0 | 2 | ****************************************************************************** |
michael@0 | 3 | * |
michael@0 | 4 | * Copyright (C) 2009-2010, International Business Machines |
michael@0 | 5 | * Corporation and others. All Rights Reserved. |
michael@0 | 6 | * |
michael@0 | 7 | ****************************************************************************** |
michael@0 | 8 | * |
michael@0 | 9 | * FILE NAME : testplug.c |
michael@0 | 10 | * |
michael@0 | 11 | * Date Name Description |
michael@0 | 12 | * 10/29/2009 srl New. |
michael@0 | 13 | ****************************************************************************** |
michael@0 | 14 | * |
michael@0 | 15 | * |
michael@0 | 16 | * This file implements a number of example ICU plugins. |
michael@0 | 17 | * |
michael@0 | 18 | */ |
michael@0 | 19 | |
michael@0 | 20 | #include "unicode/icuplug.h" |
michael@0 | 21 | #include <stdio.h> /* for fprintf */ |
michael@0 | 22 | #include <stdlib.h> /* for malloc */ |
michael@0 | 23 | #include "udbgutil.h" |
michael@0 | 24 | #include "unicode/uclean.h" |
michael@0 | 25 | #include "cmemory.h" |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * Prototypes |
michael@0 | 29 | */ |
michael@0 | 30 | #define DECLARE_PLUGIN(x) U_CAPI UPlugTokenReturn U_EXPORT2 x (UPlugData *data, UPlugReason reason, UErrorCode *status) |
michael@0 | 31 | |
michael@0 | 32 | DECLARE_PLUGIN(myPlugin); |
michael@0 | 33 | DECLARE_PLUGIN(myPluginLow); |
michael@0 | 34 | DECLARE_PLUGIN(myPluginFailQuery); |
michael@0 | 35 | DECLARE_PLUGIN(myPluginFailToken); |
michael@0 | 36 | DECLARE_PLUGIN(myPluginBad); |
michael@0 | 37 | DECLARE_PLUGIN(myPluginHigh); |
michael@0 | 38 | DECLARE_PLUGIN(debugMemoryPlugin); |
michael@0 | 39 | |
michael@0 | 40 | /** |
michael@0 | 41 | * A simple, trivial plugin. |
michael@0 | 42 | */ |
michael@0 | 43 | |
michael@0 | 44 | U_CAPI |
michael@0 | 45 | UPlugTokenReturn U_EXPORT2 myPlugin ( |
michael@0 | 46 | UPlugData *data, |
michael@0 | 47 | UPlugReason reason, |
michael@0 | 48 | UErrorCode *status) { |
michael@0 | 49 | /* Just print this for debugging */ |
michael@0 | 50 | fprintf(stderr,"MyPlugin: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); |
michael@0 | 51 | |
michael@0 | 52 | if(reason==UPLUG_REASON_QUERY) { |
michael@0 | 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. */ |
michael@0 | 54 | uplug_setPlugLevel(data, UPLUG_LEVEL_HIGH); /* This call is Mandatory in response to UPLUG_REASON_QUERY */ |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | return UPLUG_TOKEN; /* This must always be returned, to indicate that the entrypoint was actually a plugin. */ |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | |
michael@0 | 61 | U_CAPI |
michael@0 | 62 | UPlugTokenReturn U_EXPORT2 myPluginLow ( |
michael@0 | 63 | UPlugData *data, |
michael@0 | 64 | UPlugReason reason, |
michael@0 | 65 | UErrorCode *status) { |
michael@0 | 66 | fprintf(stderr,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); |
michael@0 | 67 | |
michael@0 | 68 | if(reason==UPLUG_REASON_QUERY) { |
michael@0 | 69 | uplug_setPlugName(data, "Low Plugin"); |
michael@0 | 70 | uplug_setPlugLevel(data, UPLUG_LEVEL_LOW); |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | return UPLUG_TOKEN; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | /** |
michael@0 | 77 | * Doesn't respond to QUERY properly. |
michael@0 | 78 | */ |
michael@0 | 79 | U_CAPI |
michael@0 | 80 | UPlugTokenReturn U_EXPORT2 myPluginFailQuery ( |
michael@0 | 81 | UPlugData *data, |
michael@0 | 82 | UPlugReason reason, |
michael@0 | 83 | UErrorCode *status) { |
michael@0 | 84 | fprintf(stderr,"MyPluginFailQuery: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); |
michael@0 | 85 | |
michael@0 | 86 | /* Should respond to UPLUG_REASON_QUERY here. */ |
michael@0 | 87 | |
michael@0 | 88 | return UPLUG_TOKEN; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | /** |
michael@0 | 92 | * Doesn't return the proper token. |
michael@0 | 93 | */ |
michael@0 | 94 | U_CAPI |
michael@0 | 95 | UPlugTokenReturn U_EXPORT2 myPluginFailToken ( |
michael@0 | 96 | UPlugData *data, |
michael@0 | 97 | UPlugReason reason, |
michael@0 | 98 | UErrorCode *status) { |
michael@0 | 99 | fprintf(stderr,"MyPluginFailToken: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); |
michael@0 | 100 | |
michael@0 | 101 | if(reason==UPLUG_REASON_QUERY) { |
michael@0 | 102 | uplug_setPlugName(data, "myPluginFailToken Plugin"); |
michael@0 | 103 | uplug_setPlugLevel(data, UPLUG_LEVEL_LOW); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | return 0; /* Wrong. */ |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | |
michael@0 | 110 | |
michael@0 | 111 | /** |
michael@0 | 112 | * Says it's low, but isn't. |
michael@0 | 113 | */ |
michael@0 | 114 | U_CAPI |
michael@0 | 115 | UPlugTokenReturn U_EXPORT2 myPluginBad ( |
michael@0 | 116 | UPlugData *data, |
michael@0 | 117 | UPlugReason reason, |
michael@0 | 118 | UErrorCode *status) { |
michael@0 | 119 | fprintf(stderr,"MyPluginLow: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); |
michael@0 | 120 | |
michael@0 | 121 | if(reason==UPLUG_REASON_QUERY) { |
michael@0 | 122 | uplug_setPlugName(data, "Bad Plugin"); |
michael@0 | 123 | uplug_setPlugLevel(data, UPLUG_LEVEL_LOW); |
michael@0 | 124 | } else if(reason == UPLUG_REASON_LOAD) { |
michael@0 | 125 | void *ctx = uprv_malloc(12345); |
michael@0 | 126 | |
michael@0 | 127 | uplug_setContext(data, ctx); |
michael@0 | 128 | fprintf(stderr,"I'm %p and I did a bad thing and malloced %p\n", (void*)data, (void*)ctx); |
michael@0 | 129 | } else if(reason == UPLUG_REASON_UNLOAD) { |
michael@0 | 130 | void * ctx = uplug_getContext(data); |
michael@0 | 131 | |
michael@0 | 132 | uprv_free(ctx); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | |
michael@0 | 136 | return UPLUG_TOKEN; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | U_CAPI |
michael@0 | 140 | UPlugTokenReturn U_EXPORT2 myPluginHigh ( |
michael@0 | 141 | UPlugData *data, |
michael@0 | 142 | UPlugReason reason, |
michael@0 | 143 | UErrorCode *status) { |
michael@0 | 144 | fprintf(stderr,"MyPluginHigh: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); |
michael@0 | 145 | |
michael@0 | 146 | if(reason==UPLUG_REASON_QUERY) { |
michael@0 | 147 | uplug_setPlugName(data, "High Plugin"); |
michael@0 | 148 | uplug_setPlugLevel(data, UPLUG_LEVEL_HIGH); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | return UPLUG_TOKEN; |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | |
michael@0 | 155 | /* Debug Memory Plugin (see hpmufn.c) */ |
michael@0 | 156 | static void * U_CALLCONV myMemAlloc(const void *context, size_t size) { |
michael@0 | 157 | void *retPtr = (void *)malloc(size); |
michael@0 | 158 | (void)context; /* unused */ |
michael@0 | 159 | fprintf(stderr, "MEM: malloc(%d) = %p\n", (int32_t)size, retPtr); |
michael@0 | 160 | return retPtr; |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | static void U_CALLCONV myMemFree(const void *context, void *mem) { |
michael@0 | 164 | (void)context; /* unused */ |
michael@0 | 165 | |
michael@0 | 166 | free(mem); |
michael@0 | 167 | fprintf(stderr, "MEM: free(%p)\n", mem); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | static void * U_CALLCONV myMemRealloc(const void *context, void *mem, size_t size) { |
michael@0 | 171 | void *retPtr; |
michael@0 | 172 | (void)context; /* unused */ |
michael@0 | 173 | |
michael@0 | 174 | |
michael@0 | 175 | if(mem==NULL) { |
michael@0 | 176 | retPtr = NULL; |
michael@0 | 177 | } else { |
michael@0 | 178 | retPtr = realloc(mem, size); |
michael@0 | 179 | } |
michael@0 | 180 | fprintf(stderr, "MEM: realloc(%p, %d) = %p\n", mem, (int32_t)size, retPtr); |
michael@0 | 181 | return retPtr; |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | U_CAPI |
michael@0 | 185 | UPlugTokenReturn U_EXPORT2 debugMemoryPlugin ( |
michael@0 | 186 | UPlugData *data, |
michael@0 | 187 | UPlugReason reason, |
michael@0 | 188 | UErrorCode *status) { |
michael@0 | 189 | fprintf(stderr,"debugMemoryPlugin: data=%p, reason=%s, status=%s\n", (void*)data, udbg_enumName(UDBG_UPlugReason,(int32_t)reason), u_errorName(*status)); |
michael@0 | 190 | |
michael@0 | 191 | if(reason==UPLUG_REASON_QUERY) { |
michael@0 | 192 | uplug_setPlugLevel(data, UPLUG_LEVEL_LOW); |
michael@0 | 193 | uplug_setPlugName(data, "Memory Plugin"); |
michael@0 | 194 | } else if(reason==UPLUG_REASON_LOAD) { |
michael@0 | 195 | u_setMemoryFunctions(uplug_getContext(data), &myMemAlloc, &myMemRealloc, &myMemFree, status); |
michael@0 | 196 | fprintf(stderr, "MEM: status now %s\n", u_errorName(*status)); |
michael@0 | 197 | } else if(reason==UPLUG_REASON_UNLOAD) { |
michael@0 | 198 | fprintf(stderr, "MEM: not possible to unload this plugin (no way to reset memory functions)...\n"); |
michael@0 | 199 | uplug_setPlugNoUnload(data, TRUE); |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | return UPLUG_TOKEN; |
michael@0 | 203 | } |
michael@0 | 204 |