Skip to content
Snippets Groups Projects
Commit 833636d3 authored by Julien Osman's avatar Julien Osman
Browse files

ENH: Change the XMLMetadataSupplier::FetchPartialNameValueMultiple for better handling of memory

parent fadb863e
No related branches found
No related tags found
No related merge requests found
......@@ -131,7 +131,19 @@ protected:
* @return A StringList containing only the pairs from papszStrList whose key
* contain pszName
*/
char **CSLFetchPartialNameValueMultiple(char** papszStrList, const char *pszName) const;
std::vector<std::string> FetchPartialNameValueMultiple(char** papszStrList, const char *pszName) const;
/**
* @brief In a StringList of “Name=Value” pairs, look for the values
* associated with a name containing the specified string
*
* @param StringVector A std::vector of std::string that will be searched
* @param Name A std::string that will be looked for in the keys
* @return A std::vector of std::string containing only the pairs from StringVector whose key
* contain Name
*/
std::vector<std::string> FetchPartialNameValueMultiple(const std::vector<std::string> &StringVector,
const std::string &Name) const;
private:
/** List of resource files */
......
......@@ -54,7 +54,7 @@ std::string XMLMetadataSupplier::GetFirstMetadataValue(const std::string path, b
// Search for the first joker
std::size_t found = path.find("_#");
// Looking for the keys corresponding to the part of the path before the first joker
char ** values = this->CSLFetchPartialNameValueMultiple(m_MetadataDic, path.substr(0, found).c_str());
std::vector<std::string> values = this->FetchPartialNameValueMultiple(m_MetadataDic, path.substr(0, found).c_str());
// Position of the beginning of the path after the joker
std::size_t start = found + 2;
......@@ -64,19 +64,15 @@ std::string XMLMetadataSupplier::GetFirstMetadataValue(const std::string path, b
// Look for the next joker
found = path.find("_#", start);
// Look for the keys corresponding to the part of the path between the two jokers
char ** new_values = this->CSLFetchPartialNameValueMultiple(values, path.substr(start, found).c_str());
CSLDestroy(values);
values = CSLDuplicate(new_values);
CSLDestroy(new_values);
values = this->FetchPartialNameValueMultiple(values, path.substr(start, found));
start = found + 2;
}
if ((values != nullptr) && (values[0] != nullptr))
if ((values.size() != 0) && (!values[0].empty()))
{
hasValue = true;
std::string ret = values[0];
ret = ret.substr(ret.find('=') + 1);
CSLDestroy(values);
// Return the value part
return ret;
}
......@@ -198,16 +194,30 @@ char** XMLMetadataSupplier::ReadXMLToList(CPLXMLNode* psNode, char** papszList,
return papszList;
}
char ** XMLMetadataSupplier::CSLFetchPartialNameValueMultiple(char** papszStrList, const char *pszName) const
std::vector<std::string> XMLMetadataSupplier::FetchPartialNameValueMultiple(char** papszStrList,
const char *pszName) const
{
std::vector<std::string> retStringVector;
if( papszStrList == nullptr || pszName == nullptr )
return nullptr;
return retStringVector;
char **papszValues = nullptr;
for( ; *papszStrList != nullptr ; ++papszStrList )
if( strstr(*papszStrList, pszName) )
papszValues = CSLAddString(papszValues, *papszStrList);
return papszValues;
retStringVector.push_back(*papszStrList);
return retStringVector;
}
std::vector<std::string> XMLMetadataSupplier::FetchPartialNameValueMultiple(const std::vector<std::string> &StringVector,
const std::string &Name) const
{
std::vector<std::string> retStringVector;
if( StringVector.size() == 0 || Name.empty() )
return retStringVector;
for( const auto& val : StringVector )
if( val.find(Name) != std::string::npos )
retStringVector.push_back(val);
return retStringVector;
}
int XMLMetadataSupplier::GetNbBands() const
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment