Adding buffered IO to reads
[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
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 send_length= 0;
19
20 buffer_ptr= buffer;
21 while (1)
22 {
23 unsigned int read_length;
24
25 read_length= memcached_io_read(ptr, server_key,
26 buffer_ptr, 1);
27
28 if (read_length != 1)
29 return MEMCACHED_UNKNOWN_READ_FAILURE;
30
31 if (*buffer_ptr == '\n')
32 break;
33 else
34 buffer_ptr++;
35 }
36
37 switch(buffer[0])
38 {
39 case 'V': /* VALUE */
40 return MEMCACHED_SUCCESS;
41 case 'O': /* OK */
42 return MEMCACHED_SUCCESS;
43 case 'S': /* STORED STATS SERVER_ERROR */
44 {
45 if (buffer[1] == 'T') /* STORED STATS */
46 return MEMCACHED_SUCCESS;
47 else if (buffer[1] == 'E')
48 return MEMCACHED_SERVER_ERROR;
49 else
50 return MEMCACHED_UNKNOWN_READ_FAILURE;
51 }
52 case 'D': /* DELETED */
53 return MEMCACHED_DELETED;
54 case 'N': /* NOT_FOUND */
55 {
56 if (buffer[4] == 'F')
57 return MEMCACHED_NOTFOUND;
58 else if (buffer[4] == 'S')
59 return MEMCACHED_NOTSTORED;
60 else
61 return MEMCACHED_UNKNOWN_READ_FAILURE;
62 }
63 case 'E': /* PROTOCOL ERROR or END */
64 {
65 if (buffer[1] == 'N')
66 return MEMCACHED_END;
67 else if (buffer[1] == 'R')
68 return MEMCACHED_PROTOCOL_ERROR;
69 else
70 return MEMCACHED_UNKNOWN_READ_FAILURE;
71 }
72 case 'C': /* CLIENT ERROR */
73 return MEMCACHED_CLIENT_ERROR;
74 default:
75 return MEMCACHED_UNKNOWN_READ_FAILURE;
76
77 }
78
79 return MEMCACHED_SUCCESS;
80 }