Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / service / protocol-plugin / lib / cpluff / test / plugins-source / symuser / symuser.c
1 /*-------------------------------------------------------------------------
2  * C-Pluff, a plug-in framework for C
3  * Copyright 2007 Johannes Lehtinen
4  * 
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included
13  * in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *-----------------------------------------------------------------------*/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <cpluff.h>
28
29 typedef struct plugin_data_t plugin_data_t;
30
31 struct plugin_data_t {
32         cp_context_t *ctx;
33         const char *str;
34 };
35
36 static void *create(cp_context_t *ctx) {
37         plugin_data_t *data;
38         
39         if ((data = malloc(sizeof(plugin_data_t))) != NULL) {
40                 data->ctx = ctx;
41                 data->str = NULL;
42         }
43         return data;
44 }
45
46 static int start(void *d) {
47         plugin_data_t *data = d;
48         cp_extension_t **exts;
49         
50         exts = cp_get_extensions_info(data->ctx, "symuser.strings", NULL, NULL);
51         if (exts != NULL && exts[0] != NULL) {
52                 const char *symname;
53                 
54                 symname = cp_lookup_cfg_value(exts[0]->configuration, "@string-symbol");
55                 if (symname != NULL) {
56                         data->str = cp_resolve_symbol(data->ctx, exts[0]->plugin->identifier, symname, NULL);
57                         if (data->str == NULL) {
58                                 cp_log(data->ctx, CP_LOG_ERROR, "Could not resolve symbol specified by extension.");
59                         }
60                 } else {
61                         cp_log(data->ctx, CP_LOG_ERROR, "No string-symbol attribute present in extension.");
62                 } 
63         } else {
64                 cp_log(data->ctx, CP_LOG_ERROR, "No extensions available.");
65         }
66         if (exts != NULL) {
67                 cp_release_info(data->ctx, exts);
68         }
69         if (data->str == NULL) {
70                 return CP_ERR_RUNTIME;
71         }
72         return cp_define_symbol(data->ctx, "used_string", (void *) data->str);
73 }
74
75 static void stop(void *d) {
76         plugin_data_t *data = d;
77         
78         // Check that the provided string is still available
79         if (data->str != NULL) {
80                 if (strcmp(data->str, "Provided string")) {
81                         fputs("Provided string is not available in symuser stop function.\n", stderr);
82                         abort();
83                 }
84                 cp_release_symbol(data->ctx, data->str);
85         }
86 }
87
88 static void destroy(void *d) {
89         free(d);
90 }
91
92 CP_EXPORT cp_plugin_runtime_t su_runtime = {
93         create,
94         start,
95         stop,
96         destroy
97 };