52afbb947117f37b55e0db5a427a832b6860fe60
[awesomized/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 ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
25 return MEMCACHED_BAD_KEY_PROVIDED;
26
27 if ((ptr->prefix_key_length > MEMCACHED_PREFIX_KEY_MAX_SIZE -1)
28 || (strcpy(ptr->prefix_key, key) == NULL))
29 {
30 ptr->prefix_key_length= 0;
31 return MEMCACHED_BAD_KEY_PROVIDED;
32 }
33 }
34 else
35 {
36 memset(ptr->prefix_key, 0, MEMCACHED_PREFIX_KEY_MAX_SIZE);
37 ptr->prefix_key_length= 0;
38 }
39
40 break;
41 }
42 case MEMCACHED_CALLBACK_USER_DATA:
43 {
44 ptr->user_data= data;
45 break;
46 }
47 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
48 {
49 memcached_cleanup_func func= (memcached_cleanup_func)data;
50 ptr->on_cleanup= func;
51 break;
52 }
53 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
54 {
55 memcached_clone_func func= (memcached_clone_func)data;
56 ptr->on_clone= func;
57 break;
58 }
59 case MEMCACHED_CALLBACK_MALLOC_FUNCTION:
60 {
61 memcached_malloc_function func= (memcached_malloc_function)data;
62 ptr->call_malloc= func;
63 break;
64 }
65 case MEMCACHED_CALLBACK_REALLOC_FUNCTION:
66 {
67 memcached_realloc_function func= (memcached_realloc_function)data;
68 ptr->call_realloc= func;
69 break;
70 }
71 case MEMCACHED_CALLBACK_FREE_FUNCTION:
72 {
73 memcached_free_function func= (memcached_free_function)data;
74 ptr->call_free= func;
75 break;
76 }
77 case MEMCACHED_CALLBACK_GET_FAILURE:
78 {
79 memcached_trigger_key func= (memcached_trigger_key)data;
80 ptr->get_key_failure= func;
81 break;
82 }
83 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
84 {
85 memcached_trigger_delete_key func= (memcached_trigger_delete_key)data;
86 ptr->delete_trigger= func;
87 break;
88 }
89 default:
90 return MEMCACHED_FAILURE;
91 }
92
93 return MEMCACHED_SUCCESS;
94 }
95
96 void *memcached_callback_get(memcached_st *ptr,
97 memcached_callback flag,
98 memcached_return *error)
99 {
100 memcached_return local_error;
101 if (!error)
102 error = &local_error;
103
104 switch (flag)
105 {
106 case MEMCACHED_CALLBACK_PREFIX_KEY:
107 {
108 if (ptr->prefix_key[0] == 0)
109 {
110 *error= MEMCACHED_FAILURE;
111 return NULL;
112 }
113 else
114 {
115 *error= MEMCACHED_SUCCESS;
116 return (void *)ptr->prefix_key;
117 }
118 }
119 case MEMCACHED_CALLBACK_USER_DATA:
120 {
121 *error= ptr->user_data ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
122 return (void *)ptr->user_data;
123 }
124 case MEMCACHED_CALLBACK_CLEANUP_FUNCTION:
125 {
126 *error= ptr->on_cleanup ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
127 return (void *)ptr->on_cleanup;
128 }
129 case MEMCACHED_CALLBACK_CLONE_FUNCTION:
130 {
131 *error= ptr->on_clone ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
132 return (void *)ptr->on_clone;
133 }
134 case MEMCACHED_CALLBACK_MALLOC_FUNCTION:
135 {
136 *error= ptr->call_malloc ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
137 return (void *)ptr->call_malloc;
138 }
139 case MEMCACHED_CALLBACK_REALLOC_FUNCTION:
140 {
141 *error= ptr->call_realloc ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
142 return (void *)ptr->call_realloc;
143 }
144 case MEMCACHED_CALLBACK_FREE_FUNCTION:
145 {
146 *error= ptr->call_free ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
147 return (void *)ptr->call_free;
148 }
149 case MEMCACHED_CALLBACK_GET_FAILURE:
150 {
151 *error= ptr->get_key_failure ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
152 return (void *)ptr->get_key_failure;
153 }
154 case MEMCACHED_CALLBACK_DELETE_TRIGGER:
155 {
156 *error= ptr->delete_trigger ? MEMCACHED_SUCCESS : MEMCACHED_FAILURE;
157 return (void *)ptr->delete_trigger;
158 }
159 default:
160 WATCHPOINT_ASSERT(0);
161 *error= MEMCACHED_FAILURE;
162 return NULL;
163 }
164 }