plus_struct.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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, vol;
  122. char tp;
  123. struct P_line *ptr;
  124. struct P_node *Node;
  125. G_debug(3, "dig_Rd_P_line()");
  126. if (0 >= dig__fread_port_C(&tp, 1, fp))
  127. return (-1);
  128. if (tp == 0) { /* dead */
  129. G_debug(3, " line is dead");
  130. Plus->Line[n] = NULL;
  131. return 0;
  132. }
  133. ptr = dig_alloc_line();
  134. /* type */
  135. ptr->type = dig_type_from_store(tp);
  136. G_debug(5, " line type %d -> %d", tp, ptr->type);
  137. /* offset */
  138. if (0 >= dig__fread_port_O(&(ptr->offset), 1, fp, Plus->off_t_size))
  139. return (-1);
  140. /* first node */
  141. if (ptr->type & (GV_POINTS | GV_LINES | GV_KERNEL))
  142. if (0 >= dig__fread_port_P(&(ptr->N1), 1, fp))
  143. return -1;
  144. /* second node, for points/centroids not needed */
  145. if (ptr->type & (GV_LINE | GV_BOUNDARY)) {
  146. if (0 >= dig__fread_port_P(&(ptr->N2), 1, fp))
  147. return -1;
  148. }
  149. else if (ptr->type & (GV_POINTS | GV_KERNEL))
  150. ptr->N2 = ptr->N1;
  151. /* left area for boundary, area for centroid */
  152. if (ptr->type & (GV_BOUNDARY | GV_CENTROID))
  153. if (0 >= dig__fread_port_P(&(ptr->left), 1, fp))
  154. return -1;
  155. /* right area */
  156. if (ptr->type & GV_BOUNDARY)
  157. if (0 >= dig__fread_port_P(&(ptr->right), 1, fp))
  158. return -1;
  159. if ((ptr->type & GV_FACE) && Plus->with_z) { /* reserved for face edges */
  160. if (0 >= dig__fread_port_I(&n_edges, 1, fp))
  161. return -1;
  162. /* here will be list of edges */
  163. /* left / right volume */
  164. if (0 >= dig__fread_port_P(&vol, 1, fp))
  165. return -1;
  166. if (0 >= dig__fread_port_P(&vol, 1, fp))
  167. return -1;
  168. }
  169. if ((ptr->type & GV_KERNEL) && Plus->with_z) /* reserved for kernel (volume number) */
  170. if (0 >= dig__fread_port_P(&vol, 1, fp))
  171. return -1;
  172. /* bounding box */
  173. if (ptr->type & (GV_LINE | GV_BOUNDARY | GV_FACE)) {
  174. if (0 >= dig__fread_port_D(&(ptr->N), 1, fp))
  175. return -1;
  176. if (0 >= dig__fread_port_D(&(ptr->S), 1, fp))
  177. return -1;
  178. if (0 >= dig__fread_port_D(&(ptr->E), 1, fp))
  179. return -1;
  180. if (0 >= dig__fread_port_D(&(ptr->W), 1, fp))
  181. return -1;
  182. if (Plus->with_z) {
  183. if (0 >= dig__fread_port_D(&(ptr->T), 1, fp))
  184. return -1;
  185. if (0 >= dig__fread_port_D(&(ptr->B), 1, fp))
  186. return -1;
  187. }
  188. else {
  189. ptr->T = 0.0;
  190. ptr->B = 0.0;
  191. }
  192. }
  193. else {
  194. Node = Plus->Node[ptr->N1];
  195. ptr->N = Node->y;
  196. ptr->S = Node->y;
  197. ptr->E = Node->x;
  198. ptr->W = Node->x;
  199. ptr->T = Node->z;
  200. ptr->B = Node->z;
  201. }
  202. Plus->Line[n] = ptr;
  203. return (0);
  204. }
  205. int dig_Wr_P_line(struct Plus_head *Plus, int n, struct gvfile * fp)
  206. {
  207. int n_edges = 0, vol = 0;
  208. char ch;
  209. struct P_line *ptr;
  210. G_debug(4, "dig_Wr_P_line() line = %d", n);
  211. ptr = Plus->Line[n];
  212. /* if NULL i.e. dead write just 0 instead of type */
  213. if (ptr == NULL) {
  214. G_debug(3, " line is dead -> write 0 only");
  215. ch = 0;
  216. if (0 >= dig__fwrite_port_C(&ch, 1, fp))
  217. return (-1);
  218. return 0;
  219. }
  220. /* type */
  221. ch = (char)dig_type_to_store(ptr->type);
  222. G_debug(5, " line type %d -> %d", ptr->type, ch);
  223. if (0 >= dig__fwrite_port_C(&ch, 1, fp))
  224. return (-1);
  225. /* offset */
  226. if (0 >= dig__fwrite_port_O(&(ptr->offset), 1, fp, Plus->off_t_size))
  227. return (-1);
  228. /* first node */
  229. if (ptr->type & (GV_POINTS | GV_LINES | GV_KERNEL))
  230. if (0 >= dig__fwrite_port_P(&(ptr->N1), 1, fp))
  231. return (-1);
  232. /* second node, for points/centroids not needed */
  233. if (ptr->type & (GV_LINE | GV_BOUNDARY))
  234. if (0 >= dig__fwrite_port_P(&(ptr->N2), 1, fp))
  235. return (-1);
  236. /* left area for boundary, area for centroid */
  237. if (ptr->type & (GV_BOUNDARY | GV_CENTROID))
  238. if (0 >= dig__fwrite_port_P(&(ptr->left), 1, fp))
  239. return (-1);
  240. /* right area */
  241. if (ptr->type & GV_BOUNDARY)
  242. if (0 >= dig__fwrite_port_P(&(ptr->right), 1, fp))
  243. return (-1);
  244. if ((ptr->type & GV_FACE) && Plus->with_z) { /* reserved for face */
  245. if (0 >= dig__fwrite_port_I(&n_edges, 1, fp))
  246. return (-1);
  247. /* here will be list of edges */
  248. /* left / right volume */
  249. if (0 >= dig__fwrite_port_P(&vol, 1, fp))
  250. return (-1);
  251. if (0 >= dig__fwrite_port_P(&vol, 1, fp))
  252. return (-1);
  253. }
  254. if ((ptr->type & GV_KERNEL) && Plus->with_z) /* reserved for kernel (volume number) */
  255. if (0 >= dig__fwrite_port_P(&vol, 1, fp))
  256. return (-1);
  257. /* bounding box */
  258. if (ptr->type & (GV_LINE | GV_BOUNDARY | GV_FACE)) {
  259. if (0 >= dig__fwrite_port_D(&(ptr->N), 1, fp))
  260. return (-1);
  261. if (0 >= dig__fwrite_port_D(&(ptr->S), 1, fp))
  262. return (-1);
  263. if (0 >= dig__fwrite_port_D(&(ptr->E), 1, fp))
  264. return (-1);
  265. if (0 >= dig__fwrite_port_D(&(ptr->W), 1, fp))
  266. return (-1);
  267. if (Plus->with_z) {
  268. if (0 >= dig__fwrite_port_D(&(ptr->T), 1, fp))
  269. return (-1);
  270. if (0 >= dig__fwrite_port_D(&(ptr->B), 1, fp))
  271. return (-1);
  272. }
  273. }
  274. return (0);
  275. }
  276. int dig_Rd_P_area(struct Plus_head *Plus, int n, struct gvfile * fp)
  277. {
  278. int cnt;
  279. struct P_area *ptr;
  280. #ifdef GDEBUG
  281. G_debug(3, "dig_Rd_P_area(): n = %d", n);
  282. #endif
  283. if (0 >= dig__fread_port_P(&cnt, 1, fp))
  284. return (-1);
  285. if (cnt == 0) { /* dead */
  286. Plus->Area[n] = NULL;
  287. return 0;
  288. }
  289. ptr = dig_alloc_area();
  290. /* lines */
  291. ptr->n_lines = cnt;
  292. if (dig_area_alloc_line(ptr, ptr->n_lines) == -1)
  293. return -1;
  294. if (ptr->n_lines)
  295. if (0 >= dig__fread_port_P(ptr->lines, ptr->n_lines, fp))
  296. return -1;
  297. /* isles */
  298. if (0 >= dig__fread_port_P(&(ptr->n_isles), 1, fp))
  299. return -1;
  300. if (dig_area_alloc_isle(ptr, ptr->n_isles) == -1)
  301. return -1;
  302. if (ptr->n_isles)
  303. if (0 >= dig__fread_port_P(ptr->isles, ptr->n_isles, fp))
  304. return -1;
  305. /* centroid */
  306. if (0 >= dig__fread_port_P(&(ptr->centroid), 1, fp))
  307. return -1;
  308. /* bounding box */
  309. if (0 >= dig__fread_port_D(&(ptr->N), 1, fp))
  310. return -1;
  311. if (0 >= dig__fread_port_D(&(ptr->S), 1, fp))
  312. return -1;
  313. if (0 >= dig__fread_port_D(&(ptr->E), 1, fp))
  314. return -1;
  315. if (0 >= dig__fread_port_D(&(ptr->W), 1, fp))
  316. return -1;
  317. if (Plus->with_z) {
  318. if (0 >= dig__fread_port_D(&(ptr->T), 1, fp))
  319. return -1;
  320. if (0 >= dig__fread_port_D(&(ptr->B), 1, fp))
  321. return -1;
  322. }
  323. else {
  324. ptr->T = 0.0;
  325. ptr->B = 0.0;
  326. }
  327. Plus->Area[n] = ptr;
  328. return (0);
  329. }
  330. int dig_Wr_P_area(struct Plus_head *Plus, int n, struct gvfile * fp)
  331. {
  332. int i;
  333. struct P_area *ptr;
  334. ptr = Plus->Area[n];
  335. /* If NULL i.e. dead write just 0 instead of number of lines */
  336. if (ptr == NULL) {
  337. i = 0;
  338. if (0 >= dig__fwrite_port_P(&i, 1, fp))
  339. return (-1);
  340. return 0;
  341. }
  342. /* lines */
  343. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  344. return (-1);
  345. if (ptr->n_lines)
  346. if (0 >= dig__fwrite_port_P(ptr->lines, ptr->n_lines, fp))
  347. return -1;
  348. /* isles */
  349. if (0 >= dig__fwrite_port_P(&(ptr->n_isles), 1, fp))
  350. return (-1);
  351. if (ptr->n_isles)
  352. if (0 >= dig__fwrite_port_P(ptr->isles, ptr->n_isles, fp))
  353. return -1;
  354. /* centroid */
  355. if (0 >= dig__fwrite_port_P(&(ptr->centroid), 1, fp))
  356. return (-1);
  357. /* bounding box */
  358. if (0 >= dig__fwrite_port_D(&(ptr->N), 1, fp))
  359. return (-1);
  360. if (0 >= dig__fwrite_port_D(&(ptr->S), 1, fp))
  361. return (-1);
  362. if (0 >= dig__fwrite_port_D(&(ptr->E), 1, fp))
  363. return (-1);
  364. if (0 >= dig__fwrite_port_D(&(ptr->W), 1, fp))
  365. return (-1);
  366. if (Plus->with_z) {
  367. if (0 >= dig__fwrite_port_D(&(ptr->T), 1, fp))
  368. return (-1);
  369. if (0 >= dig__fwrite_port_D(&(ptr->B), 1, fp))
  370. return (-1);
  371. }
  372. return (0);
  373. }
  374. int dig_Rd_P_isle(struct Plus_head *Plus, int n, struct gvfile * fp)
  375. {
  376. int cnt;
  377. struct P_isle *ptr;
  378. #ifdef GDEBUG
  379. G_debug(3, "dig_Rd_P_isle()");
  380. #endif
  381. if (0 >= dig__fread_port_P(&cnt, 1, fp))
  382. return (-1);
  383. if (cnt == 0) { /* dead */
  384. Plus->Isle[n] = NULL;
  385. return 0;
  386. }
  387. ptr = dig_alloc_isle();
  388. /* lines */
  389. ptr->n_lines = cnt;
  390. if (dig_isle_alloc_line(ptr, ptr->n_lines) == -1)
  391. return -1;
  392. if (ptr->n_lines)
  393. if (0 >= dig__fread_port_P(ptr->lines, ptr->n_lines, fp))
  394. return -1;
  395. /* area */
  396. if (0 >= dig__fread_port_P(&(ptr->area), 1, fp))
  397. return -1;
  398. /* bounding box */
  399. if (0 >= dig__fread_port_D(&(ptr->N), 1, fp))
  400. return -1;
  401. if (0 >= dig__fread_port_D(&(ptr->S), 1, fp))
  402. return -1;
  403. if (0 >= dig__fread_port_D(&(ptr->E), 1, fp))
  404. return -1;
  405. if (0 >= dig__fread_port_D(&(ptr->W), 1, fp))
  406. return -1;
  407. if (Plus->with_z) {
  408. if (0 >= dig__fread_port_D(&(ptr->T), 1, fp))
  409. return -1;
  410. if (0 >= dig__fread_port_D(&(ptr->B), 1, fp))
  411. return -1;
  412. }
  413. else {
  414. ptr->T = 0.0;
  415. ptr->B = 0.0;
  416. }
  417. Plus->Isle[n] = ptr;
  418. return (0);
  419. }
  420. int dig_Wr_P_isle(struct Plus_head *Plus, int n, struct gvfile * fp)
  421. {
  422. int i;
  423. struct P_isle *ptr;
  424. ptr = Plus->Isle[n];
  425. /* If NULL i.e. dead write just 0 instead of number of lines */
  426. if (ptr == NULL) {
  427. i = 0;
  428. if (0 >= dig__fwrite_port_P(&i, 1, fp))
  429. return (-1);
  430. return 0;
  431. }
  432. /* lines */
  433. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  434. return (-1);
  435. if (ptr->n_lines)
  436. if (0 >= dig__fwrite_port_P(ptr->lines, ptr->n_lines, fp))
  437. return -1;
  438. /* area */
  439. if (0 >= dig__fwrite_port_P(&(ptr->area), 1, fp))
  440. return (-1);
  441. /* bounding box */
  442. if (0 >= dig__fwrite_port_D(&(ptr->N), 1, fp))
  443. return (-1);
  444. if (0 >= dig__fwrite_port_D(&(ptr->S), 1, fp))
  445. return (-1);
  446. if (0 >= dig__fwrite_port_D(&(ptr->E), 1, fp))
  447. return (-1);
  448. if (0 >= dig__fwrite_port_D(&(ptr->W), 1, fp))
  449. return (-1);
  450. if (Plus->with_z) {
  451. if (0 >= dig__fwrite_port_D(&(ptr->T), 1, fp))
  452. return (-1);
  453. if (0 >= dig__fwrite_port_D(&(ptr->B), 1, fp))
  454. return (-1);
  455. }
  456. return (0);
  457. }
  458. /*!
  459. \brief Read Plus_head from file
  460. \param fp pointer to gvfile structure
  461. \param[in,out] ptr pointer to Plus_head structure
  462. \return -1 error
  463. \return 0 OK
  464. */
  465. int dig_Rd_Plus_head(struct gvfile * fp, struct Plus_head *ptr)
  466. {
  467. unsigned char buf[5];
  468. int byte_order;
  469. dig_rewind(fp);
  470. /* bytes 1 - 5 */
  471. if (0 >= dig__fread_port_C(buf, 5, fp))
  472. return (-1);
  473. ptr->Version_Major = buf[0];
  474. ptr->Version_Minor = buf[1];
  475. ptr->Back_Major = buf[2];
  476. ptr->Back_Minor = buf[3];
  477. byte_order = buf[4];
  478. G_debug(2,
  479. "Topo header: file version %d.%d , supported from GRASS version %d.%d",
  480. ptr->Version_Major, ptr->Version_Minor, ptr->Back_Major,
  481. ptr->Back_Minor);
  482. G_debug(2, " byte order %d", byte_order);
  483. /* check version numbers */
  484. if (ptr->Version_Major > GV_TOPO_VER_MAJOR ||
  485. ptr->Version_Minor > GV_TOPO_VER_MINOR) {
  486. /* The file was created by GRASS library with higher version than this one */
  487. if (ptr->Back_Major > GV_TOPO_VER_MAJOR ||
  488. ptr->Back_Minor > GV_TOPO_VER_MINOR) {
  489. /* This version of GRASS lib is lower than the oldest which can read this format */
  490. G_fatal_error
  491. ("Topology format version %d.%d is not supported by this release."
  492. " Try to rebuild topology or upgrade GRASS.",
  493. ptr->Version_Major, ptr->Version_Minor);
  494. return (-1);
  495. }
  496. G_warning
  497. ("Your GRASS version does not fully support topology format %d.%d of the vector."
  498. " Consider to rebuild topology or upgrade GRASS.",
  499. ptr->Version_Major, ptr->Version_Minor);
  500. }
  501. /* init Port_info structure and set as default */
  502. dig_init_portable(&(ptr->port), byte_order);
  503. dig_set_cur_port(&(ptr->port));
  504. /* bytes 6 - 9 : header size */
  505. if (0 >= dig__fread_port_L(&(ptr->head_size), 1, fp))
  506. return (-1);
  507. G_debug(2, " header size %ld", ptr->head_size);
  508. /* determine required offset size from header size */
  509. /* this is not safe in case new fields get added in later versions */
  510. /* better: add a new field with off_t_size after byte_order? */
  511. if (ptr->head_size >= 142 + 32) /* keep in sync with dig_Wr_Plus_head() */
  512. ptr->off_t_size = 8;
  513. else
  514. ptr->off_t_size = 4;
  515. if (sizeof(off_t) < ptr->off_t_size) {
  516. G_warning("Vector exceeds supported file size limit");
  517. return (-1);
  518. }
  519. G_debug(1, "topo off_t size = %d", ptr->off_t_size);
  520. /* byte 10 : dimension 2D or 3D */
  521. if (0 >= dig__fread_port_C(buf, 1, fp))
  522. return (-1);
  523. ptr->with_z = buf[0];
  524. G_debug(2, " with_z %d", ptr->with_z);
  525. /* bytes 11 - 58 : bound box */
  526. if (0 >= dig__fread_port_D(&(ptr->box.N), 1, fp))
  527. return (-1);
  528. if (0 >= dig__fread_port_D(&(ptr->box.S), 1, fp))
  529. return (-1);
  530. if (0 >= dig__fread_port_D(&(ptr->box.E), 1, fp))
  531. return (-1);
  532. if (0 >= dig__fread_port_D(&(ptr->box.W), 1, fp))
  533. return (-1);
  534. if (0 >= dig__fread_port_D(&(ptr->box.T), 1, fp))
  535. return (-1);
  536. if (0 >= dig__fread_port_D(&(ptr->box.B), 1, fp))
  537. return (-1);
  538. /* bytes 59 - 86 : number of structures */
  539. if (0 >= dig__fread_port_P(&(ptr->n_nodes), 1, fp))
  540. return (-1);
  541. if (0 >= dig__fread_port_P(&(ptr->n_edges), 1, fp))
  542. return (-1);
  543. if (0 >= dig__fread_port_P(&(ptr->n_lines), 1, fp))
  544. return (-1);
  545. if (0 >= dig__fread_port_P(&(ptr->n_areas), 1, fp))
  546. return (-1);
  547. if (0 >= dig__fread_port_P(&(ptr->n_isles), 1, fp))
  548. return (-1);
  549. if (0 >= dig__fread_port_P(&(ptr->n_volumes), 1, fp))
  550. return (-1);
  551. if (0 >= dig__fread_port_P(&(ptr->n_holes), 1, fp))
  552. return (-1);
  553. /* bytes 87 - 110 : number of line types */
  554. if (0 >= dig__fread_port_P(&(ptr->n_plines), 1, fp))
  555. return (-1);
  556. if (0 >= dig__fread_port_P(&(ptr->n_llines), 1, fp))
  557. return (-1);
  558. if (0 >= dig__fread_port_P(&(ptr->n_blines), 1, fp))
  559. return (-1);
  560. if (0 >= dig__fread_port_P(&(ptr->n_clines), 1, fp))
  561. return (-1);
  562. if (0 >= dig__fread_port_P(&(ptr->n_flines), 1, fp))
  563. return (-1);
  564. if (0 >= dig__fread_port_P(&(ptr->n_klines), 1, fp))
  565. return (-1);
  566. /* bytes 111 - 138 : Offset */
  567. if (0 >= dig__fread_port_O(&(ptr->Node_offset), 1, fp, ptr->off_t_size))
  568. return (-1);
  569. if (0 >= dig__fread_port_O(&(ptr->Edge_offset), 1, fp, ptr->off_t_size))
  570. return (-1);
  571. if (0 >= dig__fread_port_O(&(ptr->Line_offset), 1, fp, ptr->off_t_size))
  572. return (-1);
  573. if (0 >= dig__fread_port_O(&(ptr->Area_offset), 1, fp, ptr->off_t_size))
  574. return (-1);
  575. if (0 >= dig__fread_port_O(&(ptr->Isle_offset), 1, fp, ptr->off_t_size))
  576. return (-1);
  577. if (0 >= dig__fread_port_O(&(ptr->Volume_offset), 1, fp, ptr->off_t_size))
  578. return (-1);
  579. if (0 >= dig__fread_port_O(&(ptr->Hole_offset), 1, fp, ptr->off_t_size))
  580. return (-1);
  581. /* bytes 139 - 142 : Coor size and time */
  582. if (0 >= dig__fread_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  583. return (-1);
  584. G_debug(2, " coor size %"PRI_OFF_T, ptr->coor_size);
  585. dig_fseek(fp, ptr->head_size, SEEK_SET);
  586. return (0);
  587. }
  588. int dig_Wr_Plus_head(struct gvfile * fp, struct Plus_head *ptr)
  589. {
  590. unsigned char buf[10];
  591. long length = 142;
  592. dig_rewind(fp);
  593. dig_set_cur_port(&(ptr->port));
  594. /* bytes 1 - 5 */
  595. buf[0] = GV_TOPO_VER_MAJOR;
  596. buf[1] = GV_TOPO_VER_MINOR;
  597. buf[2] = GV_TOPO_EARLIEST_MAJOR;
  598. buf[3] = GV_TOPO_EARLIEST_MINOR;
  599. buf[4] = ptr->port.byte_order;
  600. if (0 >= dig__fwrite_port_C(buf, 5, fp))
  601. return (-1);
  602. /* determine required offset size from coor file size */
  603. if (ptr->coor_size > (off_t)PORT_LONG_MAX) {
  604. /* can only happen when sizeof(off_t) == 8 */
  605. ptr->off_t_size = 8;
  606. }
  607. else
  608. ptr->off_t_size = 4;
  609. /* add a new field with off_t_size after byte_order? */
  610. /* adjust header size for large files */
  611. if (ptr->off_t_size == 8) {
  612. /* 7 offset values and coor file size: add 8 * 4 */
  613. length += 32;
  614. }
  615. /* bytes 6 - 9 : header size */
  616. if (0 >= dig__fwrite_port_L(&length, 1, fp))
  617. return (0);
  618. /* byte 10 : dimension 2D or 3D */
  619. buf[0] = ptr->with_z;
  620. if (0 >= dig__fwrite_port_C(buf, 1, fp))
  621. return (0);
  622. /* bytes 11 - 58 : bound box */
  623. if (0 >= dig__fwrite_port_D(&(ptr->box.N), 1, fp))
  624. return (-1);
  625. if (0 >= dig__fwrite_port_D(&(ptr->box.S), 1, fp))
  626. return (-1);
  627. if (0 >= dig__fwrite_port_D(&(ptr->box.E), 1, fp))
  628. return (-1);
  629. if (0 >= dig__fwrite_port_D(&(ptr->box.W), 1, fp))
  630. return (-1);
  631. if (0 >= dig__fwrite_port_D(&(ptr->box.T), 1, fp))
  632. return (-1);
  633. if (0 >= dig__fwrite_port_D(&(ptr->box.B), 1, fp))
  634. return (-1);
  635. /* bytes 59 - 86 : number of structures */
  636. if (0 >= dig__fwrite_port_P(&(ptr->n_nodes), 1, fp))
  637. return (-1);
  638. if (0 >= dig__fwrite_port_P(&(ptr->n_edges), 1, fp))
  639. return (-1);
  640. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  641. return (-1);
  642. if (0 >= dig__fwrite_port_P(&(ptr->n_areas), 1, fp))
  643. return (-1);
  644. if (0 >= dig__fwrite_port_P(&(ptr->n_isles), 1, fp))
  645. return (-1);
  646. if (0 >= dig__fwrite_port_P(&(ptr->n_volumes), 1, fp))
  647. return (-1);
  648. if (0 >= dig__fwrite_port_P(&(ptr->n_holes), 1, fp))
  649. return (-1);
  650. /* bytes 87 - 110 : number of line types */
  651. if (0 >= dig__fwrite_port_P(&(ptr->n_plines), 1, fp))
  652. return (-1);
  653. if (0 >= dig__fwrite_port_P(&(ptr->n_llines), 1, fp))
  654. return (-1);
  655. if (0 >= dig__fwrite_port_P(&(ptr->n_blines), 1, fp))
  656. return (-1);
  657. if (0 >= dig__fwrite_port_P(&(ptr->n_clines), 1, fp))
  658. return (-1);
  659. if (0 >= dig__fwrite_port_P(&(ptr->n_flines), 1, fp))
  660. return (-1);
  661. if (0 >= dig__fwrite_port_P(&(ptr->n_klines), 1, fp))
  662. return (-1);
  663. /* bytes 111 - 138 : Offset */
  664. if (0 >= dig__fwrite_port_O(&(ptr->Node_offset), 1, fp, ptr->off_t_size))
  665. return (-1);
  666. if (0 >= dig__fwrite_port_O(&(ptr->Edge_offset), 1, fp, ptr->off_t_size))
  667. return (-1);
  668. if (0 >= dig__fwrite_port_O(&(ptr->Line_offset), 1, fp, ptr->off_t_size))
  669. return (-1);
  670. if (0 >= dig__fwrite_port_O(&(ptr->Area_offset), 1, fp, ptr->off_t_size))
  671. return (-1);
  672. if (0 >= dig__fwrite_port_O(&(ptr->Isle_offset), 1, fp, ptr->off_t_size))
  673. return (-1);
  674. if (0 >= dig__fwrite_port_O(&(ptr->Volume_offset), 1, fp, ptr->off_t_size))
  675. return (-1);
  676. if (0 >= dig__fwrite_port_O(&(ptr->Hole_offset), 1, fp, ptr->off_t_size))
  677. return (-1);
  678. /* bytes 139 - 142 : Coor size and time */
  679. if (0 >= dig__fwrite_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  680. return (-1);
  681. G_debug(2, "topo body offset %"PRI_OFF_T, dig_ftell(fp));
  682. return (0);
  683. }