Skip to content

Commit

Permalink
Merge branch 'main' into develop/sync_externals
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Smith authored and Dan Smith committed Jun 19, 2023
2 parents e47ccf3 + 12995e6 commit 2829f93
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 20 deletions.
4 changes: 3 additions & 1 deletion six/modules/c++/cphd/include/cphd/CPHDXMLControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <std/filesystem>
#include <vector>
#include <std/string>
#include <map>

#include <scene/sys_Conf.h>
#include <xml/lite/Element.h>
Expand Down Expand Up @@ -145,6 +146,7 @@ class CPHDXMLControl

//! \return Suported version to uri mapping
static std::unordered_map<std::string, xml::lite::Uri> getVersionUriMap();
static void getVersionUriMap(std::map<Version, xml::lite::Uri>&);

protected:
logging::Logger *mLog = nullptr;
Expand Down Expand Up @@ -179,7 +181,7 @@ class CPHDXMLControl
getParser(const xml::lite::Uri&) const;

// Given the URI get associated version
std::string uriToVersion(const xml::lite::Uri&) const;
Version uriToVersion(const xml::lite::Uri&) const;
};
}

Expand Down
6 changes: 4 additions & 2 deletions six/modules/c++/cphd/include/cphd/FileHeader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <io/SeekableStreams.h>
#include <cphd/BaseFileHeader.h>
#include <cphd/Types.h>

namespace cphd
{
Expand All @@ -36,10 +37,10 @@ namespace cphd
*
* \brief Stores CPHD file header information
*/
class FileHeader : public BaseFileHeader
class FileHeader final : public BaseFileHeader
{
public:
static const char DEFAULT_VERSION[];
static const std::string DEFAULT_VERSION;

/*
* \func FileHeader
Expand Down Expand Up @@ -96,6 +97,7 @@ class FileHeader : public BaseFileHeader
*
*/
void setVersion(const std::string& version);
void setVersion(Version);

/*
* \func set
Expand Down
4 changes: 3 additions & 1 deletion six/modules/c++/cphd/include/cphd/Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ struct Metadata final : MetadataBase

//! Get CPHD version
std::string getVersion() const;
void getVersion(Version&) const;

//! Set CPHD version
void setVersion(const std::string& version);
void setVersion(Version);

//! CollectionInfo block. Contains the general collection information
//! CPHD can use the SICD Collection Information block directly
Expand Down Expand Up @@ -152,7 +154,7 @@ struct Metadata final : MetadataBase
private:

//! Stores file Version
std::string mVersion;
Version mVersion;
};

//! Ostream operator
Expand Down
9 changes: 9 additions & 0 deletions six/modules/c++/cphd/include/cphd/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ typedef six::CollectionInformation CollectionInformation;
typedef six::GeoInfo GeoInfo;

typedef six::MatchInformation MatchInformation;

enum class Version
{
v100, // {"1.0.0", xml::lite::Uri("urn:CPHD:1.0.0")},
v101, // {"1.0.1", xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.0.1")},
v110, // {"1.1.0", xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.1.0")}
};
std::string to_string(Version); // "1.0.0", "1.0.1", "1.1.0"

}


Expand Down
5 changes: 3 additions & 2 deletions six/modules/c++/cphd/source/CPHDWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,9 @@ void CPHDWriter::writeMetadata(size_t supportSize,
{
const auto xmlMetadata(CPHDXMLControl().toXMLString(mMetadata, mSchemaPaths));

// update header version, or remains default if unset
mHeader.setVersion(mMetadata.getVersion());
Version cphdVersion;
mMetadata.getVersion(cphdVersion);
mHeader.setVersion(cphdVersion);

// update classification and release info
if (!six::Init::isUndefined(
Expand Down
62 changes: 51 additions & 11 deletions six/modules/c++/cphd/source/CPHDXMLControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
#include <set>
#include <unordered_map>
#include <algorithm>
#include <std/memory>
#include <memory>
#include <iterator>
#include <stdexcept>

#include <io/StringStream.h>
#include <logging/NullLogger.h>
Expand Down Expand Up @@ -103,21 +104,47 @@ std::unique_ptr<xml::lite::Document> CPHDXMLControl::toXML(
return doc;
}

std::unordered_map<std::string, xml::lite::Uri> CPHDXMLControl::getVersionUriMap()
static std::unordered_map<std::string, xml::lite::Uri> makeVersionUriMap_()
{
return {
{"1.0.0", xml::lite::Uri("urn:CPHD:1.0.0")},
{"1.0.1", xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.0.1")},
{"1.1.0", xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.1.0")}
std::map<Version, xml::lite::Uri> result;
CPHDXMLControl::getVersionUriMap(result);

std::unordered_map<std::string, xml::lite::Uri> retval;
for (const auto& version_and_uri : result)
{
retval[to_string(version_and_uri.first)] = version_and_uri.second;
}
return retval;
}
std::unordered_map<std::string, xml::lite::Uri> CPHDXMLControl::getVersionUriMap() // for existing code
{
static const auto retval = makeVersionUriMap_();
return retval;
}
static std::map<Version, xml::lite::Uri> getVersionUriMap_()
{
static const std::map<Version, xml::lite::Uri> retval = {
{Version::v100, xml::lite::Uri("urn:CPHD:1.0.0")},
{Version::v101, xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.0.1")},
{Version::v110, xml::lite::Uri("http://api.nsgreg.nga.mil/schema/cphd/1.1.0")}
};
return retval;
}
void CPHDXMLControl::getVersionUriMap(std::map<Version, xml::lite::Uri>& result)
{
result = getVersionUriMap_();
}

std::unique_ptr<xml::lite::Document> CPHDXMLControl::toXMLImpl(const Metadata& metadata)
{
const auto versionUriMap = getVersionUriMap();
if (versionUriMap.find(metadata.getVersion()) != versionUriMap.end())
Version cphdVersion;
metadata.getVersion(cphdVersion);

static const auto versionUriMap = getVersionUriMap_();
const auto it = versionUriMap.find(cphdVersion);
if (it != versionUriMap.end())
{
return getParser(versionUriMap.find(metadata.getVersion())->second)->toXML(metadata);
return getParser(it->second)->toXML(metadata);
}
std::ostringstream ostr;
ostr << "The version " << metadata.getVersion() << " is invalid. "
Expand Down Expand Up @@ -180,9 +207,9 @@ CPHDXMLControl::getParser(const xml::lite::Uri& uri) const
return std::make_unique<CPHDXMLParser>(uri.value, false, mLog);
}

std::string CPHDXMLControl::uriToVersion(const xml::lite::Uri& uri) const
Version CPHDXMLControl::uriToVersion(const xml::lite::Uri& uri) const
{
const auto versionUriMap = getVersionUriMap();
static const auto versionUriMap = getVersionUriMap_();
for (const auto& p : versionUriMap)
{
if (p.second == uri)
Expand All @@ -198,3 +225,16 @@ std::string CPHDXMLControl::uriToVersion(const xml::lite::Uri& uri) const
}

}

std::string cphd::to_string(Version siddVersion)
{
// Match existing version strings, see CPHDXMLControl::getVersionUriMap
switch (siddVersion)
{
case Version::v100: return "1.0.0";
case Version::v101: return "1.0.1";
case Version::v110: return "1.1.0";
default: break;
}
throw std::logic_error("Unkown 'Version' value.");
}
7 changes: 6 additions & 1 deletion six/modules/c++/cphd/source/FileHeader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
#include <except/Exception.h>
#include <str/Manip.h>
#include <cphd/FileHeader.h>
#include <cphd/Types.h>

namespace cphd
{
const char FileHeader::DEFAULT_VERSION[] = "1.0.1";
const std::string FileHeader::DEFAULT_VERSION = to_string(Version::v101);

FileHeader::FileHeader() :
mVersion(DEFAULT_VERSION),
Expand Down Expand Up @@ -181,6 +182,10 @@ void FileHeader::setVersion(const std::string& version)
{
mVersion = version;
}
void FileHeader::setVersion(Version version)
{
setVersion(to_string(version));
}

size_t FileHeader::set(int64_t xmlBlockSize,
int64_t supportBlockSize,
Expand Down
30 changes: 28 additions & 2 deletions six/modules/c++/cphd/source/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
*/
#include <cphd/Metadata.h>

#include <stdexcept>

namespace cphd
{

Metadata::Metadata()
{
// Default version defined in cphd::FileHeader
mVersion = FileHeader::DEFAULT_VERSION;
setVersion(FileHeader::DEFAULT_VERSION);
}

size_t Metadata::getNumChannels() const
Expand Down Expand Up @@ -67,9 +69,33 @@ DomainType Metadata::getDomainType() const

std::string Metadata::getVersion() const
{
return mVersion;
return to_string(mVersion);
}
void Metadata::getVersion(Version& version) const
{
version = mVersion;
}

void Metadata::setVersion(const std::string& version)
{
if (version == "1.0.0")
{
setVersion(Version::v100);
}
else if (version == "1.0.1")
{
setVersion(Version::v101);
}
else if (version == "1.1.0")
{
setVersion(Version::v110);
}
else
{
throw std::invalid_argument("Unknown version string: " + version);
}
}
void Metadata::setVersion(Version version)
{
mVersion = version;
}
Expand Down

0 comments on commit 2829f93

Please sign in to comment.