X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Foptions%2Fcontext.h;h=bbca66c855e23600646de64c627a585c8f8b5e39;hb=1a237369acc2140ec76f46202dfde37047ef140d;hp=562d3dc1176d4aaf2db049eb01ac7318dfa43b59;hpb=1d076426d9ff399efc40d8556d8bc883b47ed87c;p=m6w6%2Flibmemcached diff --git a/libmemcached/options/context.h b/libmemcached/options/context.h index 562d3dc1..bbca66c8 100644 --- a/libmemcached/options/context.h +++ b/libmemcached/options/context.h @@ -37,21 +37,94 @@ #pragma once -#include +#include class Context { public: - Context(const char *option_string, size_t option_string_length, memcached_st *memc_arg) : + Context(const char *option_string, size_t option_string_length, memcached_st *memc_arg, + memcached_return_t &rc_arg) : scanner(NULL), begin(NULL), pos(0), - memc(NULL) + memc(NULL), + rc(rc_arg), + _is_server(false), + _end(false) { + _hostname[0]= 0; buf= option_string; length= option_string_length; memc= memc_arg; init_scanner(); + rc= MEMCACHED_SUCCESS; + } + + bool end() + { + return _end; + } + + void start(); + + void set_end() + { + rc= MEMCACHED_SUCCESS; + _end= true; + } + + void set_server() + { + _is_server= true; + } + + void unset_server() + { + _is_server= false; + } + + bool is_server() + { + return _is_server; + } + + const char *set_hostname(const char *str, size_t size) + { + size_t copy_length= std::min((size_t)NI_MAXHOST, size); + memcpy(_hostname, str, copy_length); + _hostname[copy_length]= 0; + + return _hostname; + } + + const char *hostname() + { + return _hostname; + } + + void abort(const char *error) + { + if (rc == MEMCACHED_SUCCESS) + rc= MEMCACHED_PARSE_ERROR; + + memcached_string_st *error_string= memcached_string_create(memc, NULL, 1024); + memcached_string_append(error_string, memcached_string_with_size("Error occured while parsing: ")); + memcached_string_append(error_string, memcached_string_make_from_cstr(begin)); + memcached_string_append(error_string, memcached_string_with_size(" (")); + + if (rc == MEMCACHED_PARSE_ERROR and error) + { + memcached_string_append(error_string, memcached_string_make_from_cstr(error)); + } + else + { + memcached_string_append(error_string, memcached_string_make_from_cstr(memcached_strerror(NULL, rc))); + } + memcached_string_append(error_string, memcached_string_with_size(")")); + + memcached_set_error_string(memc, rc, memcached_string_value(error_string), memcached_string_length(error_string)); + + memcached_string_free(error_string); } ~Context() @@ -65,8 +138,14 @@ public: size_t pos; size_t length; memcached_st *memc; + memcached_return_t &rc; protected: void init_scanner(); void destroy_scanner(); + +private: + bool _is_server; + bool _end; + char _hostname[NI_MAXHOST]; };