metadata.h 93.7 KB
Newer Older
Josh Coalson's avatar
Josh Coalson committed
1
/* libFLAC - Free Lossless Audio Codec library
Josh Coalson's avatar
Josh Coalson committed
2
 * Copyright (C) 2001,2002,2003,2004,2005,2006 Josh Coalson
Josh Coalson's avatar
Josh Coalson committed
3
 *
4 5 6
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
Josh Coalson's avatar
Josh Coalson committed
7
 *
8 9
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
Josh Coalson's avatar
Josh Coalson committed
10
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *
 * - Neither the name of the Xiph.org Foundation nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Josh Coalson's avatar
Josh Coalson committed
30 31 32 33 34
 */

#ifndef FLAC__METADATA_H
#define FLAC__METADATA_H

35
#include "export.h"
Josh Coalson's avatar
Josh Coalson committed
36
#include "callback.h"
Josh Coalson's avatar
Josh Coalson committed
37 38
#include "format.h"

Josh Coalson's avatar
Josh Coalson committed
39 40 41 42 43
/* --------------------------------------------------------------------
   (For an example of how all these routines are used, see the source
   code for the unit tests in src/test_libFLAC/metadata_*.c, or
   metaflac in src/metaflac/)
   ------------------------------------------------------------------*/
Josh Coalson's avatar
Josh Coalson committed
44

Josh Coalson's avatar
Josh Coalson committed
45
/** \file include/FLAC/metadata.h
Josh Coalson's avatar
Josh Coalson committed
46 47 48 49 50 51
 *
 *  \brief
 *  This module provides functions for creating and manipulating FLAC
 *  metadata blocks in memory, and three progressively more powerful
 *  interfaces for traversing and editing metadata in FLAC files.
 *
Josh Coalson's avatar
Josh Coalson committed
52 53
 *  See the detailed documentation for each interface in the
 *  \link flac_metadata metadata \endlink module.
Josh Coalson's avatar
Josh Coalson committed
54
 */
Josh Coalson's avatar
Josh Coalson committed
55

Josh Coalson's avatar
Josh Coalson committed
56
/** \defgroup flac_metadata FLAC/metadata.h: metadata interfaces
Josh Coalson's avatar
Josh Coalson committed
57
 *  \ingroup flac
Josh Coalson's avatar
Josh Coalson committed
58 59 60 61
 *
 *  \brief
 *  This module provides functions for creating and manipulating FLAC
 *  metadata blocks in memory, and three progressively more powerful
Josh Coalson's avatar
Josh Coalson committed
62 63 64
 *  interfaces for traversing and editing metadata in native FLAC files.
 *  Note that this metadata interface currently does not support Ogg
 *  FLAC files.
Josh Coalson's avatar
Josh Coalson committed
65 66 67 68
 *
 *  There are three metadata interfaces of increasing complexity:
 *
 *  Level 0:
69 70
 *  Read-only access to the STREAMINFO, VORBIS_COMMENT, CUESHEET, and
 *  PICTURE blocks.
Josh Coalson's avatar
Josh Coalson committed
71 72 73 74 75 76 77 78 79 80 81 82
 *
 *  Level 1:
 *  Read-write access to all metadata blocks.  This level is write-
 *  efficient in most cases (more on this below), and uses less memory
 *  than level 2.
 *
 *  Level 2:
 *  Read-write access to all metadata blocks.  This level is write-
 *  efficient in all cases, but uses more memory since all metadata for
 *  the whole file is read into memory and manipulated before writing
 *  out again.
 *
Josh Coalson's avatar
Josh Coalson committed
83 84 85
 *  What do we mean by efficient?  Since FLAC metadata appears at the
 *  beginning of the file, when writing metadata back to a FLAC file
 *  it is possible to grow or shrink the metadata such that the entire
Josh Coalson's avatar
Josh Coalson committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99
 *  file must be rewritten.  However, if the size remains the same during
 *  changes or PADDING blocks are utilized, only the metadata needs to be
 *  overwritten, which is much faster.
 *
 *  Efficient means the whole file is rewritten at most one time, and only
 *  when necessary.  Level 1 is not efficient only in the case that you
 *  cause more than one metadata block to grow or shrink beyond what can
 *  be accomodated by padding.  In this case you should probably use level
 *  2, which allows you to edit all the metadata for a file in memory and
 *  write it out all at once.
 *
 *  All levels know how to skip over and not disturb an ID3v2 tag at the
 *  front of the file.
 *
100 101 102 103
 *  All levels access files via their filenames.  In addition, level 2
 *  has additional alternative read and write functions that take an I/O
 *  handle and callbacks, for times when access by filename is not possible.
 *
Josh Coalson's avatar
Josh Coalson committed
104
 *  In addition to the three interfaces, this module defines functions for
Josh Coalson's avatar
Josh Coalson committed
105
 *  creating and manipulating various metadata objects in memory.  As we see
Josh Coalson's avatar
Josh Coalson committed
106
 *  from the Format module, FLAC metadata blocks in memory are very primitive
Josh Coalson's avatar
Josh Coalson committed
107 108 109 110 111 112 113 114 115
 *  structures for storing information in an efficient way.  Reading
 *  information from the structures is easy but creating or modifying them
 *  directly is more complex.  The metadata object routines here facilitate
 *  this by taking care of the consistency and memory management drudgery.
 *
 *  Unless you will be using the level 1 or 2 interfaces to modify existing
 *  metadata however, you will not probably not need these.
 *
 *  From a dependency standpoint, none of the encoders or decoders require
Josh Coalson's avatar
Josh Coalson committed
116 117
 *  the metadata module.  This is so that embedded users can strip out the
 *  metadata module from libFLAC to reduce the size and complexity.
Josh Coalson's avatar
Josh Coalson committed
118 119
 */

Josh Coalson's avatar
Josh Coalson committed
120 121 122 123
#ifdef __cplusplus
extern "C" {
#endif

Josh Coalson's avatar
Josh Coalson committed
124

Josh Coalson's avatar
Josh Coalson committed
125
/** \defgroup flac_metadata_level0 FLAC/metadata.h: metadata level 0 interface
Josh Coalson's avatar
Josh Coalson committed
126
 *  \ingroup flac_metadata
Josh Coalson's avatar
Josh Coalson committed
127
 *
Josh Coalson's avatar
Josh Coalson committed
128
 *  \brief
129
 *  The level 0 interface consists of individual routines to read the
130 131
 *  STREAMINFO, VORBIS_COMMENT, CUESHEET, and PICTURE blocks, requiring
 *   only a filename.
Josh Coalson's avatar
Josh Coalson committed
132
 *
133
 *  They try to skip any ID3v2 tag at the head of the file.
Josh Coalson's avatar
Josh Coalson committed
134
 *
Josh Coalson's avatar
Josh Coalson committed
135
 * \{
Josh Coalson's avatar
Josh Coalson committed
136 137
 */

Josh Coalson's avatar
Josh Coalson committed
138
/** Read the STREAMINFO metadata block of the given FLAC file.  This function
139
 *  will try to skip any ID3v2 tag at the head of the file.
Josh Coalson's avatar
Josh Coalson committed
140
 *
Josh Coalson's avatar
Josh Coalson committed
141
 * \param filename    The path to the FLAC file to read.
142 143 144 145
 * \param streaminfo  A pointer to space for the STREAMINFO block.  Since
 *                    FLAC__StreamMetadata is a simple structure with no
 *                    memory allocation involved, you pass the address of
 *                    an existing structure.  It need not be initialized.
Josh Coalson's avatar
Josh Coalson committed
146 147 148
 * \assert
 *    \code filename != NULL \endcode
 *    \code streaminfo != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
149
 * \retval FLAC__bool
Josh Coalson's avatar
Josh Coalson committed
150 151
 *    \c true if a valid STREAMINFO block was read from \a filename.  Returns
 *    \c false if there was a memory allocation error, a file decoder error,
152 153
 *    or the file contained no STREAMINFO block.  (A memory allocation error
 *    is possible because this function must set up a file decoder.)
Josh Coalson's avatar
Josh Coalson committed
154
 */
155
FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo);
Josh Coalson's avatar
Josh Coalson committed
156

157
/** Read the VORBIS_COMMENT metadata block of the given FLAC file.  This
158
 *  function will try to skip any ID3v2 tag at the head of the file.
159 160 161 162 163 164 165
 *
 * \param filename    The path to the FLAC file to read.
 * \param tags        The address where the returned pointer will be
 *                    stored.  The \a tags object must be deleted by
 *                    the caller using FLAC__metadata_object_delete().
 * \assert
 *    \code filename != NULL \endcode
166
 *    \code tags != NULL \endcode
167 168
 * \retval FLAC__bool
 *    \c true if a valid VORBIS_COMMENT block was read from \a filename,
169
 *    and \a *tags will be set to the address of the metadata structure.
170 171 172 173 174 175
 *    Returns \c false if there was a memory allocation error, a file
 *    decoder error, or the file contained no VORBIS_COMMENT block, and
 *    \a *tags will be set to \c NULL.
 */
FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags);

176 177 178 179 180 181 182 183 184 185 186 187
/** Read the CUESHEET metadata block of the given FLAC file.  This
 *  function will try to skip any ID3v2 tag at the head of the file.
 *
 * \param filename    The path to the FLAC file to read.
 * \param cuesheet    The address where the returned pointer will be
 *                    stored.  The \a cuesheet object must be deleted by
 *                    the caller using FLAC__metadata_object_delete().
 * \assert
 *    \code filename != NULL \endcode
 *    \code cuesheet != NULL \endcode
 * \retval FLAC__bool
 *    \c true if a valid CUESHEET block was read from \a filename,
188 189 190 191
 *    and \a *cuesheet will be set to the address of the metadata
 *    structure.  Returns \c false if there was a memory allocation
 *    error, a file decoder error, or the file contained no CUESHEET
 *    block, and \a *cuesheet will be set to \c NULL.
192 193 194
 */
FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet);

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
/** Read a PICTURE metadata block of the given FLAC file.  This
 *  function will try to skip any ID3v2 tag at the head of the file.
 *  Since there can be more than one PICTURE block in a file, this
 *  function takes a number of parameters that act as constraints to
 *  the search.  The PICTURE block with the largest area matching all
 *  the constraints will be returned, or \a *picture will be set to
 *  \c NULL if there was no such block.
 *
 * \param filename    The path to the FLAC file to read.
 * \param picture     The address where the returned pointer will be
 *                    stored.  The \a picture object must be deleted by
 *                    the caller using FLAC__metadata_object_delete().
 * \param type        The desired picture type.  Use \c -1 to mean
 *                    "any type".
 * \param mime_type   The desired MIME type, e.g. "image/jpeg".  The
 *                    string will be matched exactly.  Use \c NULL to
 *                    mean "any MIME type".
 * \param description The desired description.  The string will be
 *                    matched exactly.  Use \c NULL to mean "any
 *                    description".
 * \param max_width   The maximum width in pixels desired.  Use
 *                    \c (unsigned)(-1) to mean "any width".
 * \param max_height  The maximum height in pixels desired.  Use
 *                    \c (unsigned)(-1) to mean "any height".
 * \param max_depth   The maximum color depth in bits-per-pixel desired.
 *                    Use \c (unsigned)(-1) to mean "any depth".
221 222
 * \param max_colors  The maximum number of colors desired.  Use
 *                    \c (unsigned)(-1) to mean "any number of colors".
223 224 225 226 227 228 229 230 231 232
 * \assert
 *    \code filename != NULL \endcode
 *    \code picture != NULL \endcode
 * \retval FLAC__bool
 *    \c true if a valid PICTURE block was read from \a filename,
 *    and \a *picture will be set to the address of the metadata
 *    structure.  Returns \c false if there was a memory allocation
 *    error, a file decoder error, or the file contained no PICTURE
 *    block, and \a *picture will be set to \c NULL.
 */
233
FLAC_API FLAC__bool FLAC__metadata_get_picture(const char *filename, FLAC__StreamMetadata **picture, FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors);
234

Josh Coalson's avatar
Josh Coalson committed
235 236
/* \} */

Josh Coalson's avatar
Josh Coalson committed
237

Josh Coalson's avatar
Josh Coalson committed
238
/** \defgroup flac_metadata_level1 FLAC/metadata.h: metadata level 1 interface
Josh Coalson's avatar
Josh Coalson committed
239
 *  \ingroup flac_metadata
Josh Coalson's avatar
Josh Coalson committed
240
 *
Josh Coalson's avatar
Josh Coalson committed
241 242 243 244
 * \brief
 * The level 1 interface provides read-write access to FLAC file metadata and
 * operates directly on the FLAC file.
 *
Josh Coalson's avatar
Josh Coalson committed
245 246
 * The general usage of this interface is:
 *
Josh Coalson's avatar
Josh Coalson committed
247 248 249
 * - Create an iterator using FLAC__metadata_simple_iterator_new()
 * - Attach it to a file using FLAC__metadata_simple_iterator_init() and check
 *   the exit code.  Call FLAC__metadata_simple_iterator_is_writable() to
250
 *   see if the file is writable, or only read access is allowed.
Josh Coalson's avatar
Josh Coalson committed
251
 * - Use FLAC__metadata_simple_iterator_next() and
252
 *   FLAC__metadata_simple_iterator_prev() to traverse the blocks.
Josh Coalson's avatar
Josh Coalson committed
253 254 255 256 257 258 259 260 261 262 263 264 265
 *   This is does not read the actual blocks themselves.
 *   FLAC__metadata_simple_iterator_next() is relatively fast.
 *   FLAC__metadata_simple_iterator_prev() is slower since it needs to search
 *   forward from the front of the file.
 * - Use FLAC__metadata_simple_iterator_get_block_type() or
 *   FLAC__metadata_simple_iterator_get_block() to access the actual data at
 *   the current iterator position.  The returned object is yours to modify
 *   and free.
 * - Use FLAC__metadata_simple_iterator_set_block() to write a modified block
 *   back.  You must have write permission to the original file.  Make sure to
 *   read the whole comment to FLAC__metadata_simple_iterator_set_block()
 *   below.
 * - Use FLAC__metadata_simple_iterator_insert_block_after() to add new blocks.
Josh Coalson's avatar
Josh Coalson committed
266 267
 *   Use the object creation functions from
 *   \link flac_metadata_object here \endlink to generate new objects.
Josh Coalson's avatar
Josh Coalson committed
268 269 270 271 272 273 274 275 276 277 278 279 280
 * - Use FLAC__metadata_simple_iterator_delete_block() to remove the block
 *   currently referred to by the iterator, or replace it with padding.
 * - Destroy the iterator with FLAC__metadata_simple_iterator_delete() when
 *   finished.
 *
 * \note
 * The FLAC file remains open the whole time between
 * FLAC__metadata_simple_iterator_init() and
 * FLAC__metadata_simple_iterator_delete(), so make sure you are not altering
 * the file during this time.
 *
 * \note
 * Do not modify the \a is_last, \a length, or \a type fields of returned
Josh Coalson's avatar
Josh Coalson committed
281
 * FLAC__StreamMetadata objects.  These are managed automatically.
Josh Coalson's avatar
Josh Coalson committed
282 283 284 285 286
 *
 * \note
 * If any of the modification functions
 * (FLAC__metadata_simple_iterator_set_block(),
 * FLAC__metadata_simple_iterator_delete_block(),
Josh Coalson's avatar
Josh Coalson committed
287
 * FLAC__metadata_simple_iterator_insert_block_after(), etc.) return \c false,
Josh Coalson's avatar
Josh Coalson committed
288 289 290
 * you should delete the iterator as it may no longer be valid.
 *
 * \{
Josh Coalson's avatar
Josh Coalson committed
291 292
 */

293
struct FLAC__Metadata_SimpleIterator;
Josh Coalson's avatar
Josh Coalson committed
294 295 296 297 298
/** The opaque structure definition for the level 1 iterator type.
 *  See the
 *  \link flac_metadata_level1 metadata level 1 module \endlink
 *  for a detailed description.
 */
299
typedef struct FLAC__Metadata_SimpleIterator FLAC__Metadata_SimpleIterator;
Josh Coalson's avatar
Josh Coalson committed
300

Josh Coalson's avatar
Josh Coalson committed
301 302 303 304
/** Status type for FLAC__Metadata_SimpleIterator.
 *
 *  The iterator's current status can be obtained by calling FLAC__metadata_simple_iterator_status().
 */
Josh Coalson's avatar
Josh Coalson committed
305
typedef enum {
Josh Coalson's avatar
Josh Coalson committed
306

Josh Coalson's avatar
Josh Coalson committed
307
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK = 0,
Josh Coalson's avatar
Josh Coalson committed
308 309
	/**< The iterator is in the normal OK state */

Josh Coalson's avatar
Josh Coalson committed
310
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT,
Josh Coalson's avatar
Josh Coalson committed
311
	/**< The data passed into a function violated the function's usage criteria */
Josh Coalson's avatar
Josh Coalson committed
312

Josh Coalson's avatar
Josh Coalson committed
313
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE,
Josh Coalson's avatar
Josh Coalson committed
314 315
	/**< The iterator could not open the target file */

Josh Coalson's avatar
Josh Coalson committed
316
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE,
Josh Coalson's avatar
Josh Coalson committed
317 318
	/**< The iterator could not find the FLAC signature at the start of the file */

Josh Coalson's avatar
Josh Coalson committed
319
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE,
Josh Coalson's avatar
Josh Coalson committed
320 321
	/**< The iterator tried to write to a file that was not writable */

322
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA,
Josh Coalson's avatar
Josh Coalson committed
323 324
	/**< The iterator encountered input that does not conform to the FLAC metadata specification */

Josh Coalson's avatar
Josh Coalson committed
325
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR,
Josh Coalson's avatar
Josh Coalson committed
326 327
	/**< The iterator encountered an error while reading the FLAC file */

Josh Coalson's avatar
Josh Coalson committed
328
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR,
Josh Coalson's avatar
Josh Coalson committed
329 330
	/**< The iterator encountered an error while seeking in the FLAC file */

Josh Coalson's avatar
Josh Coalson committed
331
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR,
Josh Coalson's avatar
Josh Coalson committed
332 333
	/**< The iterator encountered an error while writing the FLAC file */

Josh Coalson's avatar
Josh Coalson committed
334
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR,
Josh Coalson's avatar
Josh Coalson committed
335 336
	/**< The iterator encountered an error renaming the FLAC file */

Josh Coalson's avatar
Josh Coalson committed
337
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR,
Josh Coalson's avatar
Josh Coalson committed
338 339
	/**< The iterator encountered an error removing the temporary file */

Josh Coalson's avatar
Josh Coalson committed
340
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR,
Josh Coalson's avatar
Josh Coalson committed
341 342
	/**< Memory allocation failed */

Josh Coalson's avatar
Josh Coalson committed
343
	FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR
Josh Coalson's avatar
Josh Coalson committed
344 345
	/**< The caller violated an assertion or an unexpected error occurred */

346
} FLAC__Metadata_SimpleIteratorStatus;
Josh Coalson's avatar
Josh Coalson committed
347 348 349

/** Maps a FLAC__Metadata_SimpleIteratorStatus to a C string.
 *
Josh Coalson's avatar
Josh Coalson committed
350 351
 *  Using a FLAC__Metadata_SimpleIteratorStatus as the index to this array
 *  will give the string equivalent.  The contents should not be modified.
Josh Coalson's avatar
Josh Coalson committed
352
 */
353
extern FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[];
Josh Coalson's avatar
Josh Coalson committed
354

Josh Coalson's avatar
Josh Coalson committed
355

Josh Coalson's avatar
Josh Coalson committed
356 357 358 359
/** Create a new iterator instance.
 *
 * \retval FLAC__Metadata_SimpleIterator*
 *    \c NULL if there was an error allocating memory, else the new instance.
Josh Coalson's avatar
Josh Coalson committed
360
 */
361
FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new();
Josh Coalson's avatar
Josh Coalson committed
362 363 364 365

/** Free an iterator instance.  Deletes the object pointed to by \a iterator.
 *
 * \param iterator  A pointer to an existing iterator.
Josh Coalson's avatar
Josh Coalson committed
366 367
 * \assert
 *    \code iterator != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
368
 */
369
FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator);
Josh Coalson's avatar
Josh Coalson committed
370

Josh Coalson's avatar
Josh Coalson committed
371 372 373 374 375
/** Get the current status of the iterator.  Call this after a function
 *  returns \c false to get the reason for the error.  Also resets the status
 *  to FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK.
 *
 * \param iterator  A pointer to an existing iterator.
Josh Coalson's avatar
Josh Coalson committed
376 377
 * \assert
 *    \code iterator != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
378 379
 * \retval FLAC__Metadata_SimpleIteratorStatus
 *    The current status of the iterator.
Josh Coalson's avatar
Josh Coalson committed
380
 */
381
FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator);
Josh Coalson's avatar
Josh Coalson committed
382

Josh Coalson's avatar
Josh Coalson committed
383
/** Initialize the iterator to point to the first metadata block in the
384
 *  given FLAC file.
Josh Coalson's avatar
Josh Coalson committed
385 386 387
 *
 * \param iterator             A pointer to an existing iterator.
 * \param filename             The path to the FLAC file.
388 389 390 391 392 393 394
 * \param read_only            If \c true, the FLAC file will be opened
 *                             in read-only mode; if \c false, the FLAC
 *                             file will be opened for edit even if no
 *                             edits are performed.
 * \param preserve_file_stats  If \c true, the owner and modification
 *                             time will be preserved even if the FLAC
 *                             file is written to.
Josh Coalson's avatar
Josh Coalson committed
395 396 397
 * \assert
 *    \code iterator != NULL \endcode
 *    \code filename != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
398
 * \retval FLAC__bool
Josh Coalson's avatar
Josh Coalson committed
399 400
 *    \c false if a memory allocation error occurs, the file can't be
 *    opened, or another error occurs, else \c true.
Josh Coalson's avatar
Josh Coalson committed
401
 */
402
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats);
Josh Coalson's avatar
Josh Coalson committed
403

Josh Coalson's avatar
Josh Coalson committed
404 405 406 407 408
/** Returns \c true if the FLAC file is writable.  If \c false, calls to
 *  FLAC__metadata_simple_iterator_set_block() and
 *  FLAC__metadata_simple_iterator_insert_block_after() will fail.
 *
 * \param iterator             A pointer to an existing iterator.
Josh Coalson's avatar
Josh Coalson committed
409 410
 * \assert
 *    \code iterator != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
411
 * \retval FLAC__bool
Josh Coalson's avatar
Josh Coalson committed
412
 *    See above.
Josh Coalson's avatar
Josh Coalson committed
413
 */
414
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator);
Josh Coalson's avatar
Josh Coalson committed
415

Josh Coalson's avatar
Josh Coalson committed
416 417 418 419
/** Moves the iterator forward one metadata block, returning \c false if
 *  already at the end.
 *
 * \param iterator  A pointer to an existing initialized iterator.
Josh Coalson's avatar
Josh Coalson committed
420 421 422 423
 * \assert
 *    \code iterator != NULL \endcode
 *    \a iterator has been successfully initialized with
 *    FLAC__metadata_simple_iterator_init()
Josh Coalson's avatar
Josh Coalson committed
424 425 426
 * \retval FLAC__bool
 *    \c false if already at the last metadata block of the chain, else
 *    \c true.
Josh Coalson's avatar
Josh Coalson committed
427
 */
428
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator);
Josh Coalson's avatar
Josh Coalson committed
429 430 431 432 433

/** Moves the iterator backward one metadata block, returning \c false if
 *  already at the beginning.
 *
 * \param iterator  A pointer to an existing initialized iterator.
Josh Coalson's avatar
Josh Coalson committed
434 435 436 437
 * \assert
 *    \code iterator != NULL \endcode
 *    \a iterator has been successfully initialized with
 *    FLAC__metadata_simple_iterator_init()
Josh Coalson's avatar
Josh Coalson committed
438 439 440 441
 * \retval FLAC__bool
 *    \c false if already at the first metadata block of the chain, else
 *    \c true.
 */
442
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator);
Josh Coalson's avatar
Josh Coalson committed
443

Josh Coalson's avatar
Josh Coalson committed
444 445 446 447 448
/** Get the type of the metadata block at the current position.  This
 *  avoids reading the actual block data which can save time for large
 *  blocks.
 *
 * \param iterator  A pointer to an existing initialized iterator.
Josh Coalson's avatar
Josh Coalson committed
449 450 451 452
 * \assert
 *    \code iterator != NULL \endcode
 *    \a iterator has been successfully initialized with
 *    FLAC__metadata_simple_iterator_init()
Josh Coalson's avatar
Josh Coalson committed
453 454
 * \retval FLAC__MetadataType
 *    The type of the metadata block at the current iterator position.
Josh Coalson's avatar
Josh Coalson committed
455
 */
Josh Coalson's avatar
Josh Coalson committed
456

457
FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator);
Josh Coalson's avatar
Josh Coalson committed
458

Josh Coalson's avatar
Josh Coalson committed
459 460 461 462 463 464
/** Get the metadata block at the current position.  You can modify the
 *  block but must use FLAC__metadata_simple_iterator_set_block() to
 *  write it back to the FLAC file.
 *
 *  You must call FLAC__metadata_object_delete() on the returned object
 *  when you are finished with it.
Josh Coalson's avatar
Josh Coalson committed
465
 *
Josh Coalson's avatar
Josh Coalson committed
466
 * \param iterator  A pointer to an existing initialized iterator.
Josh Coalson's avatar
Josh Coalson committed
467 468 469 470
 * \assert
 *    \code iterator != NULL \endcode
 *    \a iterator has been successfully initialized with
 *    FLAC__metadata_simple_iterator_init()
Josh Coalson's avatar
Josh Coalson committed
471 472
 * \retval FLAC__StreamMetadata*
 *    The current metadata block.
Josh Coalson's avatar
Josh Coalson committed
473
 */
474
FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator);
Josh Coalson's avatar
Josh Coalson committed
475

Josh Coalson's avatar
Josh Coalson committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
/** Write a block back to the FLAC file.  This function tries to be
 *  as efficient as possible; how the block is actually written is
 *  shown by the following:
 *
 *  Existing block is a STREAMINFO block and the new block is a
 *  STREAMINFO block: the new block is written in place.  Make sure
 *  you know what you're doing when changing the values of a
 *  STREAMINFO block.
 *
 *  Existing block is a STREAMINFO block and the new block is a
 *  not a STREAMINFO block: this is an error since the first block
 *  must be a STREAMINFO block.  Returns \c false without altering the
 *  file.
 *
 *  Existing block is not a STREAMINFO block and the new block is a
 *  STREAMINFO block: this is an error since there may be only one
 *  STREAMINFO block.  Returns \c false without altering the file.
 *
 *  Existing block and new block are the same length: the existing
 *  block will be replaced by the new block, written in place.
 *
 *  Existing block is longer than new block: if use_padding is \c true,
 *  the existing block will be overwritten in place with the new
 *  block followed by a PADDING block, if possible, to make the total
 *  size the same as the existing block.  Remember that a padding
 *  block requires at least four bytes so if the difference in size
 *  between the new block and existing block is less than that, the
 *  entire file will have to be rewritten, using the new block's
 *  exact size.  If use_padding is \c false, the entire file will be
 *  rewritten, replacing the existing block by the new block.
 *
 *  Existing block is shorter than new block: if use_padding is \c true,
 *  the function will try and expand the new block into the following
 *  PADDING block, if it exists and doing so won't shrink the PADDING
 *  block to less than 4 bytes.  If there is no following PADDING
 *  block, or it will shrink to less than 4 bytes, or use_padding is
 *  \c false, the entire file is rewritten, replacing the existing block
 *  with the new block.  Note that in this case any following PADDING
 *  block is preserved as is.
 *
 *  After writing the block, the iterator will remain in the same
 *  place, i.e. pointing to the new block.
 *
 * \param iterator     A pointer to an existing initialized iterator.
 * \param block        The block to set.
 * \param use_padding  See above.
Josh Coalson's avatar
Josh Coalson committed
522 523 524 525 526
 * \assert
 *    \code iterator != NULL \endcode
 *    \a iterator has been successfully initialized with
 *    FLAC__metadata_simple_iterator_init()
 *    \code block != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
527 528
 * \retval FLAC__bool
 *    \c true if successful, else \c false.
Josh Coalson's avatar
Josh Coalson committed
529
 */
530
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding);
Josh Coalson's avatar
Josh Coalson committed
531

Josh Coalson's avatar
Josh Coalson committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
/** This is similar to FLAC__metadata_simple_iterator_set_block()
 *  except that instead of writing over an existing block, it appends
 *  a block after the existing block.  \a use_padding is again used to
 *  tell the function to try an expand into following padding in an
 *  attempt to avoid rewriting the entire file.
 *
 *  This function will fail and return \c false if given a STREAMINFO
 *  block.
 *
 *  After writing the block, the iterator will be pointing to the
 *  new block.
 *
 * \param iterator     A pointer to an existing initialized iterator.
 * \param block        The block to set.
 * \param use_padding  See above.
Josh Coalson's avatar
Josh Coalson committed
547 548 549 550 551
 * \assert
 *    \code iterator != NULL \endcode
 *    \a iterator has been successfully initialized with
 *    FLAC__metadata_simple_iterator_init()
 *    \code block != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
552 553
 * \retval FLAC__bool
 *    \c true if successful, else \c false.
Josh Coalson's avatar
Josh Coalson committed
554
 */
555
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding);
Josh Coalson's avatar
Josh Coalson committed
556

Josh Coalson's avatar
Josh Coalson committed
557 558 559 560 561
/** Deletes the block at the current position.  This will cause the
 *  entire FLAC file to be rewritten, unless \a use_padding is \c true,
 *  in which case the block will be replaced by an equal-sized PADDING
 *  block.  The iterator will be left pointing to the block before the
 *  one just deleted.
Josh Coalson's avatar
Josh Coalson committed
562
 *
Josh Coalson's avatar
Josh Coalson committed
563 564 565 566
 *  You may not delete the STREAMINFO block.
 *
 * \param iterator     A pointer to an existing initialized iterator.
 * \param use_padding  See above.
Josh Coalson's avatar
Josh Coalson committed
567 568 569 570
 * \assert
 *    \code iterator != NULL \endcode
 *    \a iterator has been successfully initialized with
 *    FLAC__metadata_simple_iterator_init()
Josh Coalson's avatar
Josh Coalson committed
571 572
 * \retval FLAC__bool
 *    \c true if successful, else \c false.
Josh Coalson's avatar
Josh Coalson committed
573
 */
574
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding);
Josh Coalson's avatar
Josh Coalson committed
575

Josh Coalson's avatar
Josh Coalson committed
576 577
/* \} */

Josh Coalson's avatar
Josh Coalson committed
578

Josh Coalson's avatar
Josh Coalson committed
579
/** \defgroup flac_metadata_level2 FLAC/metadata.h: metadata level 2 interface
Josh Coalson's avatar
Josh Coalson committed
580
 *  \ingroup flac_metadata
Josh Coalson's avatar
Josh Coalson committed
581
 *
Josh Coalson's avatar
Josh Coalson committed
582 583 584 585 586
 * \brief
 * The level 2 interface provides read-write access to FLAC file metadata;
 * all metadata is read into memory, operated on in memory, and then written
 * to file, which is more efficient than level 1 when editing multiple blocks.
 *
587 588 589 590
 * Currently Ogg FLAC is supported for read only, via
 * FLAC__metadata_chain_read_ogg() but a subsequent
 * FLAC__metadata_chain_write() will fail.
 *
Josh Coalson's avatar
Josh Coalson committed
591 592
 * The general usage of this interface is:
 *
Josh Coalson's avatar
Josh Coalson committed
593 594 595
 * - Create a new chain using FLAC__metadata_chain_new().  A chain is a
 *   linked list of FLAC metadata blocks.
 * - Read all metadata into the the chain from a FLAC file using
596 597
 *   FLAC__metadata_chain_read() or FLAC__metadata_chain_read_ogg() and
 *   check the status.
Josh Coalson's avatar
Josh Coalson committed
598 599 600 601 602 603
 * - Optionally, consolidate the padding using
 *   FLAC__metadata_chain_merge_padding() or
 *   FLAC__metadata_chain_sort_padding().
 * - Create a new iterator using FLAC__metadata_iterator_new()
 * - Initialize the iterator to point to the first element in the chain
 *   using FLAC__metadata_iterator_init()
Josh Coalson's avatar
Josh Coalson committed
604 605
 * - Traverse the chain using FLAC__metadata_iterator_next and
 *   FLAC__metadata_iterator_prev().
Josh Coalson's avatar
Josh Coalson committed
606 607 608 609 610 611 612 613 614 615 616 617 618
 * - Get a block for reading or modification using
 *   FLAC__metadata_iterator_get_block().  The pointer to the object
 *   inside the chain is returned, so the block is yours to modify.
 *   Changes will be reflected in the FLAC file when you write the
 *   chain.  You can also add and delete blocks (see functions below).
 * - When done, write out the chain using FLAC__metadata_chain_write().
 *   Make sure to read the whole comment to the function below.
 * - Delete the chain using FLAC__metadata_chain_delete().
 *
 * \note
 * Even though the FLAC file is not open while the chain is being
 * manipulated, you must not alter the file externally during
 * this time.  The chain assumes the FLAC file will not change
619 620
 * between the time of FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg()
 * and FLAC__metadata_chain_write().
Josh Coalson's avatar
Josh Coalson committed
621 622 623
 *
 * \note
 * Do not modify the is_last, length, or type fields of returned
Josh Coalson's avatar
Josh Coalson committed
624
 * FLAC__StreamMetadata objects.  These are managed automatically.
Josh Coalson's avatar
Josh Coalson committed
625 626 627 628 629
 *
 * \note
 * The metadata objects returned by FLAC__metadata_iterator_get_block()
 * are owned by the chain; do not FLAC__metadata_object_delete() them.
 * In the same way, blocks passed to FLAC__metadata_iterator_set_block()
Josh Coalson's avatar
Josh Coalson committed
630
 * become owned by the chain and they will be deleted when the chain is
Josh Coalson's avatar
Josh Coalson committed
631 632 633
 * deleted.
 *
 * \{
Josh Coalson's avatar
Josh Coalson committed
634 635
 */

636
struct FLAC__Metadata_Chain;
Josh Coalson's avatar
Josh Coalson committed
637 638
/** The opaque structure definition for the level 2 chain type.
 */
639
typedef struct FLAC__Metadata_Chain FLAC__Metadata_Chain;
Josh Coalson's avatar
Josh Coalson committed
640

641
struct FLAC__Metadata_Iterator;
Josh Coalson's avatar
Josh Coalson committed
642 643
/** The opaque structure definition for the level 2 iterator type.
 */
644
typedef struct FLAC__Metadata_Iterator FLAC__Metadata_Iterator;
Josh Coalson's avatar
Josh Coalson committed
645 646 647

typedef enum {
	FLAC__METADATA_CHAIN_STATUS_OK = 0,
Josh Coalson's avatar
Josh Coalson committed
648 649
	/**< The chain is in the normal OK state */

Josh Coalson's avatar
Josh Coalson committed
650
	FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT,
Josh Coalson's avatar
Josh Coalson committed
651 652
	/**< The data passed into a function violated the function's usage criteria */

Josh Coalson's avatar
Josh Coalson committed
653
	FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE,
Josh Coalson's avatar
Josh Coalson committed
654 655
	/**< The chain could not open the target file */

Josh Coalson's avatar
Josh Coalson committed
656
	FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE,
Josh Coalson's avatar
Josh Coalson committed
657 658
	/**< The chain could not find the FLAC signature at the start of the file */

Josh Coalson's avatar
Josh Coalson committed
659
	FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE,
Josh Coalson's avatar
Josh Coalson committed
660 661
	/**< The chain tried to write to a file that was not writable */

662
	FLAC__METADATA_CHAIN_STATUS_BAD_METADATA,
Josh Coalson's avatar
Josh Coalson committed
663 664
	/**< The chain encountered input that does not conform to the FLAC metadata specification */

Josh Coalson's avatar
Josh Coalson committed
665
	FLAC__METADATA_CHAIN_STATUS_READ_ERROR,
Josh Coalson's avatar
Josh Coalson committed
666 667
	/**< The chain encountered an error while reading the FLAC file */

Josh Coalson's avatar
Josh Coalson committed
668
	FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR,
Josh Coalson's avatar
Josh Coalson committed
669 670
	/**< The chain encountered an error while seeking in the FLAC file */

Josh Coalson's avatar
Josh Coalson committed
671
	FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR,
Josh Coalson's avatar
Josh Coalson committed
672 673
	/**< The chain encountered an error while writing the FLAC file */

Josh Coalson's avatar
Josh Coalson committed
674
	FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR,
Josh Coalson's avatar
Josh Coalson committed
675 676
	/**< The chain encountered an error renaming the FLAC file */

Josh Coalson's avatar
Josh Coalson committed
677
	FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR,
Josh Coalson's avatar
Josh Coalson committed
678 679
	/**< The chain encountered an error removing the temporary file */

Josh Coalson's avatar
Josh Coalson committed
680
	FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR,
Josh Coalson's avatar
Josh Coalson committed
681 682
	/**< Memory allocation failed */

683
	FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR,
Josh Coalson's avatar
Josh Coalson committed
684 685
	/**< The caller violated an assertion or an unexpected error occurred */

686
	FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS,
687 688
	/**< One or more of the required callbacks was NULL */

689 690
	FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH,
	/**< FLAC__metadata_chain_write() was called on a chain read by
691 692 693 694 695 696
	 *   FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(),
	 *   or 
	 *   FLAC__metadata_chain_write_with_callbacks()/FLAC__metadata_chain_write_with_callbacks_and_tempfile()
	 *   was called on a chain read by
	 *   FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg().
	 *   Matching read/write methods must always be used. */
697 698 699 700 701 702 703 704 705 706 707

	FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL
	/**< FLAC__metadata_chain_write_with_callbacks() was called when the
	 *   chain write requires a tempfile; use
	 *   FLAC__metadata_chain_write_with_callbacks_and_tempfile() instead.
	 *   Or, FLAC__metadata_chain_write_with_callbacks_and_tempfile() was
	 *   called when the chain write does not require a tempfile; use
	 *   FLAC__metadata_chain_write_with_callbacks() instead.
	 *   Always check FLAC__metadata_chain_check_if_tempfile_needed()
	 *   before writing via callbacks. */

708
} FLAC__Metadata_ChainStatus;
Josh Coalson's avatar
Josh Coalson committed
709 710 711

/** Maps a FLAC__Metadata_ChainStatus to a C string.
 *
Josh Coalson's avatar
Josh Coalson committed
712 713
 *  Using a FLAC__Metadata_ChainStatus as the index to this array
 *  will give the string equivalent.  The contents should not be modified.
Josh Coalson's avatar
Josh Coalson committed
714
 */
715
extern FLAC_API const char * const FLAC__Metadata_ChainStatusString[];
Josh Coalson's avatar
Josh Coalson committed
716

717
/*********** FLAC__Metadata_Chain ***********/
Josh Coalson's avatar
Josh Coalson committed
718

Josh Coalson's avatar
Josh Coalson committed
719 720 721
/** Create a new chain instance.
 *
 * \retval FLAC__Metadata_Chain*
Josh Coalson's avatar
Josh Coalson committed
722
 *    \c NULL if there was an error allocating memory, else the new instance.
Josh Coalson's avatar
Josh Coalson committed
723
 */
724
FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new();
Josh Coalson's avatar
Josh Coalson committed
725 726 727

/** Free a chain instance.  Deletes the object pointed to by \a chain.
 *
Josh Coalson's avatar
Josh Coalson committed
728
 * \param chain  A pointer to an existing chain.
Josh Coalson's avatar
Josh Coalson committed
729 730
 * \assert
 *    \code chain != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
731
 */
732
FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain);
Josh Coalson's avatar
Josh Coalson committed
733

Josh Coalson's avatar
Josh Coalson committed
734
/** Get the current status of the chain.  Call this after a function
Josh Coalson's avatar
Josh Coalson committed
735
 *  returns \c false to get the reason for the error.  Also resets the
Josh Coalson's avatar
Josh Coalson committed
736 737
 *  status to FLAC__METADATA_CHAIN_STATUS_OK.
 *
Josh Coalson's avatar
Josh Coalson committed
738
 * \param chain    A pointer to an existing chain.
Josh Coalson's avatar
Josh Coalson committed
739 740
 * \assert
 *    \code chain != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
741 742
 * \retval FLAC__Metadata_ChainStatus
 *    The current status of the chain.
Josh Coalson's avatar
Josh Coalson committed
743
 */
744
FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain);
Josh Coalson's avatar
Josh Coalson committed
745

Josh Coalson's avatar
Josh Coalson committed
746 747
/** Read all metadata from a FLAC file into the chain.
 *
Josh Coalson's avatar
Josh Coalson committed
748 749
 * \param chain    A pointer to an existing chain.
 * \param filename The path to the FLAC file to read.
Josh Coalson's avatar
Josh Coalson committed
750 751 752
 * \assert
 *    \code chain != NULL \endcode
 *    \code filename != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
753
 * \retval FLAC__bool
Josh Coalson's avatar
Josh Coalson committed
754 755
 *    \c true if a valid list of metadata blocks was read from
 *    \a filename, else \c false.  On failure, check the status with
Josh Coalson's avatar
Josh Coalson committed
756
 *    FLAC__metadata_chain_status().
Josh Coalson's avatar
Josh Coalson committed
757
 */
758
FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename);
Josh Coalson's avatar
Josh Coalson committed
759

760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
/*@@@@ add to unit tests*/
/** Read all metadata from an Ogg FLAC file into the chain.
 *
 * \note Ogg FLAC metadata data writing is not supported yet and
 * FLAC__metadata_chain_write() will fail.
 *
 * \param chain    A pointer to an existing chain.
 * \param filename The path to the Ogg FLAC file to read.
 * \assert
 *    \code chain != NULL \endcode
 *    \code filename != NULL \endcode
 * \retval FLAC__bool
 *    \c true if a valid list of metadata blocks was read from
 *    \a filename, else \c false.  On failure, check the status with
 *    FLAC__metadata_chain_status().
 */
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename);

778
/** Read all metadata from a FLAC stream into the chain via I/O callbacks.
779 780 781 782
 *
 *  The \a handle need only be open for reading, but must be seekable.
 *  The equivalent minimum stdio fopen() file mode is \c "r" (or \c "rb"
 *  for Windows).
783 784 785
 *
 * \param chain    A pointer to an existing chain.
 * \param handle   The I/O handle of the FLAC stream to read.  The
786 787
 *                 handle will NOT be closed after the metadata is read;
 *                 that is the duty of the caller.
788 789
 * \param callbacks
 *                 A set of callbacks to use for I/O.  The mandatory
790
 *                 callbacks are \a read, \a seek, and \a tell.
791 792 793 794 795 796 797 798 799
 * \assert
 *    \code chain != NULL \endcode
 * \retval FLAC__bool
 *    \c true if a valid list of metadata blocks was read from
 *    \a handle, else \c false.  On failure, check the status with
 *    FLAC__metadata_chain_status().
 */
FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks);

800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
/*@@@@ add to unit tests*/
/** Read all metadata from an Ogg FLAC stream into the chain via I/O callbacks.
 *
 *  The \a handle need only be open for reading, but must be seekable.
 *  The equivalent minimum stdio fopen() file mode is \c "r" (or \c "rb"
 *  for Windows).
 *
 * \note Ogg FLAC metadata data writing is not supported yet and
 * FLAC__metadata_chain_write() will fail.
 *
 * \param chain    A pointer to an existing chain.
 * \param handle   The I/O handle of the Ogg FLAC stream to read.  The
 *                 handle will NOT be closed after the metadata is read;
 *                 that is the duty of the caller.
 * \param callbacks
 *                 A set of callbacks to use for I/O.  The mandatory
 *                 callbacks are \a read, \a seek, and \a tell.
 * \assert
 *    \code chain != NULL \endcode
 * \retval FLAC__bool
 *    \c true if a valid list of metadata blocks was read from
 *    \a handle, else \c false.  On failure, check the status with
 *    FLAC__metadata_chain_status().
 */
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks);

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
/** Checks if writing the given chain would require the use of a
 *  temporary file, or if it could be written in place.
 *
 *  Under certain conditions, padding can be utilized so that writing
 *  edited metadata back to the FLAC file does not require rewriting the
 *  entire file.  If rewriting is required, then a temporary workfile is
 *  required.  When writing metadata using callbacks, you must check
 *  this function to know whether to call
 *  FLAC__metadata_chain_write_with_callbacks() or
 *  FLAC__metadata_chain_write_with_callbacks_and_tempfile().  When
 *  writing with FLAC__metadata_chain_write(), the temporary file is
 *  handled internally.
 *
 * \param chain    A pointer to an existing chain.
 * \param use_padding
 *                 Whether or not padding will be allowed to be used
 *                 during the write.  The value of \a use_padding given
 *                 here must match the value later passed to
 *                 FLAC__metadata_chain_write_with_callbacks() or
 *                 FLAC__metadata_chain_write_with_callbacks_with_tempfile().
 * \assert
 *    \code chain != NULL \endcode
 * \retval FLAC__bool
 *    \c true if writing the current chain would require a tempfile, or
 *    \c false if metadata can be written in place.
 */
852 853
FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding);

Josh Coalson's avatar
Josh Coalson committed
854 855 856 857 858 859 860 861
/** Write all metadata out to the FLAC file.  This function tries to be as
 *  efficient as possible; how the metadata is actually written is shown by
 *  the following:
 *
 *  If the current chain is the same size as the existing metadata, the new
 *  data is written in place.
 *
 *  If the current chain is longer than the existing metadata, and
Josh Coalson's avatar
Josh Coalson committed
862
 *  \a use_padding is \c true, and the last block is a PADDING block of
Josh Coalson's avatar
Josh Coalson committed
863 864 865 866 867 868 869 870 871 872
 *  sufficient length, the function will truncate the final padding block
 *  so that the overall size of the metadata is the same as the existing
 *  metadata, and then just rewrite the metadata.  Otherwise, if not all of
 *  the above conditions are met, the entire FLAC file must be rewritten.
 *  If you want to use padding this way it is a good idea to call
 *  FLAC__metadata_chain_sort_padding() first so that you have the maximum
 *  amount of padding to work with, unless you need to preserve ordering
 *  of the PADDING blocks for some reason.
 *
 *  If the current chain is shorter than the existing metadata, and
Josh Coalson's avatar
Josh Coalson committed
873
 *  \a use_padding is \c true, and the final block is a PADDING block, the padding
Josh Coalson's avatar
Josh Coalson committed
874
 *  is extended to make the overall size the same as the existing data.  If
Josh Coalson's avatar
Josh Coalson committed
875
 *  \a use_padding is \c true and the last block is not a PADDING block, a new
Josh Coalson's avatar
Josh Coalson committed
876 877 878 879
 *  PADDING block is added to the end of the new data to make it the same
 *  size as the existing data (if possible, see the note to
 *  FLAC__metadata_simple_iterator_set_block() about the four byte limit)
 *  and the new data is written in place.  If none of the above apply or
Josh Coalson's avatar
Josh Coalson committed
880
 *  \a use_padding is \c false, the entire FLAC file is rewritten.
Josh Coalson's avatar
Josh Coalson committed
881
 *
Josh Coalson's avatar
Josh Coalson committed
882
 *  If \a preserve_file_stats is \c true, the owner and modification time will
Josh Coalson's avatar
Josh Coalson committed
883 884
 *  be preserved even if the FLAC file is written.
 *
885
 *  For this write function to be used, the chain must have been read with
886 887
 *  FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg(), not
 *  FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks().
888
 *
Josh Coalson's avatar
Josh Coalson committed
889 890 891
 * \param chain               A pointer to an existing chain.
 * \param use_padding         See above.
 * \param preserve_file_stats See above.
Josh Coalson's avatar
Josh Coalson committed
892 893
 * \assert
 *    \code chain != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
894
 * \retval FLAC__bool
Josh Coalson's avatar
Josh Coalson committed
895
 *    \c true if the write succeeded, else \c false.  On failure,
Josh Coalson's avatar
Josh Coalson committed
896
 *    check the status with FLAC__metadata_chain_status().
Josh Coalson's avatar
Josh Coalson committed
897
 */
898
FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats);
Josh Coalson's avatar
Josh Coalson committed
899

900 901 902 903 904 905 906 907 908 909
/** Write all metadata out to a FLAC stream via callbacks.
 *
 *  (See FLAC__metadata_chain_write() for the details on how padding is
 *  used to write metadata in place if possible.)
 *
 *  The \a handle must be open for updating and be seekable.  The
 *  equivalent minimum stdio fopen() file mode is \c "r+" (or \c "r+b"
 *  for Windows).
 *
 *  For this write function to be used, the chain must have been read with
910 911
 *  FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(),
 *  not FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg().
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
 *  Also, FLAC__metadata_chain_check_if_tempfile_needed() must have returned
 *  \c false.
 *
 * \param chain        A pointer to an existing chain.
 * \param use_padding  See FLAC__metadata_chain_write()
 * \param handle       The I/O handle of the FLAC stream to write.  The
 *                     handle will NOT be closed after the metadata is
 *                     written; that is the duty of the caller.
 * \param callbacks    A set of callbacks to use for I/O.  The mandatory
 *                     callbacks are \a write and \a seek.
 * \assert
 *    \code chain != NULL \endcode
 * \retval FLAC__bool
 *    \c true if the write succeeded, else \c false.  On failure,
 *    check the status with FLAC__metadata_chain_status().
 */
928 929
FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks);

930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
/** Write all metadata out to a FLAC stream via callbacks.
 *
 *  (See FLAC__metadata_chain_write() for the details on how padding is
 *  used to write metadata in place if possible.)
 *
 *  This version of the write-with-callbacks function must be used when
 *  FLAC__metadata_chain_check_if_tempfile_needed() returns true.  In
 *  this function, you must supply an I/O handle corresponding to the
 *  FLAC file to edit, and a temporary handle to which the new FLAC
 *  file will be written.  It is the caller's job to move this temporary
 *  FLAC file on top of the original FLAC file to complete the metadata
 *  edit.
 *
 *  The \a handle must be open for reading and be seekable.  The
 *  equivalent minimum stdio fopen() file mode is \c "r" (or \c "rb"
 *  for Windows).
 *
 *  The \a temp_handle must be open for writing.  The
 *  equivalent minimum stdio fopen() file mode is \c "w" (or \c "wb"
 *  for Windows).  It should be an empty stream, or at least positioned
 *  at the start-of-file (in which case it is the caller's duty to
 *  truncate it on return).
 *
 *  For this write function to be used, the chain must have been read with
954 955
 *  FLAC__metadata_chain_read_with_callbacks()/FLAC__metadata_chain_read_ogg_with_callbacks(),
 *  not FLAC__metadata_chain_read()/FLAC__metadata_chain_read_ogg().
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
 *  Also, FLAC__metadata_chain_check_if_tempfile_needed() must have returned
 *  \c true.
 *
 * \param chain        A pointer to an existing chain.
 * \param use_padding  See FLAC__metadata_chain_write()
 * \param handle       The I/O handle of the original FLAC stream to read.
 *                     The handle will NOT be closed after the metadata is
 *                     written; that is the duty of the caller.
 * \param callbacks    A set of callbacks to use for I/O on \a handle.
 *                     The mandatory callbacks are \a read, \a seek, and
 *                     \a eof.
 * \param temp_handle  The I/O handle of the FLAC stream to write.  The
 *                     handle will NOT be closed after the metadata is
 *                     written; that is the duty of the caller.
 * \param temp_callbacks
 *                     A set of callbacks to use for I/O on temp_handle.
 *                     The only mandatory callback is \a write.
 * \assert
 *    \code chain != NULL \endcode
 * \retval FLAC__bool
 *    \c true if the write succeeded, else \c false.  On failure,
 *    check the status with FLAC__metadata_chain_status().
 */
979 980
FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks_and_tempfile(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__IOHandle temp_handle, FLAC__IOCallbacks temp_callbacks);

Josh Coalson's avatar
Josh Coalson committed
981
/** Merge adjacent PADDING blocks into a single block.
Josh Coalson's avatar
Josh Coalson committed
982
 *
Josh Coalson's avatar
Josh Coalson committed
983
 * \note This function does not write to the FLAC file, it only
Josh Coalson's avatar
Josh Coalson committed
984 985
 * modifies the chain.
 *
Josh Coalson's avatar
Josh Coalson committed
986
 * \warning Any iterator on the current chain will become invalid after this
Josh Coalson's avatar
Josh Coalson committed
987
 * call.  You should delete the iterator and get a new one.
Josh Coalson's avatar
Josh Coalson committed
988
 *
Josh Coalson's avatar
Josh Coalson committed
989
 * \param chain               A pointer to an existing chain.
Josh Coalson's avatar
Josh Coalson committed
990 991
 * \assert
 *    \code chain != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
992
 */
993
FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain);
Josh Coalson's avatar
Josh Coalson committed
994

Josh Coalson's avatar
Josh Coalson committed
995 996
/** This function will move all PADDING blocks to the end on the metadata,
 *  then merge them into a single block.
Josh Coalson's avatar
Josh Coalson committed
997
 *
Josh Coalson's avatar
Josh Coalson committed
998
 * \note This function does not write to the FLAC file, it only
Josh Coalson's avatar
Josh Coalson committed
999 1000
 * modifies the chain.
 *
Josh Coalson's avatar
Josh Coalson committed
1001
 * \warning Any iterator on the current chain will become invalid after this
Josh Coalson's avatar
Josh Coalson committed
1002
 * call.  You should delete the iterator and get a new one.
Josh Coalson's avatar
Josh Coalson committed
1003
 *
Josh Coalson's avatar
Josh Coalson committed
1004
 * \param chain  A pointer to an existing chain.
Josh Coalson's avatar
Josh Coalson committed
1005 1006
 * \assert
 *    \code chain != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
1007
 */
1008
FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain);
Josh Coalson's avatar
Josh Coalson committed
1009 1010


1011
/*********** FLAC__Metadata_Iterator ***********/
Josh Coalson's avatar
Josh Coalson committed
1012

Josh Coalson's avatar
Josh Coalson committed
1013 1014 1015
/** Create a new iterator instance.
 *
 * \retval FLAC__Metadata_Iterator*
Josh Coalson's avatar
Josh Coalson committed
1016
 *    \c NULL if there was an error allocating memory, else the new instance.
Josh Coalson's avatar
Josh Coalson committed
1017
 */
1018
FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new();
Josh Coalson's avatar
Josh Coalson committed
1019 1020 1021

/** Free an iterator instance.  Deletes the object pointed to by \a iterator.
 *
Josh Coalson's avatar
Josh Coalson committed
1022
 * \param iterator  A pointer to an existing iterator.
Josh Coalson's avatar
Josh Coalson committed
1023 1024
 * \assert
 *    \code iterator != NULL \endcode
Josh Coalson's avatar
Josh Coalson committed
1025
 */
1026
FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator);
Josh Coalson's avatar