--- xds.c 2001/08/09 19:58:35 1.3
+++ xds.c 2001/08/09 20:59:05 1.4
@@ -45,9 +45,8 @@
}
/* Allocate context structure. */
- ctx = malloc(sizeof (struct xds_context));
- if (ctx == NULL)
- return NULL; /* errno is set by calloc() */
+ if ((ctx = malloc(sizeof (struct xds_context))) == NULL)
+ return NULL; /* errno set by malloc(3) */
/* Set mode of operation in context. */
ctx->mode = mode;
@@ -68,7 +67,9 @@
void xds_destroy(xds_t *xds)
{
- /* Sanity checks. */
+ size_t i;
+
+ /* Sanity check parameter. */
assert(xds != NULL);
if (xds == NULL)
return;
@@ -78,25 +79,23 @@
|| (xds->buffer_capacity == 0 && xds->buffer_len == 0));
if (xds->buffer != NULL && xds->we_own_buffer)
free(xds->buffer);
-
assert(xds->engines != NULL || xds->engines_capacity == 0);
if (xds->engines != NULL) {
- size_t i;
- for (i = 0; i < xds->engines_len; ++i) {
+ for (i = 0; i < xds->engines_len; i++) {
assert(xds->engines[i].name != NULL);
free(xds->engines[i].name);
}
free(xds->engines);
}
-
free(xds);
+
return;
}
int xds_setbuffer(xds_t *xds, xds_scope_t flag, void *buffer,
size_t buffer_len)
{
- /* Sanity checks. */
+ /* Sanity check parameters. */
xds_check_parameter(xds != NULL);
xds_check_parameter(flag == XDS_GIFT || flag == XDS_LOAN);
xds_check_parameter((buffer != NULL && buffer_len != 0)
@@ -127,8 +126,7 @@
int xds_getbuffer(xds_t *xds, xds_scope_t flag, void **buffer,
size_t *buffer_len)
{
- /* Sanity checks. */
-
+ /* Sanity check parameters. */
xds_check_parameter(xds != NULL);
xds_check_parameter(flag == XDS_GIFT || flag == XDS_LOAN);
xds_check_parameter(buffer != NULL);
@@ -139,7 +137,8 @@
*buffer_len = xds->buffer_len;
if (flag == XDS_GIFT) {
xds->buffer = NULL;
- xds->buffer_capacity = xds->buffer_len = 0;
+ xds->buffer_capacity = 0;
+ xds->buffer_len = 0;
}
else
xds->buffer_len = 0;
@@ -148,8 +147,8 @@
}
static int xds_set_capacity(void **array, size_t *array_capacity,
- size_t new_capacity, size_t elem_size,
- size_t initial_capacity)
+ size_t new_capacity, size_t elem_size,
+ size_t initial_capacity)
{
void *buf;
size_t size;
@@ -165,13 +164,12 @@
return XDS_OK;
/* Find the correct capacity. */
- size = (*array_capacity != 0) ? *array_capacity * 2 : initial_capacity;
+ size = (*array_capacity != 0) ? (*array_capacity * 2) : initial_capacity;
while (size < new_capacity)
size *= 2;
/* Allocate the array and store the new values. */
- buf = realloc(*array, size * elem_size);
- if (buf == NULL)
+ if ((buf = realloc(*array, size * elem_size)) == NULL)
return XDS_ERR_NO_MEM;
*array = buf;
*array_capacity = size;
@@ -190,7 +188,7 @@
xds_check_parameter(pos != NULL);
/* Use binary search to find "name" in "engines". */
- for (first = 0; (last - first) > 0;) {
+ for (first = 0; (last - first) > 0; ) {
size_t half = first + ((last - first) / 2);
int rc = strcmp(engines[half].name, name);
if (rc < 0)
@@ -203,8 +201,8 @@
last = half;
assert(first <= last);
}
-
*pos = first;
+
return XDS_FALSE;
}
@@ -244,12 +242,12 @@
return rc;
memmove(&xds->engines[pos + 1], &xds->engines[pos],
(xds->engines_len - pos) * sizeof (engine_map_t));
- ++xds->engines_len;
+ xds->engines_len++;
}
/* Insert entry. */
- xds->engines[pos].name = (char *)name;
- xds->engines[pos].engine = engine;
+ xds->engines[pos].name = (char *)name;
+ xds->engines[pos].engine = engine;
xds->engines[pos].context = engine_context;
/* Everything is fine. */
@@ -264,7 +262,7 @@
/* Sanity checks. */
xds_check_parameter(xds != NULL);
xds_check_parameter(name != NULL);
- for (pos = 0; name[pos] != '\0'; ++pos) {
+ for (pos = 0; name[pos] != '\0'; pos++) {
if (!isalnum((int)name[pos]) && name[pos] != '-' && name[pos] != '_')
return XDS_ERR_INVALID_ARG;
}
@@ -279,13 +277,11 @@
free(xds->engines[pos].name);
memmove(&xds->engines[pos], &xds->engines[pos + 1],
(xds->engines_len - (pos + 1)) * sizeof (engine_map_t));
- --xds->engines_len;
+ xds->engines_len--;
/* Lower capacity if necessary. */
- rc = xds_set_capacity((void **)&xds->engines,
- &xds->engines_capacity,
- xds->engines_len,
- sizeof (engine_map_t),
+ rc = xds_set_capacity((void **)&xds->engines, &xds->engines_capacity,
+ xds->engines_len, sizeof (engine_map_t),
XDS_INITIAL_ENGINES_CAPACITY);
assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM);
if (rc != XDS_OK)
@@ -298,6 +294,7 @@
{
int rc;
va_list args;
+
va_start(args, fmt);
rc = xds_vencode(xds, fmt, args);
va_end(args);
@@ -321,11 +318,11 @@
return XDS_ERR_INVALID_MODE;
/* Ensure we have a buffer allocated ready for use. */
- if (xds->buffer == NULL) { /* allocate a new buffer */
- rc = xds_set_capacity((void **)&xds->buffer,
- &xds->buffer_capacity,
- XDS_INITIAL_BUFFER_CAPACITY,
- sizeof (char), XDS_INITIAL_BUFFER_CAPACITY);
+ if (xds->buffer == NULL) {
+ /* allocate a new buffer */
+ rc = xds_set_capacity((void **)&xds->buffer, &xds->buffer_capacity,
+ XDS_INITIAL_BUFFER_CAPACITY, sizeof (char),
+ XDS_INITIAL_BUFFER_CAPACITY);
assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM);
if (rc != XDS_OK)
return rc;
@@ -333,16 +330,15 @@
xds->we_own_buffer = XDS_TRUE;
}
- /* Iterate through the items in the format string and execute the
- apropriate engines. */
+ /* Iterate through items in format string and execute apropriate engines. */
fmt = p = strdup(fmt_arg);
if (fmt == NULL)
return XDS_ERR_NO_MEM;
buffer_len_backup = xds->buffer_len;
for (name = p; *p != '\0'; name = p) {
while (isalnum((int)*p) || *p == '-' || *p == '_')
- ++p;
- if (*p)
+ p++;
+ if (*p != '\0')
*p++ = '\0';
else
*p = '\0';
@@ -352,8 +348,8 @@
size_t used_buffer_size;
size_t pos;
- if (xds_find_engine(xds->engines, xds->engines_len, name, &pos) ==
- XDS_FALSE) {
+ if (xds_find_engine(xds->engines, xds->engines_len, name, &pos)
+ == XDS_FALSE) {
rc = XDS_ERR_UNKNOWN_ENGINE;
goto leave;
}
@@ -365,8 +361,7 @@
if (xds->we_own_buffer) {
rc = xds_set_capacity((void **)&xds->buffer,
&xds->buffer_capacity,
- xds->buffer_len + 1,
- sizeof (char),
+ xds->buffer_len + 1, sizeof (char),
XDS_INITIAL_BUFFER_CAPACITY);
assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM);
if (rc != XDS_OK)
@@ -381,13 +376,11 @@
/* Execute the engine. */
used_buffer_size = 0;
args_backup = args;
- rc = (*xds->engines[pos].engine)(xds,
- xds->engines[pos].context,
- xds->buffer +
- xds->buffer_len,
- xds->buffer_capacity -
- xds->buffer_len,
- &used_buffer_size, &args);
+ rc = (*xds->engines[pos].engine)(
+ xds, xds->engines[pos].context,
+ xds->buffer + xds->buffer_len,
+ xds->buffer_capacity - xds->buffer_len,
+ &used_buffer_size, &args);
assert(rc <= 0);
if (rc == XDS_OK) {
restart_engine = XDS_FALSE;
@@ -400,13 +393,11 @@
restart_engine = XDS_TRUE;
args = args_backup;
- rc = xds_set_capacity((void **)&xds->buffer,
- &xds->buffer_capacity,
- xds->buffer_capacity +
- ((used_buffer_size ==
- 0) ? 1 : used_buffer_size),
- sizeof (char),
- XDS_INITIAL_BUFFER_CAPACITY);
+ rc = xds_set_capacity(
+ (void **)&xds->buffer, &xds->buffer_capacity,
+ xds->buffer_capacity +
+ ((used_buffer_size == 0) ? 1 : used_buffer_size),
+ sizeof (char), XDS_INITIAL_BUFFER_CAPACITY);
assert(rc == XDS_OK || rc == XDS_ERR_NO_MEM);
if (rc != XDS_OK)
goto leave;
@@ -431,6 +422,7 @@
{
int rc;
va_list args;
+
va_start(args, fmt);
rc = xds_vdecode(xds, fmt, args);
va_end(args);
@@ -464,8 +456,8 @@
buffer_len_backup = xds->buffer_len;
for (name = p; *p != '\0'; name = p) {
while (isalnum((int)*p) || *p == '-' || *p == '_')
- ++p;
- if (*p)
+ p++;
+ if (*p != '\0')
*p++ = '\0';
else
*p = '\0';
@@ -473,14 +465,13 @@
if (strlen(name) > 0) {
size_t pos;
size_t used_buffer_size = 0;
+
if (xds_find_engine(xds->engines, xds->engines_len, name, &pos)) {
- rc = (*xds->engines[pos].engine)(xds,
- xds->engines[pos].context,
- xds->buffer +
- xds->buffer_len,
- xds->buffer_capacity -
- xds->buffer_len,
- &used_buffer_size, &args);
+ rc = (*xds->engines[pos].engine)(
+ xds, xds->engines[pos].context,
+ xds->buffer + xds->buffer_len,
+ xds->buffer_capacity - xds->buffer_len,
+ &used_buffer_size, &args);
assert(rc <= 0);
if (rc == XDS_OK)
xds->buffer_len += used_buffer_size;
|