IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 12453 for trunk/psLib


Ignore:
Timestamp:
Mar 15, 2007, 2:07:35 PM (19 years ago)
Author:
jhoblitt
Message:

simply p_psParseLevelInfo structure
fix a couple of memory leaks
misc code improvements
rename a few internal functions for clarity

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/types/psMetadataConfig.c

    r12441 r12453  
    99*  @author Ross Harman, MHPCC
    1010*  @author Eric Van Alst, MHPCC
     11*  @author Joshua Hoblitt, University of Hawaii 2006-2007
    1112*
    12 *  @version $Revision: 1.131 $ $Name: not supported by cvs2svn $
    13 *  @date $Date: 2007-03-15 02:19:09 $
     13*  @version $Revision: 1.132 $ $Name: not supported by cvs2svn $
     14*  @date $Date: 2007-03-16 00:07:35 $
    1415*
    1516*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    6465/*****************************************************************************/
    6566
    66 static psMetadata* getMetadataType(char *linePtr);
    67 static psMetadata* setMetadataItem(psMetadata* template, char* linePtr)
     67static psMetadata* genTypeTemplate(char *linePtr);
     68static psMetadata* parseTypeValues(psMetadata* template, char* linePtr)
    6869;
    6970static bool parseLine(psArray* levelArray,
     
    9394typedef struct
    9495{
    95     psArray*    nonUniqueKeyArray;     ///< non-unique key names
    96     psArray*    typeArray;             ///< array of user defined types
    97     psArray*    templateArray;         ///< array of user type templates
    98     psMetadata* metadata;              ///< metadata container
     96    psArray*    nonUniqueKeyArray;      ///< non-unique key names
     97    psHash*     typeTemplates;          ///< hash of user type templates
     98    psMetadata* metadata;               ///< metadata container
    9999}
    100100p_psParseLevelInfo;
     
    103103{
    104104    psFree(info->nonUniqueKeyArray);
    105     psFree(info->typeArray);
    106     psFree(info->templateArray);
     105    psFree(info->typeTemplates);
    107106    psFree(info->metadata);
    108107}
     
    115114
    116115    info->nonUniqueKeyArray = psArrayAllocEmpty(INITIAL_LENGTH);
    117     info->typeArray = psArrayAllocEmpty(INITIAL_LENGTH);
    118     info->templateArray = psArrayAllocEmpty(INITIAL_LENGTH);
     116    info->typeTemplates = psHashAlloc(10);
    119117    info->metadata = NULL;
    120118
     
    553551}
    554552
    555 static psMetadata* getMetadataType(char* linePtr)
     553static psMetadata* genTypeTemplate(char* linePtr)
    556554{
    557555    psMetadata*     metadataTemplate = NULL;
     
    561559
    562560    // Loop through line and generate metadata items for each token found
    563     while((token = getToken(&linePtr, " ", &status, false)) != NULL) {
     561    while ((token = getToken(&linePtr, " ", &status, false)) != NULL) {
    564562
    565563        // If not allocated then allocate new metadata
     
    570568        // Look for comment indicator #
    571569        if (strstr(token, "#") != 0) {
     570            psFree(metadataTemplate);
    572571            psFree(token);
    573572            return NULL;
     
    581580            return NULL;
    582581        }
     582        psFree(token);
    583583
    584584        // Add item to template
     
    586586            psFree(metadataTemplate);
    587587            psFree(tempItem);
    588             psFree(token);
    589588            return NULL;
    590589        }
    591 
    592590        psFree(tempItem);
    593         psFree(token);
    594591    }
    595592
     
    597594}
    598595
    599 static psMetadata* setMetadataItem(psMetadata* template,
     596
     597static psMetadata* parseTypeValues(psMetadata* template,
    600598                                   char* linePtr)
    601599{
    602600    psMetadata*      md           = NULL;
    603     psS32            items        = 0;
    604601    char*            token        = NULL;
    605602    psS32            status       = 0;
     
    608605    psListIterator*  iter         = NULL;
    609606
     607    // Allocate metadata to return
     608    md = psMetadataAlloc();
     609
    610610    // Determine the number of items in template
    611     items = template->list->n;
    612     if(items > 0 ) {
    613         // Allocate metadata
    614         md = psMetadataAlloc()
    615              ;
     611    long items = psListLength(template->list);
     612
     613    if (items > 0 ) {
    616614        // Point to first item in template
    617         iter = psListIteratorAlloc(template->
    618                                    list, PS_LIST_HEAD, true);
     615        iter = psListIteratorAlloc(template->list, PS_LIST_HEAD, true);
    619616        // For each item in template parse line string for values
    620         for(psS32 i = 0;
    621                 i < items;
    622                 i++) {
     617        for (long i = 0; i < items; i++) {
    623618            // Get template item
    624             templateItem = psListGetAndIncrement(iter)
    625                            ;
    626             if(templateItem == NULL) {
     619            templateItem = psListGetAndIncrement(iter);
     620            if (templateItem == NULL) {
    627621                psFree(md);
    628622                psFree(iter);
     
    630624                break;
    631625            }
     626
    632627            // Get the next token on the line
    633628            token = getToken(&linePtr, " ", &status, false);
    634             if(token != NULL) {
     629            if (token != NULL) {
    635630                // Allocate metadata item
    636631                mdItem = psMetadataItemAllocStr(templateItem->name,
    637632                                                templateItem->comment, token);
    638                 if(mdItem == NULL) {
     633                if (mdItem == NULL) {
    639634                    psFree(md);
    640635                    md = NULL;
     
    643638                    break;
    644639                }
     640                psFree(token);
    645641
    646642                // Add item to metadata
    647                 if(!psMetadataAddItem(md, mdItem, PS_LIST_TAIL, PS_META_DEFAULT)) {
     643                if (!psMetadataAddItem(md, mdItem, PS_LIST_TAIL, PS_META_DEFAULT)) {
    648644                    psFree(md);
    649645                    md = NULL;
    650646                    psFree(mdItem);
    651                     psFree(token);
    652647                    psFree(iter);
    653648                    break;
     
    661656                break;
    662657            }
    663 
    664             psFree(token);
    665         }
    666     }
     658        }
     659    }
     660
    667661    return md;
    668662}
     
    688682    char*                tempStr       = NULL;
    689683    psArray*             nonUniqueKeys = NULL;
    690     bool               typeFound     = false;
    691     psArray*             typeArray     = NULL;
    692     psArray*             templateArray = NULL;
    693684    psMetadata*          tempMeta      = NULL;
    694685    p_psParseLevelInfo*  nextLevelInfo = NULL;
     
    773764    } else {
    774765        // Search through user types
    775         typeArray = ((p_psParseLevelInfo*)(levelArray->data[level]))->typeArray;
    776         templateArray = ((p_psParseLevelInfo*)(levelArray->data[level]))->templateArray;
    777         for(psS32 k = 0; k < typeArray->n; k++) {
    778             if(strcmp(strType,(char*)typeArray->data[k]) == 0) {
    779                 tempMeta = setMetadataItem((psMetadata*)templateArray->data[k],linePtr);
    780                 if(tempMeta != NULL) {
    781                     // Add metadata item
    782                     md = ((p_psParseLevelInfo*)(levelArray->data[level]))->metadata;
    783                     addStatus = psMetadataAdd(md,PS_LIST_TAIL,keyName,
    784                                               PS_DATA_METADATA | flags,"",tempMeta);
    785                     // Check for add failure
    786                     if (! addStatus) {
    787                         psError(PS_ERR_IO, true,
    788                                 _("Duplicate Metadata item, %s, found.  "
    789                                   "Overwrite not allowed."),
    790                                 keyName);
    791                         psFree(strType);
    792                         psFree(tempMeta);
    793                         return false;
    794                     }
    795                     psFree(tempMeta);
    796                 } else {
    797                     // Metadata type read error
    798                     psError(PS_ERR_IO,true,
    799                             _("Failed to read a metadata %s."), keyName);
    800                     psFree(strType);
    801                     return false;
    802                 }
    803                 typeFound = true;
    804                 break;
    805             }
    806         }
    807         if(!typeFound) {
     766        // Check if type already exists in typeTempaltes
     767        psHash *typeTemplates = ((p_psParseLevelInfo*)(levelArray->data[level]))->typeTemplates;
     768
     769        psMetadata *template = psHashLookup(typeTemplates, strType);
     770        if (!template) {
    808771            psError(PS_ERR_IO, true,
    809                     _("Metadata type '%s' is invalid."), strType);
     772                    _("Metadata TYPE '%s' is invalid."), strType);
    810773            psFree(strType);
    811774            return false;
    812         } else {
     775        }
     776       
     777        // covert the template, and the rest of the line, into a metadata
     778        tempMeta = parseTypeValues(template, linePtr);
     779        if (!tempMeta) {
     780            // Metadata type read error
     781            psError(PS_ERR_IO,true,
     782                    _("Failed to read Metadata TYPE %s."), keyName);
    813783            psFree(strType);
    814             return true;
     784            return false;
     785        }
     786
     787        // get the current level's metadata
     788        md = ((p_psParseLevelInfo*)(levelArray->data[level]))->metadata;
     789
     790        // add the parsed TYPE to it
     791        addStatus = psMetadataAdd(md,PS_LIST_TAIL,keyName,
     792                PS_DATA_METADATA | flags,"",tempMeta);
     793        psFree(tempMeta);
     794
     795        // Check for add failure
     796        if (! addStatus) {
     797            psError(PS_ERR_IO, true,
     798                    _("Duplicate Metadata item, %s, found.  "
     799                     "Overwrite not allowed."), keyName);
     800            psFree(strType);
     801            return false;
    815802        }
    816803    }
     
    845832            psFree(strValue);
    846833            psFree(strComment);
     834            return false;
    847835        }
    848836    }
     
    12111199    }
    12121200
     1201    // Access typeTypes for this level/scope
     1202    psHash *typeTemplates = ((p_psParseLevelInfo*)(levelArray->data[level]))->typeTemplates;
     1203
     1204    // Check if type already exists in typeTempaltes
     1205    if (psHashLookup(typeTemplates, strType)) {
     1206        psError(PS_ERR_IO, true,
     1207            _("Specified type %s is already defined."), strType);
     1208        psFree(strType);
     1209        return false;
     1210    }
     1211
    12131212    // attempt to parse the TYPE line into a template metadata
    1214     psMetadata *tempTemplate = getMetadataType(linePtr);
     1213    psMetadata *tempTemplate = genTypeTemplate(linePtr);
    12151214    if (!tempTemplate) {
    1216         psError(PS_ERR_IO,true, _("Metadata type '%s' is invalid."), strType);
     1215        psError(PS_ERR_IO, true, _("Metadata type '%s' is invalid."), strType);
    12171216        psFree(strType);
    12181217        return false;
    12191218    }
    12201219
    1221     // Access type array
    1222     psArray *typeArray = ((p_psParseLevelInfo*)(levelArray->data[level]))->typeArray;
    1223 
    1224     // Check if type already exists in array
    1225     for (psS32 k = 0; k < psArrayLength(typeArray); k++) {
    1226         // Compare type name with the list of current types
    1227         if (strcmp(strType, (char*)typeArray->data[k]) == 0) {
    1228             psError(PS_ERR_IO,true,
    1229                     _("Specified type %s is already defined."), strType);
    1230             psFree(tempTemplate);
    1231             psFree(strType);
    1232             return false;
    1233         }
    1234     }
    1235 
    12361220    // Add key name to array of type
    1237     typeArray = psArrayAdd(typeArray, 0, strType);
     1221    // Add template to hash of templates
     1222    if (!psHashAdd(typeTemplates, strType, tempTemplate)) {
     1223        psError(PS_ERR_UNKNOWN, false, _("failed to add template for '%s' to hash"), strType);
     1224        psFree(tempTemplate);
     1225        psFree(strType);
     1226        return false;
     1227    }
     1228
     1229    psFree(tempTemplate);
    12381230    psFree(strType);
    1239 
    1240     // Add template to array of templates
    1241     psArray *templateArray = ((p_psParseLevelInfo*)(levelArray->data[level]))->templateArray;
    1242     psArrayAdd(templateArray, 0, tempTemplate);
    1243     psFree(tempTemplate);
    12441231
    12451232    return true;
Note: See TracChangeset for help on using the changeset viewer.