tif_close.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* $Id: tif_close.c,v 1.19 2010-03-10 18:56:48 bfriesen Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. */
  28. #include "tiffiop.h"
  29. #include <string.h>
  30. /************************************************************************/
  31. /* TIFFCleanup() */
  32. /************************************************************************/
  33. /**
  34. * Auxiliary function to free the TIFF structure. Given structure will be
  35. * completetly freed, so you should save opened file handle and pointer
  36. * to the close procedure in external variables before calling
  37. * _TIFFCleanup(), if you will need these ones to close the file.
  38. *
  39. * @param tif A TIFF pointer.
  40. */
  41. void
  42. TIFFCleanup(TIFF* tif)
  43. {
  44. /*
  45. * Flush buffered data and directory (if dirty).
  46. */
  47. if (tif->tif_mode != O_RDONLY)
  48. TIFFFlush(tif);
  49. (*tif->tif_cleanup)(tif);
  50. TIFFFreeDirectory(tif);
  51. if (tif->tif_dirlist)
  52. _TIFFfree(tif->tif_dirlist);
  53. /*
  54. * Clean up client info links.
  55. */
  56. while( tif->tif_clientinfo )
  57. {
  58. TIFFClientInfoLink *link = tif->tif_clientinfo;
  59. tif->tif_clientinfo = link->next;
  60. _TIFFfree( link->name );
  61. _TIFFfree( link );
  62. }
  63. if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))
  64. _TIFFfree(tif->tif_rawdata);
  65. if (isMapped(tif))
  66. TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);
  67. /*
  68. * Clean up custom fields.
  69. */
  70. if (tif->tif_fields && tif->tif_nfields > 0) {
  71. uint32 i;
  72. for (i = 0; i < tif->tif_nfields; i++) {
  73. TIFFField *fld = tif->tif_fields[i];
  74. if (fld->field_bit == FIELD_CUSTOM &&
  75. strncmp("Tag ", fld->field_name, 4) == 0) {
  76. _TIFFfree(fld->field_name);
  77. _TIFFfree(fld);
  78. }
  79. }
  80. _TIFFfree(tif->tif_fields);
  81. }
  82. if (tif->tif_nfieldscompat > 0) {
  83. uint32 i;
  84. for (i = 0; i < tif->tif_nfieldscompat; i++) {
  85. if (tif->tif_fieldscompat[i].allocated_size)
  86. _TIFFfree(tif->tif_fieldscompat[i].fields);
  87. }
  88. _TIFFfree(tif->tif_fieldscompat);
  89. }
  90. _TIFFfree(tif);
  91. }
  92. /************************************************************************/
  93. /* TIFFClose() */
  94. /************************************************************************/
  95. /**
  96. * Close a previously opened TIFF file.
  97. *
  98. * TIFFClose closes a file that was previously opened with TIFFOpen().
  99. * Any buffered data are flushed to the file, including the contents of
  100. * the current directory (if modified); and all resources are reclaimed.
  101. *
  102. * @param tif A TIFF pointer.
  103. */
  104. void
  105. TIFFClose(TIFF* tif)
  106. {
  107. TIFFCloseProc closeproc = tif->tif_closeproc;
  108. thandle_t fd = tif->tif_clientdata;
  109. TIFFCleanup(tif);
  110. (void) (*closeproc)(fd);
  111. }
  112. /* vim: set ts=8 sts=8 sw=8 noet: */
  113. /*
  114. * Local Variables:
  115. * mode: c
  116. * c-basic-offset: 8
  117. * fill-column: 78
  118. * End:
  119. */