Skip to content

Commit

Permalink
Adding support for commas in active views list
Browse files Browse the repository at this point in the history
  • Loading branch information
larochj committed Dec 10, 2024
1 parent 2a60d37 commit f85eb05
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 21 deletions.
4 changes: 4 additions & 0 deletions include/OpenColorIO/OpenColorAppHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -666,9 +666,13 @@ class OCIOEXPORT ConfigMergingParameters

void setActiveDisplays(const char * displays);
const char * getActiveDisplays() const;
int getNumActiveDisplays() const;
const char * getActiveDisplay(int index) const;

void setActiveViews(const char * views);
const char * getActiveViews() const;
int getNumActiveViews() const;
const char * getActiveView(int index) const;

void setInactiveColorspaces(const char * colorspaces);
const char * getInactiveColorSpaces() const;
Expand Down
10 changes: 10 additions & 0 deletions include/OpenColorIO/OpenColorIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,11 @@ class OCIOEXPORT Config
*/
void setActiveDisplays(const char * displays);
const char * getActiveDisplays() const;
void addActiveDisplay(const char * view);
void removeActiveDisplay(const char * view);
void clearActiveDisplays();
const char * getActiveDisplay(int index) const;
int getNumActiveDisplays() const;

/**
* \brief
Expand All @@ -1104,6 +1109,11 @@ class OCIOEXPORT Config
*/
void setActiveViews(const char * views);
const char * getActiveViews() const;
void addActiveView(const char * view);
void removeActiveView(const char * view);
void clearActiveViews();
const char * getActiveView(int index) const;
int getNumActiveViews() const;

/// Get all displays in the config, ignoring the active_displays list.
int getNumDisplaysAll() const noexcept;
Expand Down
123 changes: 123 additions & 0 deletions src/OpenColorIO/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4005,6 +4005,69 @@ const char * Config::getActiveDisplays() const
return getImpl()->m_activeDisplaysStr.c_str();
}

void Config::removeActiveDisplay(const char * display)
{
if( !display || !display[0] )
{
throw Exception("Active display could not be removed from config, display name has to be a "
"non-empty name.");
}

auto it = std::find(getImpl()->m_activeDisplays.begin(),
getImpl()->m_activeDisplays.end(), display);

if(it!=getImpl()->m_activeDisplays.end())
{
getImpl()->m_activeDisplays.erase(it);
}

getImpl()->m_displayCache.clear();

AutoMutex lock(getImpl()->m_cacheidMutex);
getImpl()->resetCacheIDs();
}

void Config::clearActiveDisplays()
{
getImpl()->m_activeDisplays.clear();

getImpl()->m_displayCache.clear();

AutoMutex lock(getImpl()->m_cacheidMutex);
getImpl()->resetCacheIDs();
}

const char * Config::getActiveDisplay( int index ) const
{
if( index<0 || index >= static_cast<int>(getImpl()->m_activeDisplays.size()))
{
return nullptr;
}

return getImpl()->m_activeDisplays[index].c_str();
}

int Config::getNumActiveDisplays() const
{
return static_cast<int>(getImpl()->m_activeDisplays.size());
}

void Config::addActiveDisplay(const char * view)
{
if( !view || !view[0] )
{
throw Exception("Active view could not be added to config, view name has to be a "
"non-empty name.");
}

getImpl()->m_activeDisplays.push_back(view);

getImpl()->m_displayCache.clear();

AutoMutex lock(getImpl()->m_cacheidMutex);
getImpl()->resetCacheIDs();
}

void Config::setActiveViews(const char * views)
{
getImpl()->m_activeViews.clear();
Expand All @@ -4022,6 +4085,66 @@ const char * Config::getActiveViews() const
return getImpl()->m_activeViewsStr.c_str();
}

void Config::addActiveView(const char * view)
{
if( !view || !view[0] )
{
throw Exception("Active view could not be added to config, view name has to be a "
"non-empty name.");
}

getImpl()->m_activeViews.push_back(view);

getImpl()->m_displayCache.clear();
AutoMutex lock(getImpl()->m_cacheidMutex);
getImpl()->resetCacheIDs();
}

void Config::removeActiveView(const char * view)
{
if( !view || !view[0] )
{
throw Exception("Active view could not be removed from config, view name has to be a "
"non-empty name.");
}

auto it = std::find(getImpl()->m_activeViews.begin(),
getImpl()->m_activeViews.end(), view);

if(it!=getImpl()->m_activeViews.end())
{
getImpl()->m_activeViews.erase(it);
}

getImpl()->m_displayCache.clear();
AutoMutex lock(getImpl()->m_cacheidMutex);
getImpl()->resetCacheIDs();
}

void Config::clearActiveViews()
{
getImpl()->m_activeViews.clear();

getImpl()->m_displayCache.clear();
AutoMutex lock(getImpl()->m_cacheidMutex);
getImpl()->resetCacheIDs();
}

const char * Config::getActiveView( int index ) const
{
if( index<0 || index >= static_cast<int>(getImpl()->m_activeViews.size()))
{
return nullptr;
}

return getImpl()->m_activeViews[index].c_str();
}

int Config::getNumActiveViews() const
{
return static_cast<int>(getImpl()->m_activeViews.size());
}

int Config::getNumDisplaysAll() const noexcept
{
return static_cast<int>(getImpl()->m_displays.size());
Expand Down
19 changes: 15 additions & 4 deletions src/OpenColorIO/OCIOYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4992,13 +4992,24 @@ inline void save(YAML::Emitter & out, const Config & config)
out << YAML::Newline;
out << YAML::Key << "active_displays";
StringUtils::StringVec active_displays;
if(config.getActiveDisplays() != NULL && strlen(config.getActiveDisplays()) > 0)
active_displays = SplitStringEnvStyle(config.getActiveDisplays());
int nDisplays = config.getNumActiveDisplays();
active_displays.reserve( nDisplays );
for (int i = 0; i < nDisplays; i++)
{
active_displays.push_back(config.getActiveDisplay(i));
}

out << YAML::Value << YAML::Flow << active_displays;


out << YAML::Key << "active_views";
StringUtils::StringVec active_views;
if(config.getActiveViews() != NULL && strlen(config.getActiveViews()) > 0)
active_views = SplitStringEnvStyle(config.getActiveViews());
int nViews = config.getNumActiveViews();
active_views.reserve( nViews );
for (int i = 0; i < nViews; i++)
{
active_views.push_back(config.getActiveView(i));
}
out << YAML::Value << YAML::Flow << active_views;

const std::string inactiveCSs = config.getInactiveColorSpaces();
Expand Down
97 changes: 84 additions & 13 deletions src/OpenColorIO/ParseUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -690,33 +690,104 @@ bool StrEqualsCaseIgnore(const std::string & a, const std::string & b)
return 0 == Platform::Strcasecmp(a.c_str(), b.c_str());
}

// Find the end of a name from a list contained in a string.
// The element of the list are separeted by ":" or ",".
// The name can be surrounded by quotes to enable name including theses symbols.
static size_t findEndOfName(const std::string & s, size_t start)
{
size_t currentPos = start;
size_t nameEndPos = currentPos;
bool isEndFound = false;

while( !isEndFound )
{
nameEndPos = s.find_first_of("\",:", currentPos);
if(nameEndPos == std::string::npos)
{
// We reached the end of the list
nameEndPos = s.size() - 1;
isEndFound = true;
}
else if( s[nameEndPos] == '"' )
{
// We found a quote, we need to find the next one
nameEndPos = s.find_first_of("\"", nameEndPos + 1);
if(nameEndPos == std::string::npos)
{
// We reached the end of the list instead
nameEndPos = s.size() - 1;
isEndFound = true;
}
else
{
// We found the second quote,
// we need to continue the search for a symbol separating the elements (: or ,)
currentPos = nameEndPos + 1;
}
}
else if( s[nameEndPos] == ',' ||
s[nameEndPos] == ':' )
{
// We found a symbol separating the elements, we stop here
nameEndPos--;
isEndFound = true;
}
}

return nameEndPos;
}

StringUtils::StringVec SplitStringEnvStyle(const std::string & str)
{
StringUtils::StringVec outputvec;
const std::string s = StringUtils::Trim(str);
if (StringUtils::Find(s, ",") != std::string::npos)
{
outputvec = StringUtils::Split(s, ',');
}
else if (StringUtils::Find(s, ":") != std::string::npos)
{
outputvec = StringUtils::Split(s, ':');
}
else
size_t currentPos = 0;


while( s.size() > 0 &&
currentPos < s.size() - 1 )
{
outputvec.push_back(s);
size_t nameEndPos = findEndOfName(s, currentPos);
outputvec.push_back(s.substr(currentPos, nameEndPos - currentPos + 1));
currentPos = nameEndPos + 2;
}

for (auto & val : outputvec)
for ( auto & val : outputvec )
{
val = StringUtils::Trim(val);
const std::string trimmedValue = StringUtils::Trim(val);

// If the trimmed value is surrounded by quotes, we remove them
if( trimmedValue.size() > 1 &&
trimmedValue[0] == '"' &&
trimmedValue[trimmedValue.size() - 1] == '"' )
{
val = trimmedValue.substr(1, trimmedValue.size() - 2);
}
else
{
val = trimmedValue;
}
}

return outputvec;
}

std::string JoinStringEnvStyle(const StringUtils::StringVec & outputvec)
{
return StringUtils::Join(outputvec, ',');
std::string result;
for( const auto & val : outputvec )
{
if( val.find_first_of(',') != std::string::npos )
{
result += "\"" + val + "\", ";
}
else
{
result += val + ", ";
}
}

return result;
}

// Return a vector of strings that are both in vec1 and vec2.
Expand Down
20 changes: 20 additions & 0 deletions src/OpenColorIO/apphelpers/mergeconfigs/MergeConfigsHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ const char * ConfigMergingParameters::getActiveDisplays() const
return getImpl()->m_overrideCfg->getActiveDisplays();
}

int ConfigMergingParameters::getNumActiveDisplays() const
{
return getImpl()->m_overrideCfg->getNumActiveDisplays();
}

const char * ConfigMergingParameters::getActiveDisplay(int index) const
{
return getImpl()->m_overrideCfg->getActiveDisplay(index);
}

void ConfigMergingParameters::setActiveViews(const char * views)
{
getImpl()->m_overrideCfg->setActiveViews(views);
Expand All @@ -163,6 +173,16 @@ const char * ConfigMergingParameters::getActiveViews() const
return getImpl()->m_overrideCfg->getActiveViews();
}

int ConfigMergingParameters::getNumActiveViews() const
{
return getImpl()->m_overrideCfg->getNumActiveViews();
}

const char * ConfigMergingParameters::getActiveView(int index) const
{
return getImpl()->m_overrideCfg->getActiveView(index);
}

void ConfigMergingParameters::setInactiveColorspaces(const char * colorspaces)
{
getImpl()->m_overrideCfg->setInactiveColorSpaces(colorspaces);
Expand Down
19 changes: 15 additions & 4 deletions src/OpenColorIO/apphelpers/mergeconfigs/OCIOMYaml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -548,15 +548,26 @@ inline void save(YAML::Emitter & out, const ConfigMerger & merger)

out << YAML::Key << "active_displays";
StringUtils::StringVec active_displays;
if (p->getActiveDisplays() != NULL && strlen(p->getActiveDisplays()) > 0)
active_displays = SplitStringEnvStyle(p->getActiveDisplays());
int nDisplays = p->getNumActiveDisplays();
active_displays.reserve( nDisplays );
for (int i = 0; i < nDisplays; i++)
{
active_displays.push_back(p->getActiveDisplay(i));
}


out << YAML::Value << YAML::Flow << active_displays;
out << YAML::Newline;

out << YAML::Key << "active_views";
StringUtils::StringVec active_views;
if (p->getActiveViews() != NULL && strlen(p->getActiveViews()) > 0)
active_views = SplitStringEnvStyle(p->getActiveViews());
int nViews = p->getNumActiveViews();
active_views.reserve( nViews );
for (int i = 0; i < nViews; i++)
{
active_views.push_back(p->getActiveView(i));
}

out << YAML::Value << YAML::Flow << active_views;

out << YAML::Key << "inactive_colorspaces";
Expand Down

0 comments on commit f85eb05

Please sign in to comment.