g3dclose.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #ifdef __MINGW32__
  2. # include <windows.h>
  3. #endif
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <unistd.h>
  8. #include <grass/raster.h>
  9. #include "G3d_intern.h"
  10. /*---------------------------------------------------------------------------*/
  11. static int G3d_closeNew(G3D_Map * map)
  12. {
  13. char path[GPATH_MAX];
  14. struct Categories cats;
  15. struct History hist;
  16. G3d_removeColor(map->fileName);
  17. /* create empty cats file */
  18. Rast_init_cats(NULL, &cats);
  19. G3d_writeCats(map->fileName, &cats);
  20. Rast_free_cats(&cats);
  21. /*genrate the history file, use the normal G_ functions */
  22. Rast_short_history(map->fileName, "raster3d", &hist);
  23. Rast_command_history(&hist);
  24. /*Use the G3d function to write the history file,
  25. * otherwise the path is wrong */
  26. if (!G3d_writeHistory(map->fileName, &hist)) {
  27. G3d_error("G3d_closeNew: can't write raster3d history");
  28. }
  29. G3d_range_write(map);
  30. close(map->data_fd);
  31. /* finally move tempfile to data file */
  32. G3d_filename(path, G3D_CELL_ELEMENT, map->fileName, map->mapset);
  33. #ifdef __MINGW32__
  34. if (CopyFile(map->tempName, path, FALSE) == 0) {
  35. #else
  36. if (link(map->tempName, path) < 0) {
  37. #endif
  38. if (rename(map->tempName, path)) {
  39. G3d_error
  40. ("G3d_closeNew: can't move temp raster map %s\nto 3d data file %s",
  41. map->tempName, path);
  42. return 0;
  43. }
  44. }
  45. else
  46. remove(map->tempName);
  47. return 1;
  48. }
  49. /*---------------------------------------------------------------------------*/
  50. static int G3d_closeCellNew(G3D_Map * map)
  51. {
  52. long ltmp;
  53. if (map->useCache)
  54. if (!G3d_flushAllTiles(map)) {
  55. G3d_error("G3d_closeCellNew: error in G3d_flushAllTiles");
  56. return 0;
  57. }
  58. if (!G3d_flushIndex(map)) {
  59. G3d_error("G3d_closeCellNew: error in G3d_flushIndex");
  60. return 0;
  61. }
  62. /* write the header info which was filled with dummy values at the */
  63. /* opening time */
  64. if (lseek(map->data_fd,
  65. (long)(map->offset - sizeof(int) - sizeof(long)),
  66. SEEK_SET) == -1) {
  67. G3d_error("G3d_closeCellNew: can't position file");
  68. return 0;
  69. }
  70. if (!G3d_writeInts(map->data_fd, map->useXdr, &(map->indexNbytesUsed), 1)) {
  71. G3d_error("G3d_closeCellNew: can't write header");
  72. return 0;
  73. }
  74. G3d_longEncode(&(map->indexOffset), (unsigned char *)&ltmp, 1);
  75. if (write(map->data_fd, &ltmp, sizeof(long)) != sizeof(long)) {
  76. G3d_error("G3d_closeCellNew: can't write header");
  77. return 0;
  78. }
  79. if (!G3d_closeNew(map) != 0) {
  80. G3d_error("G3d_closeCellNew: error in G3d_closeNew");
  81. return 0;
  82. }
  83. return 1;
  84. }
  85. /*---------------------------------------------------------------------------*/
  86. static int G3d_closeOld(G3D_Map * map)
  87. {
  88. if (close(map->data_fd) != 0) {
  89. G3d_error("G3d_closeOld: could not close file");
  90. return 0;
  91. }
  92. return 1;
  93. }
  94. /*---------------------------------------------------------------------------*/
  95. static int G3d_closeCellOld(G3D_Map * map)
  96. {
  97. if (!G3d_closeOld(map) != 0) {
  98. G3d_error("G3d_closeCellOld: error in G3d_closeOld");
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. /*---------------------------------------------------------------------------*/
  104. /*!
  105. * \brief
  106. *
  107. * Closes g3d-file. If <em>map</em> is new
  108. * and cache-mode is used for <em>map</em> then every tile which is not flushed
  109. * before closing is flushed.
  110. *
  111. * \param map
  112. * \return 1 ... if successful,
  113. * 0 ... otherwise.
  114. */
  115. int G3d_closeCell(G3D_Map * map)
  116. {
  117. if (map->operation == G3D_WRITE_DATA) {
  118. if (!G3d_closeCellNew(map)) {
  119. G3d_error("G3d_closeCell: error in G3d_closeCellNew");
  120. return 0;
  121. }
  122. }
  123. else {
  124. if (!G3d_closeCellOld(map) != 0) {
  125. G3d_error("G3d_closeCell: error in G3d_closeCellOld");
  126. return 0;
  127. }
  128. }
  129. G3d_free(map->index);
  130. G3d_free(map->tileLength);
  131. if (map->useCache) {
  132. if (!G3d_disposeCache(map)) {
  133. G3d_error("G3d_closeCell: error in G3d_disposeCache");
  134. return 0;
  135. }
  136. }
  137. else
  138. G3d_free(map->data);
  139. if (map->operation == G3D_WRITE_DATA)
  140. if (!G3d_writeHeader(map,
  141. map->region.proj, map->region.zone,
  142. map->region.north, map->region.south,
  143. map->region.east, map->region.west,
  144. map->region.top, map->region.bottom,
  145. map->region.rows, map->region.cols,
  146. map->region.depths,
  147. map->region.ew_res, map->region.ns_res,
  148. map->region.tb_res,
  149. map->tileX, map->tileY, map->tileZ,
  150. map->type,
  151. map->compression, map->useRle, map->useLzw,
  152. map->precision, map->offset, map->useXdr,
  153. map->hasIndex, map->unit)) {
  154. G3d_error("G3d_closeCell: error in G3d_writeHeader");
  155. return 0;
  156. }
  157. G3d_free(map->unit);
  158. G3d_free(map);
  159. return 1;
  160. }