Index: trunk/psLib/src/collections/psMetadata.c
===================================================================
--- trunk/psLib/src/collections/psMetadata.c	(revision 3769)
+++ trunk/psLib/src/collections/psMetadata.c	(revision 3779)
@@ -12,6 +12,6 @@
 *  @author Ross Harman, MHPCC
 *
-*  @version $Revision: 1.59 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2005-04-23 02:07:27 $
+*  @version $Revision: 1.60 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2005-04-28 02:45:12 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -39,32 +39,5 @@
 #include "psConstants.h"
 
-/******************************************************************************/
-/*  DEFINE STATEMENTS                                                         */
-/******************************************************************************/
-
-/** Maximum size of a string */
-#define MAX_STRING_LENGTH 1024
-
-/******************************************************************************/
-/*  TYPE DEFINITIONS                                                          */
-/******************************************************************************/
-
-// None
-
-/*****************************************************************************/
-/*  GLOBAL VARIABLES                                                         */
-/*****************************************************************************/
-
-// None
-
-/*****************************************************************************/
-/*  FILE STATIC VARIABLES                                                    */
-/*****************************************************************************/
-
 static psS32 metadataId = 0;
-
-/*****************************************************************************/
-/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
-/*****************************************************************************/
 
 static psMetadataItem* makeMetaMulti(psHash* table, const char* key, psMetadataItem* existing)
@@ -75,11 +48,11 @@
     }
 
-    // move any existing entry into a psList
-    psList* newList = psListAlloc(existing);
 
     psMetadataItem* item = psMetadataItemAlloc(key,
                            PS_META_MULTI,
-                           "",
-                           newList);
+                           "List of Metadata Items",
+                           NULL);
+
+    psListAdd(item->data.list,PS_LIST_TAIL,existing);
 
     if (existing != NULL) {
@@ -87,8 +60,7 @@
     }
 
-    psHashAdd(table, key, item); // put in the new entry
-
-    // free local references of newly allocated items.
-    psFree(newList);
+    psHashAdd(table, key, item); // put in the new MULTI list entry
+
+    // free local references of newly allocated item.
     psFree(item);
 
@@ -174,5 +146,6 @@
 {
     psMetadataItem* metadataItem = NULL;
-
+    char tmp;
+    int nBytes;
 
     PS_PTR_CHECK_NULL(name,NULL);
@@ -185,5 +158,5 @@
     psMemSetDeallocator(metadataItem, (psFreeFcn) metadataItemFree);
 
-    // Allocate and set metadata item comment
+    // set metadata item comment
     if (comment == NULL) {
         // Per SDRS, null isn't allowed, must use "" instead
@@ -200,5 +173,6 @@
 
     // Allocate and set metadata item name
-    metadataItem->name = (char *)psAlloc(sizeof(char) * MAX_STRING_LENGTH);
+    nBytes = vsnprintf(&tmp, 0, name, argPtr) + 1;
+    metadataItem->name = (char *)psAlloc(sizeof(char) * nBytes);
     vsprintf(metadataItem->name, name, argPtr);
 
@@ -219,5 +193,9 @@
     case PS_META_STR:
         // Perform copy of input strings
-        metadataItem->data.V = psStringNCopy(va_arg(argPtr, char *), MAX_STRING_LENGTH);
+        metadataItem->data.V = psStringCopy(va_arg(argPtr, char *));
+        break;
+    case PS_META_MULTI:
+        // MULTI needs to create a psList entry, value must be NULL
+        metadataItem->data.list = psListAlloc(NULL);
         break;
     case PS_META_LIST:
@@ -229,5 +207,4 @@
     case PS_META_ASTROM:
     case PS_META_UNKNOWN:
-    case PS_META_MULTI:
         // Copy of input data not performed due to variability of data types
         metadataItem->data.V = psMemIncrRefCounter(va_arg(argPtr, psPtr));
@@ -281,12 +258,19 @@
 
     // See if key is already in table
-    existingEntry = (psMetadataItem*)psHashLookup(mdTable, key);
-
+    existingEntry = psMetadataLookup(md, key);
+
+    // if replace is set, remove any existing items of the same key
+    if (existingEntry != NULL && (flags & PS_META_REPLACE) != 0) {
+        psMetadataRemove(md,0,key);
+        existingEntry = NULL;
+    }
+
+    // if the metadataItem is MULTI, just add the encapsulated entries
     if (metadataItem->type == PS_META_MULTI) {
-        // the incoming entry is PS_META_MULTI
-
-        // force the hash entry to be PS_META_MULTI
+
+        // make sure the existing entry is PS_META_MULTI
         existingEntry = makeMetaMulti(mdTable,key,existingEntry);
 
+        psBool status = true;
         // add all the items in the incoming entry to metadata
         psList* list = metadataItem->data.list;
@@ -294,53 +278,41 @@
             psListIterator* iter = psListIteratorAlloc(list,PS_LIST_HEAD,true);
             psMetadataItem* listItem;
-            while ((listItem=(psMetadataItem*)psListGetAndIncrement(iter)) != NULL) {
-                psMetadataAddItem(md,listItem,location,flags);
+            while (status && (listItem=(psMetadataItem*)psListGetAndIncrement(iter)) != NULL) {
+                status |= psMetadataAddItem(md,listItem,location,flags);
             }
             psFree(iter);
         }
 
-        return true; // all done.
+        return status; // all done.
     }
 
     // how the item is added to the hash depends on prior existence, flags, etc.
-    if(existingEntry != NULL) { // prior existence
+    if(existingEntry == NULL) { // no prior existence
+        // Node doesn't already exist - Add new metadata item to metadata collection's hash
+        if(!psHashAdd(mdTable, key, metadataItem)) {
+            psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_TABLE_FAILED,key);
+            return false;
+        }
+    } else {
         if (existingEntry->type == PS_META_MULTI || (flags & PS_META_DUPLICATE_OK) != 0) {
             // duplicate entries allowed - add another entry.
 
-            // make sure the existing entry is PS_META_MULTI
+            // make sure the existing hash entry is PS_META_MULTI
             existingEntry = makeMetaMulti(mdTable,key,existingEntry);
 
-            // add to the hash's list of duplicate entries
+            // add to the hash key's list of entries
             if (! psListAdd(existingEntry->data.list, PS_LIST_TAIL, metadataItem) ) {
                 psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_COLLECTION_FAILED,key);
                 return false;
             }
-        } else if ((flags & PS_META_REPLACE) != 0) {
-            // replace entry instead of creating a duplicate entry.
-
-            // remove the existing entry from metadata
-            psMetadataRemove(md,0,key);
-
-            // treat as if new (added to list below)
-            if(!psHashAdd(mdTable, key, metadataItem)) {
-                psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_TABLE_FAILED,key);
-                return false;
-            }
         } else {
-            // default is to error on duplicate entry.
+            // error on duplicate entry.
             psError(PS_ERR_BAD_PARAMETER_VALUE, true,
                     PS_ERRORTEXT_psMetadata_DUPLICATE_NOT_ALLOWED);
             return false;
         }
-    } else {
-        // OK, this is a new item.
-
-        // Node doesn't exist - Add new metadata item to metadata collection's hash
-        if(!psHashAdd(mdTable, key, metadataItem)) {
-            psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_TABLE_FAILED,key);
-            return false;
-        }
-    }
-
+    }
+
+    // finally, add the metadataItem to the metadata's list.
     if(!psListAdd(mdList, location, metadataItem)) {
         psError(PS_ERR_UNKNOWN,false,PS_ERRORTEXT_psMetadata_ADD_COLLECTION_FAILED,key);
