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