Initial commit.
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmxmlrpc / xmlrpc_array.c
blob36010c67c8010de6926a011048e8b75f74038cc3
1 /* Copyright information is at the end of the file */
3 /*=========================================================================
4 ** XML-RPC Array Functions
5 **=========================================================================
6 */
8 #include "xmlrpc_config.h"
10 #include <stddef.h>
11 #include <stdlib.h>
13 #include "xmlrpc.h"
14 #include "xmlrpc_int.h"
17 void
18 xmlrpc_abort_if_array_bad(xmlrpc_value * const arrayP) {
20 if (arrayP == NULL)
21 abort();
22 else if (arrayP->_type != XMLRPC_TYPE_ARRAY)
23 abort();
24 else {
25 size_t const arraySize =
26 XMLRPC_MEMBLOCK_SIZE(xmlrpc_value*, &arrayP->_block);
27 xmlrpc_value ** const contents =
28 XMLRPC_MEMBLOCK_CONTENTS(xmlrpc_value*, &arrayP->_block);
30 if (contents == NULL)
31 abort();
32 else {
33 unsigned int xmIndex;
35 for (xmIndex = 0; xmIndex < arraySize; ++xmIndex) {
36 xmlrpc_value * const itemP = contents[xmIndex];
37 if (itemP == NULL)
38 abort();
39 else if (itemP->_refcount < 1)
40 abort();
48 void
49 xmlrpc_destroyArrayContents(xmlrpc_value * const arrayP) {
50 /*----------------------------------------------------------------------------
51 Dispose of the contents of an array (but not the array value itself).
52 The value is not valid after this.
53 -----------------------------------------------------------------------------*/
54 size_t const arraySize =
55 XMLRPC_MEMBLOCK_SIZE(xmlrpc_value*, &arrayP->_block);
56 xmlrpc_value ** const contents =
57 XMLRPC_MEMBLOCK_CONTENTS(xmlrpc_value*, &arrayP->_block);
59 unsigned int xmIndex;
61 XMLRPC_ASSERT_ARRAY_OK(arrayP);
63 /* Release our reference to each item in the array */
64 for (xmIndex = 0; xmIndex < arraySize; ++xmIndex) {
65 xmlrpc_value * const itemP = contents[xmIndex];
66 xmlrpc_DECREF(itemP);
68 XMLRPC_MEMBLOCK_CLEAN(xmlrpc_value *, &arrayP->_block);
73 int
74 xmlrpc_array_size(xmlrpc_env * const env,
75 const xmlrpc_value * const array) {
77 int retval;
79 /* Suppress a compiler warning about uninitialized variables. */
80 retval = 0;
82 XMLRPC_ASSERT_ENV_OK(env);
83 XMLRPC_ASSERT_VALUE_OK(array);
84 XMLRPC_TYPE_CHECK(env, array, XMLRPC_TYPE_ARRAY);
86 retval = (int)XMLRPC_TYPED_MEM_BLOCK_SIZE(xmlrpc_value*, &array->_block);
88 cleanup:
89 if (env->fault_occurred)
90 return -1;
91 else
92 return retval;
97 void
98 xmlrpc_array_append_item(xmlrpc_env * const envP,
99 xmlrpc_value * const arrayP,
100 xmlrpc_value * const valueP) {
102 XMLRPC_ASSERT_ENV_OK(envP);
103 XMLRPC_ASSERT_VALUE_OK(arrayP);
105 if (xmlrpc_value_type(arrayP) != XMLRPC_TYPE_ARRAY)
106 xmlrpc_env_set_fault_formatted(
107 envP, XMLRPC_TYPE_ERROR, "Value is not an array");
108 else {
109 size_t const size =
110 XMLRPC_MEMBLOCK_SIZE(xmlrpc_value *, &arrayP->_block);
112 XMLRPC_MEMBLOCK_RESIZE(xmlrpc_value *, envP, &arrayP->_block, size+1);
114 if (!envP->fault_occurred) {
115 xmlrpc_value ** const contents =
116 XMLRPC_MEMBLOCK_CONTENTS(xmlrpc_value*, &arrayP->_block);
117 xmlrpc_INCREF(valueP);
118 contents[size] = valueP;
125 void
126 xmlrpc_array_read_item(xmlrpc_env * const envP,
127 const xmlrpc_value * const arrayP,
128 unsigned int const xmIndex,
129 xmlrpc_value ** const valuePP) {
131 XMLRPC_ASSERT_ENV_OK(envP);
132 XMLRPC_ASSERT_VALUE_OK(arrayP);
133 XMLRPC_ASSERT_PTR_OK(valuePP);
135 if (arrayP->_type != XMLRPC_TYPE_ARRAY)
136 xmlrpc_env_set_fault_formatted(
137 envP, XMLRPC_TYPE_ERROR, "Attempt to read array item from "
138 "a value that is not an array");
139 else {
140 xmlrpc_value ** const contents =
141 XMLRPC_MEMBLOCK_CONTENTS(xmlrpc_value *, &arrayP->_block);
142 size_t const size =
143 XMLRPC_MEMBLOCK_SIZE(xmlrpc_value *, &arrayP->_block);
145 if (xmIndex >= size)
146 xmlrpc_env_set_fault_formatted(
147 envP, XMLRPC_INDEX_ERROR, "Array index %u is beyond end "
148 "of %u-item array", xmIndex, (unsigned int)size);
149 else {
150 *valuePP = contents[xmIndex];
151 xmlrpc_INCREF(*valuePP);
158 xmlrpc_value *
159 xmlrpc_array_get_item(xmlrpc_env * const envP,
160 const xmlrpc_value * const arrayP,
161 int const xmIndex) {
163 xmlrpc_value * valueP;
165 if (xmIndex < 0)
166 xmlrpc_env_set_fault_formatted(
167 envP, XMLRPC_INDEX_ERROR, "Index %d is negative.");
168 else {
169 xmlrpc_array_read_item(envP, arrayP, xmIndex, &valueP);
171 if (!envP->fault_occurred)
172 xmlrpc_DECREF(valueP);
174 if (envP->fault_occurred)
175 valueP = NULL;
177 return valueP;
182 /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
183 ** Copyright (C) 2001 by Eric Kidd. All rights reserved.
185 ** Redistribution and use in source and binary forms, with or without
186 ** modification, are permitted provided that the following conditions
187 ** are met:
188 ** 1. Redistributions of source code must retain the above copyright
189 ** notice, this list of conditions and the following disclaimer.
190 ** 2. Redistributions in binary form must reproduce the above copyright
191 ** notice, this list of conditions and the following disclaimer in the
192 ** documentation and/or other materials provided with the distribution.
193 ** 3. The name of the author may not be used to endorse or promote products
194 ** derived from this software without specific prior written permission.
196 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
197 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
198 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199 ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
200 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
201 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
202 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
203 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
204 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
205 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
206 ** SUCH DAMAGE. */