
proc APSAddToTagValueList {args} {
    set listID ""
    set tag ""
    set value ""
    APSStrictParseArguments {listID tag value} 
    if {![string length $listID] || ![string length $tag] ||
        ![string length $value]} {
        return -code error "APSAddToTagValueList: invalid arguments"
    }
    global ${listID}TagList ${listID}ValueList
    if {[info exists ${listID}TagList] && [info exists ${listID}ValueList]} {
        if {[catch {APSRemoveFromTagValueList -listID $listID -tag "$tag"} result]} {
            return -code error "APSAddToTagValueList: $result"
        }
    }
    lappend ${listID}TagList "$tag"
    lappend ${listID}ValueList "$value"
}

proc APSRemoveFromTagValueList {args} {
    set listID ""
    set tag ""
        APSStrictParseArguments {listID tag value}
    if {![string length $listID] || ![string length $tag]} {
        return -code error "APSRemoveFromTagValueList: invalid arguments"
    }
    global ${listID}TagList ${listID}ValueList
    set index [lsearch -exact [subst \$${listID}TagList] $tag]
    if {$index==-1} {
        return
    }
    set ${listID}TagList [lreplace [subst \$${listID}TagList] $index $index]
    set ${listID}ValueList [lreplace [subst \$${listID}ValueList] $index $index]
}

proc APSRetrieveTaggedValue {args} {
    set listID ""
    set tag ""
    set mustBeFile 0
    APSStrictParseArguments {listID tag mustBeFile}
    if {![string length $listID] || ![string length $tag]} {
        return -code error "APSRetrieveTaggedValue: invalid arguments"
    }
    global ${listID}TagList ${listID}ValueList
    if {![info exists ${listID}TagList]} {
        return -code ok [list -1 ""]
    }
    set index [lsearch -exact [subst \$${listID}TagList] $tag]
    if {$index==-1} {
        return [list -1 ""]
    } else {
        set filename [lindex [subst \$${listID}ValueList] $index]
        if {[file exists $filename] || !$mustBeFile} {
            return [list $index [lindex [subst \$${listID}ValueList] $index]]
        } else {
            return [list -1 ""]
        }
    }
}
