2fa624075d18ce3f42d6d921f65342e89458daf9
[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() { return _end; }
45
46 void start();
47
48 void set_end() {
49 rc = MEMCACHED_SUCCESS;
50 _end = true;
51 }
52
53 bool set_hash(memcached_hash_t hash);
54
55 void set_server() { _is_server = true; }
56
57 void unset_server() { _is_server = false; }
58
59 bool is_server() const { return _is_server; }
60
61 void hostname(const char *, size_t, server_t &);
62
63 bool string_buffer(const char *, size_t, memcached_string_t &);
64
65 const char *hostname() const { return _hostname; }
66
67 void abort(const char *, config_tokentype, const char *);
68 void error(const char *, config_tokentype, const char *);
69
70 ~Context() {
71 memcached_string_free(&_string_buffer);
72 destroy_scanner();
73 memc->state.is_parsing = false;
74 }
75
76 config_tokentype previous_token;
77 void *scanner;
78 const char *buf;
79 const char *begin;
80 size_t pos;
81 size_t length;
82 memcached_st *memc;
83 memcached_return_t &rc;
84
85 protected:
86 void init_scanner();
87 void destroy_scanner();
88
89 private:
90 bool _is_server;
91 bool _end;
92 char _hostname[NI_MAXHOST];
93 bool _has_hash;
94 memcached_string_st _string_buffer;
95 };