memcached_server_add() now works so you can connect to host other then
[awesomized/libmemcached] / lib / memcached.c
1 /*
2 Memcached library
3 */
4 #include <memcached.h>
5
6 memcached_st *memcached_init(memcached_st *ptr)
7 {
8 if (!ptr)
9 {
10 ptr= (memcached_st *)malloc(sizeof(memcached_st));
11
12 if (!ptr)
13 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
14
15 memset(ptr, 0, sizeof(memcached_st));
16 ptr->is_allocated= MEMCACHED_ALLOCATED;
17 }
18 else
19 {
20 memset(ptr, 0, sizeof(memcached_st));
21 }
22 ptr->fd= -1;
23
24 return ptr;
25 }
26
27 memcached_return memcached_increment(memcached_st *ptr, char *key, size_t key_length,
28 unsigned int count)
29 {
30 return MEMCACHED_SUCCESS;
31 }
32
33 memcached_return memcached_decrement(memcached_st *ptr, char *key, size_t key_length,
34 unsigned int count)
35 {
36 return MEMCACHED_SUCCESS;
37 }
38
39 memcached_return memcached_flush(memcached_st *ptr, time_t expiration)
40 {
41 size_t send_length;
42 memcached_return rc;
43 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
44
45 rc= memcached_connect(ptr);
46
47 if (expiration)
48 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
49 "flush_all %u\r\n", expiration);
50 else
51 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
52 "flush_all\r\n");
53 if ((send(ptr->fd, buffer, send_length, 0) == -1))
54 {
55 fprintf(stderr, "failed flush_all TCP\n");
56
57 return MEMCACHED_WRITE_FAILURE;
58 }
59
60 return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
61 }
62
63 char *memcached_version(memcached_st *ptr, memcached_return *error)
64 {
65 return MEMCACHED_SUCCESS;
66 }
67
68 memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity)
69 {
70 return MEMCACHED_SUCCESS;
71 }
72
73 memcached_return memcached_quit(memcached_st *ptr)
74 {
75 return MEMCACHED_SUCCESS;
76 }
77
78 void memcached_deinit(memcached_st *ptr)
79 {
80 memcached_host_st *host_ptr;
81
82 if (ptr->fd > 0)
83 close(ptr->fd);
84
85 for (host_ptr= ptr->hosts; host_ptr;)
86 {
87 memcached_host_st *temp;
88
89 temp= host_ptr;
90 host_ptr= host_ptr->next;
91 if (temp->hostname)
92 free(temp->hostname);
93 free(temp);
94 }
95
96 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
97 free(ptr);
98 else
99 memset(ptr, 0, sizeof(memcached_st));
100 }
101
102 char *memcached_strerror(memcached_st *ptr, memcached_return rc)
103 {
104 switch (rc)
105 {
106 case MEMCACHED_SUCCESS:
107 return "SUCCESS";
108 case MEMCACHED_FAILURE:
109 return "FAILURE";
110 case MEMCACHED_HOST_LOCKUP_FAILURE:
111 return "HOSTNAME LOOKUP FAILURE";
112 case MEMCACHED_CONNECTION_FAILURE:
113 return "CONNECTION FAILURE";
114 case MEMCACHED_CONNECTION_BIND_FAILURE:
115 return "CONNECTION BIND FAILURE";
116 case MEMCACHED_READ_FAILURE:
117 return "READ FAILURE";
118 case MEMCACHED_UNKNOWN_READ_FAILURE:
119 return "UNKNOWN READ FAILURE";
120 case MEMCACHED_PROTOCOL_ERROR:
121 return "PROTOCOL ERROR";
122 case MEMCACHED_CLIENT_ERROR:
123 return "CLIENT ERROR";
124 case MEMCACHED_SERVER_ERROR:
125 return "SERVER ERROR";
126 case MEMCACHED_WRITE_FAILURE:
127 return "WRITE FAILURE";
128 case MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE:
129 return "CONNECTION SOCKET CREATE FAILURE";
130 case MEMCACHED_DATA_EXISTS:
131 return "CONNECTION DATA EXISTS";
132 case MEMCACHED_DATA_DOES_NOT_EXIST:
133 return "CONNECTION DATA DOES NOT EXIST";
134 case MEMCACHED_NOTSTORED:
135 return "NOT STORED";
136 case MEMCACHED_NOTFOUND:
137 return "NOT FOUND";
138 case MEMCACHED_MEMORY_ALLOCATION_FAILURE:
139 return "MEMORY ALLOCATION FAILURE";
140 case MEMCACHED_PARTIAL_READ:
141 return "PARTIAL READ";
142 };
143 }