plus_struct.c 21 KB

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