1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
39 #include <libmemcached/options.hpp>
41 #include <libmemcached/csl/context.h>
43 const char *memcached_parse_filename(memcached_st
*memc
)
45 assert_msg(memc
, "Invalid memcached_st");
46 return memcached_array_string(memc
->configure
.filename
);
49 size_t memcached_parse_filename_length(memcached_st
*memc
)
51 return memcached_array_size(memc
->configure
.filename
);
54 static memcached_return_t
_parse_file_options(memcached_st
& self
, memcached_array_st
*real_name
)
56 FILE *fp
= fopen(memcached_array_string(real_name
), "r");
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
);
65 memcached_return_t rc
= MEMCACHED_INVALID_ARGUMENTS
;
66 while (fgets(buffer
, sizeof(buffer
), fp
))
68 size_t length
= strlen(buffer
);
70 if (length
== 1 and buffer
[0] == '\n')
73 if (memcached_failed(rc
= memcached_parse_configuration(&self
, buffer
, length
)))
81 memcached_return_t
libmemcached_check_configuration(const char *option_string
, size_t length
, char *error_buffer
, size_t error_buffer_size
)
83 memcached_st memc
, *memc_ptr
;
85 if (option_string
== NULL
or length
== 0)
87 return MEMCACHED_INVALID_ARGUMENTS
;
90 if (error_buffer
and error_buffer_size
)
95 if (not (memc_ptr
= memcached_create(&memc
)))
97 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
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
)
103 strncpy(error_buffer
, memcached_last_error_message(memc_ptr
), error_buffer_size
);
104 error_buffer
[error_buffer_size
-1]= 0;
107 bool has_filename
= memcached_behavior_get(memc_ptr
, MEMCACHED_BEHAVIOR_LOAD_FROM_FILE
);
108 if (memcached_success(rc
) and has_filename
)
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
);
114 if (memcached_failed(rc
) and error_buffer
and error_buffer_size
)
116 strncpy(error_buffer
, memcached_last_error_message(memc_ptr
), error_buffer_size
);
117 error_buffer
[error_buffer_size
-1]= 0;
121 memcached_free(memc_ptr
);
126 memcached_return_t
memcached_parse_configuration(memcached_st
*self
, char const *option_string
, size_t length
)
128 WATCHPOINT_ASSERT(self
);
131 return MEMCACHED_INVALID_ARGUMENTS
;
134 memcached_return_t rc
;
135 Context
context(option_string
, length
, self
, rc
);
142 void memcached_set_configuration_file(memcached_st
*self
, const char *filename
, size_t filename_length
)
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
);
150 memcached_return_t
memcached_parse_configure_file(memcached_st
& self
, const char *filename
, size_t length
)
154 return memcached_set_error(self
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
);
157 WATCHPOINT_ASSERT(self
);
160 return memcached_set_error(self
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
);
163 memcached_array_st
*tmp_array
= memcached_strcpy(&self
, filename
, length
);
167 return memcached_set_error(self
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
170 memcached_return_t rc
= memcached_parse_configure_file(self
, *tmp_array
);
171 memcached_array_free(tmp_array
);
176 memcached_return_t
memcached_parse_configure_file(memcached_st
& self
, memcached_array_st
& filename
)
178 WATCHPOINT_ASSERT(memcached_array_size(&filename
));
179 if (not memcached_array_size(&filename
))
181 return memcached_set_error(self
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
);
184 return _parse_file_options(self
, &filename
);