Merge in updates (including removal of some depcrated bits from the examples).
[awesomized/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_list_st memcached_servers_parse(const char *server_strings)
11 {
12 char *string;
13 const char *begin_ptr;
14 const char *end_ptr;
15 memcached_server_st *servers= NULL;
16 memcached_return_t rc;
17
18 WATCHPOINT_ASSERT(server_strings);
19
20 end_ptr= server_strings + strlen(server_strings);
21
22 for (begin_ptr= server_strings, string= index(server_strings, ',');
23 begin_ptr != end_ptr;
24 string= index(begin_ptr, ','))
25 {
26 char buffer[HUGE_STRING_LEN];
27 char *ptr, *ptr2;
28 uint32_t weight= 0;
29
30 if (string)
31 {
32 memcpy(buffer, begin_ptr, (size_t) (string - begin_ptr));
33 buffer[(unsigned int)(string - begin_ptr)]= 0;
34 begin_ptr= string+1;
35 }
36 else
37 {
38 size_t length= strlen(begin_ptr);
39 memcpy(buffer, begin_ptr, length);
40 buffer[length]= 0;
41 begin_ptr= end_ptr;
42 }
43
44 ptr= index(buffer, ':');
45
46 in_port_t port= 0;
47 if (ptr)
48 {
49 ptr[0]= 0;
50
51 ptr++;
52
53 port= (in_port_t) strtoul(ptr, (char **)NULL, 10);
54
55 ptr2= index(ptr, ' ');
56 if (! ptr2)
57 ptr2= index(ptr, ':');
58
59 if (ptr2)
60 {
61 ptr2++;
62 weight = (uint32_t) strtoul(ptr2, (char **)NULL, 10);
63 }
64 }
65
66 servers= memcached_server_list_append_with_weight(servers, buffer, port, weight, &rc);
67
68 if (isspace(*begin_ptr))
69 begin_ptr++;
70 }
71
72 return servers;
73 }