tileio.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include "raster3d_intern.h"
  6. /*---------------------------------------------------------------------------*/
  7. /*---------------------------------------------------------------------------*/
  8. /* EXPORTED FUNCTIONS */
  9. /*---------------------------------------------------------------------------*/
  10. /*---------------------------------------------------------------------------*/
  11. /*!
  12. * \brief
  13. *
  14. * This function
  15. * returns a pointer to a tile which contains the data for the tile with index
  16. * <em>tileIndex</em>. The type of the data stored in the tile depends on the type
  17. * specified at the initialization time of <em>map</em>. The functionality is
  18. * different depending on whether <em>map</em> is old or new and depending on the
  19. * cache-mode of <em>map</em>.<br>
  20. *
  21. * If <em>map</em> is old and the cache is not used the tile with <em>tileIndex</em>
  22. * is read from file and stored in the buffer provided by the map structure.
  23. * The pointer to this buffer is returned. If the buffer already contains the
  24. * tile with <em>tileIndex</em> reading is skipped. Data which was stored in
  25. * earlier calls to <tt>Rast3d_get_tile_ptr</tt> is destroyed.
  26. * If the tile with <em>tileIndex</em> is not stored on the file corresponding
  27. * to <em>map</em>, and <em>tileIndex</em> is a valid index the buffer is filled with NULL-values.<br>
  28. *
  29. * If <em>map</em> is old and the cache is used the tile with <em>tileIndex</em> is
  30. * read from file and stored in one of the cache buffers. The pointer to buffer
  31. * is returned. If no free cache buffer is available an unlocked cache-buffer
  32. * is freed up and the new tile is stored in its place. If the tile with <em>tileIndex</em>
  33. * is not stored on the file corresponding to <em>map</em>, and <em>tileIndex</em> is a valid
  34. * index the buffer is filled with NULL-values. If one
  35. * of the cache buffers already contains the tile with <em>tileIndex</em> reading
  36. * is skipped and the pointer to this buffer is returned.<br>
  37. *
  38. * If <em>map</em> is new and the cache is not used the functionality is the same
  39. * as if <em>map</em> is old and the cache is not used. If the tile with <em>tileIndex</em>
  40. * is already stored on file, it is read into the buffer, if not,
  41. * the cells are set to null-values. If the buffer corresponding to the pointer
  42. * is used for writing, subsequent calls to <tt>Rast3d_get_tile_ptr</tt> may destroy the
  43. * values already stored in the buffer. Use <tt>Rast3d_flush_tile</tt> to write the buffer
  44. * to the file before reusing it for a different index. The use of this buffer
  45. * as write buffer is discouraged.<br>
  46. *
  47. * If <em>map</em> is new and the cache is used the functionality is the same as if
  48. * <em>map</em> is old and the cache is used with the following exception. If <em>tileIndex</em>
  49. * is a valid index and the tile with this index is not found in
  50. * the cache and is not stored on the file corresponding to <em>map</em>, then the
  51. * file cache is queried next. If the file-cache contains the tile it is loaded
  52. * into the cache (memory-cache). Only if the file-cache does not contain the
  53. * tile it is filled with NULL-values. Tile contents of buffers are never
  54. * destroyed. If a cache buffer needs to be freed up, and the tile stored in the
  55. * buffer has not been written to the file corresponding to <em>map</em> yet, the
  56. * tile is copied into the file-cache.<br>
  57. *
  58. * Care has to be taken if this function is used in non-cache mode since it is
  59. * implicitly invoked every time a read or write request is issued. The only
  60. * I/O-functions for which it is safe to assume that they do not invoke
  61. * <tt>Rast3d_get_tile_ptr</tt> are <tt>Rast3d_read_tile()</tt> and
  62. * <tt>Rast3d_write_tile()</tt> and their corresponding type-specific versions.
  63. *
  64. * \param map
  65. * \param tileIndex
  66. * \return void * a pointer to a buffer ... if successful,
  67. * NULL ... otherwise.
  68. */
  69. void *Rast3d_get_tile_ptr(RASTER3D_Map * map, int tileIndex)
  70. {
  71. void *ptr;
  72. if ((tileIndex >= map->nTiles) || (tileIndex < 0)) {
  73. Rast3d_error("Rast3d_get_tile_ptr: tileIndex out of range");
  74. return NULL;
  75. }
  76. if (map->useCache) {
  77. ptr = Rast3d_cache_elt_ptr(map->cache, tileIndex);
  78. if (ptr == NULL) {
  79. Rast3d_error("Rast3d_get_tile_ptr: error in Rast3d_cache_elt_ptr");
  80. return NULL;
  81. }
  82. return ptr;
  83. }
  84. if (map->currentIndex == tileIndex)
  85. return map->data;
  86. map->currentIndex = tileIndex;
  87. if (!Rast3d_read_tile(map, map->currentIndex, map->data, map->typeIntern)) {
  88. Rast3d_error("Rast3d_get_tile_ptr: error in Rast3d_read_tile");
  89. return NULL;
  90. }
  91. return map->data;
  92. }
  93. /*---------------------------------------------------------------------------*/
  94. /*!
  95. * \brief
  96. *
  97. * Same functionality as <tt>Rast3d_get_tile_ptr()</tt> but does not return the pointer.
  98. *
  99. * \param map
  100. * \param tileIndex
  101. * \return 1 ... if successful,
  102. * 0 ... otherwise.
  103. */
  104. int Rast3d_tile_load(RASTER3D_Map * map, int tileIndex)
  105. {
  106. if (Rast3d_get_tile_ptr(map, tileIndex) == NULL) {
  107. Rast3d_error("Rast3d_tile_load: error in Rast3d_get_tile_ptr");
  108. return 0;
  109. }
  110. return 1;
  111. }
  112. /*---------------------------------------------------------------------------*/
  113. int Rast3d__remove_tile(RASTER3D_Map * map, int tileIndex)
  114. {
  115. if (!map->useCache)
  116. return 1;
  117. if (!Rast3d_cache_remove_elt(map->cache, tileIndex)) {
  118. Rast3d_error("Rast3d_removeTile: error in Rast3d_cache_remove_elt");
  119. return 0;
  120. }
  121. return 1;
  122. }