/src/FreeImage/Source/LibTIFF/tif_close.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 126 lines · 46 code · 18 blank · 62 comment · 13 complexity · 4eafb3616c18dc4bf3811fa5c67dc990 MD5 · raw file

  1. /* $Id: tif_close.c,v 1.37 2011/04/10 17:14:09 drolon 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. /************************************************************************/
  30. /* TIFFCleanup() */
  31. /************************************************************************/
  32. /**
  33. * Auxiliary function to free the TIFF structure. Given structure will be
  34. * completetly freed, so you should save opened file handle and pointer
  35. * to the close procedure in external variables before calling
  36. * _TIFFCleanup(), if you will need these ones to close the file.
  37. *
  38. * @param tif A TIFF pointer.
  39. */
  40. void
  41. TIFFCleanup(TIFF* tif)
  42. {
  43. if (tif->tif_mode != O_RDONLY)
  44. /*
  45. * Flush buffered data and directory (if dirty).
  46. */
  47. TIFFFlush(tif);
  48. (*tif->tif_cleanup)(tif);
  49. TIFFFreeDirectory(tif);
  50. if (tif->tif_dirlist)
  51. _TIFFfree(tif->tif_dirlist);
  52. /* Clean up client info links */
  53. while( tif->tif_clientinfo )
  54. {
  55. TIFFClientInfoLink *link = tif->tif_clientinfo;
  56. tif->tif_clientinfo = link->next;
  57. _TIFFfree( link->name );
  58. _TIFFfree( link );
  59. }
  60. if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))
  61. _TIFFfree(tif->tif_rawdata);
  62. if (isMapped(tif))
  63. TIFFUnmapFileContents(tif, tif->tif_base, tif->tif_size);
  64. /* Clean up custom fields */
  65. if (tif->tif_nfields > 0)
  66. {
  67. size_t i;
  68. for (i = 0; i < tif->tif_nfields; i++)
  69. {
  70. TIFFFieldInfo *fld = tif->tif_fieldinfo[i];
  71. if (fld->field_bit == FIELD_CUSTOM &&
  72. strncmp("Tag ", fld->field_name, 4) == 0)
  73. {
  74. _TIFFfree(fld->field_name);
  75. _TIFFfree(fld);
  76. }
  77. }
  78. _TIFFfree(tif->tif_fieldinfo);
  79. }
  80. _TIFFfree(tif);
  81. }
  82. /************************************************************************/
  83. /* TIFFClose() */
  84. /************************************************************************/
  85. /**
  86. * Close a previously opened TIFF file.
  87. *
  88. * TIFFClose closes a file that was previously opened with TIFFOpen().
  89. * Any buffered data are flushed to the file, including the contents of
  90. * the current directory (if modified); and all resources are reclaimed.
  91. *
  92. * @param tif A TIFF pointer.
  93. */
  94. void
  95. TIFFClose(TIFF* tif)
  96. {
  97. TIFFCloseProc closeproc = tif->tif_closeproc;
  98. thandle_t fd = tif->tif_clientdata;
  99. TIFFCleanup(tif);
  100. (void) (*closeproc)(fd);
  101. }
  102. /*
  103. * Local Variables:
  104. * mode: c
  105. * c-basic-offset: 8
  106. * fill-column: 78
  107. * End:
  108. */