# FLAC/metadata.h: metadata level 1 interface
[FLAC/metadata.h: metadata interfaces]

## Detailed Description

The level 1 interface provides read-write access to FLAC file metadata and operates directly on the FLAC file.

The general usage of this interface is:

- 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 see if the file is writable, or only read access is allowed.
- Use FLAC__metadata_simple_iterator_next() and FLAC__metadata_simple_iterator_prev() to traverse the blocks. 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. Use the object creation functions from here to generate new objects.
- 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.

  Do not modify the is_last, length, or type fields of returned FLAC__StreamMetadata objects. These are managed automatically.

  If any of the modification functions (FLAC__metadata_simple_iterator_set_block(), FLAC__metadata_simple_iterator_delete_block(), FLAC__metadata_simple_iterator_insert_block_after(), etc.) return `false`, you should delete the iterator as it may no longer be valid.

## Typedefs

typedef FLAC__Metadata_SimpleIterator | FLAC__Metadata_SimpleIterator

## Enumerations

enum

FLAC__Metadata_SimpleIteratorStatus {
FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK = 0, FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT, FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE, FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE,
FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE, FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA, FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR, FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR,
FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR, FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR, FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR, FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR,
FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR
}

## Functions

FLAC__Metadata_SimpleIterator * | FLAC__metadata_simple_iterator_new (void)
void | FLAC__metadata_simple_iterator_delete (FLAC__Metadata_SimpleIterator *iterator)
FLAC__Metadata_SimpleIteratorStatus | FLAC__metadata_simple_iterator_status (FLAC__Metadata_SimpleIterator *iterator)
FLAC__bool | FLAC__metadata_simple_iterator_init (FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats)
FLAC__bool | FLAC__metadata_simple_iterator_is_writable (const FLAC__Metadata_SimpleIterator *iterator)
FLAC__bool | FLAC__metadata_simple_iterator_next (FLAC__Metadata_SimpleIterator *iterator)
FLAC__bool | FLAC__metadata_simple_iterator_prev (FLAC__Metadata_SimpleIterator *iterator)
FLAC__bool | FLAC__metadata_simple_iterator_is_last (const FLAC__Metadata_SimpleIterator *iterator)
off_t | FLAC__metadata_simple_iterator_get_block_offset (const FLAC__Metadata_SimpleIterator *iterator)
FLAC__MetadataType | FLAC__metadata_simple_iterator_get_block_type (const FLAC__Metadata_SimpleIterator *iterator)
unsigned | FLAC__metadata_simple_iterator_get_block_length (const FLAC__Metadata_SimpleIterator *iterator)
FLAC__bool | FLAC__metadata_simple_iterator_get_application_id (FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id)
FLAC__StreamMetadata * | FLAC__metadata_simple_iterator_get_block (FLAC__Metadata_SimpleIterator *iterator)
FLAC__bool | FLAC__metadata_simple_iterator_set_block (FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
FLAC__bool | FLAC__metadata_simple_iterator_insert_block_after (FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
FLAC__bool | FLAC__metadata_simple_iterator_delete_block (FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding)

## Variables

const char *const | FLAC__Metadata_SimpleIteratorStatusString []

## Typedef Documentation

typedef struct FLAC__Metadata_SimpleIterator FLAC__Metadata_SimpleIterator

| The opaque structure definition for the level 1 iterator type. See the metadata level 1 module for a detailed description.

## Enumeration Type Documentation

enum FLAC__Metadata_SimpleIteratorStatus

Status type for FLAC__Metadata_SimpleIterator.

The iterator's current status can be obtained by calling FLAC__metadata_simple_iterator_status().

Enumeration values:

  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK | The iterator is in the normal OK state
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT | The data passed into a function violated the function's usage criteria
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE | The iterator could not open the target file
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE | The iterator could not find the FLAC signature at the start of the file
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE | The iterator tried to write to a file that was not writable
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA | The iterator encountered input that does not conform to the FLAC metadata specification
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR | The iterator encountered an error while reading the FLAC file
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR | The iterator encountered an error while seeking in the FLAC file
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR | The iterator encountered an error while writing the FLAC file
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR | The iterator encountered an error renaming the FLAC file
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR | The iterator encountered an error removing the temporary file
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR | Memory allocation failed
  FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR | The caller violated an assertion or an unexpected error occurred

## Function Documentation

FLAC__Metadata_SimpleIterator* FLAC__metadata_simple_iterator_new | ( | void |  | ) |

Create a new iterator instance.

Return values:

  | FLAC__Metadata_SimpleIterator* | `NULL` if there was an error allocating memory, else the new instance.

void FLAC__metadata_simple_iterator_delete | ( | FLAC__Metadata_SimpleIterator * | iterator | ) |

Free an iterator instance. Deletes the object pointed to by iterator.

Parameters:

  | iterator | A pointer to an existing iterator.

Assertions:

       iterator != NULL

FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status | ( | FLAC__Metadata_SimpleIterator * | iterator | ) |

Get the current status of the iterator. Call this after a function returns `false` to get the reason for the error. Also resets the status to FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK.

Parameters:

  | iterator | A pointer to an existing iterator.

Assertions:

       iterator != NULL

Return values:

  | FLAC__Metadata_SimpleIteratorStatus | The current status of the iterator.

FLAC__bool FLAC__metadata_simple_iterator_init | ( | FLAC__Metadata_SimpleIterator * | iterator,
|  | const char * | filename,
|  | FLAC__bool | read_only,
|  | FLAC__bool | preserve_file_stats
| ) |

Initialize the iterator to point to the first metadata block in the given FLAC file.

Parameters:

  | iterator | A pointer to an existing iterator.
  | filename | The path to the FLAC file.
  | read_only | If `true`, the FLAC file will be opened in read-only mode; if `false`, the FLAC file will be opened for edit even if no edits are performed.
  | preserve_file_stats | If `true`, the owner and modification time will be preserved even if the FLAC file is written to.

Assertions:

       iterator != NULL

       filename != NULL

Return values:

  | FLAC__bool | `false` if a memory allocation error occurs, the file can't be opened, or another error occurs, else `true`.

FLAC__bool FLAC__metadata_simple_iterator_is_writable | ( | const FLAC__Metadata_SimpleIterator * | iterator | ) |

Returns `true` if the FLAC file is writable. If `false`, calls to FLAC__metadata_simple_iterator_set_block() and FLAC__metadata_simple_iterator_insert_block_after() will fail.

Parameters:

  | iterator | A pointer to an existing iterator.

Assertions:

       iterator != NULL

Return values:

  | FLAC__bool | See above.

FLAC__bool FLAC__metadata_simple_iterator_next | ( | FLAC__Metadata_SimpleIterator * | iterator | ) |

Moves the iterator forward one metadata block, returning `false` if already at the end.

Parameters:

  | iterator | A pointer to an existing initialized iterator.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

Return values:

  | FLAC__bool | `false` if already at the last metadata block of the chain, else `true`.

FLAC__bool FLAC__metadata_simple_iterator_prev | ( | FLAC__Metadata_SimpleIterator * | iterator | ) |

Moves the iterator backward one metadata block, returning `false` if already at the beginning.

Parameters:

  | iterator | A pointer to an existing initialized iterator.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

Return values:

  | FLAC__bool | `false` if already at the first metadata block of the chain, else `true`.

FLAC__bool FLAC__metadata_simple_iterator_is_last | ( | const FLAC__Metadata_SimpleIterator * | iterator | ) |

Returns a flag telling if the current metadata block is the last.

Parameters:

  | iterator | A pointer to an existing initialized iterator.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

Return values:

  | FLAC__bool | `true` if the current metadata block is the last in the file, else `false`.

off_t FLAC__metadata_simple_iterator_get_block_offset | ( | const FLAC__Metadata_SimpleIterator * | iterator | ) |

Get the offset of the metadata block at the current position. This avoids reading the actual block data which can save time for large blocks.

Parameters:

  | iterator | A pointer to an existing initialized iterator.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

Return values:

  | off_t | The offset of the metadata block at the current iterator position. This is the byte offset relative to the beginning of the file of the current metadata block's header.

FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type | ( | const FLAC__Metadata_SimpleIterator * | iterator | ) |

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.

Parameters:

  | iterator | A pointer to an existing initialized iterator.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

Return values:

  | FLAC__MetadataType | The type of the metadata block at the current iterator position.

unsigned FLAC__metadata_simple_iterator_get_block_length | ( | const FLAC__Metadata_SimpleIterator * | iterator | ) |

Get the length of the metadata block at the current position. This avoids reading the actual block data which can save time for large blocks.

Parameters:

  | iterator | A pointer to an existing initialized iterator.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

Return values:

  | unsigned | The length of the metadata block at the current iterator position. The is same length as that in the metadata block header, i.e. the length of the metadata body that follows the header.

FLAC__bool FLAC__metadata_simple_iterator_get_application_id | ( | FLAC__Metadata_SimpleIterator * | iterator,
|  | FLAC__byte * | id
| ) |

Get the application ID of the `APPLICATION` block at the current position. This avoids reading the actual block data which can save time for large blocks.

Parameters:

  | iterator | A pointer to an existing initialized iterator.
  | id | A pointer to a buffer of at least `4` bytes where the ID will be stored.

Assertions:

       iterator != NULL

       id != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

Return values:

  FLAC__bool

  `true` if the ID was successfully read, else `false`, in which case you should check FLAC__metadata_simple_iterator_status() to find out why. If the status is `FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT`, then the current metadata block is not an `APPLICATION` block. Otherwise if the status is `FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR` or `FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR`, an I/O error occurred and the iterator can no longer be used.

FLAC__StreamMetadata* FLAC__metadata_simple_iterator_get_block | ( | FLAC__Metadata_SimpleIterator * | iterator | ) |

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.

Parameters:

  | iterator | A pointer to an existing initialized iterator.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

Return values:

  | FLAC__StreamMetadata* | The current metadata block, or `NULL` if there was a memory allocation error.

FLAC__bool FLAC__metadata_simple_iterator_set_block | ( | FLAC__Metadata_SimpleIterator * | iterator,
|  | FLAC__StreamMetadata * | block,
|  | FLAC__bool | use_padding
| ) |

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 `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 `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 `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 `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 `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 `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.

Parameters:

  | iterator | A pointer to an existing initialized iterator.
  | block | The block to set.
  | use_padding | See above.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

       block != NULL

Return values:

  | FLAC__bool | `true` if successful, else `false`.

FLAC__bool FLAC__metadata_simple_iterator_insert_block_after | ( | FLAC__Metadata_SimpleIterator * | iterator,
|  | FLAC__StreamMetadata * | block,
|  | FLAC__bool | use_padding
| ) |

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. 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 `false` if given a STREAMINFO block.

After writing the block, the iterator will be pointing to the new block.

Parameters:

  | iterator | A pointer to an existing initialized iterator.
  | block | The block to set.
  | use_padding | See above.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

       block != NULL

Return values:

  | FLAC__bool | `true` if successful, else `false`.

FLAC__bool FLAC__metadata_simple_iterator_delete_block | ( | FLAC__Metadata_SimpleIterator * | iterator,
|  | FLAC__bool | use_padding
| ) |

Deletes the block at the current position. This will cause the entire FLAC file to be rewritten, unless use_padding is `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.

You may not delete the STREAMINFO block.

Parameters:

  | iterator | A pointer to an existing initialized iterator.
  | use_padding | See above.

Assertions:

       iterator != NULL

  iterator has been successfully initialized with FLAC__metadata_simple_iterator_init()

Return values:

  | FLAC__bool | `true` if successful, else `false`.

## Variable Documentation

const char* const FLAC__Metadata_SimpleIteratorStatusString[]

| Maps a FLAC__Metadata_SimpleIteratorStatus to a C string. Using a FLAC__Metadata_SimpleIteratorStatus as the index to this array will give the string equivalent. The contents should not be modified.

Copyright (c) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson |