OpenColorIO icon indicating copy to clipboard operation
OpenColorIO copied to clipboard

Add helper methods to FileRules class

Open doug-walker opened this issue 1 year ago • 4 comments

It would be helpful to add additional features to the FileRules class to compare a pair of rules for equality and to copy a rule from one FileRules to another. Here are some stand-alone functions with the desired functionality. The API of the functions below should be altered, as appropriate. As always, unit tests must be added.

bool fileRulesAreEqual(const ConstFileRulesRcPtr & f1,
                       size_t f1Idx,
                       const ConstFileRulesRcPtr & f2,
                       size_t f2Idx)
{
    // NB: No need to compare the name of the rules, that should be done in the caller.

    // Compare color space name, pattern, extension, and regex strings.

    if (Platform::Strcasecmp(f1->getColorSpace(f1Idx), f2->getColorSpace(f2Idx)) != 0 ||
        Platform::Strcasecmp(f1->getPattern(f1Idx), f2->getPattern(f2Idx)) != 0 ||
        Platform::Strcasecmp(f1->getRegex(f1Idx), f2->getRegex(f2Idx)) != 0 ||
        Platform::Strcasecmp(f1->getExtension(f1Idx), f2->getExtension(f2Idx)) != 0)
    {
        return false;
    }

    // Compare the custom keys, handling the case where they may be in a different order.

    if (f1->getNumCustomKeys(f1Idx) != f2->getNumCustomKeys(f2Idx))
    {
        return false;
    }

    CustomKeysContainer f1CustomKeys;
    for (size_t m = 0; m < f1->getNumCustomKeys(f1Idx); m++)
    {
        f1CustomKeys.set(f1->getCustomKeyName(f1Idx, m), f1->getCustomKeyValue(f1Idx, m));
    }

    for (size_t m = 0; m < f2->getNumCustomKeys(f2Idx); m++)
    {
        if (!f1CustomKeys.hasKey(f2->getCustomKeyName(f2Idx, m)))
        {
            return false;
        }
        else
        {
            if (Platform::Strcasecmp(f1CustomKeys.getValueForKey(f2->getCustomKeyName(f2Idx, m)),
                                     f2->getCustomKeyValue(f2Idx, m)) != 0)
            {
                return false;
            }
        }
    }

    return true;
}

void copyRule(const ConstFileRulesRcPtr & input,   // rule source
              size_t inputRuleIdx,                 // rule source index
              FileRulesRcPtr & merged,             // rule dest
              size_t mergedRuleIdx)                // rule dest index
{
    // Handle case where the rule is ColorSpaceNamePathSearch.

    const char * name = input->getName(inputRuleIdx);
    if (Platform::Strcasecmp(name, FileRules::FilePathSearchRuleName) == 0)
    {
        merged->insertPathSearchRule(mergedRuleIdx);
        return;
    }

    // Normal rule case.

    const char * regex = input->getRegex(inputRuleIdx);
    if (!regex || !*regex)
    {
        // The regex is empty --> handle it as a pattern & extension type rule.
        const char * pattern = input->getPattern(inputRuleIdx);
        const char * extension = input->getExtension(inputRuleIdx);
        merged->insertRule(mergedRuleIdx,
                           name,
                           input->getColorSpace(inputRuleIdx),
                           (!pattern || !*pattern) ? "*" : pattern,
                           (!extension || !*extension) ? "*" : extension);
    }
    else
    {
        // Handle it as a regex type rule.
        merged->insertRule(mergedRuleIdx,
                           name,
                           input->getColorSpace(inputRuleIdx),
                           regex);
    }

    // Copy over any custom keys.

    for (size_t k = 0; k < input->getNumCustomKeys(inputRuleIdx); k++)
    {
        merged->setCustomKey(mergedRuleIdx,
                             input->getCustomKeyName(inputRuleIdx, k),
                             input->getCustomKeyValue(inputRuleIdx, k));
    }
}

doug-walker avatar Sep 20 '24 21:09 doug-walker

Hi, I am going to have a look at this as part of ASWF dev days if that's ok!

quick7silverilm avatar Sep 26 '24 14:09 quick7silverilm

Hi @quick7silverilm - just wanted to do a quick follow-up and see if you are still planning on tackling this issue? We'd love to have you participate - but either way, just let us know! :)

carolalynn avatar Sep 30 '24 15:09 carolalynn

Hi @carolalynn, I would love to keep the task and finish it as I was halfway through it at the end of dev days. Let me know if there is any urgency on this.

quick7silverilm avatar Oct 17 '24 14:10 quick7silverilm

Sounds great, Marianna! No, there is no urgency. Let us know if you need anything!

carolalynn avatar Oct 17 '24 15:10 carolalynn