Broke library up into multiple files.
[m6w6/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 void memcached_server_add(memcached_st *ptr, char *server_name, unsigned int port)
28 {
29 }
30
31
32 memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_length,
33 time_t expiration)
34 {
35 size_t send_length;
36 memcached_return rc;
37 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
38
39 rc= memcached_connect(ptr);
40
41 if (expiration)
42 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
43 "delete %.*s %u\r\n", key_length, key, expiration);
44 else
45 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
46 "delete %.*s\r\n", key_length, key);
47 if ((write(ptr->fd, buffer, send_length) == -1))
48 {
49 fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
50
51 return MEMCACHED_WRITE_FAILURE;
52 }
53
54 return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
55 }
56
57 memcached_return memcached_increment(memcached_st *ptr, char *key, size_t key_length,
58 unsigned int count)
59 {
60 return MEMCACHED_SUCCESS;
61 }
62
63 memcached_return memcached_decrement(memcached_st *ptr, char *key, size_t key_length,
64 unsigned int count)
65 {
66 return MEMCACHED_SUCCESS;
67 }
68
69 memcached_return memcached_flush(memcached_st *ptr, time_t expiration)
70 {
71 size_t send_length;
72 memcached_return rc;
73 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
74
75 rc= memcached_connect(ptr);
76
77 if (expiration)
78 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
79 "flush_all %u\r\n", expiration);
80 else
81 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
82 "flush_all\r\n");
83 if ((send(ptr->fd, buffer, send_length, 0) == -1))
84 {
85 fprintf(stderr, "failed flush_all TCP\n");
86
87 return MEMCACHED_WRITE_FAILURE;
88 }
89
90 return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
91 }
92
93 char *memcached_version(memcached_st *ptr, memcached_return *error)
94 {
95 return MEMCACHED_SUCCESS;
96 }
97
98 memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity)
99 {
100 return MEMCACHED_SUCCESS;
101 }
102
103 memcached_return memcached_quit(memcached_st *ptr)
104 {
105 return MEMCACHED_SUCCESS;
106 }
107
108 void memcached_deinit(memcached_st *ptr)
109 {
110 if (ptr->fd == -1)
111 close(ptr->fd);
112
113 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
114 free(ptr);
115 else
116 memset(ptr, 0, sizeof(memcached_st));
117 }
118
119 char *memcached_strerror(memcached_st *ptr, memcached_return rc)
120 {
121 switch (rc)
122 {
123 case MEMCACHED_SUCCESS:
124 return "SUCCESS";
125 case MEMCACHED_FAILURE:
126 return "FAILURE";
127 case MEMCACHED_HOST_LOCKUP_FAILURE:
128 return "HOSTNAME LOOKUP FAILURE";
129 case MEMCACHED_CONNECTION_FAILURE:
130 return "CONNECTION FAILURE";
131 case MEMCACHED_CONNECTION_BIND_FAILURE:
132 return "CONNECTION BIND FAILURE";
133 case MEMCACHED_READ_FAILURE:
134 return "READ FAILURE";
135 case MEMCACHED_UNKNOWN_READ_FAILURE:
136 return "UNKNOWN READ FAILURE";
137 case MEMCACHED_PROTOCOL_ERROR:
138 return "PROTOCOL ERROR";
139 case MEMCACHED_CLIENT_ERROR:
140 return "CLIENT ERROR";
141 case MEMCACHED_SERVER_ERROR:
142 return "SERVER ERROR";
143 case MEMCACHED_WRITE_FAILURE:
144 return "WRITE FAILURE";
145 case MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE:
146 return "CONNECTION SOCKET CREATE FAILURE";
147 case MEMCACHED_DATA_EXISTS:
148 return "CONNECTION DATA EXISTS";
149 case MEMCACHED_DATA_DOES_NOT_EXIST:
150 return "CONNECTION DATA DOES NOT EXIST";
151 case MEMCACHED_NOTSTORED:
152 return "NOT STORED";
153 case MEMCACHED_NOTFOUND:
154 return "NOT FOUND";
155 case MEMCACHED_MEMORY_ALLOCATION_FAILURE:
156 return "MEMORY ALLOCATION FAILURE";
157 case MEMCACHED_PARTIAL_READ:
158 return "PARTIAL READ";
159 };
160 }