Fixed merge
[awesomized/libmemcached] / lib / memcached_response.c
1 /*
2 Memcached library
3
4 memcached_response() is used to determine the return result
5 from an issued command.
6 */
7
8 #include <memcached.h>
9
10 memcached_return memcached_response(memcached_st *ptr,
11 char *buffer, size_t buffer_length,
12 unsigned int server_key)
13 {
14 size_t send_length;
15 char *buffer_ptr;
16
17 memset(buffer, 0, buffer_length);
18
19 buffer_ptr= buffer;
20 while (1)
21 {
22 unsigned int read_length;
23 read_length= read(ptr->hosts[server_key].fd, buffer_ptr, 1);
24
25 if (read_length != 1)
26 return MEMCACHED_UNKNOWN_READ_FAILURE;
27
28 if (*buffer_ptr == '\n')
29 break;
30 else
31 buffer_ptr++;
32 }
33
34 if (send_length)
35 switch(buffer[0])
36 {
37 case 'V': /* VALUE */
38 return MEMCACHED_SUCCESS;
39 case 'O': /* OK */
40 return MEMCACHED_SUCCESS;
41 case 'S': /* STORED STATS SERVER_ERROR */
42 {
43 if (buffer[1] == 'T') /* STORED STATS */
44 return MEMCACHED_SUCCESS;
45 else if (buffer[1] == 'E')
46 return MEMCACHED_SERVER_ERROR;
47 else
48 return MEMCACHED_UNKNOWN_READ_FAILURE;
49 }
50 case 'D': /* DELETED */
51 return MEMCACHED_SUCCESS;
52 case 'N': /* NOT_FOUND */
53 {
54 if (buffer[4] == 'F')
55 return MEMCACHED_NOTFOUND;
56 else if (buffer[4] == 'S')
57 return MEMCACHED_NOTSTORED;
58 else
59 return MEMCACHED_UNKNOWN_READ_FAILURE;
60 }
61 case 'E': /* PROTOCOL ERROR or END */
62 {
63 if (buffer[1] == 'N')
64 return MEMCACHED_NOTFOUND;
65 else if (buffer[1] == 'R')
66 return MEMCACHED_PROTOCOL_ERROR;
67 else
68 return MEMCACHED_UNKNOWN_READ_FAILURE;
69 }
70 case 'C': /* CLIENT ERROR */
71 return MEMCACHED_CLIENT_ERROR;
72 default:
73 return MEMCACHED_UNKNOWN_READ_FAILURE;
74 }
75
76 return MEMCACHED_READ_FAILURE;
77 }