5b0b7831cd8b7fef75d43e806cfa0c30393bde90
[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 "common.h"
9 #include "memcached_io.h"
10
11 memcached_return memcached_response(memcached_st *ptr,
12 char *buffer, size_t buffer_length,
13 unsigned int server_key)
14 {
15 unsigned int x;
16 size_t send_length;
17 char *buffer_ptr;
18
19 memset(buffer, 0, buffer_length);
20 send_length= 0;
21
22 for (x= 0; x <= ptr->stack_responses; x++)
23 {
24 buffer_ptr= buffer;
25 while (1)
26 {
27 unsigned int read_length;
28
29 read_length= memcached_io_read(ptr, server_key,
30 buffer_ptr, 1);
31
32 if (read_length != 1)
33 return MEMCACHED_UNKNOWN_READ_FAILURE;
34
35 if (*buffer_ptr == '\n')
36 break;
37 else
38 buffer_ptr++;
39 }
40 }
41 ptr->stack_responses= 0;
42
43 switch(buffer[0])
44 {
45 case 'V': /* VALUE */
46 return MEMCACHED_SUCCESS;
47 case 'O': /* OK */
48 return MEMCACHED_SUCCESS;
49 case 'S': /* STORED STATS SERVER_ERROR */
50 {
51 if (buffer[2] == 'A') /* STORED STATS */
52 return MEMCACHED_STAT;
53 else if (buffer[1] == 'E')
54 return MEMCACHED_SERVER_ERROR;
55 else if (buffer[1] == 'T')
56 return MEMCACHED_STORED;
57 else
58 return MEMCACHED_UNKNOWN_READ_FAILURE;
59 }
60 case 'D': /* DELETED */
61 return MEMCACHED_DELETED;
62 case 'N': /* NOT_FOUND */
63 {
64 if (buffer[4] == 'F')
65 return MEMCACHED_NOTFOUND;
66 else if (buffer[4] == 'S')
67 return MEMCACHED_NOTSTORED;
68 else
69 return MEMCACHED_UNKNOWN_READ_FAILURE;
70 }
71 case 'E': /* PROTOCOL ERROR or END */
72 {
73 if (buffer[1] == 'N')
74 return MEMCACHED_END;
75 else if (buffer[1] == 'R')
76 return MEMCACHED_PROTOCOL_ERROR;
77 else
78 return MEMCACHED_UNKNOWN_READ_FAILURE;
79 }
80 case 'C': /* CLIENT ERROR */
81 return MEMCACHED_CLIENT_ERROR;
82 default:
83 return MEMCACHED_UNKNOWN_READ_FAILURE;
84
85 }
86
87 return MEMCACHED_SUCCESS;
88 }