consolidate
[m6w6/libmemcached] / src / libmemcached / array.cc
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 #include "libmemcached/common.h"
17
18 #include <cassert>
19
20 struct memcached_array_st {
21 Memcached *root;
22 size_t size;
23 char c_str[];
24 };
25
26 memcached_array_st *memcached_array_clone(Memcached *memc, const memcached_array_st *original) {
27 if (original) {
28 return memcached_strcpy(memc, original->c_str, original->size);
29 }
30
31 return NULL;
32 }
33
34 memcached_array_st *memcached_strcpy(Memcached *memc, const char *str, size_t str_length) {
35 assert(memc);
36 assert(str);
37 assert(str_length);
38
39 memcached_array_st *array = (struct memcached_array_st *) libmemcached_malloc(
40 memc, sizeof(struct memcached_array_st) + str_length + 1);
41
42 if (array) {
43 array->root = memc;
44 array->size = str_length; // We don't count the NULL ending
45 memcpy(array->c_str, str, str_length);
46 array->c_str[str_length] = 0;
47 }
48
49 return array;
50 }
51
52 bool memcached_array_is_null(memcached_array_st *array) {
53 if (array) {
54 return false;
55 }
56
57 return true;
58 }
59
60 memcached_string_t memcached_array_to_string(memcached_array_st *array) {
61 assert(array);
62 assert(array->c_str);
63 assert(array->size);
64 memcached_string_t tmp;
65 tmp.c_str = array->c_str;
66 tmp.size = array->size;
67
68 return tmp;
69 }
70
71 void memcached_array_free(memcached_array_st *array) {
72 if (array) {
73 WATCHPOINT_ASSERT(array->root);
74 libmemcached_free(array->root, array);
75 }
76 }
77
78 size_t memcached_array_size(memcached_array_st *array) {
79 if (array) {
80 return array->size;
81 }
82
83 return 0;
84 }
85
86 const char *memcached_array_string(memcached_array_st *array) {
87 if (array) {
88 return array->c_str;
89 }
90
91 return NULL;
92 }