plus_struct.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Dave Gerdes, CERL.
  7. * Update to GRASS 5.7 Radim Blazek.
  8. *
  9. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  10. *
  11. * COPYRIGHT: (C) 2001 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <sys/types.h>
  19. #include <string.h>
  20. #include <grass/vector.h>
  21. /*
  22. * Routines for reading and writing Dig+ structures.
  23. * return 0 on success, -1 on failure of whatever kind
  24. * if you dont want it written out, then dont call these routines
  25. * ie check for deleted status before calling a write routine
  26. * in as much as it would be nice to hide that code in here,
  27. * this is a library routine and we chose to make it dependent on
  28. * as few external files as possible
  29. */
  30. /* These routines assume ptr->alloc_lines is valid
  31. * Make sure it is initialized before calling
  32. */
  33. /*
  34. * Internally, my default variables for lines/areas/nodes/isles are type
  35. * plus_t which is typedefed as short. This limits the current version
  36. * to no more than 32K lines, nodes etc. (excluding points)
  37. * All in the name of future expansion, I have converted these values to
  38. * longs in the dig_plus data file.
  39. *
  40. * NOTE: 3.10 changes plus_t to ints.
  41. * This assumes that any reasonable machine will use 4 bytes to
  42. * store an int. The mapdev code is not guaranteed to work if
  43. * plus_t is changed to a type that is larger than an int.
  44. */
  45. int dig_Rd_P_node(struct Plus_head *Plus, int n, struct gvfile * fp)
  46. {
  47. int cnt, n_edges;
  48. struct P_node *ptr;
  49. G_debug(3, "dig_Rd_P_node()");
  50. if (0 >= dig__fread_port_P(&cnt, 1, fp))
  51. return (-1);
  52. if (cnt == 0) { /* dead */
  53. G_debug(3, " node is dead");
  54. Plus->Node[n] = NULL;
  55. return 0;
  56. }
  57. ptr = dig_alloc_node();
  58. ptr->n_lines = cnt;
  59. if (dig_node_alloc_line(ptr, ptr->n_lines) == -1)
  60. return -1;
  61. if (ptr->n_lines) {
  62. if (0 >= dig__fread_port_P(ptr->lines, ptr->n_lines, fp))
  63. return (-1);
  64. if (0 >= dig__fread_port_F(ptr->angles, ptr->n_lines, fp))
  65. return (-1);
  66. }
  67. if (Plus->with_z)
  68. if (0 >= dig__fread_port_P(&n_edges, 1, fp)) /* reserved for edges */
  69. return (-1);
  70. /* here will be edges */
  71. if (0 >= dig__fread_port_D(&(ptr->x), 1, fp))
  72. return (-1);
  73. if (0 >= dig__fread_port_D(&(ptr->y), 1, fp))
  74. return (-1);
  75. if (Plus->with_z) {
  76. if (0 >= dig__fread_port_D(&(ptr->z), 1, fp))
  77. return (-1);
  78. }
  79. else
  80. ptr->z = 0;
  81. Plus->Node[n] = ptr;
  82. return (0);
  83. }
  84. int dig_Wr_P_node(struct Plus_head *Plus, int n, struct gvfile * fp)
  85. {
  86. int i, n_edges = 0;
  87. struct P_node *ptr;
  88. G_debug(3, "dig_Wr_P_node()");
  89. ptr = Plus->Node[n];
  90. /* If NULL i.e. dead write just 0 instead of number of lines */
  91. if (ptr == NULL) {
  92. G_debug(3, " node is dead -> write 0 only");
  93. i = 0;
  94. if (0 >= dig__fwrite_port_P(&i, 1, fp))
  95. return (-1);
  96. return 0;
  97. }
  98. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  99. return (-1);
  100. if (ptr->n_lines) {
  101. if (0 >= dig__fwrite_port_P(ptr->lines, ptr->n_lines, fp))
  102. return (-1);
  103. if (0 >= dig__fwrite_port_F(ptr->angles, ptr->n_lines, fp))
  104. return (-1);
  105. }
  106. if (Plus->with_z)
  107. if (0 >= dig__fwrite_port_P(&n_edges, 1, fp)) /* reserved for edges */
  108. return (-1);
  109. /* here will be edges */
  110. if (0 >= dig__fwrite_port_D(&(ptr->x), 1, fp))
  111. return (-1);
  112. if (0 >= dig__fwrite_port_D(&(ptr->y), 1, fp))
  113. return (-1);
  114. if (Plus->with_z)
  115. if (0 >= dig__fwrite_port_D(&(ptr->z), 1, fp))
  116. return (-1);
  117. return (0);
  118. }
  119. int dig_Rd_P_line(struct Plus_head *Plus, int n, struct gvfile * fp)
  120. {
  121. int n_edges;
  122. char tp;
  123. struct P_line *ptr;
  124. G_debug(3, "dig_Rd_P_line()");
  125. if (0 >= dig__fread_port_C(&tp, 1, fp))
  126. return (-1);
  127. if (tp == 0) { /* dead */
  128. G_debug(3, " line is dead");
  129. Plus->Line[n] = NULL;
  130. return 0;
  131. }
  132. ptr = dig_alloc_line();
  133. /* type */
  134. ptr->type = dig_type_from_store(tp);
  135. G_debug(5, " line type %d -> %d", tp, ptr->type);
  136. /* offset */
  137. if (0 >= dig__fread_port_O(&(ptr->offset), 1, fp, Plus->off_t_size))
  138. return (-1);
  139. if (ptr->type == GV_POINT) {
  140. ptr->topo = NULL;
  141. }
  142. else {
  143. ptr->topo = dig_alloc_topo(ptr->type);
  144. }
  145. /* centroids */
  146. if (ptr->type & GV_CENTROID) {
  147. struct P_topo_c *topo = (struct P_topo_c *)ptr->topo;
  148. if (0 >= dig__fread_port_P(&(topo->area), 1, fp))
  149. return -1;
  150. }
  151. /* lines */
  152. else if (ptr->type & GV_LINE) {
  153. struct P_topo_l *topo = (struct P_topo_l *)ptr->topo;
  154. if (0 >= dig__fread_port_P(&(topo->N1), 1, fp))
  155. return -1;
  156. if (0 >= dig__fread_port_P(&(topo->N2), 1, fp))
  157. return -1;
  158. }
  159. /* boundaries */
  160. if (ptr->type & GV_BOUNDARY) {
  161. struct P_topo_b *topo = (struct P_topo_b *)ptr->topo;
  162. if (0 >= dig__fread_port_P(&(topo->N1), 1, fp))
  163. return -1;
  164. if (0 >= dig__fread_port_P(&(topo->N2), 1, fp))
  165. return -1;
  166. if (0 >= dig__fread_port_P(&(topo->left), 1, fp))
  167. return -1;
  168. if (0 >= dig__fread_port_P(&(topo->right), 1, fp))
  169. return -1;
  170. }
  171. /* faces */
  172. else if ((ptr->type & GV_FACE) && Plus->with_z) { /* reserved for face edges */
  173. struct P_topo_f *topo = (struct P_topo_f *)ptr->topo;
  174. if (0 >= dig__fread_port_I(&n_edges, 1, fp))
  175. return -1;
  176. /* here will be list of edges */
  177. /* left / right volume */
  178. if (0 >= dig__fread_port_P(&(topo->left), 1, fp))
  179. return -1;
  180. if (0 >= dig__fread_port_P(&(topo->left), 1, fp))
  181. return -1;
  182. }
  183. /* kernels */
  184. else if ((ptr->type & GV_KERNEL) && Plus->with_z) { /* reserved for kernel (volume number) */
  185. struct P_topo_k *topo = (struct P_topo_k *)ptr->topo;
  186. if (0 >= dig__fread_port_P(&(topo->volume), 1, fp))
  187. return -1;
  188. }
  189. Plus->Line[n] = ptr;
  190. return (0);
  191. }
  192. int dig_Wr_P_line(struct Plus_head *Plus, int n, struct gvfile * fp)
  193. {
  194. int n_edges = 0;
  195. char ch;
  196. struct P_line *ptr;
  197. G_debug(4, "dig_Wr_P_line() line = %d", n);
  198. ptr = Plus->Line[n];
  199. /* if NULL i.e. dead write just 0 instead of type */
  200. if (ptr == NULL) {
  201. G_debug(3, " line is dead -> write 0 only");
  202. ch = 0;
  203. if (0 >= dig__fwrite_port_C(&ch, 1, fp))
  204. return (-1);
  205. return 0;
  206. }
  207. /* type */
  208. ch = (char)dig_type_to_store(ptr->type);
  209. G_debug(5, " line type %d -> %d", ptr->type, ch);
  210. if (0 >= dig__fwrite_port_C(&ch, 1, fp))
  211. return (-1);
  212. /* offset */
  213. if (0 >= dig__fwrite_port_O(&(ptr->offset), 1, fp, Plus->off_t_size))
  214. return (-1);
  215. if (!ptr->topo)
  216. return (0);
  217. /* nothing else for points */
  218. /* centroids */
  219. if (ptr->type & GV_CENTROID) {
  220. struct P_topo_c *topo = (struct P_topo_c *)ptr->topo;
  221. if (0 >= dig__fwrite_port_P(&(topo->area), 1, fp))
  222. return (-1);
  223. }
  224. /* lines */
  225. else if (ptr->type & GV_LINE) {
  226. struct P_topo_l *topo = (struct P_topo_l *)ptr->topo;
  227. if (0 >= dig__fwrite_port_P(&(topo->N1), 1, fp))
  228. return (-1);
  229. if (0 >= dig__fwrite_port_P(&(topo->N2), 1, fp))
  230. return (-1);
  231. }
  232. /* boundaries */
  233. else if (ptr->type & GV_BOUNDARY) {
  234. struct P_topo_b *topo = (struct P_topo_b *)ptr->topo;
  235. if (0 >= dig__fwrite_port_P(&(topo->N1), 1, fp))
  236. return (-1);
  237. if (0 >= dig__fwrite_port_P(&(topo->N2), 1, fp))
  238. return (-1);
  239. if (0 >= dig__fwrite_port_P(&(topo->left), 1, fp))
  240. return (-1);
  241. if (0 >= dig__fwrite_port_P(&(topo->right), 1, fp))
  242. return (-1);
  243. }
  244. /* faces */
  245. else if ((ptr->type & GV_FACE) && Plus->with_z) { /* reserved for face */
  246. struct P_topo_f *topo = (struct P_topo_f *)ptr->topo;
  247. if (0 >= dig__fwrite_port_I(&n_edges, 1, fp))
  248. return (-1);
  249. /* here will be list of edges */
  250. /* left / right volume / hole */
  251. if (0 >= dig__fwrite_port_P(&(topo->left), 1, fp))
  252. return (-1);
  253. if (0 >= dig__fwrite_port_P(&(topo->right), 1, fp))
  254. return (-1);
  255. }
  256. /* kernels */
  257. else if ((ptr->type & GV_KERNEL) && Plus->with_z) { /* reserved for kernel (volume number) */
  258. struct P_topo_k *topo = (struct P_topo_k *)ptr->topo;
  259. /* volume */
  260. if (0 >= dig__fwrite_port_P(&(topo->volume), 1, fp))
  261. return (-1);
  262. }
  263. return (0);
  264. }
  265. int dig_Rd_P_area(struct Plus_head *Plus, int n, struct gvfile * fp)
  266. {
  267. int cnt;
  268. struct P_area *ptr;
  269. G_debug(3, "dig_Rd_P_area(): n = %d", n);
  270. if (0 >= dig__fread_port_P(&cnt, 1, fp))
  271. return (-1);
  272. if (cnt == 0) { /* dead */
  273. Plus->Area[n] = NULL;
  274. return 0;
  275. }
  276. ptr = dig_alloc_area();
  277. /* boundaries */
  278. ptr->n_lines = cnt;
  279. if (dig_area_alloc_line(ptr, ptr->n_lines) == -1)
  280. return -1;
  281. if (ptr->n_lines)
  282. if (0 >= dig__fread_port_P(ptr->lines, ptr->n_lines, fp))
  283. return -1;
  284. /* isles */
  285. if (0 >= dig__fread_port_P(&(ptr->n_isles), 1, fp))
  286. return -1;
  287. if (dig_area_alloc_isle(ptr, ptr->n_isles) == -1)
  288. return -1;
  289. if (ptr->n_isles)
  290. if (0 >= dig__fread_port_P(ptr->isles, ptr->n_isles, fp))
  291. return -1;
  292. /* centroid */
  293. if (0 >= dig__fread_port_P(&(ptr->centroid), 1, fp))
  294. return -1;
  295. Plus->Area[n] = ptr;
  296. return (0);
  297. }
  298. int dig_Wr_P_area(struct Plus_head *Plus, int n, struct gvfile * fp)
  299. {
  300. int i;
  301. struct P_area *ptr;
  302. ptr = Plus->Area[n];
  303. /* If NULL i.e. dead write just 0 instead of number of lines */
  304. if (ptr == NULL) {
  305. i = 0;
  306. if (0 >= dig__fwrite_port_P(&i, 1, fp))
  307. return (-1);
  308. return 0;
  309. }
  310. /* boundaries */
  311. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  312. return (-1);
  313. if (ptr->n_lines)
  314. if (0 >= dig__fwrite_port_P(ptr->lines, ptr->n_lines, fp))
  315. return -1;
  316. /* isles */
  317. if (0 >= dig__fwrite_port_P(&(ptr->n_isles), 1, fp))
  318. return (-1);
  319. if (ptr->n_isles)
  320. if (0 >= dig__fwrite_port_P(ptr->isles, ptr->n_isles, fp))
  321. return -1;
  322. /* centroid */
  323. if (0 >= dig__fwrite_port_P(&(ptr->centroid), 1, fp))
  324. return (-1);
  325. return (0);
  326. }
  327. int dig_Rd_P_isle(struct Plus_head *Plus, int n, struct gvfile * fp)
  328. {
  329. int cnt;
  330. struct P_isle *ptr;
  331. G_debug(3, "dig_Rd_P_isle()");
  332. if (0 >= dig__fread_port_P(&cnt, 1, fp))
  333. return (-1);
  334. if (cnt == 0) { /* dead */
  335. Plus->Isle[n] = NULL;
  336. return 0;
  337. }
  338. ptr = dig_alloc_isle();
  339. /* boundaries */
  340. ptr->n_lines = cnt;
  341. if (dig_isle_alloc_line(ptr, ptr->n_lines) == -1)
  342. return -1;
  343. if (ptr->n_lines)
  344. if (0 >= dig__fread_port_P(ptr->lines, ptr->n_lines, fp))
  345. return -1;
  346. /* area */
  347. if (0 >= dig__fread_port_P(&(ptr->area), 1, fp))
  348. return -1;
  349. Plus->Isle[n] = ptr;
  350. return (0);
  351. }
  352. int dig_Wr_P_isle(struct Plus_head *Plus, int n, struct gvfile * fp)
  353. {
  354. int i;
  355. struct P_isle *ptr;
  356. ptr = Plus->Isle[n];
  357. /* If NULL i.e. dead write just 0 instead of number of lines */
  358. if (ptr == NULL) {
  359. i = 0;
  360. if (0 >= dig__fwrite_port_P(&i, 1, fp))
  361. return (-1);
  362. return 0;
  363. }
  364. /* lines */
  365. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  366. return (-1);
  367. if (ptr->n_lines)
  368. if (0 >= dig__fwrite_port_P(ptr->lines, ptr->n_lines, fp))
  369. return -1;
  370. /* area */
  371. if (0 >= dig__fwrite_port_P(&(ptr->area), 1, fp))
  372. return (-1);
  373. return (0);
  374. }
  375. /*!
  376. \brief Read Plus_head from file
  377. \param fp pointer to gvfile structure
  378. \param[in,out] ptr pointer to Plus_head structure
  379. \return -1 error
  380. \return 0 OK
  381. */
  382. int dig_Rd_Plus_head(struct gvfile * fp, struct Plus_head *ptr)
  383. {
  384. unsigned char buf[5];
  385. int byte_order;
  386. dig_rewind(fp);
  387. /* bytes 1 - 5 */
  388. if (0 >= dig__fread_port_C(buf, 5, fp))
  389. return (-1);
  390. ptr->Version_Major = buf[0];
  391. ptr->Version_Minor = buf[1];
  392. ptr->Back_Major = buf[2];
  393. ptr->Back_Minor = buf[3];
  394. byte_order = buf[4];
  395. G_debug(2,
  396. "Topo header: file version %d.%d , supported from GRASS version %d.%d",
  397. ptr->Version_Major, ptr->Version_Minor, ptr->Back_Major,
  398. ptr->Back_Minor);
  399. G_debug(2, " byte order %d", byte_order);
  400. /* check version numbers */
  401. if (ptr->Version_Major > GV_TOPO_VER_MAJOR ||
  402. ptr->Version_Minor > GV_TOPO_VER_MINOR) {
  403. /* The file was created by GRASS library with higher version than this one */
  404. if (ptr->Back_Major > GV_TOPO_VER_MAJOR ||
  405. ptr->Back_Minor > GV_TOPO_VER_MINOR) {
  406. /* This version of GRASS lib is lower than the oldest which can read this format */
  407. G_fatal_error
  408. ("Topology format version %d.%d is not supported by this release."
  409. " Try to rebuild topology or upgrade GRASS.",
  410. ptr->Version_Major, ptr->Version_Minor);
  411. return (-1);
  412. }
  413. G_warning
  414. ("Your GRASS version does not fully support topology format %d.%d of the vector."
  415. " Consider to rebuild topology or upgrade GRASS.",
  416. ptr->Version_Major, ptr->Version_Minor);
  417. }
  418. /* init Port_info structure and set as default */
  419. dig_init_portable(&(ptr->port), byte_order);
  420. dig_set_cur_port(&(ptr->port));
  421. /* bytes 6 - 9 : header size */
  422. if (0 >= dig__fread_port_L(&(ptr->head_size), 1, fp))
  423. return (-1);
  424. G_debug(2, " header size %ld", ptr->head_size);
  425. /* determine required offset size from header size */
  426. /* this is not safe in case new fields get added in later versions */
  427. /* better: add a new field with off_t_size after byte_order? */
  428. if (ptr->head_size >= 142 + 32) /* keep in sync with dig_Wr_Plus_head() */
  429. ptr->off_t_size = 8;
  430. else
  431. ptr->off_t_size = 4;
  432. if (sizeof(off_t) < ptr->off_t_size) {
  433. G_warning("Vector exceeds supported file size limit");
  434. return (-1);
  435. }
  436. G_debug(1, "topo off_t size = %d", ptr->off_t_size);
  437. /* byte 10 : dimension 2D or 3D */
  438. if (0 >= dig__fread_port_C(buf, 1, fp))
  439. return (-1);
  440. ptr->with_z = buf[0];
  441. G_debug(2, " with_z %d", ptr->with_z);
  442. /* bytes 11 - 58 : bound box */
  443. if (0 >= dig__fread_port_D(&(ptr->box.N), 1, fp))
  444. return (-1);
  445. if (0 >= dig__fread_port_D(&(ptr->box.S), 1, fp))
  446. return (-1);
  447. if (0 >= dig__fread_port_D(&(ptr->box.E), 1, fp))
  448. return (-1);
  449. if (0 >= dig__fread_port_D(&(ptr->box.W), 1, fp))
  450. return (-1);
  451. if (0 >= dig__fread_port_D(&(ptr->box.T), 1, fp))
  452. return (-1);
  453. if (0 >= dig__fread_port_D(&(ptr->box.B), 1, fp))
  454. return (-1);
  455. /* bytes 59 - 86 : number of structures */
  456. if (0 >= dig__fread_port_P(&(ptr->n_nodes), 1, fp))
  457. return (-1);
  458. if (0 >= dig__fread_port_P(&(ptr->n_edges), 1, fp))
  459. return (-1);
  460. if (0 >= dig__fread_port_P(&(ptr->n_lines), 1, fp))
  461. return (-1);
  462. if (0 >= dig__fread_port_P(&(ptr->n_areas), 1, fp))
  463. return (-1);
  464. if (0 >= dig__fread_port_P(&(ptr->n_isles), 1, fp))
  465. return (-1);
  466. if (0 >= dig__fread_port_P(&(ptr->n_volumes), 1, fp))
  467. return (-1);
  468. if (0 >= dig__fread_port_P(&(ptr->n_holes), 1, fp))
  469. return (-1);
  470. /* bytes 87 - 110 : number of line types */
  471. if (0 >= dig__fread_port_P(&(ptr->n_plines), 1, fp))
  472. return (-1);
  473. if (0 >= dig__fread_port_P(&(ptr->n_llines), 1, fp))
  474. return (-1);
  475. if (0 >= dig__fread_port_P(&(ptr->n_blines), 1, fp))
  476. return (-1);
  477. if (0 >= dig__fread_port_P(&(ptr->n_clines), 1, fp))
  478. return (-1);
  479. if (0 >= dig__fread_port_P(&(ptr->n_flines), 1, fp))
  480. return (-1);
  481. if (0 >= dig__fread_port_P(&(ptr->n_klines), 1, fp))
  482. return (-1);
  483. /* bytes 111 - 138 : Offset */
  484. if (0 >= dig__fread_port_O(&(ptr->Node_offset), 1, fp, ptr->off_t_size))
  485. return (-1);
  486. if (0 >= dig__fread_port_O(&(ptr->Edge_offset), 1, fp, ptr->off_t_size))
  487. return (-1);
  488. if (0 >= dig__fread_port_O(&(ptr->Line_offset), 1, fp, ptr->off_t_size))
  489. return (-1);
  490. if (0 >= dig__fread_port_O(&(ptr->Area_offset), 1, fp, ptr->off_t_size))
  491. return (-1);
  492. if (0 >= dig__fread_port_O(&(ptr->Isle_offset), 1, fp, ptr->off_t_size))
  493. return (-1);
  494. if (0 >= dig__fread_port_O(&(ptr->Volume_offset), 1, fp, ptr->off_t_size))
  495. return (-1);
  496. if (0 >= dig__fread_port_O(&(ptr->Hole_offset), 1, fp, ptr->off_t_size))
  497. return (-1);
  498. /* bytes 139 - 142 : Coor size and time */
  499. if (0 >= dig__fread_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  500. return (-1);
  501. G_debug(2, " coor size %"PRI_OFF_T, ptr->coor_size);
  502. dig_fseek(fp, ptr->head_size, SEEK_SET);
  503. return (0);
  504. }
  505. int dig_Wr_Plus_head(struct gvfile * fp, struct Plus_head *ptr)
  506. {
  507. unsigned char buf[10];
  508. long length = 142;
  509. dig_rewind(fp);
  510. dig_set_cur_port(&(ptr->port));
  511. /* bytes 1 - 5 */
  512. buf[0] = GV_TOPO_VER_MAJOR;
  513. buf[1] = GV_TOPO_VER_MINOR;
  514. buf[2] = GV_TOPO_EARLIEST_MAJOR;
  515. buf[3] = GV_TOPO_EARLIEST_MINOR;
  516. buf[4] = ptr->port.byte_order;
  517. if (0 >= dig__fwrite_port_C(buf, 5, fp))
  518. return (-1);
  519. /* determine required offset size from coor file size */
  520. if (ptr->coor_size > (off_t)PORT_LONG_MAX) {
  521. /* can only happen when sizeof(off_t) == 8 */
  522. ptr->off_t_size = 8;
  523. }
  524. else
  525. ptr->off_t_size = 4;
  526. /* add a new field with off_t_size after byte_order? */
  527. /* adjust header size for large files */
  528. if (ptr->off_t_size == 8) {
  529. /* 7 offset values and coor file size: add 8 * 4 */
  530. length += 32;
  531. }
  532. /* bytes 6 - 9 : header size */
  533. if (0 >= dig__fwrite_port_L(&length, 1, fp))
  534. return (0);
  535. /* byte 10 : dimension 2D or 3D */
  536. buf[0] = ptr->with_z;
  537. if (0 >= dig__fwrite_port_C(buf, 1, fp))
  538. return (0);
  539. /* bytes 11 - 58 : bound box */
  540. if (0 >= dig__fwrite_port_D(&(ptr->box.N), 1, fp))
  541. return (-1);
  542. if (0 >= dig__fwrite_port_D(&(ptr->box.S), 1, fp))
  543. return (-1);
  544. if (0 >= dig__fwrite_port_D(&(ptr->box.E), 1, fp))
  545. return (-1);
  546. if (0 >= dig__fwrite_port_D(&(ptr->box.W), 1, fp))
  547. return (-1);
  548. if (0 >= dig__fwrite_port_D(&(ptr->box.T), 1, fp))
  549. return (-1);
  550. if (0 >= dig__fwrite_port_D(&(ptr->box.B), 1, fp))
  551. return (-1);
  552. /* bytes 59 - 86 : number of structures */
  553. if (0 >= dig__fwrite_port_P(&(ptr->n_nodes), 1, fp))
  554. return (-1);
  555. if (0 >= dig__fwrite_port_P(&(ptr->n_edges), 1, fp))
  556. return (-1);
  557. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  558. return (-1);
  559. if (0 >= dig__fwrite_port_P(&(ptr->n_areas), 1, fp))
  560. return (-1);
  561. if (0 >= dig__fwrite_port_P(&(ptr->n_isles), 1, fp))
  562. return (-1);
  563. if (0 >= dig__fwrite_port_P(&(ptr->n_volumes), 1, fp))
  564. return (-1);
  565. if (0 >= dig__fwrite_port_P(&(ptr->n_holes), 1, fp))
  566. return (-1);
  567. /* bytes 87 - 110 : number of line types */
  568. if (0 >= dig__fwrite_port_P(&(ptr->n_plines), 1, fp))
  569. return (-1);
  570. if (0 >= dig__fwrite_port_P(&(ptr->n_llines), 1, fp))
  571. return (-1);
  572. if (0 >= dig__fwrite_port_P(&(ptr->n_blines), 1, fp))
  573. return (-1);
  574. if (0 >= dig__fwrite_port_P(&(ptr->n_clines), 1, fp))
  575. return (-1);
  576. if (0 >= dig__fwrite_port_P(&(ptr->n_flines), 1, fp))
  577. return (-1);
  578. if (0 >= dig__fwrite_port_P(&(ptr->n_klines), 1, fp))
  579. return (-1);
  580. /* bytes 111 - 138 : Offset */
  581. if (0 >= dig__fwrite_port_O(&(ptr->Node_offset), 1, fp, ptr->off_t_size))
  582. return (-1);
  583. if (0 >= dig__fwrite_port_O(&(ptr->Edge_offset), 1, fp, ptr->off_t_size))
  584. return (-1);
  585. if (0 >= dig__fwrite_port_O(&(ptr->Line_offset), 1, fp, ptr->off_t_size))
  586. return (-1);
  587. if (0 >= dig__fwrite_port_O(&(ptr->Area_offset), 1, fp, ptr->off_t_size))
  588. return (-1);
  589. if (0 >= dig__fwrite_port_O(&(ptr->Isle_offset), 1, fp, ptr->off_t_size))
  590. return (-1);
  591. if (0 >= dig__fwrite_port_O(&(ptr->Volume_offset), 1, fp, ptr->off_t_size))
  592. return (-1);
  593. if (0 >= dig__fwrite_port_O(&(ptr->Hole_offset), 1, fp, ptr->off_t_size))
  594. return (-1);
  595. /* bytes 139 - 142 : Coor size and time */
  596. if (0 >= dig__fwrite_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  597. return (-1);
  598. G_debug(2, "topo body offset %"PRI_OFF_T, dig_ftell(fp));
  599. return (0);
  600. }