Work inspired by matt knox
[m6w6/libmemcached] / libmemcached / memcached_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 "libmemcached/memcached.h"
9 #include "common.h"
10
11 memcached_server_st *memcached_servers_parse(const char *server_strings)
12 {
13 char *string;
14 unsigned int port;
15 uint32_t weight;
16 const char *begin_ptr;
17 const char *end_ptr;
18 memcached_server_st *servers= NULL;
19 memcached_return rc;
20
21 WATCHPOINT_ASSERT(server_strings);
22
23 end_ptr= server_strings + strlen(server_strings);
24
25 for (begin_ptr= server_strings, string= index(server_strings, ',');
26 begin_ptr != end_ptr;
27 string= index(begin_ptr, ','))
28 {
29 char buffer[HUGE_STRING_LEN];
30 char *ptr, *ptr2;
31 port= 0;
32 weight= 0;
33
34 if (string)
35 {
36 memcpy(buffer, begin_ptr, string - begin_ptr);
37 buffer[(unsigned int)(string - begin_ptr)]= 0;
38 begin_ptr= string+1;
39 }
40 else
41 {
42 size_t length= strlen(begin_ptr);
43 memcpy(buffer, begin_ptr, length);
44 buffer[length]= 0;
45 begin_ptr= end_ptr;
46 }
47
48 ptr= index(buffer, ':');
49
50 if (ptr)
51 {
52 ptr[0]= 0;
53
54 ptr++;
55
56 port= strtoul(ptr, (char **)NULL, 10);
57
58 ptr2= index(ptr, ' ');
59 if (! ptr2)
60 ptr2= index(ptr, ':');
61 if (ptr2)
62 {
63 ptr2++;
64 weight = strtoul(ptr2, (char **)NULL, 10);
65 }
66 }
67
68 servers= memcached_server_list_append_with_weight(servers, buffer, port, weight, &rc);
69
70 if (isspace(*begin_ptr))
71 begin_ptr++;
72 }
73
74 return servers;
75 }