~piotr-sikora/libmemcached/fix-tests-on-openbsd

« back to all changes in this revision

Viewing changes to libmemcached/array.c

  • Committer: Brian Aker
  • Date: 2011-05-24 20:43:14 UTC
  • mfrom: (929.1.110 libmemcached-build)
  • Revision ID: brian@tangent.org-20110524204314-9ag1kkk4c1a6b3z3
Merge in local trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
 *
36
36
 */
37
37
 
38
 
#include "libmemcached/common.h"
 
38
#include <libmemcached/common.h>
 
39
#include <assert.h>
 
40
#include <iso646.h>
39
41
 
40
42
struct memcached_array_st
41
43
{
42
 
  memcached_st *root;
 
44
  struct memcached_st *root;
43
45
  size_t size;
44
46
  char c_str[];
45
47
};
46
48
 
47
 
memcached_array_st *memcached_array_clone(memcached_st *memc, const memcached_array_st *original)
 
49
 
 
50
memcached_array_st *memcached_array_clone(struct memcached_st *memc, const memcached_array_st *original)
48
51
{
49
 
  if (! original)
 
52
  if (not original)
50
53
    return NULL;
51
54
 
52
55
  return memcached_strcpy(memc, original->c_str, original->size);
53
56
}
54
57
 
55
 
memcached_array_st *memcached_strcpy(memcached_st *memc, const char *str, size_t str_length)
 
58
memcached_array_st *memcached_strcpy(struct memcached_st *memc, const char *str, size_t str_length)
56
59
{
 
60
  assert(memc);
 
61
  assert(str);
 
62
  assert(str_length);
 
63
 
57
64
  memcached_array_st *array= (struct memcached_array_st *)libmemcached_malloc(memc, sizeof(struct memcached_array_st) +str_length +1);
58
65
 
59
 
  if (! array)
 
66
  if (not array)
60
67
    return NULL;
61
68
 
62
69
  array->root= memc;
63
 
  array->size= str_length -1; // We don't count the NULL ending
 
70
  array->size= str_length; // We don't count the NULL ending
64
71
  memcpy(array->c_str, str, str_length);
65
72
  array->c_str[str_length]= 0;
66
73
 
67
74
  return array;
68
75
}
69
76
 
 
77
bool memcached_array_is_null(memcached_array_st *array)
 
78
{
 
79
  assert(array);
 
80
  assert(array->root);
 
81
 
 
82
  if (not array)
 
83
    return false;
 
84
 
 
85
  if (array->size and array->c_str)
 
86
    return false;
 
87
 
 
88
  assert(not array->size and not array->c_str);
 
89
 
 
90
  return true;
 
91
}
 
92
 
70
93
memcached_string_t memcached_array_to_string(memcached_array_st *array)
71
94
{
 
95
  assert(array);
 
96
  assert(array->c_str);
 
97
  assert(array->size);
72
98
  memcached_string_t tmp;
73
99
  tmp.c_str= array->c_str;
74
100
  tmp.size= array->size;
78
104
 
79
105
void memcached_array_free(memcached_array_st *array)
80
106
{
81
 
  if (! array)
 
107
  if (not array)
82
108
    return;
83
109
 
84
110
  WATCHPOINT_ASSERT(array->root);
85
 
  if (array && array->root)
86
 
  {
87
 
    libmemcached_free(array->root, array);
88
 
  }
89
 
  else if (array)
90
 
  {
91
 
    free(array);
92
 
  }
 
111
  libmemcached_free(array->root, array);
93
112
}
94
113
 
95
114
size_t memcached_array_size(memcached_array_st *array)
96
115
{
97
 
  if (! array)
 
116
  if (not array)
98
117
    return 0;
99
118
 
100
119
  return array->size;
102
121
 
103
122
const char *memcached_array_string(memcached_array_st *array)
104
123
{
105
 
  if (! array)
 
124
  if (not array)
106
125
    return NULL;
107
126
 
108
127
  return array->c_str;