IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 1458


Ignore:
Timestamp:
Aug 10, 2004, 1:32:51 PM (22 years ago)
Author:
harman
Message:

Modified addItem function

Location:
trunk/psLib/src
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/astronomy/psMetadata.c

    r1447 r1458  
    1212*  @author Ross Harman, MHPCC
    1313*
    14 *  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
    15 *  @date $Date: 2004-08-10 01:43:05 $
     14*  @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
     15*  @date $Date: 2004-08-10 23:32:51 $
    1616*
    1717*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919
    2020/******************************************************************************/
    21 
    2221/*  INCLUDE FILES                                                             */
    23 
    2422/******************************************************************************/
    2523#include<stdio.h>
     
    3836
    3937/******************************************************************************/
    40 
    4138/*  DEFINE STATEMENTS                                                         */
    42 
    4339/******************************************************************************/
    4440
     
    6056
    6157/******************************************************************************/
    62 
    6358/*  TYPE DEFINITIONS                                                          */
    64 
    6559/******************************************************************************/
    6660
     
    6862
    6963/*****************************************************************************/
    70 
    7164/*  GLOBAL VARIABLES                                                         */
    72 
    7365/*****************************************************************************/
    7466
     
    7668
    7769/*****************************************************************************/
    78 
    7970/*  FILE STATIC VARIABLES                                                    */
    80 
    8171/*****************************************************************************/
    8272
     
    8474
    8575/*****************************************************************************/
    86 
    8776/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    88 
    8977/*****************************************************************************/
     78
    9079static void metadataItemFree(psMetadataItem* metadataItem)
    9180{
     
    119108
    120109/*****************************************************************************/
    121 
    122110/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    123 
    124111/*****************************************************************************/
    125112
     
    175162    // Set metadata item value
    176163    switch (type) {
     164    case PS_META_ITEM_SET:
     165        metadataItem->data.V = NULL;
     166        break;
    177167    case PS_META_BOOL:
    178         metadataItem->data.B = (bool) va_arg(argPtr, int);
    179 
     168        metadataItem->data.B = (bool)va_arg(argPtr, int);
    180169        break;
    181170    case PS_META_S32:
     
    183172        break;
    184173    case PS_META_F32:
    185         metadataItem->data.F32 = (psF32) va_arg(argPtr, psF64);
     174        metadataItem->data.F32 = (psF32)va_arg(argPtr, psF64);
    186175        break;
    187176    case PS_META_F64:
     
    190179    case PS_META_STR:
    191180        metadataItem->data.V = psStringNCopy(va_arg(argPtr, char *), MAX_STRING_LENGTH);
    192 
    193181        break;
    194182    case PS_META_IMG:
     
    242230}
    243231
    244 bool psMetadataAddItem(psMetadata* restrict md, int where, psMetadataItem* restrict metadataItem)
    245 {
    246     char *key = NULL;
    247     psHash* mdTable = NULL;
    248     psList* mdList = NULL;
    249     psMetadataItem* value = NULL;
    250     psMetadataType type = PS_META_ITEM_SET;
    251 
    252     if (md == NULL) {
    253         psError(__func__, "Null metadata collection not allowed");
    254         return false;
    255     }
    256 
    257     if (metadataItem == NULL) {
    258         psError(__func__, "Null metadata item not allowed");
    259         return false;
    260     }
    261 
    262     type = metadataItem->type;
     232bool psMetadataAddItem( psMetadata *restrict md, int where, psMetadataItem *restrict metadataItem )
     233{
     234    char * key = NULL;
     235    psHash *mdTable = NULL;
     236    psList *mdList = NULL;
     237    psMetadataItem *existingEntry = NULL;
     238    psMetadataItem *newFolderEntry = NULL;
     239    psMetadataType newType = PS_META_ITEM_SET;
     240
     241    if(md == NULL) {
     242        psError( __func__, "Null metadata collection not allowed" );
     243        return false;
     244    }
     245
     246    if(metadataItem == NULL) {
     247        psError( __func__, "Null metadata item not allowed" );
     248        return false;
     249    }
     250
     251    newType = metadataItem->type;
    263252
    264253    mdTable = md->table;
    265     if (mdTable == NULL) {
    266         psError(__func__, "Null metadata table not allowed");
     254    if(mdTable == NULL) {
     255        psError( __func__, "Null metadata table not allowed" );
    267256        return false;
    268257    }
    269258
    270259    mdList = md->list;
    271     if (mdList == NULL) {
    272         psError(__func__, "Null metadata list not allowed");
     260    if(mdList == NULL) {
     261        psError( __func__, "Null metadata list not allowed");
    273262        return false;
    274263    }
    275264
    276265    key = metadataItem->name;
    277     if (key == NULL) {
     266    if(key == NULL) {
    278267        psError(__func__, "Null key item not allowed");
    279268        return false;
    280269    }
    281     // Check if key is already in table
    282     value = (psMetadataItem* ) psHashLookup(mdTable, key);
    283     if (value != NULL && type != PS_META_ITEM_SET) {
    284 
    285         // The key was found and the new metadata item is a leaf node (its type isn't PS_META_ITEM_SET), so
    286         // add the new metadata item to hash as a child of the existing metadata item folder node.
    287         if (!psListAdd(value->items, metadataItem, where)) {
    288             psError(__func__, "Couldn't add metadata item to items list. Name: %s", metadataItem->name);
     270
     271    // See if key is already in table
     272    existingEntry = (psMetadataItem*)psHashLookup(mdTable, key);
     273    if(existingEntry != NULL) {
     274
     275        if(existingEntry->type == PS_META_ITEM_SET) {
     276
     277            // Add leaf node to existing folder node
     278            if(!psListAdd(existingEntry->items, metadataItem, PS_LIST_TAIL)) {
     279                psError( __func__, "Couldn't add metadata item to items list. Name: %s",
     280                         metadataItem->name );
     281                return false;
     282            }
     283        } else if(existingEntry->type != PS_META_ITEM_SET && newType!= PS_META_ITEM_SET) {
     284
     285            // Leaf node replaces another leaf. Make new folder node and add leaves.
     286            newFolderEntry = psMetadataItemAlloc(key, PS_META_ITEM_SET, NULL, NULL);
     287
     288            if(!psListAdd(newFolderEntry->items, existingEntry, PS_LIST_TAIL)) {
     289                psError( __func__, "Couldn't add metadata item to items list. Name: %s", key);
     290                psFree(newFolderEntry);
     291                return false;
     292            }
     293
     294            if(!psListAdd(newFolderEntry->items, metadataItem, PS_LIST_TAIL)) {
     295                psError( __func__, "Couldn't add metadata item to items list. Name: %s", key);
     296                psFree(newFolderEntry);
     297                return false;
     298            }
     299
     300            if(!psHashRemove(mdTable, key)) {
     301                psError( __func__, "Couldn't remove metadata item from metadata table. Name: %s", key);
     302                psFree(newFolderEntry);
     303                return false;
     304            }
     305
     306            if(!psHashAdd(mdTable, key, newFolderEntry)) {
     307                psError( __func__, "Couldn't add metadata item to metadata table. Name: %s", key);
     308                psFree(newFolderEntry);
     309                return false;
     310            }
     311
     312            // Remove local reference to new folder node
     313            psMemDecrRefCounter(newFolderEntry);
     314        } else {
     315
     316            // Folder node replaces leaf or folder node - Put old node into new folder and remove from table
     317            if(!psListAdd(metadataItem->items, existingEntry, PS_LIST_TAIL)) {
     318                psError( __func__, "Couldn't add metadata item to items list. Name: %s", key);
     319                return false;
     320            }
     321
     322            if(!psHashRemove(mdTable, key)) {
     323                psError( __func__, "Couldn't remove metadata item from metadata table. Name: %s", key);
     324                return false;
     325            }
     326
     327            if(!psHashAdd(mdTable, key, metadataItem)) {
     328                psError( __func__, "Couldn't add metadata item to metadata table. Name: %s", key);
     329                return false;
     330            }
     331        }
     332    } else {
     333
     334        // Node doesn't exist - Add new metadata item to metadata collection's hash
     335        if(!psHashAdd(mdTable, key, metadataItem)) {
     336            psError( __func__, "Couldn't add metadata item to metadata table. Name: %s", key);
    289337            return false;
    290338        }
    291     } else if (value != NULL) {
    292 
    293         // The key was found and the new metadata item is a folder node. Don't add new metadata item, since
    294         // it will wipe out existing node.
    295         psError(__func__, "Metadata already exists in metadata collection. Item not added. Name: %s",
    296                 metadataItem->name);
    297         return false;
    298     } else {
    299 
    300         // Duplicate key not found. Add new metadata item to metadata collection's hash
    301         if (!psHashAdd(mdTable, key, metadataItem)) {
    302             psError(__func__, "Couldn't add metadata item to metadata collection table. Name: %s",
    303                     metadataItem->name);
    304             return false;
    305         }
    306339    }
    307340
    308341    // Add all items to metadata collection's list, even if they have the same metadata item names
    309     if (!psListAdd(md->list, metadataItem, where)) {
    310         psError(__func__, "Couldn't add metadata item to metadata collection list. Name: %s",
    311                 metadataItem->name);
     342    if(!psListAdd( mdList, metadataItem, where )) {
     343        psError( __func__, "Couldn't add metadata item to metadata collection list. Name: %s",
     344                 metadataItem->name );
    312345        return false;
    313346    }
     
    338371    return true;
    339372}
     373
     374
    340375
    341376bool psMetadataRemove(psMetadata* restrict md, int where, const char *restrict key)
  • trunk/psLib/src/collections/psMetadata.c

    r1447 r1458  
    1212*  @author Ross Harman, MHPCC
    1313*
    14 *  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
    15 *  @date $Date: 2004-08-10 01:43:05 $
     14*  @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
     15*  @date $Date: 2004-08-10 23:32:51 $
    1616*
    1717*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919
    2020/******************************************************************************/
    21 
    2221/*  INCLUDE FILES                                                             */
    23 
    2422/******************************************************************************/
    2523#include<stdio.h>
     
    3836
    3937/******************************************************************************/
    40 
    4138/*  DEFINE STATEMENTS                                                         */
    42 
    4339/******************************************************************************/
    4440
     
    6056
    6157/******************************************************************************/
    62 
    6358/*  TYPE DEFINITIONS                                                          */
    64 
    6559/******************************************************************************/
    6660
     
    6862
    6963/*****************************************************************************/
    70 
    7164/*  GLOBAL VARIABLES                                                         */
    72 
    7365/*****************************************************************************/
    7466
     
    7668
    7769/*****************************************************************************/
    78 
    7970/*  FILE STATIC VARIABLES                                                    */
    80 
    8171/*****************************************************************************/
    8272
     
    8474
    8575/*****************************************************************************/
    86 
    8776/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    88 
    8977/*****************************************************************************/
     78
    9079static void metadataItemFree(psMetadataItem* metadataItem)
    9180{
     
    119108
    120109/*****************************************************************************/
    121 
    122110/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    123 
    124111/*****************************************************************************/
    125112
     
    175162    // Set metadata item value
    176163    switch (type) {
     164    case PS_META_ITEM_SET:
     165        metadataItem->data.V = NULL;
     166        break;
    177167    case PS_META_BOOL:
    178         metadataItem->data.B = (bool) va_arg(argPtr, int);
    179 
     168        metadataItem->data.B = (bool)va_arg(argPtr, int);
    180169        break;
    181170    case PS_META_S32:
     
    183172        break;
    184173    case PS_META_F32:
    185         metadataItem->data.F32 = (psF32) va_arg(argPtr, psF64);
     174        metadataItem->data.F32 = (psF32)va_arg(argPtr, psF64);
    186175        break;
    187176    case PS_META_F64:
     
    190179    case PS_META_STR:
    191180        metadataItem->data.V = psStringNCopy(va_arg(argPtr, char *), MAX_STRING_LENGTH);
    192 
    193181        break;
    194182    case PS_META_IMG:
     
    242230}
    243231
    244 bool psMetadataAddItem(psMetadata* restrict md, int where, psMetadataItem* restrict metadataItem)
    245 {
    246     char *key = NULL;
    247     psHash* mdTable = NULL;
    248     psList* mdList = NULL;
    249     psMetadataItem* value = NULL;
    250     psMetadataType type = PS_META_ITEM_SET;
    251 
    252     if (md == NULL) {
    253         psError(__func__, "Null metadata collection not allowed");
    254         return false;
    255     }
    256 
    257     if (metadataItem == NULL) {
    258         psError(__func__, "Null metadata item not allowed");
    259         return false;
    260     }
    261 
    262     type = metadataItem->type;
     232bool psMetadataAddItem( psMetadata *restrict md, int where, psMetadataItem *restrict metadataItem )
     233{
     234    char * key = NULL;
     235    psHash *mdTable = NULL;
     236    psList *mdList = NULL;
     237    psMetadataItem *existingEntry = NULL;
     238    psMetadataItem *newFolderEntry = NULL;
     239    psMetadataType newType = PS_META_ITEM_SET;
     240
     241    if(md == NULL) {
     242        psError( __func__, "Null metadata collection not allowed" );
     243        return false;
     244    }
     245
     246    if(metadataItem == NULL) {
     247        psError( __func__, "Null metadata item not allowed" );
     248        return false;
     249    }
     250
     251    newType = metadataItem->type;
    263252
    264253    mdTable = md->table;
    265     if (mdTable == NULL) {
    266         psError(__func__, "Null metadata table not allowed");
     254    if(mdTable == NULL) {
     255        psError( __func__, "Null metadata table not allowed" );
    267256        return false;
    268257    }
    269258
    270259    mdList = md->list;
    271     if (mdList == NULL) {
    272         psError(__func__, "Null metadata list not allowed");
     260    if(mdList == NULL) {
     261        psError( __func__, "Null metadata list not allowed");
    273262        return false;
    274263    }
    275264
    276265    key = metadataItem->name;
    277     if (key == NULL) {
     266    if(key == NULL) {
    278267        psError(__func__, "Null key item not allowed");
    279268        return false;
    280269    }
    281     // Check if key is already in table
    282     value = (psMetadataItem* ) psHashLookup(mdTable, key);
    283     if (value != NULL && type != PS_META_ITEM_SET) {
    284 
    285         // The key was found and the new metadata item is a leaf node (its type isn't PS_META_ITEM_SET), so
    286         // add the new metadata item to hash as a child of the existing metadata item folder node.
    287         if (!psListAdd(value->items, metadataItem, where)) {
    288             psError(__func__, "Couldn't add metadata item to items list. Name: %s", metadataItem->name);
     270
     271    // See if key is already in table
     272    existingEntry = (psMetadataItem*)psHashLookup(mdTable, key);
     273    if(existingEntry != NULL) {
     274
     275        if(existingEntry->type == PS_META_ITEM_SET) {
     276
     277            // Add leaf node to existing folder node
     278            if(!psListAdd(existingEntry->items, metadataItem, PS_LIST_TAIL)) {
     279                psError( __func__, "Couldn't add metadata item to items list. Name: %s",
     280                         metadataItem->name );
     281                return false;
     282            }
     283        } else if(existingEntry->type != PS_META_ITEM_SET && newType!= PS_META_ITEM_SET) {
     284
     285            // Leaf node replaces another leaf. Make new folder node and add leaves.
     286            newFolderEntry = psMetadataItemAlloc(key, PS_META_ITEM_SET, NULL, NULL);
     287
     288            if(!psListAdd(newFolderEntry->items, existingEntry, PS_LIST_TAIL)) {
     289                psError( __func__, "Couldn't add metadata item to items list. Name: %s", key);
     290                psFree(newFolderEntry);
     291                return false;
     292            }
     293
     294            if(!psListAdd(newFolderEntry->items, metadataItem, PS_LIST_TAIL)) {
     295                psError( __func__, "Couldn't add metadata item to items list. Name: %s", key);
     296                psFree(newFolderEntry);
     297                return false;
     298            }
     299
     300            if(!psHashRemove(mdTable, key)) {
     301                psError( __func__, "Couldn't remove metadata item from metadata table. Name: %s", key);
     302                psFree(newFolderEntry);
     303                return false;
     304            }
     305
     306            if(!psHashAdd(mdTable, key, newFolderEntry)) {
     307                psError( __func__, "Couldn't add metadata item to metadata table. Name: %s", key);
     308                psFree(newFolderEntry);
     309                return false;
     310            }
     311
     312            // Remove local reference to new folder node
     313            psMemDecrRefCounter(newFolderEntry);
     314        } else {
     315
     316            // Folder node replaces leaf or folder node - Put old node into new folder and remove from table
     317            if(!psListAdd(metadataItem->items, existingEntry, PS_LIST_TAIL)) {
     318                psError( __func__, "Couldn't add metadata item to items list. Name: %s", key);
     319                return false;
     320            }
     321
     322            if(!psHashRemove(mdTable, key)) {
     323                psError( __func__, "Couldn't remove metadata item from metadata table. Name: %s", key);
     324                return false;
     325            }
     326
     327            if(!psHashAdd(mdTable, key, metadataItem)) {
     328                psError( __func__, "Couldn't add metadata item to metadata table. Name: %s", key);
     329                return false;
     330            }
     331        }
     332    } else {
     333
     334        // Node doesn't exist - Add new metadata item to metadata collection's hash
     335        if(!psHashAdd(mdTable, key, metadataItem)) {
     336            psError( __func__, "Couldn't add metadata item to metadata table. Name: %s", key);
    289337            return false;
    290338        }
    291     } else if (value != NULL) {
    292 
    293         // The key was found and the new metadata item is a folder node. Don't add new metadata item, since
    294         // it will wipe out existing node.
    295         psError(__func__, "Metadata already exists in metadata collection. Item not added. Name: %s",
    296                 metadataItem->name);
    297         return false;
    298     } else {
    299 
    300         // Duplicate key not found. Add new metadata item to metadata collection's hash
    301         if (!psHashAdd(mdTable, key, metadataItem)) {
    302             psError(__func__, "Couldn't add metadata item to metadata collection table. Name: %s",
    303                     metadataItem->name);
    304             return false;
    305         }
    306339    }
    307340
    308341    // Add all items to metadata collection's list, even if they have the same metadata item names
    309     if (!psListAdd(md->list, metadataItem, where)) {
    310         psError(__func__, "Couldn't add metadata item to metadata collection list. Name: %s",
    311                 metadataItem->name);
     342    if(!psListAdd( mdList, metadataItem, where )) {
     343        psError( __func__, "Couldn't add metadata item to metadata collection list. Name: %s",
     344                 metadataItem->name );
    312345        return false;
    313346    }
     
    338371    return true;
    339372}
     373
     374
    340375
    341376bool psMetadataRemove(psMetadata* restrict md, int where, const char *restrict key)
  • trunk/psLib/src/types/psMetadata.c

    r1447 r1458  
    1212*  @author Ross Harman, MHPCC
    1313*
    14 *  @version $Revision: 1.21 $ $Name: not supported by cvs2svn $
    15 *  @date $Date: 2004-08-10 01:43:05 $
     14*  @version $Revision: 1.22 $ $Name: not supported by cvs2svn $
     15*  @date $Date: 2004-08-10 23:32:51 $
    1616*
    1717*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919
    2020/******************************************************************************/
    21 
    2221/*  INCLUDE FILES                                                             */
    23 
    2422/******************************************************************************/
    2523#include<stdio.h>
     
    3836
    3937/******************************************************************************/
    40 
    4138/*  DEFINE STATEMENTS                                                         */
    42 
    4339/******************************************************************************/
    4440
     
    6056
    6157/******************************************************************************/
    62 
    6358/*  TYPE DEFINITIONS                                                          */
    64 
    6559/******************************************************************************/
    6660
     
    6862
    6963/*****************************************************************************/
    70 
    7164/*  GLOBAL VARIABLES                                                         */
    72 
    7365/*****************************************************************************/
    7466
     
    7668
    7769/*****************************************************************************/
    78 
    7970/*  FILE STATIC VARIABLES                                                    */
    80 
    8171/*****************************************************************************/
    8272
     
    8474
    8575/*****************************************************************************/
    86 
    8776/*  FUNCTION IMPLEMENTATION - LOCAL                                          */
    88 
    8977/*****************************************************************************/
     78
    9079static void metadataItemFree(psMetadataItem* metadataItem)
    9180{
     
    119108
    120109/*****************************************************************************/
    121 
    122110/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    123 
    124111/*****************************************************************************/
    125112
     
    175162    // Set metadata item value
    176163    switch (type) {
     164    case PS_META_ITEM_SET:
     165        metadataItem->data.V = NULL;
     166        break;
    177167    case PS_META_BOOL:
    178         metadataItem->data.B = (bool) va_arg(argPtr, int);
    179 
     168        metadataItem->data.B = (bool)va_arg(argPtr, int);
    180169        break;
    181170    case PS_META_S32:
     
    183172        break;
    184173    case PS_META_F32:
    185         metadataItem->data.F32 = (psF32) va_arg(argPtr, psF64);
     174        metadataItem->data.F32 = (psF32)va_arg(argPtr, psF64);
    186175        break;
    187176    case PS_META_F64:
     
    190179    case PS_META_STR:
    191180        metadataItem->data.V = psStringNCopy(va_arg(argPtr, char *), MAX_STRING_LENGTH);
    192 
    193181        break;
    194182    case PS_META_IMG:
     
    242230}
    243231
    244 bool psMetadataAddItem(psMetadata* restrict md, int where, psMetadataItem* restrict metadataItem)
    245 {
    246     char *key = NULL;
    247     psHash* mdTable = NULL;
    248     psList* mdList = NULL;
    249     psMetadataItem* value = NULL;
    250     psMetadataType type = PS_META_ITEM_SET;
    251 
    252     if (md == NULL) {
    253         psError(__func__, "Null metadata collection not allowed");
    254         return false;
    255     }
    256 
    257     if (metadataItem == NULL) {
    258         psError(__func__, "Null metadata item not allowed");
    259         return false;
    260     }
    261 
    262     type = metadataItem->type;
     232bool psMetadataAddItem( psMetadata *restrict md, int where, psMetadataItem *restrict metadataItem )
     233{
     234    char * key = NULL;
     235    psHash *mdTable = NULL;
     236    psList *mdList = NULL;
     237    psMetadataItem *existingEntry = NULL;
     238    psMetadataItem *newFolderEntry = NULL;
     239    psMetadataType newType = PS_META_ITEM_SET;
     240
     241    if(md == NULL) {
     242        psError( __func__, "Null metadata collection not allowed" );
     243        return false;
     244    }
     245
     246    if(metadataItem == NULL) {
     247        psError( __func__, "Null metadata item not allowed" );
     248        return false;
     249    }
     250
     251    newType = metadataItem->type;
    263252
    264253    mdTable = md->table;
    265     if (mdTable == NULL) {
    266         psError(__func__, "Null metadata table not allowed");
     254    if(mdTable == NULL) {
     255        psError( __func__, "Null metadata table not allowed" );
    267256        return false;
    268257    }
    269258
    270259    mdList = md->list;
    271     if (mdList == NULL) {
    272         psError(__func__, "Null metadata list not allowed");
     260    if(mdList == NULL) {
     261        psError( __func__, "Null metadata list not allowed");
    273262        return false;
    274263    }
    275264
    276265    key = metadataItem->name;
    277     if (key == NULL) {
     266    if(key == NULL) {
    278267        psError(__func__, "Null key item not allowed");
    279268        return false;
    280269    }
    281     // Check if key is already in table
    282     value = (psMetadataItem* ) psHashLookup(mdTable, key);
    283     if (value != NULL && type != PS_META_ITEM_SET) {
    284 
    285         // The key was found and the new metadata item is a leaf node (its type isn't PS_META_ITEM_SET), so
    286         // add the new metadata item to hash as a child of the existing metadata item folder node.
    287         if (!psListAdd(value->items, metadataItem, where)) {
    288             psError(__func__, "Couldn't add metadata item to items list. Name: %s", metadataItem->name);
     270
     271    // See if key is already in table
     272    existingEntry = (psMetadataItem*)psHashLookup(mdTable, key);
     273    if(existingEntry != NULL) {
     274
     275        if(existingEntry->type == PS_META_ITEM_SET) {
     276
     277            // Add leaf node to existing folder node
     278            if(!psListAdd(existingEntry->items, metadataItem, PS_LIST_TAIL)) {
     279                psError( __func__, "Couldn't add metadata item to items list. Name: %s",
     280                         metadataItem->name );
     281                return false;
     282            }
     283        } else if(existingEntry->type != PS_META_ITEM_SET && newType!= PS_META_ITEM_SET) {
     284
     285            // Leaf node replaces another leaf. Make new folder node and add leaves.
     286            newFolderEntry = psMetadataItemAlloc(key, PS_META_ITEM_SET, NULL, NULL);
     287
     288            if(!psListAdd(newFolderEntry->items, existingEntry, PS_LIST_TAIL)) {
     289                psError( __func__, "Couldn't add metadata item to items list. Name: %s", key);
     290                psFree(newFolderEntry);
     291                return false;
     292            }
     293
     294            if(!psListAdd(newFolderEntry->items, metadataItem, PS_LIST_TAIL)) {
     295                psError( __func__, "Couldn't add metadata item to items list. Name: %s", key);
     296                psFree(newFolderEntry);
     297                return false;
     298            }
     299
     300            if(!psHashRemove(mdTable, key)) {
     301                psError( __func__, "Couldn't remove metadata item from metadata table. Name: %s", key);
     302                psFree(newFolderEntry);
     303                return false;
     304            }
     305
     306            if(!psHashAdd(mdTable, key, newFolderEntry)) {
     307                psError( __func__, "Couldn't add metadata item to metadata table. Name: %s", key);
     308                psFree(newFolderEntry);
     309                return false;
     310            }
     311
     312            // Remove local reference to new folder node
     313            psMemDecrRefCounter(newFolderEntry);
     314        } else {
     315
     316            // Folder node replaces leaf or folder node - Put old node into new folder and remove from table
     317            if(!psListAdd(metadataItem->items, existingEntry, PS_LIST_TAIL)) {
     318                psError( __func__, "Couldn't add metadata item to items list. Name: %s", key);
     319                return false;
     320            }
     321
     322            if(!psHashRemove(mdTable, key)) {
     323                psError( __func__, "Couldn't remove metadata item from metadata table. Name: %s", key);
     324                return false;
     325            }
     326
     327            if(!psHashAdd(mdTable, key, metadataItem)) {
     328                psError( __func__, "Couldn't add metadata item to metadata table. Name: %s", key);
     329                return false;
     330            }
     331        }
     332    } else {
     333
     334        // Node doesn't exist - Add new metadata item to metadata collection's hash
     335        if(!psHashAdd(mdTable, key, metadataItem)) {
     336            psError( __func__, "Couldn't add metadata item to metadata table. Name: %s", key);
    289337            return false;
    290338        }
    291     } else if (value != NULL) {
    292 
    293         // The key was found and the new metadata item is a folder node. Don't add new metadata item, since
    294         // it will wipe out existing node.
    295         psError(__func__, "Metadata already exists in metadata collection. Item not added. Name: %s",
    296                 metadataItem->name);
    297         return false;
    298     } else {
    299 
    300         // Duplicate key not found. Add new metadata item to metadata collection's hash
    301         if (!psHashAdd(mdTable, key, metadataItem)) {
    302             psError(__func__, "Couldn't add metadata item to metadata collection table. Name: %s",
    303                     metadataItem->name);
    304             return false;
    305         }
    306339    }
    307340
    308341    // Add all items to metadata collection's list, even if they have the same metadata item names
    309     if (!psListAdd(md->list, metadataItem, where)) {
    310         psError(__func__, "Couldn't add metadata item to metadata collection list. Name: %s",
    311                 metadataItem->name);
     342    if(!psListAdd( mdList, metadataItem, where )) {
     343        psError( __func__, "Couldn't add metadata item to metadata collection list. Name: %s",
     344                 metadataItem->name );
    312345        return false;
    313346    }
     
    338371    return true;
    339372}
     373
     374
    340375
    341376bool psMetadataRemove(psMetadata* restrict md, int where, const char *restrict key)
Note: See TracChangeset for help on using the changeset viewer.