Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add helper methods to FileRules class #2048

Open
doug-walker opened this issue Sep 20, 2024 · 4 comments
Open

Add helper methods to FileRules class #2048

doug-walker opened this issue Sep 20, 2024 · 4 comments
Assignees
Labels
Feature Request New addition to OCIO functionality. good first issue Standard label for new developers to locate good issues to tackle to learn about OCIO development. help wanted Issues that the TSC has decided are worth implementing, but don't currently have the dev resources.

Comments

@doug-walker
Copy link
Collaborator

doug-walker commented Sep 20, 2024

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 doug-walker added Feature Request New addition to OCIO functionality. good first issue Standard label for new developers to locate good issues to tackle to learn about OCIO development. help wanted Issues that the TSC has decided are worth implementing, but don't currently have the dev resources. labels Sep 20, 2024
@quick7silverilm
Copy link

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

@carolalynn
Copy link
Collaborator

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! :)

@quick7silverilm
Copy link

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.

@carolalynn
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request New addition to OCIO functionality. good first issue Standard label for new developers to locate good issues to tackle to learn about OCIO development. help wanted Issues that the TSC has decided are worth implementing, but don't currently have the dev resources.
Projects
None yet
Development

No branches or pull requests

3 participants