plus_struct.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. /* bounding box */
  311. if (0 >= dig__fread_port_D(&(ptr->N), 1, fp))
  312. return -1;
  313. if (0 >= dig__fread_port_D(&(ptr->S), 1, fp))
  314. return -1;
  315. if (0 >= dig__fread_port_D(&(ptr->E), 1, fp))
  316. return -1;
  317. if (0 >= dig__fread_port_D(&(ptr->W), 1, fp))
  318. return -1;
  319. if (Plus->with_z) {
  320. if (0 >= dig__fread_port_D(&(ptr->T), 1, fp))
  321. return -1;
  322. if (0 >= dig__fread_port_D(&(ptr->B), 1, fp))
  323. return -1;
  324. }
  325. else {
  326. ptr->T = 0.0;
  327. ptr->B = 0.0;
  328. }
  329. Plus->Area[n] = ptr;
  330. return (0);
  331. }
  332. int dig_Wr_P_area(struct Plus_head *Plus, int n, struct gvfile * fp)
  333. {
  334. int i;
  335. struct P_area *ptr;
  336. ptr = Plus->Area[n];
  337. /* If NULL i.e. dead write just 0 instead of number of lines */
  338. if (ptr == NULL) {
  339. i = 0;
  340. if (0 >= dig__fwrite_port_P(&i, 1, fp))
  341. return (-1);
  342. return 0;
  343. }
  344. /* lines */
  345. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  346. return (-1);
  347. if (ptr->n_lines)
  348. if (0 >= dig__fwrite_port_P(ptr->lines, ptr->n_lines, fp))
  349. return -1;
  350. /* isles */
  351. if (0 >= dig__fwrite_port_P(&(ptr->n_isles), 1, fp))
  352. return (-1);
  353. if (ptr->n_isles)
  354. if (0 >= dig__fwrite_port_P(ptr->isles, ptr->n_isles, fp))
  355. return -1;
  356. /* centroid */
  357. if (0 >= dig__fwrite_port_P(&(ptr->centroid), 1, fp))
  358. return (-1);
  359. /* bounding box */
  360. if (0 >= dig__fwrite_port_D(&(ptr->N), 1, fp))
  361. return (-1);
  362. if (0 >= dig__fwrite_port_D(&(ptr->S), 1, fp))
  363. return (-1);
  364. if (0 >= dig__fwrite_port_D(&(ptr->E), 1, fp))
  365. return (-1);
  366. if (0 >= dig__fwrite_port_D(&(ptr->W), 1, fp))
  367. return (-1);
  368. if (Plus->with_z) {
  369. if (0 >= dig__fwrite_port_D(&(ptr->T), 1, fp))
  370. return (-1);
  371. if (0 >= dig__fwrite_port_D(&(ptr->B), 1, fp))
  372. return (-1);
  373. }
  374. return (0);
  375. }
  376. int dig_Rd_P_isle(struct Plus_head *Plus, int n, struct gvfile * fp)
  377. {
  378. int cnt;
  379. struct P_isle *ptr;
  380. #ifdef GDEBUG
  381. G_debug(3, "dig_Rd_P_isle()");
  382. #endif
  383. if (0 >= dig__fread_port_P(&cnt, 1, fp))
  384. return (-1);
  385. if (cnt == 0) { /* dead */
  386. Plus->Isle[n] = NULL;
  387. return 0;
  388. }
  389. ptr = dig_alloc_isle();
  390. /* lines */
  391. ptr->n_lines = cnt;
  392. if (dig_isle_alloc_line(ptr, ptr->n_lines) == -1)
  393. return -1;
  394. if (ptr->n_lines)
  395. if (0 >= dig__fread_port_P(ptr->lines, ptr->n_lines, fp))
  396. return -1;
  397. /* area */
  398. if (0 >= dig__fread_port_P(&(ptr->area), 1, fp))
  399. return -1;
  400. /* bounding box */
  401. if (0 >= dig__fread_port_D(&(ptr->N), 1, fp))
  402. return -1;
  403. if (0 >= dig__fread_port_D(&(ptr->S), 1, fp))
  404. return -1;
  405. if (0 >= dig__fread_port_D(&(ptr->E), 1, fp))
  406. return -1;
  407. if (0 >= dig__fread_port_D(&(ptr->W), 1, fp))
  408. return -1;
  409. if (Plus->with_z) {
  410. if (0 >= dig__fread_port_D(&(ptr->T), 1, fp))
  411. return -1;
  412. if (0 >= dig__fread_port_D(&(ptr->B), 1, fp))
  413. return -1;
  414. }
  415. else {
  416. ptr->T = 0.0;
  417. ptr->B = 0.0;
  418. }
  419. Plus->Isle[n] = ptr;
  420. return (0);
  421. }
  422. int dig_Wr_P_isle(struct Plus_head *Plus, int n, struct gvfile * fp)
  423. {
  424. int i;
  425. struct P_isle *ptr;
  426. ptr = Plus->Isle[n];
  427. /* If NULL i.e. dead write just 0 instead of number of lines */
  428. if (ptr == NULL) {
  429. i = 0;
  430. if (0 >= dig__fwrite_port_P(&i, 1, fp))
  431. return (-1);
  432. return 0;
  433. }
  434. /* lines */
  435. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  436. return (-1);
  437. if (ptr->n_lines)
  438. if (0 >= dig__fwrite_port_P(ptr->lines, ptr->n_lines, fp))
  439. return -1;
  440. /* area */
  441. if (0 >= dig__fwrite_port_P(&(ptr->area), 1, fp))
  442. return (-1);
  443. /* bounding box */
  444. if (0 >= dig__fwrite_port_D(&(ptr->N), 1, fp))
  445. return (-1);
  446. if (0 >= dig__fwrite_port_D(&(ptr->S), 1, fp))
  447. return (-1);
  448. if (0 >= dig__fwrite_port_D(&(ptr->E), 1, fp))
  449. return (-1);
  450. if (0 >= dig__fwrite_port_D(&(ptr->W), 1, fp))
  451. return (-1);
  452. if (Plus->with_z) {
  453. if (0 >= dig__fwrite_port_D(&(ptr->T), 1, fp))
  454. return (-1);
  455. if (0 >= dig__fwrite_port_D(&(ptr->B), 1, fp))
  456. return (-1);
  457. }
  458. return (0);
  459. }
  460. /*!
  461. \brief Read Plus_head from file
  462. \param fp pointer to gvfile structure
  463. \param[in,out] ptr pointer to Plus_head structure
  464. \return -1 error
  465. \return 0 OK
  466. */
  467. int dig_Rd_Plus_head(struct gvfile * fp, struct Plus_head *ptr)
  468. {
  469. unsigned char buf[5];
  470. int byte_order;
  471. dig_rewind(fp);
  472. /* bytes 1 - 5 */
  473. if (0 >= dig__fread_port_C(buf, 5, fp))
  474. return (-1);
  475. ptr->Version_Major = buf[0];
  476. ptr->Version_Minor = buf[1];
  477. ptr->Back_Major = buf[2];
  478. ptr->Back_Minor = buf[3];
  479. byte_order = buf[4];
  480. G_debug(2,
  481. "Topo header: file version %d.%d , supported from GRASS version %d.%d",
  482. ptr->Version_Major, ptr->Version_Minor, ptr->Back_Major,
  483. ptr->Back_Minor);
  484. G_debug(2, " byte order %d", byte_order);
  485. /* check version numbers */
  486. if (ptr->Version_Major > GV_TOPO_VER_MAJOR ||
  487. ptr->Version_Minor > GV_TOPO_VER_MINOR) {
  488. /* The file was created by GRASS library with higher version than this one */
  489. if (ptr->Back_Major > GV_TOPO_VER_MAJOR ||
  490. ptr->Back_Minor > GV_TOPO_VER_MINOR) {
  491. /* This version of GRASS lib is lower than the oldest which can read this format */
  492. G_fatal_error
  493. ("Topology format version %d.%d is not supported by this release."
  494. " Try to rebuild topology or upgrade GRASS.",
  495. ptr->Version_Major, ptr->Version_Minor);
  496. return (-1);
  497. }
  498. G_warning
  499. ("Your GRASS version does not fully support topology format %d.%d of the vector."
  500. " Consider to rebuild topology or upgrade GRASS.",
  501. ptr->Version_Major, ptr->Version_Minor);
  502. }
  503. /* init Port_info structure and set as default */
  504. dig_init_portable(&(ptr->port), byte_order);
  505. dig_set_cur_port(&(ptr->port));
  506. /* bytes 6 - 9 : header size */
  507. if (0 >= dig__fread_port_L(&(ptr->head_size), 1, fp))
  508. return (-1);
  509. G_debug(2, " header size %ld", ptr->head_size);
  510. /* determine required offset size from header size */
  511. /* this is not safe in case new fields get added in later versions */
  512. /* better: add a new field with off_t_size after byte_order? */
  513. if (ptr->head_size >= 142 + 32) /* keep in sync with dig_Wr_Plus_head() */
  514. ptr->off_t_size = 8;
  515. else
  516. ptr->off_t_size = 4;
  517. if (sizeof(off_t) < ptr->off_t_size) {
  518. G_warning("Vector exceeds supported file size limit");
  519. return (-1);
  520. }
  521. G_debug(1, "topo off_t size = %d", ptr->off_t_size);
  522. /* byte 10 : dimension 2D or 3D */
  523. if (0 >= dig__fread_port_C(buf, 1, fp))
  524. return (-1);
  525. ptr->with_z = buf[0];
  526. G_debug(2, " with_z %d", ptr->with_z);
  527. /* bytes 11 - 58 : bound box */
  528. if (0 >= dig__fread_port_D(&(ptr->box.N), 1, fp))
  529. return (-1);
  530. if (0 >= dig__fread_port_D(&(ptr->box.S), 1, fp))
  531. return (-1);
  532. if (0 >= dig__fread_port_D(&(ptr->box.E), 1, fp))
  533. return (-1);
  534. if (0 >= dig__fread_port_D(&(ptr->box.W), 1, fp))
  535. return (-1);
  536. if (0 >= dig__fread_port_D(&(ptr->box.T), 1, fp))
  537. return (-1);
  538. if (0 >= dig__fread_port_D(&(ptr->box.B), 1, fp))
  539. return (-1);
  540. /* bytes 59 - 86 : number of structures */
  541. if (0 >= dig__fread_port_P(&(ptr->n_nodes), 1, fp))
  542. return (-1);
  543. if (0 >= dig__fread_port_P(&(ptr->n_edges), 1, fp))
  544. return (-1);
  545. if (0 >= dig__fread_port_P(&(ptr->n_lines), 1, fp))
  546. return (-1);
  547. if (0 >= dig__fread_port_P(&(ptr->n_areas), 1, fp))
  548. return (-1);
  549. if (0 >= dig__fread_port_P(&(ptr->n_isles), 1, fp))
  550. return (-1);
  551. if (0 >= dig__fread_port_P(&(ptr->n_volumes), 1, fp))
  552. return (-1);
  553. if (0 >= dig__fread_port_P(&(ptr->n_holes), 1, fp))
  554. return (-1);
  555. /* bytes 87 - 110 : number of line types */
  556. if (0 >= dig__fread_port_P(&(ptr->n_plines), 1, fp))
  557. return (-1);
  558. if (0 >= dig__fread_port_P(&(ptr->n_llines), 1, fp))
  559. return (-1);
  560. if (0 >= dig__fread_port_P(&(ptr->n_blines), 1, fp))
  561. return (-1);
  562. if (0 >= dig__fread_port_P(&(ptr->n_clines), 1, fp))
  563. return (-1);
  564. if (0 >= dig__fread_port_P(&(ptr->n_flines), 1, fp))
  565. return (-1);
  566. if (0 >= dig__fread_port_P(&(ptr->n_klines), 1, fp))
  567. return (-1);
  568. /* bytes 111 - 138 : Offset */
  569. if (0 >= dig__fread_port_O(&(ptr->Node_offset), 1, fp, ptr->off_t_size))
  570. return (-1);
  571. if (0 >= dig__fread_port_O(&(ptr->Edge_offset), 1, fp, ptr->off_t_size))
  572. return (-1);
  573. if (0 >= dig__fread_port_O(&(ptr->Line_offset), 1, fp, ptr->off_t_size))
  574. return (-1);
  575. if (0 >= dig__fread_port_O(&(ptr->Area_offset), 1, fp, ptr->off_t_size))
  576. return (-1);
  577. if (0 >= dig__fread_port_O(&(ptr->Isle_offset), 1, fp, ptr->off_t_size))
  578. return (-1);
  579. if (0 >= dig__fread_port_O(&(ptr->Volume_offset), 1, fp, ptr->off_t_size))
  580. return (-1);
  581. if (0 >= dig__fread_port_O(&(ptr->Hole_offset), 1, fp, ptr->off_t_size))
  582. return (-1);
  583. /* bytes 139 - 142 : Coor size and time */
  584. if (0 >= dig__fread_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  585. return (-1);
  586. G_debug(2, " coor size %"PRI_OFF_T, ptr->coor_size);
  587. dig_fseek(fp, ptr->head_size, SEEK_SET);
  588. return (0);
  589. }
  590. int dig_Wr_Plus_head(struct gvfile * fp, struct Plus_head *ptr)
  591. {
  592. unsigned char buf[10];
  593. long length = 142;
  594. dig_rewind(fp);
  595. dig_set_cur_port(&(ptr->port));
  596. /* bytes 1 - 5 */
  597. buf[0] = GV_TOPO_VER_MAJOR;
  598. buf[1] = GV_TOPO_VER_MINOR;
  599. buf[2] = GV_TOPO_EARLIEST_MAJOR;
  600. buf[3] = GV_TOPO_EARLIEST_MINOR;
  601. buf[4] = ptr->port.byte_order;
  602. if (0 >= dig__fwrite_port_C(buf, 5, fp))
  603. return (-1);
  604. /* determine required offset size from coor file size */
  605. if (ptr->coor_size > (off_t)PORT_LONG_MAX) {
  606. /* can only happen when sizeof(off_t) == 8 */
  607. ptr->off_t_size = 8;
  608. }
  609. else
  610. ptr->off_t_size = 4;
  611. /* add a new field with off_t_size after byte_order? */
  612. /* adjust header size for large files */
  613. if (ptr->off_t_size == 8) {
  614. /* 7 offset values and coor file size: add 8 * 4 */
  615. length += 32;
  616. }
  617. /* bytes 6 - 9 : header size */
  618. if (0 >= dig__fwrite_port_L(&length, 1, fp))
  619. return (0);
  620. /* byte 10 : dimension 2D or 3D */
  621. buf[0] = ptr->with_z;
  622. if (0 >= dig__fwrite_port_C(buf, 1, fp))
  623. return (0);
  624. /* bytes 11 - 58 : bound box */
  625. if (0 >= dig__fwrite_port_D(&(ptr->box.N), 1, fp))
  626. return (-1);
  627. if (0 >= dig__fwrite_port_D(&(ptr->box.S), 1, fp))
  628. return (-1);
  629. if (0 >= dig__fwrite_port_D(&(ptr->box.E), 1, fp))
  630. return (-1);
  631. if (0 >= dig__fwrite_port_D(&(ptr->box.W), 1, fp))
  632. return (-1);
  633. if (0 >= dig__fwrite_port_D(&(ptr->box.T), 1, fp))
  634. return (-1);
  635. if (0 >= dig__fwrite_port_D(&(ptr->box.B), 1, fp))
  636. return (-1);
  637. /* bytes 59 - 86 : number of structures */
  638. if (0 >= dig__fwrite_port_P(&(ptr->n_nodes), 1, fp))
  639. return (-1);
  640. if (0 >= dig__fwrite_port_P(&(ptr->n_edges), 1, fp))
  641. return (-1);
  642. if (0 >= dig__fwrite_port_P(&(ptr->n_lines), 1, fp))
  643. return (-1);
  644. if (0 >= dig__fwrite_port_P(&(ptr->n_areas), 1, fp))
  645. return (-1);
  646. if (0 >= dig__fwrite_port_P(&(ptr->n_isles), 1, fp))
  647. return (-1);
  648. if (0 >= dig__fwrite_port_P(&(ptr->n_volumes), 1, fp))
  649. return (-1);
  650. if (0 >= dig__fwrite_port_P(&(ptr->n_holes), 1, fp))
  651. return (-1);
  652. /* bytes 87 - 110 : number of line types */
  653. if (0 >= dig__fwrite_port_P(&(ptr->n_plines), 1, fp))
  654. return (-1);
  655. if (0 >= dig__fwrite_port_P(&(ptr->n_llines), 1, fp))
  656. return (-1);
  657. if (0 >= dig__fwrite_port_P(&(ptr->n_blines), 1, fp))
  658. return (-1);
  659. if (0 >= dig__fwrite_port_P(&(ptr->n_clines), 1, fp))
  660. return (-1);
  661. if (0 >= dig__fwrite_port_P(&(ptr->n_flines), 1, fp))
  662. return (-1);
  663. if (0 >= dig__fwrite_port_P(&(ptr->n_klines), 1, fp))
  664. return (-1);
  665. /* bytes 111 - 138 : Offset */
  666. if (0 >= dig__fwrite_port_O(&(ptr->Node_offset), 1, fp, ptr->off_t_size))
  667. return (-1);
  668. if (0 >= dig__fwrite_port_O(&(ptr->Edge_offset), 1, fp, ptr->off_t_size))
  669. return (-1);
  670. if (0 >= dig__fwrite_port_O(&(ptr->Line_offset), 1, fp, ptr->off_t_size))
  671. return (-1);
  672. if (0 >= dig__fwrite_port_O(&(ptr->Area_offset), 1, fp, ptr->off_t_size))
  673. return (-1);
  674. if (0 >= dig__fwrite_port_O(&(ptr->Isle_offset), 1, fp, ptr->off_t_size))
  675. return (-1);
  676. if (0 >= dig__fwrite_port_O(&(ptr->Volume_offset), 1, fp, ptr->off_t_size))
  677. return (-1);
  678. if (0 >= dig__fwrite_port_O(&(ptr->Hole_offset), 1, fp, ptr->off_t_size))
  679. return (-1);
  680. /* bytes 139 - 142 : Coor size and time */
  681. if (0 >= dig__fwrite_port_O(&(ptr->coor_size), 1, fp, ptr->off_t_size))
  682. return (-1);
  683. G_debug(2, "topo body offset %"PRI_OFF_T, dig_ftell(fp));
  684. return (0);
  685. }