IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 7918 for trunk/psLib/src/types


Ignore:
Timestamp:
Jul 17, 2006, 12:41:38 PM (20 years ago)
Author:
Paul Price
Message:

Fixing use of regex in psMetadataIterator; see bug 779. Adding new tests for psListIterator and psMetadataIterator.

File:
1 edited

Legend:

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

    r7914 r7918  
    1212 *  @author Ross Harman, MHPCC
    1313 *
    14  *  @version $Revision: 1.117 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2006-07-15 02:57:12 $
     14 *  @version $Revision: 1.118 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2006-07-17 22:41:38 $
    1616 *
    1717 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    919919
    920920    psMetadataIterator* newIter = psAlloc(sizeof(psMetadataIterator));
    921     newIter->regex = NULL;
    922     newIter->iter = NULL;
    923 
    924     // Set deallocator
    925921    psMemSetDeallocator(newIter, (psFreeFunc) metadataIteratorFree);
    926922
     
    929925    if (regex) {
    930926        newIter->regex = psAlloc(sizeof(regex_t));
    931         int regRtn = regcomp(newIter->regex,regex,0);
     927        int regRtn = regcomp(newIter->regex, regex, 0);
    932928        if (regRtn != 0) {
    933929            char errMsg[256];
    934930            regerror(regRtn, newIter->regex, errMsg, 256);
    935             regfree(newIter->regex);
    936931            psError(PS_ERR_BAD_PARAMETER_VALUE, true,
    937932                    PS_ERRORTEXT_psMetadata_REGEX_INVALID,
     
    940935            return NULL;
    941936        }
     937        if (!psMetadataIteratorSet(newIter, location)) {
     938            psError(PS_ERR_UNKNOWN, false, "Unable to set location=%d for metadata iterator.\n", location);
     939            psFree(newIter);
     940            return NULL;
     941        }
     942    } else {
     943        newIter->regex = NULL;
    942944    }
    943945
     
    948950                           long location)
    949951{
    950     int match;
    951     psMetadataItem* cursor;
    952 
    953     PS_ASSERT_PTR_NON_NULL(iterator,NULL);
    954 
    955     psListIterator* iter = iterator->iter;
    956     PS_ASSERT_PTR_NON_NULL(iterator->iter,NULL);
    957 
    958     regex_t* regex = iterator->regex;
     952    PS_ASSERT_PTR_NON_NULL(iterator, NULL);
     953    psListIterator *listIter = iterator->iter;
     954    PS_ASSERT_PTR_NON_NULL(listIter, NULL);
     955
     956    regex_t *regex = iterator->regex;
    959957
    960958    // handle trivial case where no regex subsetting is required.
    961959    if (regex == NULL) {
    962         return psListIteratorSet(iter,location);
    963     }
    964 
     960        return psListIteratorSet(listIter, location);
     961    }
     962
     963    // If there's a regex, then we need to count into the list.
     964    // We count ONLY those entries that match the regex
     965
     966    // Here we count in from the tail
    965967    if (location < 0) {
    966         // match from the tail
    967         match = 0;
    968         psListIteratorSet(iter,PS_LIST_TAIL);
    969         while ( (cursor=(psMetadataItem*)iter->cursor) != NULL) {
    970             if (regexec(regex, cursor->name, 0, NULL, 0) == 0) {
     968        psListIteratorSet(listIter, PS_LIST_TAIL);
     969        psMetadataItem *item;           // Item from iteration
     970        int match = 0;                  // Match number
     971        while (listIter->cursor && (item = listIter->cursor->data)) {
     972            if (regexec(regex, item->name, 0, NULL, 0) == 0) {
    971973                // this key is a match
    972974                match--;
     
    975977                }
    976978            }
    977             (void)psListGetAndDecrement(iter);
     979            (void)psListGetAndDecrement(listIter);
    978980        }
    979981        return (match == location);
    980982    }
    981983
    982     // find the n-th match from the head
    983     match = -1;
    984     psListIteratorSet(iter,PS_LIST_HEAD);
    985     while ( (cursor=(psMetadataItem*)iter->cursor) != NULL) {
    986         if (regexec(regex, cursor->name, 0, NULL, 0) == 0) {
     984    // Here we count in from the head
     985    psListIteratorSet(listIter, PS_LIST_HEAD);
     986    psMetadataItem *item;               // Item from iteration
     987    int match = -1;                     // Match number
     988    while (listIter->cursor && (item = listIter->cursor->data)) {
     989        if (regexec(regex, item->name, 0, NULL, 0) == 0) {
    987990            // this key is a match
    988991            match++;
     
    991994            }
    992995        }
    993         (void)psListGetAndIncrement(iter);
     996        (void)psListGetAndIncrement(listIter);
    994997    }
    995998    return (match == location);
     
    9981001psMetadataItem* psMetadataGetAndIncrement(psMetadataIterator* iterator)
    9991002{
    1000     psMetadataItem* oldValue;
    1001 
    10021003    PS_ASSERT_PTR_NON_NULL(iterator,NULL);
    1003 
    1004     psListIterator* iter = iterator->iter;
    1005     PS_ASSERT_PTR_NON_NULL(iterator->iter,NULL);
     1004    psListIterator* listIter = iterator->iter;
     1005    PS_ASSERT_PTR_NON_NULL(listIter,NULL);
    10061006
    10071007    regex_t* regex = iterator->regex;
     
    10091009    // handle trivial case where no regex subsetting is required.
    10101010    if (regex == NULL) {
    1011         return (psMetadataItem*)psListGetAndIncrement(iter);
    1012     }
    1013 
    1014     oldValue = (psMetadataItem*)iter->cursor;
    1015 
    1016     while (psListGetAndIncrement(iter) != NULL) {
    1017         if (iter->cursor != NULL &&
    1018                 regexec(regex, ((psMetadataItem*)iter->cursor->data)->name, 0, NULL, 0) == 0) {
     1011        return (psMetadataItem*)psListGetAndIncrement(listIter);
     1012    }
     1013
     1014    // Iterate until we find something matching the regex
     1015    psMetadataItem *newItem;            // New MD item from iteration
     1016    while ((newItem = psListGetAndIncrement(listIter))) {
     1017        if (regexec(regex, newItem->name, 0, NULL, 0) == 0) {
    10191018            // this key is a match
    10201019            break;
    10211020        }
    10221021    }
    1023     return oldValue;
     1022    return newItem;
    10241023}
    10251024
    10261025psMetadataItem* psMetadataGetAndDecrement(psMetadataIterator* iterator)
    10271026{
    1028     psMetadataItem* oldValue;
    1029 
    1030     PS_ASSERT_PTR_NON_NULL(iterator,NULL);
    1031 
    1032     psListIterator* iter = iterator->iter;
    1033     PS_ASSERT_PTR_NON_NULL(iterator->iter,NULL);
     1027    PS_ASSERT_PTR_NON_NULL(iterator, NULL);
     1028    psListIterator* listIter = iterator->iter;
     1029    PS_ASSERT_PTR_NON_NULL(listIter, NULL);
    10341030
    10351031    regex_t* regex = iterator->regex;
     
    10371033    // handle trivial case where no regex subsetting is required.
    10381034    if (regex == NULL) {
    1039         return (psMetadataItem*)psListGetAndDecrement(iter);
    1040     }
    1041 
    1042     oldValue = (psMetadataItem*)iter->cursor;
    1043 
    1044     while (psListGetAndDecrement(iter) != NULL) {
    1045         if (iter->cursor != NULL &&
    1046                 regexec(regex, ((psMetadataItem*)iter->cursor)->name, 0, NULL, 0) == 0) {
     1035        return (psMetadataItem*)psListGetAndDecrement(listIter);
     1036    }
     1037
     1038    // Iterate until we find something matching the regex
     1039    psMetadataItem *newItem;            // New MD item from iteration
     1040    while ((newItem = psListGetAndDecrement(listIter))) {
     1041        if (regexec(regex, newItem->name, 0, NULL, 0) == 0) {
    10471042            // this key is a match
    10481043            break;
    10491044        }
    10501045    }
    1051     return oldValue;
     1046    return newItem;
    10521047}
    10531048
Note: See TracChangeset for help on using the changeset viewer.