Merge Monty
[m6w6/libmemcached] / libmemcached / memcached_callback.c
1 #include "common.h"
2 #include <sys/types.h>
3 #include <sys/socket.h>
4 #include <netinet/tcp.h>
5
6 /*
7 These functions provide data and function callback support
8 */
9
10 memcached_return memcached_callback_set(memcached_st *ptr,
11 memcached_callback flag,
12 void *data)
13 {
14 switch (flag)
15 {
16 case MEMCACHED_CALLBACK_PREFIX_KEY:
17 {
18 char *key= (char *)data;
19
20 if (key)
21 {
22 size_t key_length= strlen(key);
23
24 if (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)
25 {
26 return MEMCACHED_BAD_KEY_PROVIDED;
27 }
28
29 if ((key_length > MEMCACHED_PREFIX_KEY_MAX_SIZE -1)
30 || (strcpy(ptr->prefix_key, key) == NULL))
31 {
32 ptr->prefix_key_length= 0;
33 return MEMCACHED_BAD_KEY_PROVIDED;
34 }
35 else
36 {
37 ptr->prefix_key_length= key_length;
38 }
39 }
40 else
41 {
42 memset(ptr->prefix_key, 0, MEMCACHED_PREFIX_KEY_MAX_SIZE);
43 ptr->prefix_key_length= 0;
44 }
45
46 break;
47 }
48 case MEMCACHED_CALLBACK_USER_DATA:
49 {
50 ptr->user_data= data;
51 break;
52 }
53 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
54 {
55 memcached_cleanup_func func= *(memcached_cleanup_func *)&data;
56 ptr->on_cleanup= func;
57 break;
58 }
59 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
60 {
61 memcached_clone_func func= *(memcached_clone_func *)&data;
62 ptr->on_clone= func;
63 break;
64 }
65 #ifdef MEMCACHED_ENABLE_DEPRECATED
66 case MEMCACHED_CALLBACK_MALLOC_FUNCTION:
67 {
68 memcached_malloc_function func= *(memcached_malloc_function *)&data;
69 ptr->call_malloc= func;
70 break;
71 }
72 case MEMCACHED_CALLBACK_REALLOC_FUNCTION:
73 {
74 memcached_realloc_function func= *(memcached_realloc_function *)&data;
75 ptr->call_realloc= func;
76 break;
77 }
78 case MEMCACHED_CALLBACK_FREE_FUNCTION:
79 {
80 memcached_free_function func= *(memcached_free_function *)&data;
81 ptr->call_free= func;
82 break;
83 }
84 #endif
85 case MEMCACHED_CALLBACK_GET_FAILURE:
86 {
87 memcached_trigger_key func= *(memcached_trigger_key *)&data;
88 ptr->get_key_failure= func;
89 break;
90 }
91 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
92 {
93 memcached_trigger_delete_key func= *(memcached_trigger_delete_key *)&data;
94 ptr->delete_trigger= func;
95 break;
96 }
97 default:
98 return MEMCACHED_FAILURE;
99 }
100
101 return MEMCACHED_SUCCESS;
102 }
103
104 void *memcached_callback_get(memcached_st *ptr,
105 memcached_callback flag,
106 memcached_return *error)
107 {
108 memcached_return local_error;
109
110 if (!error)
111 error = &local_error;
112
113 switch (flag)
114 {
115 case MEMCACHED_CALLBACK_PREFIX_KEY:
116 {
117 if (ptr->prefix_key[0] == 0)
118 {
119 *error= MEMCACHED_FAILURE;
120 return NULL;
121 }
122 else
123 {
124 *error= MEMCACHED_SUCCESS;
125 return (void *)ptr->prefix_key;
126 }
127 }
128 case MEMCACHED_CALLBACK_USER_DATA:
129 {
130 *error= ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
131 return (void *)ptr->user_data;
132 }
133 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
134 {
135 *error= ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
136 return *(void **)&ptr->on_cleanup;
137 }
138 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
139 {
140 *error= ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
141 return *(void **)&ptr->on_clone;
142 }
143 #ifdef MEMCACHED_ENABLE_DEPRECATED
144 case MEMCACHED_CALLBACK_MALLOC_FUNCTION:
145 {
146 *error= ptr->call_malloc ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
147 return *(void **)&ptr->call_malloc;
148 }
149 case MEMCACHED_CALLBACK_REALLOC_FUNCTION:
150 {
151 *error= ptr->call_realloc ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
152 return *(void **)&ptr->call_realloc;
153 }
154 case MEMCACHED_CALLBACK_FREE_FUNCTION:
155 {
156 *error= ptr->call_free ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
157 return *(void **)&ptr->call_free;
158 }
159 #endif
160 case MEMCACHED_CALLBACK_GET_FAILURE:
161 {
162 *error= ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
163 return *(void **)&ptr->get_key_failure;
164 }
165 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
166 {
167 *error= ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
168 return *(void **)&ptr->delete_trigger;
169 }
170 default:
171 WATCHPOINT_ASSERT(0);
172 *error= MEMCACHED_FAILURE;
173 return NULL;
174 }
175 }