Remove use of system(). Not very portable
[awesomized/libmemcached] / libmemcached / fetch.c
1 #include "common.h"
2
3 char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
4 size_t *value_length,
5 uint32_t *flags,
6 memcached_return_t *error)
7 {
8 memcached_result_st *result_buffer= &ptr->result;
9
10 unlikely (ptr->flags.use_udp)
11 {
12 *error= MEMCACHED_NOT_SUPPORTED;
13 return NULL;
14 }
15
16 result_buffer= memcached_fetch_result(ptr, result_buffer, error);
17
18 if (result_buffer == NULL || *error != MEMCACHED_SUCCESS)
19 {
20 WATCHPOINT_ASSERT(result_buffer == NULL);
21 *value_length= 0;
22 return NULL;
23 }
24
25 *value_length= memcached_string_length(&result_buffer->value);
26
27 if (key)
28 {
29 if (result_buffer->key_length > MEMCACHED_MAX_KEY)
30 {
31 *error= MEMCACHED_KEY_TOO_BIG;
32 *value_length= 0;
33
34 return NULL;
35 }
36 strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :(
37 *key_length= result_buffer->key_length;
38 }
39
40 *flags= result_buffer->item_flags;
41
42 return memcached_string_c_copy(&result_buffer->value);
43 }
44
45 memcached_result_st *memcached_fetch_result(memcached_st *ptr,
46 memcached_result_st *result,
47 memcached_return_t *error)
48 {
49 memcached_server_st *server;
50
51 unlikely (ptr->flags.use_udp)
52 {
53 *error= MEMCACHED_NOT_SUPPORTED;
54 return NULL;
55 }
56
57 if (result == NULL)
58 if ((result= memcached_result_create(ptr, NULL)) == NULL)
59 return NULL;
60
61 while ((server= memcached_io_get_readable_server(ptr)) != NULL)
62 {
63 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
64 *error= memcached_response(server, buffer, sizeof(buffer), result);
65
66 if (*error == MEMCACHED_SUCCESS)
67 return result;
68 else if (*error == MEMCACHED_END)
69 memcached_server_response_reset(server);
70 else if (*error != MEMCACHED_NOTFOUND)
71 break;
72 }
73
74 /* We have completed reading data */
75 if (memcached_is_allocated(result))
76 {
77 memcached_result_free(result);
78 }
79 else
80 {
81 memcached_string_reset(&result->value);
82 }
83
84 return NULL;
85 }
86
87 memcached_return_t memcached_fetch_execute(memcached_st *ptr,
88 memcached_execute_fn *callback,
89 void *context,
90 uint32_t number_of_callbacks)
91 {
92 memcached_result_st *result= &ptr->result;
93 memcached_return_t rc= MEMCACHED_FAILURE;
94
95 while ((result= memcached_fetch_result(ptr, result, &rc)) != NULL)
96 {
97 if (rc == MEMCACHED_SUCCESS)
98 {
99 for (uint32_t x= 0; x < number_of_callbacks; x++)
100 {
101 rc= (*callback[x])(ptr, result, context);
102 if (rc != MEMCACHED_SUCCESS)
103 break;
104 }
105 }
106 }
107 return rc;
108 }