change license header library name to libmemcached-awesome
[awesomized/libmemcached] / src / libmemcached / csl / context.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - 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-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/csl/common.h"
17 #include "libmemcached/csl/context.h"
18
19 void Context::abort(const char *error_arg, config_tokentype last_token,
20 const char *last_token_str) {
21 rc = MEMCACHED_PARSE_ERROR;
22 (void) last_token;
23
24 if (error_arg) {
25 memcached_set_parser_error(*memc, MEMCACHED_AT, "%s", error_arg);
26 return;
27 }
28
29 if (last_token_str) {
30 memcached_set_parser_error(*memc, MEMCACHED_AT, "%s", last_token_str);
31 return;
32 }
33
34 memcached_set_parser_error(*memc, MEMCACHED_AT, "unknown parsing error");
35 }
36
37 void Context::error(const char *error_arg, config_tokentype last_token,
38 const char *last_token_str) {
39 rc = MEMCACHED_PARSE_ERROR;
40 if (not error_arg) {
41 memcached_set_parser_error(*memc, MEMCACHED_AT, "Unknown error occured during parsing (%s)",
42 last_token_str ? last_token_str : " ");
43 return;
44 }
45
46 if (strcmp(error_arg, "memory exhausted") == 0) {
47 (void) memcached_set_error(*memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
48 memcached_string_make_from_cstr(error_arg));
49 return;
50 }
51
52 // We now test if it is something other then a syntax error, if it we
53 // return a generic message
54 if (strcmp(error_arg, "syntax error")) {
55 memcached_set_parser_error(*memc, MEMCACHED_AT,
56 "Error occured during parsing (%s): last_token=%s(%d)", error_arg,
57 last_token_str, last_token);
58 return;
59 }
60
61 if (last_token == UNKNOWN_OPTION and begin) {
62 memcached_set_parser_error(*memc, MEMCACHED_AT, "Unknown option: %s", begin);
63 } else if (last_token == UNKNOWN) {
64 memcached_set_parser_error(*memc, MEMCACHED_AT,
65 "Error occured durring parsing, an unknown token was found.");
66 } else {
67 memcached_set_parser_error(*memc, MEMCACHED_AT, "Error occured while parsing (%s)",
68 last_token_str ? last_token_str : " ");
69 }
70 }
71
72 void Context::hostname(const char *str, size_t size, server_t &server_) {
73 size_t copy_length = size_t(NI_MAXHOST) > size ? size : size_t(NI_MAXHOST);
74 memcpy(_hostname, str, copy_length);
75 _hostname[copy_length] = 0;
76
77 server_.port = MEMCACHED_DEFAULT_PORT;
78 server_.weight = 1;
79 server_.c_str = _hostname;
80 server_.size = size;
81 }
82
83 bool Context::string_buffer(const char *str, size_t size, memcached_string_t &string_) {
84 if (memcached_string_set(_string_buffer, str, size)) {
85 string_.c_str = memcached_string_value(_string_buffer);
86 string_.size = memcached_string_length(_string_buffer);
87
88 return true;
89 }
90
91 return false;
92 }
93
94 bool Context::set_hash(memcached_hash_t hash) {
95 if (_has_hash) {
96 return false;
97 }
98
99 if ((memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, hash)) != MEMCACHED_SUCCESS) {
100 return false;
101 }
102
103 return _has_hash = true;
104 }