tileio.c 5.4 KB

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