/*************************************************************************** * * put_integer - puts integers into the byte stream. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: * This function copies a given number of 4 byte integers into the * byte stream. This function will convert the values to the correct * byte order if necessary. The pointer into the buffer will be * advanced to the position immediately following the copied integers. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: * * buf_ptr == (char **) Address of pointer into byte stream * swap_bytes == (int) 1 if bytes need to be swapped * num_ints == (int) Number of elements in int_vals array * int_vals == (int *) Pointer to array of integers * * RETURN == (void) * ****************************************************************************/ void put_integer ( char **buf_ptr, int swap_bytes, int num_ints, int *int_vals ) { int i; char *to; char *from; if (swap_bytes) { /* If required, we need to byte swap */ to = *buf_ptr; from = (char *)int_vals; for (i = 0; i < num_ints; i++) { to[0] = from[3]; to[1] = from[2]; to[2] = from[1]; to[3] = from[0]; to += 4; from += 4; } } else { /* Simply copy the data */ memcpy (*buf_ptr, int_vals, num_ints * sizeof (int)); } /* Advance the pointer */ *buf_ptr += num_ints * sizeof (int); return; } /*************************************************************************** * * put_double - puts doubles into the byte stream. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: * This function copies a given number of 8 byte doubles into the byte * stream. This function will convert the values to the correct byte * order if necessary. The pointer into the buffer will be advanced * to the position immediately following the copied doubles. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: * * buf_ptr == (char **) Address of pointer into byte stream * swap_bytes == (int) 1 if bytes need to be swapped * num_doubles == (int) Number of elements in double_vals array * double_vals == (double *) Pointer to array of doubles * * RETURN == (void) * ****************************************************************************/ void put_double ( char **buf_ptr, int swap_bytes, int num_doubles, double *double_vals ) { int i; char *from; char *to; if (swap_bytes) { /* Operating system endian-ness does not match data, /* the double array needs to be byte swapped. */ to = *buf_ptr; from = (char *)double_vals; for (i = 0; i < num_doubles; i++) { to[0] = from[7]; to[1] = from[6]; to[2] = from[5]; to[3] = from[4]; to[4] = from[3]; to[5] = from[2]; to[6] = from[1]; to[7] = from[0]; to += 8; from += 8; } } else { /* Simply copy the data. */ memcpy (*buf_ptr, double_vals, num_doubles * sizeof (double)); } /* Advance the pointer. */ *buf_ptr += num_doubles * sizeof (double); return; } /*************************************************************************** * * put_xy - puts xy points into the byte stream. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: * This function copies a given number of xy double precision coordinates * into the byte stream. This function will convert the values to the * correct byte order if necessary. The pointer into the buffer will be * advanced to the position immediately following the requested points. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: * buf_ptr == (char **) Address of pointer into byte stream * swap_bytes == (int) 1 if bytes need to be swapped * num_points == (int) Number of elements in points array * points == (Point *) Pointer to array of points * * RETURN == (void) * ****************************************************************************/ void put_xy ( char **buf_ptr, int swap_bytes, int num_points, Point *points ) { int i; char *from; char *to; Point *pt; if (swap_bytes) { /* Operating system endian-ness does not match data, /* so the point array needs be byte swapped. */ to = *buf_ptr; pt = points; for (i = 0; i < num_points; i++) { /* swap X value */ from = (char *)&pt->x; to[0] = from[7]; to[1] = from[6]; to[2] = from[5]; to[3] = from[4]; to[4] = from[3]; to[5] = from[2]; to[6] = from[1]; to[7] = from[0]; /* swap Y value */ from = (char *)&pt->y; to += 8; to[0] = from[7]; to[1] = from[6]; to[2] = from[5]; to[3] = from[4]; to[4] = from[3]; to[5] = from[2]; to[6] = from[1]; to[7] = from[0]; to += 8; pt++; } } else { /* Simply copy the data. */ memcpy (*buf_ptr, points, num_points * sizeof (Point)); } /* Advance the pointer. */ *buf_ptr += num_points * sizeof (Point); return; } /*************************************************************************** * * get_integer - gets integers from the byte stream. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: * This function extracts a given number of 4 byte integers * from the byte stream. This function will byte swap if necessary. * The pointer into the buffer will be advanced to the position * immediately following the requested integers. *E *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: * buf_ptr == (char **) Address of pointer into byte stream * swap_bytes == (int) 1 if bytes need to be swapped * num_ints == (int) Number of elements in int_vals array * int_vals == (int *) Pointer to array of integers * * RETURN == (void) * ****************************************************************************/ void get_integer ( char **buf_ptr, int swap_bytes, int num_ints, int *int_vals ) { int i; char *to; char *from; if (swap_bytes) { /* We need to byte swap */ from = *buf_ptr; to = (char *)int_vals; for (i = 0; i < num_ints; i++) { to[0] = from[3]; to[1] = from[2]; to[2] = from[1]; to[3] = from[0]; to += 4; from += 4; } } else { /* Simply copy the data */ memcpy (int_vals, *buf_ptr, num_ints * sizeof (int)); } /* Advance the pointer */ *buf_ptr += num_ints * sizeof (int); return; } /*************************************************************************** * * get_double - gets doubles from the byte stream. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: * This function extracts a given number of 8 byte doubles * from the byte stream. This function will byte swap if necessary. * The pointer into the buffer will be advanced to the position * immediately following the requested doubles. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: * buf_ptr == (char **) Address of pointer into byte stream * swap_bytes == (int) 1 if bytes need to be swapped * num_doubles == (int) Number of elements in double_vals array * double_vals == (double *) Pointer to array of doubles * * RETURN == (void) * ****************************************************************************/ void get_double ( char **buf_ptr, int swap_bytes, int num_doubles, double *double_vals ) { int i; char *from; char *to; if (swap_bytes) { /* Operating system endian-ness does not match data, /* the double array needs to be byte swapped. */ from = *buf_ptr; to = (char *)double_vals; for (i = 0; i < num_doubles; i++) { to[0] = from[7]; to[1] = from[6]; to[2] = from[5]; to[3] = from[4]; to[4] = from[3]; to[5] = from[2]; to[6] = from[1]; to[7] = from[0]; to += 8; from += 8; } } else { /* Simply copy the data. */ memcpy (double_vals, *buf_ptr, num_doubles * sizeof (double)); } /* Advance the pointer. */ *buf_ptr += num_doubles * sizeof (double); return; } /*************************************************************************** * * get_xy - Gets xy points from the byte stream. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: * This function extracts a given number of xy double precision * coordinates from the byte stream. This function will byte swap * if necessary. The pointer into the buffer will be advanced to * the position immediately following the requested points. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: * buf_ptr == (char **) Address of pointer into byte stream * swap_bytes == (int) 1 if bytes need to be swapped * num_points == (int) Number of elements in points array * points == (Point *) Pointer to array of points * * RETURN == (void) * ****************************************************************************/ void get_xy ( char **buf_ptr, int swap_bytes, int num_points, Point *points ) { int i; char *from; char *to; Point *pt; if (swap_bytes) { /* Operating system endian-ness does not match data, /* so the point array needs be byte swapped. */ from = *buf_ptr; pt = points; for (i = 0; i < num_points; i++) { /* swap X value */ to = (char *)&pt->x; to[0] = from[7]; to[1] = from[6]; to[2] = from[5]; to[3] = from[4]; to[4] = from[3]; to[5] = from[2]; to[6] = from[1]; to[7] = from[0]; /* swap Y value */ to = (char *)&pt->y; from += 8; to[0] = from[7]; to[1] = from[6]; to[2] = from[5]; to[3] = from[4]; to[4] = from[3]; to[5] = from[2]; to[6] = from[1]; to[7] = from[0]; from += 8; pt++; } } else { /* Simply copy the data. */ memcpy (points, *buf_ptr, num_points * sizeof (Point)); } /* Advance the pointer. */ *buf_ptr += num_points * sizeof (Point); return; } /*********************************************************************** * * geom_allocate - Allocates or reallocates a geometry's point, * z and/or m buffers. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Purpose: * This function allocates the point buffers of the given geometry * to the specified size. If the buffers are already allocated and * they are currently smaller than the specified size they will be * reallocated to the larger size (plus an additional number of * points to prevent the need to realloc for just a couple of points). * If the specified size is smaller than the currently allocated size * nothing will be done. * * The geometry type is examined to see if Z and/or M values are * specified, and the Z and/or M buffers are allocated or reallocated * in the same way as the point buffer. * *::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: * * Parameters: * geom == (Geometry *) A geometry object * to allocate the point buffers for. * npoints == (int) The size to allocate the point * buffers to allocate/reallocate. * * RETURN == (int) Error code * ***********************************************************************/ int geom_allocate( Geometry *geom, int npoints ) { int allocPerformed = 0; int newAllocSize; double *newArray; Point *newPoints; int need_m; int need_z; need_m = geom->type & 0x01; need_z = geom->type & 0x02; if (geom->alloc_size < npoints || geom->alloc_size == 0) { /* * We have to increase the buffer sizes, make sure that * we the newly allocated size is bigger at least by * the POINT_ALLOC_THRESHOLD value. */ if (npoints > (geom->alloc_size + POINT_ALLOC_THRESHOLD)) newAllocSize = npoints; else newAllocSize = geom->alloc_size + POINT_ALLOC_THRESHOLD; } else { /* We don't have to increase the buffer sizes */ newAllocSize = geom->alloc_size; } if (geom->pt == NULL) { /* Allocate a new array of XY points */ geom->pt = malloc (newAllocSize * sizeof(Point)); if (geom->pt == NULL) return GEOM_OUT_OF_MEMORY; allocPerformed = 1; } else if (npoints > geom->alloc_size) { /* Reallocate an array of XY points */ newPoints = malloc (newAllocSize * sizeof(Point)); if (newPoints == NULL) return GEOM_OUT_OF_MEMORY; memcpy (newPoints, geom->pt, geom->alloc_size * sizeof(Point)); free (geom->pt); geom->pt = newPoints; allocPerformed = 1; } if (need_z) { if (geom->z == NULL) { /* Allocate a new array of Z values */ geom->z = malloc (newAllocSize * sizeof(double)); if (geom->z == NULL) return GEOM_OUT_OF_MEMORY; allocPerformed = 1; } else if (npoints > geom->alloc_size) { /* Reallocate an array of Z values */ newArray = malloc (newAllocSize * sizeof(double)); if (newArray == NULL) return GEOM_OUT_OF_MEMORY; memcpy (newArray, geom->z, geom->alloc_size * sizeof(double)); free (geom->z); geom->z = newArray; allocPerformed = 1; } } if (need_m) { if (geom->m == NULL) { /* Allocate a new array of M values */ geom->m = malloc (newAllocSize * sizeof(double)); if (geom->m == NULL) return GEOM_OUT_OF_MEMORY; allocPerformed = 1; } else if (npoints > geom->alloc_size) { /* Reallocate an array of M values */ newArray = malloc (newAllocSize * sizeof(double)); if (newArray == NULL) return GEOM_OUT_OF_MEMORY; memcpy (newArray, geom->m, geom->alloc_size * sizeof(double)); free (geom->m); geom->m = newArray; allocPerformed = 1; } } /* * If we allocated or reallocated the buffers, increase the * allocated size. */ if (allocPerformed) geom->alloc_size = newAllocSize; return GEOM_SUCCESS; }