Fix problem with bumping the command counter _before_ connecting to the server
[m6w6/libmemcached] / libmemcached / parse.c
1 /*
2 I debated about putting this in the client library since it does an
3 action I don't really believe belongs in the library.
4
5 Frankly its too damn useful not to be here though.
6 */
7
8 #include "common.h"
9
10 memcached_server_st *memcached_servers_parse(const char *server_strings)
11 {
12 char *string;
13 uint32_t port;
14 uint32_t weight;
15 const char *begin_ptr;
16 const char *end_ptr;
17 memcached_server_st *servers= NULL;
18 memcached_return_t rc;
19
20 WATCHPOINT_ASSERT(server_strings);
21
22 end_ptr= server_strings + strlen(server_strings);
23
24 for (begin_ptr= server_strings, string= index(server_strings, ',');
25 begin_ptr != end_ptr;
26 string= index(begin_ptr, ','))
27 {
28 char buffer[HUGE_STRING_LEN];
29 char *ptr, *ptr2;
30 port= 0;
31 weight= 0;
32
33 if (string)
34 {
35 memcpy(buffer, begin_ptr, (size_t) (string - begin_ptr));
36 buffer[(unsigned int)(string - begin_ptr)]= 0;
37 begin_ptr= string+1;
38 }
39 else
40 {
41 size_t length= strlen(begin_ptr);
42 memcpy(buffer, begin_ptr, length);
43 buffer[length]= 0;
44 begin_ptr= end_ptr;
45 }
46
47 ptr= index(buffer, ':');
48
49 if (ptr)
50 {
51 ptr[0]= 0;
52
53 ptr++;
54
55 port= (uint32_t) strtoul(ptr, (char **)NULL, 10);
56
57 ptr2= index(ptr, ' ');
58 if (! ptr2)
59 ptr2= index(ptr, ':');
60 if (ptr2)
61 {
62 ptr2++;
63 weight = (uint32_t) strtoul(ptr2, (char **)NULL, 10);
64 }
65 }
66
67 servers= memcached_server_list_append_with_weight(servers, buffer, port, weight, &rc);
68
69 if (isspace(*begin_ptr))
70 begin_ptr++;
71 }
72
73 return servers;
74 }