
/** @file  psMetadata.h
*
*  @brief Contains metadata struuctures, enumerations and functions prototypes
*
*  This file defines metadata item, metadata type, metadata flags, metadata containers, and function
*  prototypes necessary creating psLib metadata APIs
*
*  @ingroup Metadata
*
*  @author Ross Harman, MHPCC
*
*  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
*  @date $Date: 2005-01-17 20:58:20 $
*
*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
*/
#ifndef PS_METADATA_H
#define PS_METADATA_H

#include <stdarg.h>
#include <stdio.h>

#include "psHash.h"
#include "psList.h"

/// @addtogroup Metadata
/// @{

/** Metadata item type.
 *
 * Enumeration for maintaining metadata item types.
 */
typedef enum {
    PS_META_PRIMITIVE,                 ///< Primitive data.
    PS_META_LIST,                      ///< List data (Stored as item.data.list).
    PS_META_STR,                       ///< String data (Stored as item.data.V).
    PS_META_VEC,                       ///< Vector data (Stored as item.data.V).
    PS_META_IMG,                       ///< Image data (Stored as item.data.V).
    PS_META_HASH,                      ///< Hash data (Stored as item.data.V).
    PS_META_LOOKUPTABLE,               ///< Lookup table data (Stored as item.data.V).
    PS_META_JPEG,                      ///< JPEG data (Stored as item.data.V).
    PS_META_PNG,                       ///< PNG data (Stored as item.data.V).
    PS_META_ASTROM,                    ///< Astrometric coefficients (Stored as item.data.V).
    PS_META_UNKNOWN,                   ///< Other data (Stored as item.data.V).
    PS_META_NTYPE                      ///< Number of types. Must be last.
} psMetadataType;

/** Metadata data structure.
 *
 *  Struct for holding metadata items. Metadata items are held in two
 *  containers. The first employs a doubly-linked list to preserve the order
 *  of the metadata. The second container employs a hash table which
 *  allows fast lookup when given a metadata keyword.
 */
typedef struct psMetadata
{
    psList*  list;                     ///< Metadata in linked-list
    psHash*  table;                    ///< Metadata in a hash table
}
psMetadata;

/** Metadata item data structure.
 *
 * Struct for maintaining metadata items of varying types. It also contains
 * information about the item name, flags, comments, and other items with the same name.
 */
typedef struct psMetadataItem
{
    const psS32 id;                    ///< Unique ID for metadata item.
    char *name;                        ///< Name of metadata item.
    psMetadataType type;               ///< Type of metadata item.
    psElemType pType;                  ///< Primitive data type
    union {
        psBool B;                      ///< boolean data
        psS32 S32;                     ///< Signed 32-bit integer data.
        psF32 F32;                     ///< Single-precision float data.
        psF64 F64;                     ///< Double-precision float data.
        psList *list;                  ///< List data.
        psMetadata *md;                ///< Metadata data.
        psPtr V;                       ///< Pointer to other type of data.
    } data;                            ///< Union for data types.
    char *comment;                     ///< Optional comment ("", not NULL).
}
psMetadataItem;

/** Create a metadata item.
 *
 *  Returns a fill psMetadataItem ready for insertion into the psMetadata
 *  struct. The name argument specifies the name to use for this item, and
 *  may include sprintf formatting codes. The format entry specifies both
 *  the metadata type and optional flags and is created by bit-wise or of the
 *  appropriate type and flag. The comment argument is a fixed string used to
 *  comment the metadata item. The arguments to the name formatting codes and
 *  the metadata itself are passed as arguments following the comment string.
 *  The data must be a pointer for any of the elements stored in data.void.
 *  The argument list must be interpreted appropriately by the va_list
 *  operators in the function specified size and type.
 *
 * @return psMetadataItem* : Pointer metadata item.
 */
psMetadataItem* psMetadataItemAlloc(
    const char *name,                  ///< Name of metadata item.
    psElemType pType,                  ///< Primitive type.
    psMetadataType type,               ///< Type of metadata item.
    const char *comment,               ///< Comment for metadata item.
    ...                                ///< Arguments for name formatting and metadata item data.
);

/** Create a metadata item with va_list.
 *
 *  Returns a fill psMetadataItem ready for insertion into the psMetadata
 *  struct. The name argument specifies the name to use for this item, and
 *  may include sprintf formatting codes. The format entry specifies both
 *  the metadata type and optional flags and is created by bit-wise or of the
 *  appropriate type and flag. The comment argument is a fixed string used to
 *  comment the metadata item. The arguments to the name formatting codes and
 *  the metadata itself are passed as arguments following the comment string.
 *  The data must be a pointer for any of the elements stored in data.void.
 *  The argument list must be interpreted appropriately by the va_list
 *  operators in the function specified size and type.
 *
 * @return psMetadataItem* : Pointer metadata item.
 */
psMetadataItem* psMetadataItemAllocV(
    const char *name,                  ///< Name of metadata item.
    psElemType pType,                  ///< Primitive type.
    psMetadataType type,               ///< Type of metadata item.
    const char *comment,               ///< Comment for metadata item.
    va_list list                       ///< Arguments for name formatting and metadata item data.
);

/** Create a metadata collection.
 *
 *  Returns an empty metadata container with fully allocated internal metadata
 *  containers.
 *
 *  @return psMetadata* : Pointer metadata.
 */
psMetadata* psMetadataAlloc(void);

/** Add existing metadata item to metadata collection.
 *
 *  Add a metadata item that has already been created to the metadata
 *  collection.
 *
 *  @return bool: True for success, false for failure.
 */
psBool psMetadataAddItem(
    psMetadata*  md,          ///< Metadata collection to insert metadat item.
    psMetadataItem*  item,    ///< Metadata item to be added.
    psS32 location            ///< Location to be added.
);

/** Create and add a metadata item to metadata collection.
 *
 * Creates a new metadata item add to the metadata collection.
 *
 * @return bool: True for success, false for failure.
 */
psBool psMetadataAdd(
    psMetadata* md,           ///< Metadata collection to insert metadat item.
    psS32 where,              ///< Location to be added.
    const char *name,         ///< Name of metadata item.
    psElemType pType,         ///< Primitive type.
    psMetadataType type,      ///< Type of metadata item.
    const char *comment,      ///< Comment for metadata item.
    ...                       ///< Arguments for name formatting and metadata item data.
);

/** Remove an item from metadata collection.
 *
 *  Items may be removed from metadata by specifing a key or location. If the
 *  name is null, the where argument is used instead. If name is not null,
 *  where is set to PS_LIST_UNKNOWN. If the item is found, it is removed from
 *  the metadata and true is returned.  If the key is not unique, then all
 *  items corresponding to it are removed.
 *
 * @return bool: True for success, false for failure.
 */
psBool psMetadataRemove(
    psMetadata*  md,           ///< Metadata collection to remove metadata item.
    psS32 where,               ///< Location to be removed.
    const char * key           ///< Name of metadata key.
);

/** Find an item in the metadata collection based on key name.
 *
 *  Items may be found in the metadata by providing a key. If the key is
 *  non-unique, the first item is returned. If the item is not found, null is
 *  returned.
 *
 * @return psMetadataItem* : Pointer metadata item.
 */
psMetadataItem* psMetadataLookup(
    psMetadata * md,           ///< Metadata collection to lookup metadata item.
    const char * key           ///< Name of metadata key.
);

/** Find an item in the metadata collection based on key name and return its double precision value.
 *
 *  Items may be found in the metadata by providing a key. If the key is
 *  non-unique, the value of the first item is returned. If the item is not found, zero is
 *  returned.
 *
 * @return psF64 : Value of metadata item.
 */
psF64 psMetadataLookupF64(
    psMetadata *md,            ///< Metadata collection to lookup metadata item.
    const char *key,           ///< Name of metadata key.
    psBool *status              ///< Status of lookup.
);

/** Find an item in the metadata collection based on key name and return its single precision value.
 *
 *  Items may be found in the metadata by providing a key. If the key is
 *  non-unique, the value of the first item is returned. If the item is not found, zero is
 *  returned.
 *
 * @return psF32 : Value of metadata item.
 */
psF32 psMetadataLookupF32(
    psMetadata *md,            ///< Metadata collection to lookup metadata item.
    const char *key,           ///< Name of metadata key.
    psBool *status              ///< Status of lookup.
);

/** Find an item in the metadata collection based on key name and return its integer value.
 *
 *  Items may be found in the metadata by providing a key. If the key is
 *  non-unique, the value of the first item is returned. If the item is not found, zero is
 *  returned.
 *
 * @return psS32 : Value of metadata item.
 */
psS32 psMetadataLookupS32(
    psMetadata *md,            ///< Metadata collection to lookup metadata item.
    const char *key,           ///< Name of metadata key.
    psBool *status              ///< Status of lookup.
);

/** Find an item in the metadata collection based on key name and return its boolean value.
 *
 *  Items may be found in the metadata by providing a key. If the key is
 *  non-unique, the value of the first item is returned. If the item is not found, zero is
 *  returned.
 *
 * @return psBool : Value of metadata item.
 */
psBool psMetadataLookupBool(
    psMetadata *md,            ///< Metadata collection to lookup metadata item.
    const char *key,           ///< Name of metadata key.
    psBool *status              ///< Status of lookup.
);

/** Find an item in the metadata collection based on key name and return its integer value.
 *
 *  Items may be found in the metadata by providing a key. If the key is
 *  non-unique, the value of the first item is returned. If the item is not found, zero is
 *  returned.
 *
 * @return void* : Value of metadata item.
 */
void* psMetadataLookupPtr(
    psMetadata* md,            ///< Metadata collection to lookup metadata item.
    const char *key,           ///< Name of metadata key.
    psBool *status              ///< Status of lookup.
);

/** Find an item in the metadata collection based on list index.
 *
 *  Items may be found in the metadata by their entry position in the list
 *  container.
 *
 *  @return psMetadataItem* : Pointer metadata item.
 */
psMetadataItem* psMetadataGet(
    psMetadata*  md,           ///< Metadata collection to insert metadat item.
    psS32 where                ///< Location to be retrieved.
);

/// @}

#endif
