4339f9e5b9a9a74584fd2da69e2e647d27d529c9
[awesomized/libmemcached] / src / libmemcached / csl / context.h
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #include "libmemcached/csl/common.h"
19 #include "libmemcached/csl/parser.h"
20
21 class Context {
22 public:
23 Context(const char *option_string, size_t option_string_length, memcached_st *memc_,
24 memcached_return_t &rc_arg)
25 : previous_token(END)
26 , scanner(NULL)
27 , buf(option_string)
28 , begin(NULL)
29 , pos(0)
30 , length(option_string_length)
31 , memc(memc_)
32 , rc(rc_arg)
33 , _is_server(false)
34 , _end(false)
35 , _has_hash(false) {
36 _hostname[0] = 0;
37 init_scanner();
38 rc = MEMCACHED_SUCCESS;
39
40 memc->state.is_parsing = true;
41 memcached_string_create(memc, &_string_buffer, 1024);
42 }
43
44 bool end() {
45 return _end;
46 }
47
48 void start();
49
50 void set_end() {
51 rc = MEMCACHED_SUCCESS;
52 _end = true;
53 }
54
55 bool set_hash(memcached_hash_t hash);
56
57 void set_server() {
58 _is_server = true;
59 }
60
61 void unset_server() {
62 _is_server = false;
63 }
64
65 bool is_server() const {
66 return _is_server;
67 }
68
69 void hostname(const char *, size_t, server_t &);
70
71 bool string_buffer(const char *, size_t, memcached_string_t &);
72
73 const char *hostname() const {
74 return _hostname;
75 }
76
77 void abort(const char *, config_tokentype, const char *);
78 void error(const char *, config_tokentype, const char *);
79
80 ~Context() {
81 memcached_string_free(&_string_buffer);
82 destroy_scanner();
83 memc->state.is_parsing = false;
84 }
85
86 config_tokentype previous_token;
87 void *scanner;
88 const char *buf;
89 const char *begin;
90 size_t pos;
91 size_t length;
92 memcached_st *memc;
93 memcached_return_t &rc;
94
95 protected:
96 void init_scanner();
97 void destroy_scanner();
98
99 private:
100 bool _is_server;
101 bool _end;
102 char _hostname[NI_MAXHOST];
103 bool _has_hash;
104 memcached_string_st _string_buffer;
105 };