1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <dlfcn.h>
typedef struct { const char* name; const char* version; const char* description; } PluginInfo;
typedef struct { PluginInfo* (*get_info)(void); int (*initialize)(void); void (*execute)(const char* input, char* output, size_t output_size); void (*cleanup)(void); } PluginInterface;
typedef struct PluginNode { char* name; void* handle; PluginInterface interface; struct PluginNode* next; } PluginNode;
typedef struct { PluginNode* plugins; int plugin_count; } PluginManager;
static PluginManager g_plugin_manager = {NULL, 0};
static PluginInfo text_plugin_info = { .name = "TextTransform", .version = "1.0.0", .description = "文本转换插件" };
PluginInfo* text_plugin_get_info(void) { return &text_plugin_info; }
int text_plugin_initialize(void) { printf("文本转换插件初始化\n"); return 1; }
void text_plugin_execute(const char* input, char* output, size_t output_size) { strncpy(output, input, output_size - 1); output[output_size - 1] = '\0'; for (char* p = output; *p; p++) { if (*p >= 'a' && *p <= 'z') { *p = *p - 'a' + 'A'; } } }
void text_plugin_cleanup(void) { printf("文本转换插件清理\n"); }
static PluginInfo math_plugin_info = { .name = "MathCalculator", .version = "2.1.0", .description = "数学计算插件" };
PluginInfo* math_plugin_get_info(void) { return &math_plugin_info; }
int math_plugin_initialize(void) { printf("数学计算插件初始化\n"); return 1; }
void math_plugin_execute(const char* input, char* output, size_t output_size) { int a, b; if (sscanf(input, "%d+%d", &a, &b) == 2) { snprintf(output, output_size, "%d", a + b); } else { snprintf(output, output_size, "错误:不支持的表达式"); } }
void math_plugin_cleanup(void) { printf("数学计算插件清理\n"); }
void register_static_plugin(const char* name, PluginInterface interface) { PluginNode* node = malloc(sizeof(PluginNode)); node->name = strdup(name); node->handle = NULL; node->interface = interface; node->next = g_plugin_manager.plugins; g_plugin_manager.plugins = node; g_plugin_manager.plugin_count++; if (interface.initialize) { interface.initialize(); } }
PluginNode* find_plugin(const char* name) { PluginNode* current = g_plugin_manager.plugins; while (current) { if (strcmp(current->name, name) == 0) { return current; } current = current->next; } return NULL; }
void execute_plugin(const char* plugin_name, const char* input) { PluginNode* plugin = find_plugin(plugin_name); if (!plugin) { printf("错误:找不到插件 '%s'\n", plugin_name); return; } char output[256]; if (plugin->interface.execute) { plugin->interface.execute(input, output, sizeof(output)); printf("插件 '%s' 执行结果: %s\n", plugin_name, output); } else { printf("错误:插件 '%s' 没有执行函数\n", plugin_name); } }
void list_plugins(void) { printf("\n已注册的插件:\n"); printf("%-15s %-10s %s\n", "名称", "版本", "描述"); printf("%-15s %-10s %s\n", "----", "----", "----"); PluginNode* current = g_plugin_manager.plugins; while (current) { if (current->interface.get_info) { PluginInfo* info = current->interface.get_info(); printf("%-15s %-10s %s\n", info->name, info->version, info->description); } current = current->next; } printf("\n总共 %d 个插件\n", g_plugin_manager.plugin_count); }
void cleanup_plugin_manager(void) { PluginNode* current = g_plugin_manager.plugins; while (current) { PluginNode* next = current->next; if (current->interface.cleanup) { current->interface.cleanup(); } if (current->handle) { dlclose(current->handle); } free(current->name); free(current); current = next; } g_plugin_manager.plugins = NULL; g_plugin_manager.plugin_count = 0; }
void plugin_system_demo() { printf("=== 插件系统演示 ===\n"); PluginInterface text_interface = { .get_info = text_plugin_get_info, .initialize = text_plugin_initialize, .execute = text_plugin_execute, .cleanup = text_plugin_cleanup }; PluginInterface math_interface = { .get_info = math_plugin_get_info, .initialize = math_plugin_initialize, .execute = math_plugin_execute, .cleanup = math_plugin_cleanup }; register_static_plugin("TextTransform", text_interface); register_static_plugin("MathCalculator", math_interface); list_plugins(); printf("\n执行插件示例:\n"); execute_plugin("TextTransform", "hello world"); execute_plugin("MathCalculator", "15+25"); execute_plugin("MathCalculator", "invalid expression"); execute_plugin("NonExistent", "test"); cleanup_plugin_manager(); }
int main() { plugin_system_demo(); return 0; }
|