Merge branch 'gh-132/static-libs' into v1.x
[awesomized/libmemcached] / src / libmemcached / namespace.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/common.h"
17 #include "libmemcached/assert.hpp"
18
19 memcached_return_t memcached_set_namespace(Memcached &memc, const char *key, size_t key_length) {
20 if (key and key_length == 0) {
21 WATCHPOINT_ASSERT(key_length);
22 return memcached_set_error(
23 memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
24 memcached_literal_param("Invalid namespace, namespace string had value but length was 0"));
25 } else if (key_length and key == NULL) {
26 WATCHPOINT_ASSERT(key);
27 return memcached_set_error(
28 memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
29 memcached_literal_param(
30 "Invalid namespace, namespace string length was > 1 but namespace string was null "));
31 } else if (key and key_length) {
32 bool orig = memc.flags.verify_key;
33 memc.flags.verify_key = true;
34 if (memcached_failed(memcached_key_test(memc, (const char **) &key, &key_length, 1))) {
35 memc.flags.verify_key = orig;
36 return memcached_last_error(&memc);
37 }
38 memc.flags.verify_key = orig;
39
40 if ((key_length > MEMCACHED_MAX_NAMESPACE - 1)) {
41 return memcached_set_error(memc, MEMCACHED_KEY_TOO_BIG, MEMCACHED_AT);
42 }
43
44 memcached_array_free(memc._namespace);
45 memc._namespace = memcached_strcpy(&memc, key, key_length);
46
47 if (memc._namespace == NULL) {
48 return memcached_set_error(memc, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT);
49 }
50 } else {
51 memcached_array_free(memc._namespace);
52 memc._namespace = NULL;
53 }
54
55 return MEMCACHED_SUCCESS;
56 }
57
58 const char *memcached_get_namespace(Memcached &memc) {
59 if (memc._namespace == NULL) {
60 return NULL;
61 }
62
63 return memcached_array_string(memc._namespace);
64 }