[ Previous | Next | Contents | Glossary | Home | Search ]
Ultimedia Services Version 2 for AIX: Programmer's Guide and Reference

UMSRiffReadWrite Object

The RIFF read/write object provides methods to access, read, write, and parse files in the RIFF format.

To learn more about the UMSRiffReadWrite object, see:

For introductory information, see Formatted File Access Objects.

RIFF Format

A RIFF file is composed of chunks. Each chunk has at least three parts. The first part is an ID string identifying the type of chunk. The second part is an integer giving the size of the data. The third part is the data followed by a pad byte if the size of the data is an odd number.

The ID is a 4-byte string of ASCII characters identifying the type of chunk. When this value is represented as an unsigned integer, it is called a FOURCC value.

The size is a 4-byte unsigned integer least-significant byte (LSB) first. The specified size does not include the four bytes of the ID, the four bytes of the size, or the pad byte.

The data part of the chunk, shown in the illustration, can be any size from 0 to 4,294,967,294 bytes. If the size is an odd number, a pad byte is added to round the real size to an even number. The size value does not include this pad byte.

Every RIFF file has at least one as the top-level chunk. The first four bytes of a LIST chunk's data specify the LIST's type. The rest of the data is used to hold zero or more chunks. These chunks are considered to be inside the LIST chunk and are called subchunks. The subchunks can also be LIST chunks containing other subchunks.

The list type, shown in the illustration, is a 4-byte string of ASCII characters. When the list type is represented as an unsigned integer, it is called a FOURCC value just like the chunk ID.

Enumeration Lists

To avoid name collision, integer values are defined in enumerated lists. The set of valid values from an enumerated list can vary with the application. The enumerated lists that are defined for the UMSRiffReadWrite class are listed below. The valid return values for each of the UMSRiffReadWrite methods are listed after the method's description on the following pages.

enum ReturnCode {
   Success, 
   Failure, 
   InvalidListOrRiff, 
   InvalidCreatePosition, 
   NoChunk, 
   NoSubChunk, 
   NotAtChunkStart, 
   FileNotOpen, 
   StorageAllocationFailure, 
   ReadFailure, 
   WriteFailure, 
   ReadOnly, 
   WriteOnly, 
   InvalidArgument, 
   EndOfListChunk, 
   FIOFailure, 
   BadInternalState, 
   FilePermissions 
   };

Type Definitions

The object uses the unistd.h file definition for the following:

SEEK_SET
SEEK_CUR
SEEK_END

Method Descriptions

ReturnCode ascend()

Description

Use the ascend function to indicate completion of reading or modifying the current chunk. This function moves the file pointer to the byte position after the end of the chunk, and sets the current chunk to the parent of the chunk from which you are ascending. If create_chunk was used to enter the chunk, this function also updates the chunk header to the real size of the data written.

Return Values
Success
Failure
NoChunk
FileNotOpen

ReturnCode close()

Description

This function flushes buffers and closes the file. Upon destruction, a UMSRiffReadWrite object automatically calls the close function if needed.

Return Values
Success
Failure
FileNotOpen

ReturnCode create_chunk(in unsigned long type, in unsigned long advisory_size, in unsigned long riff_or_list_type)

Description

This function creates a new chunk at the current file position. If the current file position is at the end of the file, the new chunk is appended; otherwise, the new chunk is inserted at that position. If the file system does not contain enough space for the temporary files, this method returns an error. The current file position after this call is the start of the data part of the newly created chunk.

Arguments
in unsigned long type Indicates the type of chunk to create.
in unsigned long advisory_size This is the expected size of the chunk. Providing an accurate value for this can improve performance when writing a RIFF file. In any case, ascent function sets the chunk's size in the chunk's header to the amount of data actually written into the chunk. The real size can be greater than, less than, or equal to the advisory_size.
in unsigned long riff_or_list_type This value indicates that either a RIFF or LIST chunk should be created. If type is RIFF or LIST and riff_or_list_type is 0, an error is returned and nothing is created.
Return Values
Success
Failure
InvalidCreatePosition
ReadOnly
FileNotOpen
WriteFailure
InvalidListOrRiff
StorageAllocationFailure
InvalidArgument

ReturnCode delete_chunk()

Description

This function:

  1. Deletes the current chunk (and any subchunks).
  2. Sets the current chunk to the parent of the deleted chunk.
  3. Moves the file pointer to the start of the deleted chunk.
  4. Moves the data after the deleted chunk to close the gap.
  5. Updates the parent chunks (if any) to reflect the new size.

This function uses a temporary file.

Return Values
Success
Failure
NoChunk
FileNotOpen

ReturnCode descend(in unsigned long type, in unsigned long riff_or_list_type)

Description

This function descends into a RIFF chunk. If type is 0, it descends into the chunk at the current file position. If type is nonzero, it searches for a chunk of that type starting at the current file position and ending with either having found the chunk or having reached the end of the current RIFF or LIST chunk. If type is RIFF or LIST and riff_or_list_type is nonzero, it searches for a RIFF or LIST chunk with the type riff_or_list_type. The current file position must be the start of a chunk at the time this function is called.

Arguments
in unsigned long type Indicates the type of chunk to be found. A 0 value indicates that the next chunk should be found no matter what type it is.
in unsigned long riff_or_list_type When type is the FOURCC value for RIFF or LIST, this is the type of RIFF or LIST to find. If this value is 0, the next RIFF or LIST chunk is found. If type is not RIFF or LIST, this value is ignored.
Return Values
Success
Failure
NoSubChunk
NoChunk
InvalidListOrRiff
NotAtChunkStart
FileNotOpen
WriteOnly
EndOfListChunk
ReadFailure
StorageAllocationFailure

ReturnCode flush()

Description

When writing to a file, flush flushes all buffers and updates chunk sizes (if needed) to match what has been written.

Return Values
Success
Failure
FileNotOpen
WriteFailure

long get_current_data_end()

Description

On success, the end address of the RIFF chunk is returned. A 0 value is returned if not successful.

Return Values

The end of data position for the current RIFF chunk.

long get_current_data_position()

Description

On success, the data position of the RIFF chunk is returned. A 0 value is returned if not successful.

Return Values

The start of data position for the current RIFF chunk.

unsigned long get_current_ID()

Description

On success, the ID of the RIFF chunk is returned. A 0 value is returned if not successful. This ID is a 4-byte unsigned integer that identifies the type of a chunk. For example a common value for the ID is 0x5453494c, which identifies a LIST chunk.

Use the string_to_FOURCC and FOURCC_to_string methods to convert to and from strings, such as LIST, and FOURCC chunk IDs, such as 0x5453494c.

Return Values

The riff_fourcc representation of the current chunk ID.

string get_current_ID_string(in string s)

Description

On success, the ID string is returned at the location pointed to by s. This method is similar to the get_current_ID method but returns a string instead of a FOURCC value.

Arguments
in string s This is a pointer to the returned ID string. This is always four characters plus a null. Your application must provide this space.
Return Values

The return value is either the pointer to the string s or null indicating that function was not successful.

unsigned long get_current_list_type()

Description

On success, the ID (type) of the current RIFF or LIST chunk is returned. A 0 value is returned if the function is not successful or if the current chunk is not a RIFF or LIST chunk. This ID is a 4-byte unsigned integer that identifies the type of LIST chunk. For example, a common value for the list type is 0x4f464e49, which identifies the chunk as an INFO list chunk. Use the string_to_FOURCC and FOURCC_to_string methods to convert to and from stings, such as INFO, and FOURCC values, such as 0x4f464e49.

Return Values

The FOURCC list type of the current RIFF chunk.

string get_current_list_type_string(in string s)

Description

On success, the list type string is returned to the location pointed to by the s parameter. This method is similar to the get_current_list_type method.

Arguments
in string s This is a pointer to the returned list type string. This is always four characters plus a null. Your application must provide this space.
Return Values

The return value is either the pointer to the string (same as ListType) or null if the function is not successful.

long get_current_seek_position()

Description

On success, the start of the RIFF chunk is returned. A 0 value is returned if the function is not successful.

Return Values

The start position for the current RIFF chunk or 0 if the function is not successful.

unsigned long get_current_size()

Description

On success, the size of the RIFF chunk in bytes is returned. A 0 value is returned if the function is not successful.

Return Values

The size in bytes of the current RIFF chunk or 0 if the function is not successful.

long lseek(in long offset, in long whence)

Description

This is similar to the lseek libc function call but should only be used to seek within the data part of the current chunk. Also, if the current chunk is a RIFF or LIST chunk, then lseek should only be used to seek to the start of a sub-chunk. Any other destination for the seek has undefined results.

Arguments
in long offset Depending on the value of the whence parameter, the offset is either the absolute position in bytes to seek to in the file (whence = SEEK_SET), the distance in bytes to move forward or backward from the current position (whence = SEEK_CUR), or the distance in bytes to move forward or backward from the end of the file (whence = SEEK_END).
in long whence This is one three possible whence values defined in unistd.h include file:
SEEK_SET Sets the file pointer to the specified offset.
SEEK_CUR Sets the file pointer to the current file parameter's value plus the specified offset.
SEEK_END Sets the file pointer to end of file (EOF) plus the specified offset.
Return Values

The return value is the new absolute position in the file. A return value of -1 indicates an error.

long read(in void *buf, in long nbyte)

Description

This is similar to the read libc function call, but it only operates within the data part of the current RIFF chunk. The logical position within the RIFF chunk is modified accordingly.

Arguments
in void *buf This is a pointer to a buffer that holds the data to be read. It must be at least nbytes in size.
in long nbyte This is the number of bytes of data to be read.
Return Values

The return value is the actual number of bytes read. A return value of -1 indicates an error; a return value of 0 indicates that the end of the RIFF data chunk or end of the file has been reached.

long write(in void *buf, in long nbyte)

Description

Similar to the write libc function call. It only operates within the data part of the current RIFF chunk. If data is written past the end of the current chunk, then the chunk is extended. The operation of this method is affected by the set_insert and set_overwrite methods.

Arguments
in void *buf A pointer to the data to be written.
in long nbyte The number of bytes of data to be written.
Return Values

The return value is the number of bytes written. A return value of -1 indicates an error.

ReturnCode open(in string path, in long oflag, in long filemode)

Description

This function opens a RIFF file for reading or writing.

Arguments
in string path The path name of the RIFF file to be opened.
in long oflag The bitwise-OR of the flags from the fcntl.h file used the same way they are used for the libc open function.
in long filemode When building a new file, the filemode parameter specifies the new file's permissions modes. This value is ignored if the file already exists.
Return Values
Success 
Failure
FilePermissions

ReturnCode set_insert()

Description

After your application calls the set_insert function, data written to a chunk is inserted at the current file location. This is the default. For example, when N bytes are written to a chunk, everything from the current file location to the end of the file is moved N bytes and the new data is inserted in the gap. This mode increases the size of the chunk by N bytes. This method does not affect the operation of the create_chunk method.

Return Values
Success
Failure
FileNotOpen 
WriteFailure

ReturnCode set_overwrite()

Description

After your application calls the set_overwrite function, data written to a chunk overwrites the data at the current file location. For example, when N bytes are written to a chunk, the next N bytes of data in the file are overwritten by the new data.

However, if there are only N2 bytes left in the chunk, where N2 is less than N, N2 bytes are overwritten, and (N-N2) bytes is inserted. This increases the size of the chunk by (N-N2) bytes.

This method does not affect the operation of the create_chunk method.

Return Values
Success
Failure
FileNotOpen 
WriteFailure

ReturnCode set_tmp(in string *dpath)

Description

Use the set_tmp function to specify the directory in which the UMSRiffReadWrite object should write temporary files. If this function is not called, the /tmp directory is used by default.

Arguments
in string *dpath Name of the directory to be used for temporary files. This value can be null. If it is null, the /tmp directory is used.
Return Values
Success
Failure
InvalidArgument
FileNotOpen
StorageAllocationFailure

ReturnCode statusok()

Description

This function checks the status of the UMSRiffReadWrite object. If the object has not opened a file, this method returns FileNotOpen. If the file has been corrupted in some way, this method returns Failure. This method enables your code to check the status of a UMSRiffReadWrite object to see if the object is usable.

Return Values
Success
Failure
FileNotOpen

unsigned long string_to_FOURCC(in string s)

Description

Converts a string name into a FOURCC for use with chunk headers and RIFF or LIST types. If the string is longer than four characters, it is truncated to the first (leftmost) four. If the string is shorter than four characters, it is padded with spaces to four. If the length is 0 or if s is a null pointer, a 0 value is returned.

Arguments
in string s The string to be converted to a FOURCC.
Return Values

The return value is the FOURCC value. A 0 value indicates an error.

ssize_t write(void *buf, size_t nbyte)

Description

This is similar to the write libc function call. It only operates within the data part of the current RIFF chunk. If data is written past the end of the current chunk, the chunk is extended. The operation of this method is affected by the set_insert and set_overwrite methods.

Arguments
void * buf This is a pointer to the data to be written.
size_t nbyte This is the number of bytes of data to be written.
Return Values

The return value is the number of bytes written. A return value of -1 indicates an error.

string FOURCC_to_string(in unsigned long v, in string s)

Description

This converts a FOURCC value into a string. The string representation of the value v is returned at location s. The string is always four bytes long with one null terminator byte. The string includes any spaces needed to pad the type to four bytes when it was created.

Arguments
in unsigned long v This is the FOURCC value to be converted to a string.
in string s This is a pointer to the memory location to return the string representation of the value v.
Return Values

If successful a pointer to the string location is returned. A null pointer is returned on failure.

For introductory information, see Formatted File Access Objects.


[ Previous | Next | Contents | Glossary | Home | Search ]