Fix problem with bumping the command counter _before_ connecting to the server
[m6w6/libmemcached] / libmemcached / result.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: Functions to manipulate the result structure.
9 *
10 */
11
12 /*
13 memcached_result_st are used to internally represent the return values from
14 memcached. We use a structure so that long term as identifiers are added
15 to memcached we will be able to absorb new attributes without having
16 to addjust the entire API.
17 */
18 #include "common.h"
19
20 memcached_result_st *memcached_result_create(memcached_st *memc,
21 memcached_result_st *ptr)
22 {
23 WATCHPOINT_ASSERT(memc && memc->options.is_initialized);
24
25 /* Saving malloc calls :) */
26 if (ptr)
27 {
28 memset(ptr, 0, sizeof(memcached_result_st));
29 }
30 else
31 {
32 ptr= memc->call_calloc(memc, 1, sizeof(memcached_result_st));
33
34 if (ptr == NULL)
35 return NULL;
36 ptr->options.is_allocated= true;
37 }
38
39 ptr->options.is_initialized= true;
40
41 ptr->root= memc;
42 memcached_string_create(memc, &ptr->value, 0);
43 WATCHPOINT_ASSERT_INITIALIZED(&ptr->value);
44 WATCHPOINT_ASSERT(ptr->value.string == NULL);
45
46 return ptr;
47 }
48
49 void memcached_result_reset(memcached_result_st *ptr)
50 {
51 ptr->key_length= 0;
52 memcached_string_reset(&ptr->value);
53 ptr->flags= 0;
54 ptr->cas= 0;
55 ptr->expiration= 0;
56 }
57
58 /*
59 NOTE turn into macro
60 */
61 memcached_return_t memcached_result_set_value(memcached_result_st *ptr, const char *value, size_t length)
62 {
63 return memcached_string_append(&ptr->value, value, length);
64 }
65
66 void memcached_result_free(memcached_result_st *ptr)
67 {
68 if (ptr == NULL)
69 return;
70
71 memcached_string_free(&ptr->value);
72
73 if (memcached_is_allocated(ptr))
74 {
75 if (ptr->root != NULL)
76 {
77 ptr->root->call_free(ptr->root, ptr);
78 }
79 else
80 {
81 free(ptr);
82 }
83 }
84 else
85 {
86 ptr->options.is_initialized= false;
87 }
88 }
89
90
91 char *memcached_result_value(memcached_result_st *ptr)
92 {
93 memcached_string_st *sptr= &ptr->value;
94 return memcached_string_value(sptr);
95 }
96
97 size_t memcached_result_length(memcached_result_st *ptr)
98 {
99 memcached_string_st *sptr= &ptr->value;
100 return memcached_string_length(sptr);
101 }
102