Remove requirements for rpm that were unused.
[awesomized/libmemcached] / libmemcached / options.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libmemcached/common.h>
39 #include <libmemcached/options.hpp>
40
41 #include <libmemcached/csl/context.h>
42
43 const char *memcached_parse_filename(memcached_st *memc)
44 {
45 assert_msg(memc, "Invalid memcached_st");
46 return memcached_array_string(memc->configure.filename);
47 }
48
49 size_t memcached_parse_filename_length(memcached_st *memc)
50 {
51 return memcached_array_size(memc->configure.filename);
52 }
53
54 static memcached_return_t _parse_file_options(memcached_st& self, memcached_array_st *real_name)
55 {
56 FILE *fp= fopen(memcached_array_string(real_name), "r");
57 if (not fp)
58 {
59 memcached_string_t error_message= memcached_array_to_string(real_name);
60 memcached_return_t rc= memcached_set_errno(self, errno, MEMCACHED_AT, error_message);
61 return rc;
62 }
63
64 char buffer[BUFSIZ];
65 memcached_return_t rc= MEMCACHED_INVALID_ARGUMENTS;
66 while (fgets(buffer, sizeof(buffer), fp))
67 {
68 size_t length= strlen(buffer);
69
70 if (length == 1 and buffer[0] == '\n')
71 continue;
72
73 if (memcached_failed(rc= memcached_parse_configuration(&self, buffer, length)))
74 break;
75 }
76 fclose(fp);
77
78 return rc;
79 }
80
81 memcached_return_t libmemcached_check_configuration(const char *option_string, size_t length, char *error_buffer, size_t error_buffer_size)
82 {
83 memcached_st memc, *memc_ptr;
84
85 if (option_string == NULL or length == 0)
86 {
87 return MEMCACHED_INVALID_ARGUMENTS;
88 }
89
90 if (error_buffer and error_buffer_size)
91 {
92 error_buffer[0]= 0;
93 }
94
95 if (not (memc_ptr= memcached_create(&memc)))
96 {
97 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
98 }
99
100 memcached_return_t rc= memcached_parse_configuration(memc_ptr, option_string, length);
101 if (memcached_failed(rc) and error_buffer and error_buffer_size)
102 {
103 strncpy(error_buffer, memcached_last_error_message(memc_ptr), error_buffer_size);
104 error_buffer[error_buffer_size -1]= 0;
105 }
106
107 bool has_filename= memcached_behavior_get(memc_ptr, MEMCACHED_BEHAVIOR_LOAD_FROM_FILE);
108 if (memcached_success(rc) and has_filename)
109 {
110 assert_msg(memcached_parse_filename(memc_ptr), "Invalid configuration file");
111 assert_msg(memcached_parse_filename_length(memc_ptr), "Invalid configuration file");
112 rc= _parse_file_options(*memc_ptr, memc_ptr->configure.filename);
113
114 if (memcached_failed(rc) and error_buffer and error_buffer_size)
115 {
116 strncpy(error_buffer, memcached_last_error_message(memc_ptr), error_buffer_size);
117 error_buffer[error_buffer_size -1]= 0;
118 }
119 }
120
121 memcached_free(memc_ptr);
122
123 return rc;
124 }
125
126 memcached_return_t memcached_parse_configuration(memcached_st *self, char const *option_string, size_t length)
127 {
128 WATCHPOINT_ASSERT(self);
129 if (not self)
130 {
131 return MEMCACHED_INVALID_ARGUMENTS;
132 }
133
134 memcached_return_t rc;
135 Context context(option_string, length, self, rc);
136
137 context.start();
138
139 return rc;
140 }
141
142 void memcached_set_configuration_file(memcached_st *self, const char *filename, size_t filename_length)
143 {
144 assert_msg(filename, "Invalid filename");
145 assert_msg(filename_length, "Invalid filename_length");
146 memcached_array_free(self->configure.filename);
147 self->configure.filename= memcached_strcpy(self, filename, filename_length);
148 }
149
150 memcached_return_t memcached_parse_configure_file(memcached_st& self, const char *filename, size_t length)
151 {
152 if (not filename)
153 {
154 return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT);
155 }
156
157 WATCHPOINT_ASSERT(self);
158 if (not length)
159 {
160 return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT);
161 }
162
163 memcached_array_st *tmp_array= memcached_strcpy(&self, filename, length);
164
165 if (not tmp_array)
166 {
167 return memcached_set_error(self, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
168 }
169
170 memcached_return_t rc= memcached_parse_configure_file(self, *tmp_array);
171 memcached_array_free(tmp_array);
172
173 return rc;
174 }
175
176 memcached_return_t memcached_parse_configure_file(memcached_st& self, memcached_array_st& filename)
177 {
178 WATCHPOINT_ASSERT(memcached_array_size(&filename));
179 if (not memcached_array_size(&filename))
180 {
181 return memcached_set_error(self, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT);
182 }
183
184 return _parse_file_options(self, &filename);
185 }