Commit 833636d3 authored by Julien Osman's avatar Julien Osman

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

parent fadb863e
Pipeline #5278 passed with stages
in 90 minutes and 41 seconds
...@@ -131,7 +131,19 @@ protected: ...@@ -131,7 +131,19 @@ protected:
* @return A StringList containing only the pairs from papszStrList whose key * @return A StringList containing only the pairs from papszStrList whose key
* contain pszName * 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: private:
/** List of resource files */ /** List of resource files */
......
...@@ -54,7 +54,7 @@ std::string XMLMetadataSupplier::GetFirstMetadataValue(const std::string path, b ...@@ -54,7 +54,7 @@ std::string XMLMetadataSupplier::GetFirstMetadataValue(const std::string path, b
// Search for the first joker // Search for the first joker
std::size_t found = path.find("_#"); std::size_t found = path.find("_#");
// Looking for the keys corresponding to the part of the path before the first joker // 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 // Position of the beginning of the path after the joker
std::size_t start = found + 2; std::size_t start = found + 2;
...@@ -64,19 +64,15 @@ std::string XMLMetadataSupplier::GetFirstMetadataValue(const std::string path, b ...@@ -64,19 +64,15 @@ std::string XMLMetadataSupplier::GetFirstMetadataValue(const std::string path, b
// Look for the next joker // Look for the next joker
found = path.find("_#", start); found = path.find("_#", start);
// Look for the keys corresponding to the part of the path between the two jokers // 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()); values = this->FetchPartialNameValueMultiple(values, path.substr(start, found));
CSLDestroy(values);
values = CSLDuplicate(new_values);
CSLDestroy(new_values);
start = found + 2; start = found + 2;
} }
if ((values != nullptr) && (values[0] != nullptr)) if ((values.size() != 0) && (!values[0].empty()))
{ {
hasValue = true; hasValue = true;
std::string ret = values[0]; std::string ret = values[0];
ret = ret.substr(ret.find('=') + 1); ret = ret.substr(ret.find('=') + 1);
CSLDestroy(values);
// Return the value part // Return the value part
return ret; return ret;
} }
...@@ -198,16 +194,30 @@ char** XMLMetadataSupplier::ReadXMLToList(CPLXMLNode* psNode, char** papszList, ...@@ -198,16 +194,30 @@ char** XMLMetadataSupplier::ReadXMLToList(CPLXMLNode* psNode, char** papszList,
return 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 ) if( papszStrList == nullptr || pszName == nullptr )
return nullptr; return retStringVector;
char **papszValues = nullptr;
for( ; *papszStrList != nullptr ; ++papszStrList ) for( ; *papszStrList != nullptr ; ++papszStrList )
if( strstr(*papszStrList, pszName) ) if( strstr(*papszStrList, pszName) )
papszValues = CSLAddString(papszValues, *papszStrList); retStringVector.push_back(*papszStrList);
return papszValues; 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 int XMLMetadataSupplier::GetNbBands() const
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment