Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions QtPMbrowser/pmbrowserwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,28 +604,28 @@ void PMbrowserWindow::formatStimMetadataAsTableExport(std::ostream& os, int max_
const auto tli = ui->treePulse->topLevelItem(i);
if (tli->isHidden()) continue;
const auto& grp = *(tli->data(0, Qt::UserRole).value<hkTreeNode*>());
auto gpr_count = grp.extractValue<int32_t>(GrGroupCount);
auto gpr_count = grp.extractValue<std::int32_t>(GrGroupCount);
std::string grp_entry = formatParamListExportTable(grp, parametersGroup);
int Nse = tli->childCount();
for (int j = 0; j < Nse; ++j) { // level: series
const auto se_item = tli->child(j);
if (se_item->isHidden()) continue;
const auto& series = *(se_item->data(0, Qt::UserRole).value<hkTreeNode*>());
auto se_count = series.extractValue<int32_t>(SeSeriesCount);
auto se_count = series.extractValue<std::int32_t>(SeSeriesCount);
std::string se_entry = formatParamListExportTable(series, parametersSeries);
int M = se_item->childCount();
for (int k = 0; k < M; ++k) { // level: sweep
const auto sw_item = se_item->child(k);
if (sw_item->isHidden()) continue;
const auto& sweep = *(sw_item->data(0, Qt::UserRole).value<hkTreeNode*>());
auto sw_count = sweep.extractValue<int32_t>(SwSweepCount);
auto sw_count = sweep.extractValue<std::int32_t>(SwSweepCount);
std::string sw_entry = formatParamListExportTable(sweep, parametersSweep);
int Nsw = sw_item->childCount();
for (int l = 0; l < Nsw; ++l) { // level: trace
const auto tr_item = sw_item->child(l);
if (tr_item->isHidden()) continue;
const auto& trace = *(tr_item->data(0, Qt::UserRole).value<hkTreeNode*>());
auto tr_count = trace.extractValue<int32_t>(TrTraceCount);
auto tr_count = trace.extractValue<std::int32_t>(TrTraceCount);
std::string tr_entry = formatParamListExportTable(trace, parametersTrace);
os << gpr_count << '\t' << se_count << '\t' << sw_count << '\t'
<< tr_count <<
Expand Down Expand Up @@ -1051,29 +1051,22 @@ void PMbrowserWindow::printAmplifierState(const hkTreeNode* series)
{
assert(series->getLevel() == hkTreeNode::LevelSeries);
hkTreeNode amprecord;
amprecord.len = AmplifierStateSize;
//auto buffer = std::make_unique<char[]>(amprecord.len);
//amprecord.Data = buffer.get();
amprecord.isSwapped = series->getIsSwapped();
auto ampstateflag = series->extractInt32(SeAmplStateFlag),
ampstateref = series->extractInt32(SeAmplStateRef);
if (ampstateflag > 0 || ampstateref == 0) {
// use local amp state record
amprecord.Data = series->Data + SeOldAmpState;
//std::memcpy(buffer.get(), series->Data + SeOldAmpState, amprecord.len);
amprecord.Data = series->Data.subspan(SeOldAmpState, AmplifierStateSize);
std::string s;
formatParamList(amprecord, parametersAmpplifierState, s);
ui->textEdit->append(QString("Amplifier State:\n%1\n").arg(QString(s.c_str())));
}
else {
// auto secount = series->extractInt32(SeSeriesCount);
const auto& amproot = datfile->GetAmpTree().GetRootNode();
const auto& ampse = amproot.Children.at(size_t(ampstateref) - 1); // Is this correct? Or seCount?
const auto& ampse = amproot.Children.at(static_cast<std::size_t>(ampstateref) - 1); // Is this correct? Or seCount?
for(const auto& ampre : ampse.Children) { // there might be multiple amplifiers
auto ampstatecount = ampre.extractInt32(AmStateCount);
amprecord.Data = series->Data + SeOldAmpState;
//std::memcpy(buffer.get(), ampre.Data + AmAmplifierState, amprecord.len);
//amprecord.Data = ampre.Data.get() + AmAmplifierState;
amprecord.Data = ampre.Data.subspan(AmAmplifierState, AmplifierStateSize);
std::string s;
formatParamList(amprecord, parametersAmpplifierState, s);
ui->textEdit->append(QString("Amplifier State (Amp #%1):\n%2\n").arg(ampstatecount).arg(QString(s.c_str())));
Expand Down
12 changes: 6 additions & 6 deletions hekatoolslib/DatFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,16 @@ void DatFile::formatStimMetadataAsTableExport(std::ostream& os, int max_level)
auto& rootnode = GetPulTree().GetRootNode();
metadataCreateTableHeader(os);
for (const auto& grp : rootnode.Children) {
auto gpr_count = grp.extractValue<int32_t>(GrGroupCount);
auto gpr_count = grp.extractValue<std::int32_t>(GrGroupCount);
std::string grp_entry = formatParamListExportTable(grp, parametersGroup);
for (const auto& series : grp.Children) {
auto se_count = series.extractValue<int32_t>(SeSeriesCount);
auto se_count = series.extractValue<std::int32_t>(SeSeriesCount);
std::string se_entry = formatParamListExportTable(series, parametersSeries);
for (const auto& sweep : series.Children) {
auto sw_count = sweep.extractValue<int32_t>(SwSweepCount);
auto sw_count = sweep.extractValue<std::int32_t>(SwSweepCount);
std::string sw_entry = formatParamListExportTable(sweep, parametersSweep);
for (const auto& trace : sweep.Children) {
auto tr_count = trace.extractValue<int32_t>(TrTraceCount);
auto tr_count = trace.extractValue<std::int32_t>(TrTraceCount);
std::string tr_entry = formatParamListExportTable(trace, parametersTrace);
os << gpr_count << '\t' << se_count << '\t' << sw_count << '\t'
<< tr_count <<
Expand All @@ -162,10 +162,10 @@ double DatFile::getTraceHolding(const hkTreeNode& trace, std::string& unit)
}
if (std::isnan(holding)) {
// we can also try to get this info from the stim tree (usuful for old files):
auto linkedDAchannel = trace.extractValue<int32_t>(TrLinkDAChannel) - 1;
auto linkedDAchannel = trace.extractValue<std::int32_t>(TrLinkDAChannel) - 1;
assert(linkedDAchannel >= 0);
const auto& sweep_record = *trace.getParent();
int stim_index = sweep_record.extractValue<int32_t>(SwStimCount) - 1;
int stim_index = sweep_record.extractValue<std::int32_t>(SwStimCount) - 1;
const auto& stim_node = GetPgfTree().GetRootNode().Children.at(stim_index);
const auto& channel0_record = stim_node.Children.at(linkedDAchannel);
int linked_channel = channel0_record.extractInt32(chLinkedChannel) - 1;
Expand Down
14 changes: 7 additions & 7 deletions hekatoolslib/hkTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ namespace hkLib {
void hkTree::LoadToNode(hkTreeNode* parent, hkTreeNode& node, char** pdata, int level)
{
node.tree = this;
auto size = static_cast<unsigned>(LevelSizes.at(level));
auto size = static_cast<std::size_t>(LevelSizes.at(level));
node.level = level;
node.len = size;
//node.len = size;
node.isSwapped = isSwapped;
node.Parent = parent;
node.Data = *pdata;
node.Data = std::span( *pdata, size );
*pdata += size;
uint32_t nchildren;
std::memcpy(&nchildren, *pdata, sizeof(uint32_t));
Expand Down Expand Up @@ -129,15 +129,15 @@ namespace hkLib {

char hkTreeNode::getChar(std::size_t offset) const
{
if (len < offset + sizeof(char)) {
if (Data.size() < offset + sizeof(char)) {
throw std::out_of_range("offset too large while accessing tree node");
}
return Data[offset];
}

const std::optional<UserParamDescr> hkTreeNode::getUserParamDescr(std::size_t offset) const
{
if (len < offset + UserParamDescr::Size) {
if (Data.size() < offset + UserParamDescr::Size) {
//throw std::out_of_range("offset too large while accessing tree node");
return std::nullopt;
}
Expand All @@ -151,10 +151,10 @@ namespace hkLib {

const std::string_view hkTreeNode::getString(std::size_t offset) const
{
if (len <= offset) {
if (Data.size() <= offset) {
throw std::out_of_range("offset too large while accessing tree node");
}
return std::string_view(Data + offset);
return std::string_view(Data.data() + offset);
}

std::ostream& operator<<(std::ostream& os, const UserParamDescr& p)
Expand Down
29 changes: 16 additions & 13 deletions hekatoolslib/hkTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <cstring>
#include <cstdint>
#include <cstddef>
#include <span>

namespace hkLib {

Expand Down Expand Up @@ -206,7 +207,7 @@ namespace hkLib {
{
static_assert(std::is_arithmetic_v<T>, "must be arithmetic type");
T t{};
auto src = Data + offset;
auto src = Data.data() + offset;
if (!isSwapped) {
std::copy(src, src + sizeof t, reinterpret_cast<char*>(&t));
}
Expand All @@ -215,8 +216,12 @@ namespace hkLib {
}
return t;
}
template<typename T> bool checkOffset(std::size_t offset) const noexcept
{
return (Data.size() >= offset + sizeof(T));
}
public:
hkTreeNode() : Parent{ nullptr }, Data{ nullptr }, len{ 0 }, Children{}, level{ -1 }, isSwapped{ false } {};
hkTreeNode() : Parent{ nullptr }, Data{ }, Children{}, level{ -1 }, isSwapped{ false } {};
hkTreeNode(hkTreeNode&&) = default;
hkTreeNode(const hkTreeNode&) = delete;
hkTreeNode& operator=(const hkTreeNode&) = delete;
Expand All @@ -231,7 +236,7 @@ namespace hkLib {
/// <returns>extracted value</returns>
template<typename T> T extractValue(std::size_t offset) const
{
if (len < offset + sizeof(T)) {
if (!checkOffset<T>(offset)) {
throw std::out_of_range("offset too large while accessing tree node");
}
return extractValueNoCheck<T>(offset);
Expand All @@ -244,7 +249,7 @@ namespace hkLib {
/// <returns>std::optional containing value if offset is valid</returns>
template<typename T> std::optional<T> extractValueOpt(std::size_t offset) const
{
if (len < offset + sizeof(T)) {
if (!checkOffset<T>(offset)) {
return std::nullopt;
}
else {
Expand All @@ -262,7 +267,7 @@ namespace hkLib {
/// <returns>extracted value or default value</returns>
template<typename T> T extractValue(std::size_t offset, T defaultValue) const noexcept
{
if (len < offset + sizeof(T)) {
if (!checkOffset<T>(offset)) {
return defaultValue;
}
return extractValueNoCheck<T>(offset);
Expand All @@ -274,8 +279,8 @@ namespace hkLib {
LevelSweep = 3,
LevelTrace = 4
};
int32_t extractInt32(std::size_t offset) const { return extractValue<int32_t>(offset); };
uint16_t extractUInt16(std::size_t offset) const { return extractValue<uint16_t>(offset); };
int32_t extractInt32(std::size_t offset) const { return extractValue<std::int32_t>(offset); };
uint16_t extractUInt16(std::size_t offset) const { return extractValue<std::uint16_t>(offset); };
double extractLongReal(std::size_t offset) const { return extractValue<double>(offset); };
double extractLongRealNoThrow(std::size_t offset) const noexcept //! instead of throwing an exception, returns NaN if out of range
{
Expand All @@ -286,14 +291,14 @@ namespace hkLib {
const std::optional<UserParamDescr> getUserParamDescr(std::size_t offset) const;
template<std::size_t N> const std::string_view getString(std::size_t offset) const
{
if (len < offset + N) {
if (!checkOffset<char[N]>(offset)) {
return "n/a";
}
const auto* p = Data + offset;
const auto* p = Data.data() + offset;
if (p[N - 1]) {
// in theory, string is not zero terminated
// unfortunately, some PM version mess this up
// by not prperly zero-initializing the cahr array
// by not prperly zero-initializing the char array
return std::string_view(p, std::min(std::strlen(p), N));
}
else {
Expand All @@ -315,9 +320,7 @@ namespace hkLib {

public:
hkTreeNode* Parent;
//std::unique_ptr<char[]> Data;
const char* Data;
std::size_t len; //!< Length (in bytes) of data
std::span<char> Data;
std::vector<hkTreeNode> Children;
int level;
bool isSwapped;
Expand Down
Loading