123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594 |
- /***************************************************************************
- *
- * 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 <In/Out> == (char **) Address of pointer into byte stream
- * swap_bytes <Input> == (int) 1 if bytes need to be swapped
- * num_ints <Input> == (int) Number of elements in int_vals array
- * int_vals <Input> == (int *) Pointer to array of integers
- *
- * RETURN <Output> == (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 <In/Out> == (char **) Address of pointer into byte stream
- * swap_bytes <Input> == (int) 1 if bytes need to be swapped
- * num_doubles <Input> == (int) Number of elements in double_vals array
- * double_vals <Input> == (double *) Pointer to array of doubles
- *
- * RETURN <Output> == (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 <In/Out> == (char **) Address of pointer into byte stream
- * swap_bytes <Input> == (int) 1 if bytes need to be swapped
- * num_points <Input> == (int) Number of elements in points array
- * points <Input> == (Point *) Pointer to array of points
- *
- * RETURN <Output> == (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 <In/Out> == (char **) Address of pointer into byte stream
- * swap_bytes <Input> == (int) 1 if bytes need to be swapped
- * num_ints <Input> == (int) Number of elements in int_vals array
- * int_vals <Input> == (int *) Pointer to array of integers
- *
- * RETURN <Output> == (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 <In/Out> == (char **) Address of pointer into byte stream
- * swap_bytes <Input> == (int) 1 if bytes need to be swapped
- * num_doubles <Input> == (int) Number of elements in double_vals array
- * double_vals <Input> == (double *) Pointer to array of doubles
- *
- * RETURN <Output> == (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 <In/Out> == (char **) Address of pointer into byte stream
- * swap_bytes <Input> == (int) 1 if bytes need to be swapped
- * num_points <Input> == (int) Number of elements in points array
- * points <Output> == (Point *) Pointer to array of points
- *
- * RETURN <Output> == (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 <In/Out> == (Geometry *) A geometry object
- * to allocate the point buffers for.
- * npoints <Input> == (int) The size to allocate the point
- * buffers to allocate/reallocate.
- *
- * RETURN <Output> == (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;
- }
|