commfuncs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. /***************************************************************************
  2. *
  3. * put_integer - puts integers into the byte stream.
  4. *
  5. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  6. *
  7. * Purpose:
  8. * This function copies a given number of 4 byte integers into the
  9. * byte stream. This function will convert the values to the correct
  10. * byte order if necessary. The pointer into the buffer will be
  11. * advanced to the position immediately following the copied integers.
  12. *
  13. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  14. *
  15. * Parameters:
  16. *
  17. * buf_ptr <In/Out> == (char **) Address of pointer into byte stream
  18. * swap_bytes <Input> == (int) 1 if bytes need to be swapped
  19. * num_ints <Input> == (int) Number of elements in int_vals array
  20. * int_vals <Input> == (int *) Pointer to array of integers
  21. *
  22. * RETURN <Output> == (void)
  23. *
  24. ****************************************************************************/
  25. void put_integer (
  26. char **buf_ptr,
  27. int swap_bytes,
  28. int num_ints,
  29. int *int_vals
  30. )
  31. {
  32. int i;
  33. char *to;
  34. char *from;
  35. if (swap_bytes)
  36. {
  37. /* If required, we need to byte swap */
  38. to = *buf_ptr;
  39. from = (char *)int_vals;
  40. for (i = 0; i < num_ints; i++)
  41. {
  42. to[0] = from[3];
  43. to[1] = from[2];
  44. to[2] = from[1];
  45. to[3] = from[0];
  46. to += 4;
  47. from += 4;
  48. }
  49. }
  50. else
  51. {
  52. /* Simply copy the data */
  53. memcpy (*buf_ptr, int_vals, num_ints * sizeof (int));
  54. }
  55. /* Advance the pointer */
  56. *buf_ptr += num_ints * sizeof (int);
  57. return;
  58. }
  59. /***************************************************************************
  60. *
  61. * put_double - puts doubles into the byte stream.
  62. *
  63. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  64. *
  65. * Purpose:
  66. * This function copies a given number of 8 byte doubles into the byte
  67. * stream. This function will convert the values to the correct byte
  68. * order if necessary. The pointer into the buffer will be advanced
  69. * to the position immediately following the copied doubles.
  70. *
  71. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  72. *
  73. * Parameters:
  74. *
  75. * buf_ptr <In/Out> == (char **) Address of pointer into byte stream
  76. * swap_bytes <Input> == (int) 1 if bytes need to be swapped
  77. * num_doubles <Input> == (int) Number of elements in double_vals array
  78. * double_vals <Input> == (double *) Pointer to array of doubles
  79. *
  80. * RETURN <Output> == (void)
  81. *
  82. ****************************************************************************/
  83. void put_double (
  84. char **buf_ptr,
  85. int swap_bytes,
  86. int num_doubles,
  87. double *double_vals
  88. )
  89. {
  90. int i;
  91. char *from;
  92. char *to;
  93. if (swap_bytes)
  94. {
  95. /* Operating system endian-ness does not match data,
  96. /* the double array needs to be byte swapped. */
  97. to = *buf_ptr;
  98. from = (char *)double_vals;
  99. for (i = 0; i < num_doubles; i++)
  100. {
  101. to[0] = from[7];
  102. to[1] = from[6];
  103. to[2] = from[5];
  104. to[3] = from[4];
  105. to[4] = from[3];
  106. to[5] = from[2];
  107. to[6] = from[1];
  108. to[7] = from[0];
  109. to += 8;
  110. from += 8;
  111. }
  112. }
  113. else
  114. {
  115. /* Simply copy the data. */
  116. memcpy (*buf_ptr, double_vals, num_doubles * sizeof (double));
  117. }
  118. /* Advance the pointer. */
  119. *buf_ptr += num_doubles * sizeof (double);
  120. return;
  121. }
  122. /***************************************************************************
  123. *
  124. * put_xy - puts xy points into the byte stream.
  125. *
  126. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  127. *
  128. * Purpose:
  129. * This function copies a given number of xy double precision coordinates
  130. * into the byte stream. This function will convert the values to the
  131. * correct byte order if necessary. The pointer into the buffer will be
  132. * advanced to the position immediately following the requested points.
  133. *
  134. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  135. *
  136. * Parameters:
  137. * buf_ptr <In/Out> == (char **) Address of pointer into byte stream
  138. * swap_bytes <Input> == (int) 1 if bytes need to be swapped
  139. * num_points <Input> == (int) Number of elements in points array
  140. * points <Input> == (Point *) Pointer to array of points
  141. *
  142. * RETURN <Output> == (void)
  143. *
  144. ****************************************************************************/
  145. void put_xy (
  146. char **buf_ptr,
  147. int swap_bytes,
  148. int num_points,
  149. Point *points
  150. )
  151. {
  152. int i;
  153. char *from;
  154. char *to;
  155. Point *pt;
  156. if (swap_bytes)
  157. {
  158. /* Operating system endian-ness does not match data,
  159. /* so the point array needs be byte swapped. */
  160. to = *buf_ptr;
  161. pt = points;
  162. for (i = 0; i < num_points; i++)
  163. {
  164. /* swap X value */
  165. from = (char *)&pt->x;
  166. to[0] = from[7];
  167. to[1] = from[6];
  168. to[2] = from[5];
  169. to[3] = from[4];
  170. to[4] = from[3];
  171. to[5] = from[2];
  172. to[6] = from[1];
  173. to[7] = from[0];
  174. /* swap Y value */
  175. from = (char *)&pt->y;
  176. to += 8;
  177. to[0] = from[7];
  178. to[1] = from[6];
  179. to[2] = from[5];
  180. to[3] = from[4];
  181. to[4] = from[3];
  182. to[5] = from[2];
  183. to[6] = from[1];
  184. to[7] = from[0];
  185. to += 8;
  186. pt++;
  187. }
  188. }
  189. else
  190. {
  191. /* Simply copy the data. */
  192. memcpy (*buf_ptr, points, num_points * sizeof (Point));
  193. }
  194. /* Advance the pointer. */
  195. *buf_ptr += num_points * sizeof (Point);
  196. return;
  197. }
  198. /***************************************************************************
  199. *
  200. * get_integer - gets integers from the byte stream.
  201. *
  202. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  203. *
  204. * Purpose:
  205. * This function extracts a given number of 4 byte integers
  206. * from the byte stream. This function will byte swap if necessary.
  207. * The pointer into the buffer will be advanced to the position
  208. * immediately following the requested integers.
  209. *E
  210. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  211. *
  212. * Parameters:
  213. * buf_ptr <In/Out> == (char **) Address of pointer into byte stream
  214. * swap_bytes <Input> == (int) 1 if bytes need to be swapped
  215. * num_ints <Input> == (int) Number of elements in int_vals array
  216. * int_vals <Input> == (int *) Pointer to array of integers
  217. *
  218. * RETURN <Output> == (void)
  219. *
  220. ****************************************************************************/
  221. void get_integer (
  222. char **buf_ptr,
  223. int swap_bytes,
  224. int num_ints,
  225. int *int_vals
  226. )
  227. {
  228. int i;
  229. char *to;
  230. char *from;
  231. if (swap_bytes)
  232. {
  233. /* We need to byte swap */
  234. from = *buf_ptr;
  235. to = (char *)int_vals;
  236. for (i = 0; i < num_ints; i++)
  237. {
  238. to[0] = from[3];
  239. to[1] = from[2];
  240. to[2] = from[1];
  241. to[3] = from[0];
  242. to += 4;
  243. from += 4;
  244. }
  245. }
  246. else
  247. {
  248. /* Simply copy the data */
  249. memcpy (int_vals, *buf_ptr, num_ints * sizeof (int));
  250. }
  251. /* Advance the pointer */
  252. *buf_ptr += num_ints * sizeof (int);
  253. return;
  254. }
  255. /***************************************************************************
  256. *
  257. * get_double - gets doubles from the byte stream.
  258. *
  259. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  260. *
  261. * Purpose:
  262. * This function extracts a given number of 8 byte doubles
  263. * from the byte stream. This function will byte swap if necessary.
  264. * The pointer into the buffer will be advanced to the position
  265. * immediately following the requested doubles.
  266. *
  267. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  268. *
  269. * Parameters:
  270. * buf_ptr <In/Out> == (char **) Address of pointer into byte stream
  271. * swap_bytes <Input> == (int) 1 if bytes need to be swapped
  272. * num_doubles <Input> == (int) Number of elements in double_vals array
  273. * double_vals <Input> == (double *) Pointer to array of doubles
  274. *
  275. * RETURN <Output> == (void)
  276. *
  277. ****************************************************************************/
  278. void get_double (
  279. char **buf_ptr,
  280. int swap_bytes,
  281. int num_doubles,
  282. double *double_vals
  283. )
  284. {
  285. int i;
  286. char *from;
  287. char *to;
  288. if (swap_bytes)
  289. {
  290. /* Operating system endian-ness does not match data,
  291. /* the double array needs to be byte swapped. */
  292. from = *buf_ptr;
  293. to = (char *)double_vals;
  294. for (i = 0; i < num_doubles; i++)
  295. {
  296. to[0] = from[7];
  297. to[1] = from[6];
  298. to[2] = from[5];
  299. to[3] = from[4];
  300. to[4] = from[3];
  301. to[5] = from[2];
  302. to[6] = from[1];
  303. to[7] = from[0];
  304. to += 8;
  305. from += 8;
  306. }
  307. }
  308. else
  309. {
  310. /* Simply copy the data. */
  311. memcpy (double_vals, *buf_ptr, num_doubles * sizeof (double));
  312. }
  313. /* Advance the pointer. */
  314. *buf_ptr += num_doubles * sizeof (double);
  315. return;
  316. }
  317. /***************************************************************************
  318. *
  319. * get_xy - Gets xy points from the byte stream.
  320. *
  321. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  322. *
  323. * Purpose:
  324. * This function extracts a given number of xy double precision
  325. * coordinates from the byte stream. This function will byte swap
  326. * if necessary. The pointer into the buffer will be advanced to
  327. * the position immediately following the requested points.
  328. *
  329. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  330. *
  331. * Parameters:
  332. * buf_ptr <In/Out> == (char **) Address of pointer into byte stream
  333. * swap_bytes <Input> == (int) 1 if bytes need to be swapped
  334. * num_points <Input> == (int) Number of elements in points array
  335. * points <Output> == (Point *) Pointer to array of points
  336. *
  337. * RETURN <Output> == (void)
  338. *
  339. ****************************************************************************/
  340. void get_xy (
  341. char **buf_ptr,
  342. int swap_bytes,
  343. int num_points,
  344. Point *points
  345. )
  346. {
  347. int i;
  348. char *from;
  349. char *to;
  350. Point *pt;
  351. if (swap_bytes)
  352. {
  353. /* Operating system endian-ness does not match data,
  354. /* so the point array needs be byte swapped. */
  355. from = *buf_ptr;
  356. pt = points;
  357. for (i = 0; i < num_points; i++)
  358. {
  359. /* swap X value */
  360. to = (char *)&pt->x;
  361. to[0] = from[7];
  362. to[1] = from[6];
  363. to[2] = from[5];
  364. to[3] = from[4];
  365. to[4] = from[3];
  366. to[5] = from[2];
  367. to[6] = from[1];
  368. to[7] = from[0];
  369. /* swap Y value */
  370. to = (char *)&pt->y;
  371. from += 8;
  372. to[0] = from[7];
  373. to[1] = from[6];
  374. to[2] = from[5];
  375. to[3] = from[4];
  376. to[4] = from[3];
  377. to[5] = from[2];
  378. to[6] = from[1];
  379. to[7] = from[0];
  380. from += 8;
  381. pt++;
  382. }
  383. }
  384. else
  385. {
  386. /* Simply copy the data. */
  387. memcpy (points, *buf_ptr, num_points * sizeof (Point));
  388. }
  389. /* Advance the pointer. */
  390. *buf_ptr += num_points * sizeof (Point);
  391. return;
  392. }
  393. /***********************************************************************
  394. *
  395. * geom_allocate - Allocates or reallocates a geometry's point,
  396. * z and/or m buffers.
  397. *
  398. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  399. *
  400. * Purpose:
  401. * This function allocates the point buffers of the given geometry
  402. * to the specified size. If the buffers are already allocated and
  403. * they are currently smaller than the specified size they will be
  404. * reallocated to the larger size (plus an additional number of
  405. * points to prevent the need to realloc for just a couple of points).
  406. * If the specified size is smaller than the currently allocated size
  407. * nothing will be done.
  408. *
  409. * The geometry type is examined to see if Z and/or M values are
  410. * specified, and the Z and/or M buffers are allocated or reallocated
  411. * in the same way as the point buffer.
  412. *
  413. *:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  414. *
  415. * Parameters:
  416. * geom <In/Out> == (Geometry *) A geometry object
  417. * to allocate the point buffers for.
  418. * npoints <Input> == (int) The size to allocate the point
  419. * buffers to allocate/reallocate.
  420. *
  421. * RETURN <Output> == (int) Error code
  422. *
  423. ***********************************************************************/
  424. int geom_allocate(
  425. Geometry *geom,
  426. int npoints
  427. )
  428. {
  429. int allocPerformed = 0;
  430. int newAllocSize;
  431. double *newArray;
  432. Point *newPoints;
  433. int need_m;
  434. int need_z;
  435. need_m = geom->type & 0x01;
  436. need_z = geom->type & 0x02;
  437. if (geom->alloc_size < npoints || geom->alloc_size == 0)
  438. {
  439. /*
  440. * We have to increase the buffer sizes, make sure that
  441. * we the newly allocated size is bigger at least by
  442. * the POINT_ALLOC_THRESHOLD value.
  443. */
  444. if (npoints > (geom->alloc_size + POINT_ALLOC_THRESHOLD))
  445. newAllocSize = npoints;
  446. else
  447. newAllocSize = geom->alloc_size + POINT_ALLOC_THRESHOLD;
  448. }
  449. else
  450. {
  451. /* We don't have to increase the buffer sizes */
  452. newAllocSize = geom->alloc_size;
  453. }
  454. if (geom->pt == NULL)
  455. {
  456. /* Allocate a new array of XY points */
  457. geom->pt = malloc (newAllocSize * sizeof(Point));
  458. if (geom->pt == NULL)
  459. return GEOM_OUT_OF_MEMORY;
  460. allocPerformed = 1;
  461. }
  462. else if (npoints > geom->alloc_size)
  463. {
  464. /* Reallocate an array of XY points */
  465. newPoints = malloc (newAllocSize * sizeof(Point));
  466. if (newPoints == NULL)
  467. return GEOM_OUT_OF_MEMORY;
  468. memcpy (newPoints, geom->pt, geom->alloc_size * sizeof(Point));
  469. free (geom->pt);
  470. geom->pt = newPoints;
  471. allocPerformed = 1;
  472. }
  473. if (need_z)
  474. {
  475. if (geom->z == NULL)
  476. {
  477. /* Allocate a new array of Z values */
  478. geom->z = malloc (newAllocSize * sizeof(double));
  479. if (geom->z == NULL)
  480. return GEOM_OUT_OF_MEMORY;
  481. allocPerformed = 1;
  482. }
  483. else if (npoints > geom->alloc_size)
  484. {
  485. /* Reallocate an array of Z values */
  486. newArray = malloc (newAllocSize * sizeof(double));
  487. if (newArray == NULL)
  488. return GEOM_OUT_OF_MEMORY;
  489. memcpy (newArray, geom->z, geom->alloc_size * sizeof(double));
  490. free (geom->z);
  491. geom->z = newArray;
  492. allocPerformed = 1;
  493. }
  494. }
  495. if (need_m)
  496. {
  497. if (geom->m == NULL)
  498. {
  499. /* Allocate a new array of M values */
  500. geom->m = malloc (newAllocSize * sizeof(double));
  501. if (geom->m == NULL)
  502. return GEOM_OUT_OF_MEMORY;
  503. allocPerformed = 1;
  504. }
  505. else if (npoints > geom->alloc_size)
  506. {
  507. /* Reallocate an array of M values */
  508. newArray = malloc (newAllocSize * sizeof(double));
  509. if (newArray == NULL)
  510. return GEOM_OUT_OF_MEMORY;
  511. memcpy (newArray, geom->m, geom->alloc_size * sizeof(double));
  512. free (geom->m);
  513. geom->m = newArray;
  514. allocPerformed = 1;
  515. }
  516. }
  517. /*
  518. * If we allocated or reallocated the buffers, increase the
  519. * allocated size.
  520. */
  521. if (allocPerformed)
  522. geom->alloc_size = newAllocSize;
  523. return GEOM_SUCCESS;
  524. }