Skip to content
Snippets Groups Projects
Commit 6c8ed5ba authored by Anton Piccardo-Selg's avatar Anton Piccardo-Selg
Browse files

Refs #15058 New updates from David

parent 10bd4163
No related branches found
No related tags found
No related merge requests found
...@@ -17,12 +17,17 @@ namespace ANSTO { ...@@ -17,12 +17,17 @@ namespace ANSTO {
// ProgressTracker // ProgressTracker
ProgressTracker::ProgressTracker(API::Progress &progBar, const char *msg, ProgressTracker::ProgressTracker(API::Progress &progBar, const char *msg,
int64_t target, size_t count) int64_t target, size_t count)
: m_msg(msg), m_count(count), m_step(target / count), m_next(m_step), : m_msg(msg),
m_progBar(progBar) { m_count(count),
m_step(target / count),
m_next(m_step),
m_progBar(progBar) {
m_progBar.doReport(m_msg); m_progBar.doReport(m_msg);
} }
ProgressTracker::~ProgressTracker() { complete(); } ProgressTracker::~ProgressTracker() {
complete();
}
void ProgressTracker::update(int64_t position) { void ProgressTracker::update(int64_t position) {
while (m_next <= position) { while (m_next <= position) {
m_progBar.report(m_msg); m_progBar.report(m_msg);
...@@ -50,17 +55,23 @@ void ProgressTracker::complete() { ...@@ -50,17 +55,23 @@ void ProgressTracker::complete() {
} }
// EventProcessor // EventProcessor
EventProcessor::EventProcessor(const std::vector<bool> &roi, EventProcessor::EventProcessor(const std::vector<bool> &roi, const size_t stride,
const size_t stride, const double period, const double period, const double phase,
const double phase, const double tofMinBoundary, const double tofMinBoundary,
const double tofMaxBoundary, const double tofMaxBoundary,
const double timeMinBoundary, const double timeMinBoundary,
const double timeMaxBoundary) const double timeMaxBoundary)
: m_roi(roi), m_stride(stride), m_frames(0), m_period(period), : m_roi(roi), m_stride(stride),
m_phase(phase), m_tofMinBoundary(tofMinBoundary), m_frames(0),
m_tofMaxBoundary(tofMaxBoundary), m_timeMinBoundary(timeMinBoundary), m_period(period), m_phase(phase),
m_timeMaxBoundary(timeMaxBoundary) {} m_tofMinBoundary(tofMinBoundary),
void EventProcessor::newFrame() { m_frames++; } m_tofMaxBoundary(tofMaxBoundary),
m_timeMinBoundary(timeMinBoundary),
m_timeMaxBoundary(timeMaxBoundary) {
}
void EventProcessor::newFrame() {
m_frames++;
}
void EventProcessor::addEvent(size_t x, size_t y, double tof) { void EventProcessor::addEvent(size_t x, size_t y, double tof) {
// tof correction // tof correction
if (m_period > 0.0) { if (m_period > 0.0) {
...@@ -75,7 +86,7 @@ void EventProcessor::addEvent(size_t x, size_t y, double tof) { ...@@ -75,7 +86,7 @@ void EventProcessor::addEvent(size_t x, size_t y, double tof) {
// frame boundary // frame boundary
double frameTime = (m_frames * m_period) * 1e-6; // in seconds double frameTime = (m_frames * m_period) * 1e-6; // in seconds
if ((frameTime <= m_timeMinBoundary) || (frameTime > m_timeMaxBoundary)) if ((frameTime < m_timeMinBoundary) || (frameTime > m_timeMaxBoundary))
return; return;
// ToF boundary // ToF boundary
...@@ -97,16 +108,17 @@ void EventProcessor::addEvent(size_t x, size_t y, double tof) { ...@@ -97,16 +108,17 @@ void EventProcessor::addEvent(size_t x, size_t y, double tof) {
// EventCounter // EventCounter
EventCounter::EventCounter(const std::vector<bool> &roi, const size_t stride, EventCounter::EventCounter(const std::vector<bool> &roi, const size_t stride,
const double period, const double phase, const double period, const double phase,
const double tofMinBoundary, const double tofMinBoundary, const double tofMaxBoundary,
const double tofMaxBoundary, const double timeMinBoundary, const double timeMaxBoundary,
const double timeMinBoundary,
const double timeMaxBoundary,
std::vector<size_t> &eventCounts) std::vector<size_t> &eventCounts)
: EventProcessor(roi, stride, period, phase, tofMinBoundary, tofMaxBoundary, : EventProcessor(roi, stride, period, phase, tofMinBoundary, tofMaxBoundary, timeMinBoundary, timeMaxBoundary),
timeMinBoundary, timeMaxBoundary), m_eventCounts(eventCounts),
m_eventCounts(eventCounts), m_tofMin(std::numeric_limits<double>::max()), m_tofMin(std::numeric_limits<double>::max()),
m_tofMax(std::numeric_limits<double>::min()) {} m_tofMax(std::numeric_limits<double>::min()) {
size_t EventCounter::numFrames() const { return m_frames; } }
size_t EventCounter::numFrames() const {
return m_frames;
}
double EventCounter::tofMin() const { double EventCounter::tofMin() const {
return m_tofMin <= m_tofMax ? m_tofMin : 0.0; return m_tofMin <= m_tofMax ? m_tofMin : 0.0;
} }
...@@ -125,14 +137,12 @@ void EventCounter::addEventImpl(size_t id, double tof) { ...@@ -125,14 +137,12 @@ void EventCounter::addEventImpl(size_t id, double tof) {
// EventAssigner // EventAssigner
EventAssigner::EventAssigner(const std::vector<bool> &roi, const size_t stride, EventAssigner::EventAssigner(const std::vector<bool> &roi, const size_t stride,
const double period, const double phase, const double period, const double phase,
const double tofMinBoundary, const double tofMinBoundary, const double tofMaxBoundary,
const double tofMaxBoundary, const double timeMinBoundary, const double timeMaxBoundary,
const double timeMinBoundary,
const double timeMaxBoundary,
std::vector<EventVector_pt> &eventVectors) std::vector<EventVector_pt> &eventVectors)
: EventProcessor(roi, stride, period, phase, tofMinBoundary, tofMaxBoundary, : EventProcessor(roi, stride, period, phase, tofMinBoundary, tofMaxBoundary, timeMinBoundary, timeMaxBoundary),
timeMinBoundary, timeMaxBoundary), m_eventVectors(eventVectors) {
m_eventVectors(eventVectors) {} }
void EventAssigner::addEventImpl(size_t id, double tof) { void EventAssigner::addEventImpl(size_t id, double tof) {
m_eventVectors[id]->push_back(tof); m_eventVectors[id]->push_back(tof);
} }
...@@ -143,8 +153,12 @@ FastReadOnlyFile::FastReadOnlyFile(const char *filename) { ...@@ -143,8 +153,12 @@ FastReadOnlyFile::FastReadOnlyFile(const char *filename) {
m_handle = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, m_handle = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
} }
FastReadOnlyFile::~FastReadOnlyFile() { close(); } FastReadOnlyFile::~FastReadOnlyFile() {
void *FastReadOnlyFile::handle() const { return m_handle; } close();
}
void *FastReadOnlyFile::handle() const {
return m_handle;
}
void FastReadOnlyFile::close() { void FastReadOnlyFile::close() {
CloseHandle(m_handle); CloseHandle(m_handle);
m_handle = NULL; m_handle = NULL;
...@@ -162,8 +176,12 @@ bool FastReadOnlyFile::seek(int64_t offset, int whence, int64_t *newPosition) { ...@@ -162,8 +176,12 @@ bool FastReadOnlyFile::seek(int64_t offset, int whence, int64_t *newPosition) {
FastReadOnlyFile::FastReadOnlyFile(const char *filename) { FastReadOnlyFile::FastReadOnlyFile(const char *filename) {
m_handle = fopen(filename, "rb"); m_handle = fopen(filename, "rb");
} }
FastReadOnlyFile::~FastReadOnlyFile() { close(); } FastReadOnlyFile::~FastReadOnlyFile() {
void *FastReadOnlyFile::handle() const { return m_handle; } close();
}
void *FastReadOnlyFile::handle() const {
return m_handle;
}
void FastReadOnlyFile::close() { void FastReadOnlyFile::close() {
fclose(m_handle); fclose(m_handle);
m_handle = NULL; m_handle = NULL;
...@@ -179,16 +197,17 @@ bool FastReadOnlyFile::seek(int64_t offset, int whence, int64_t *newPosition) { ...@@ -179,16 +197,17 @@ bool FastReadOnlyFile::seek(int64_t offset, int whence, int64_t *newPosition) {
#endif #endif
namespace Tar { namespace Tar {
void EntryHeader::writeChecksum() { void EntryHeader::writeChecksum() {
memset(Checksum, ' ', sizeof(Checksum)); memset(Checksum, ' ', sizeof(Checksum));
size_t value = std::accumulate( size_t value = std::accumulate(
(const char *)this, (const char *)this + sizeof(EntryHeader), (size_t)0); (const char*)this,
(const char*)this + sizeof(EntryHeader),
(size_t)0);
std::ostringstream buffer; std::ostringstream buffer;
buffer << std::oct << std::setfill('0') << std::setw(sizeof(Checksum) - 1) buffer << std::oct << std::setfill('0') << std::setw(sizeof(Checksum) - 1) << value;
<< value;
std::string string = buffer.str(); std::string string = buffer.str();
std::copy(string.cbegin(), string.cend(), Checksum); std::copy(string.cbegin(), string.cend(), Checksum);
...@@ -197,8 +216,7 @@ void EntryHeader::writeChecksum() { ...@@ -197,8 +216,7 @@ void EntryHeader::writeChecksum() {
void EntryHeader::writeFileSize(int64_t value) { void EntryHeader::writeFileSize(int64_t value) {
std::ostringstream buffer; std::ostringstream buffer;
buffer << std::oct << std::setfill('0') << std::setw(sizeof(FileSize) - 1) buffer << std::oct << std::setfill('0') << std::setw(sizeof(FileSize) - 1) << value;
<< value;
std::string string = buffer.str(); std::string string = buffer.str();
std::copy(string.cbegin(), string.cend(), FileSize); std::copy(string.cbegin(), string.cend(), FileSize);
...@@ -217,8 +235,8 @@ int64_t EntryHeader::readFileSize() { ...@@ -217,8 +235,8 @@ int64_t EntryHeader::readFileSize() {
// construction // construction
File::File(const std::string &path) File::File(const std::string &path)
: m_good(true), m_file(path.c_str()), m_selected(static_cast<size_t>(-1)), : m_good(true), m_file(path.c_str()), m_selected(static_cast<size_t>(-1)), m_position(0),
m_position(0), m_size(0), m_bufferPosition(0), m_bufferAvailable(0) { m_size(0), m_bufferPosition(0), m_bufferAvailable(0) {
m_good = m_file.handle() != NULL; m_good = m_file.handle() != NULL;
while (m_good) { while (m_good) {
...@@ -263,13 +281,21 @@ void File::close() { ...@@ -263,13 +281,21 @@ void File::close() {
} }
// properties // properties
bool File::good() const { return m_good; } bool File::good() const {
const std::vector<std::string> &File::files() const { return m_fileNames; } return m_good;
}
const std::vector<std::string> &File::files() const {
return m_fileNames;
}
const std::string &File::selected_name() const { const std::string &File::selected_name() const {
return m_fileNames[m_selected]; return m_fileNames[m_selected];
} }
int64_t File::selected_position() const { return m_position; } int64_t File::selected_position() const {
int64_t File::selected_size() const { return m_size; } return m_position;
}
int64_t File::selected_size() const {
return m_size;
}
// methods // methods
bool File::select(const char *file) { bool File::select(const char *file) {
...@@ -382,11 +408,11 @@ int File::read_byte() { ...@@ -382,11 +408,11 @@ int File::read_byte() {
m_position++; m_position++;
return m_buffer[m_bufferPosition++]; return m_buffer[m_bufferPosition++];
} }
bool File::append(const std::string &path, const std::string &name, bool File::append(const std::string &path, const std::string &name, const void *buffer, size_t size) {
const void *buffer, size_t size) { std::unique_ptr<FILE, decltype(&fclose)> handle(
std::unique_ptr<FILE, decltype(&fclose)> handle(fopen(path.c_str(), "rb+"), fopen(path.c_str(), "rb+"),
fclose); fclose);
bool good = handle != NULL; bool good = handle != NULL;
int64_t lastHeaderPosition = 0; int64_t lastHeaderPosition = 0;
int64_t targetPosition = -1; int64_t targetPosition = -1;
...@@ -396,7 +422,7 @@ bool File::append(const std::string &path, const std::string &name, ...@@ -396,7 +422,7 @@ bool File::append(const std::string &path, const std::string &name,
int64_t position; int64_t position;
lastHeaderPosition = static_cast<int64_t>(ftell(handle.get())); lastHeaderPosition = static_cast<int64_t>(ftell(handle.get()));
good &= 1 == fread(&header, sizeof(EntryHeader), 1, handle.get()); good &= 1 == fread(&header, sizeof(EntryHeader), 1, handle.get());
good &= 0 == fseek(handle.get(), 512 - sizeof(EntryHeader), SEEK_CUR); good &= 0 == fseek(handle.get(), 512 - sizeof(EntryHeader), SEEK_CUR);
good &= 0 <= (position = static_cast<int64_t>(ftell(handle.get()))); good &= 0 <= (position = static_cast<int64_t>(ftell(handle.get())));
...@@ -407,12 +433,11 @@ bool File::append(const std::string &path, const std::string &name, ...@@ -407,12 +433,11 @@ bool File::append(const std::string &path, const std::string &name,
std::string fileName(header.FileName); std::string fileName(header.FileName);
if (fileName.length() == 0) if (fileName.length() == 0)
break; break;
if (fileName.compare(name) == 0) if (fileName.compare(name) == 0)
targetPosition = lastHeaderPosition; targetPosition = lastHeaderPosition;
else if (targetPosition != -1) else if (targetPosition != -1)
throw std::exception( throw std::exception("format exception"); // it has to be the last file in the archive
"format exception"); // it has to be the last file in the archive
FileInfo fileInfo; FileInfo fileInfo;
fileInfo.Offset = position; fileInfo.Offset = position;
...@@ -422,8 +447,7 @@ bool File::append(const std::string &path, const std::string &name, ...@@ -422,8 +447,7 @@ bool File::append(const std::string &path, const std::string &name,
if (offset != 0) if (offset != 0)
offset = 512 - offset; offset = 512 - offset;
good &= 0 == fseek(handle.get(), static_cast<long>(fileInfo.Size + offset), good &= 0 == fseek(handle.get(), static_cast<long>(fileInfo.Size + offset), SEEK_CUR);
SEEK_CUR);
} }
if (!good) if (!good)
...@@ -431,7 +455,7 @@ bool File::append(const std::string &path, const std::string &name, ...@@ -431,7 +455,7 @@ bool File::append(const std::string &path, const std::string &name,
if (targetPosition == -1) if (targetPosition == -1)
targetPosition = lastHeaderPosition; targetPosition = lastHeaderPosition;
// empty buffer // empty buffer
char padding[512]; char padding[512];
memset(padding, 0, 512); memset(padding, 0, 512);
...@@ -444,7 +468,7 @@ bool File::append(const std::string &path, const std::string &name, ...@@ -444,7 +468,7 @@ bool File::append(const std::string &path, const std::string &name,
memset(header.OwnerUserID, '0', sizeof(header.OwnerUserID) - 1); memset(header.OwnerUserID, '0', sizeof(header.OwnerUserID) - 1);
memset(header.OwnerGroupID, '0', sizeof(header.OwnerGroupID) - 1); memset(header.OwnerGroupID, '0', sizeof(header.OwnerGroupID) - 1);
memset(header.LastModification, '0', sizeof(header.LastModification) - 1); memset(header.LastModification, '0', sizeof(header.LastModification) - 1);
header.TypeFlag = TarTypeFlag_NormalFile; header.TypeFlag = TarTypeFlag_NormalFile;
header.writeFileSize(size); header.writeFileSize(size);
header.writeChecksum(); header.writeChecksum();
...@@ -456,7 +480,7 @@ bool File::append(const std::string &path, const std::string &name, ...@@ -456,7 +480,7 @@ bool File::append(const std::string &path, const std::string &name,
// write content // write content
good &= 1 == fwrite(buffer, size, 1, handle.get()); good &= 1 == fwrite(buffer, size, 1, handle.get());
// write padding // write padding
size_t offset = static_cast<size_t>(size % 512); size_t offset = static_cast<size_t>(size % 512);
if (offset != 0) { if (offset != 0) {
......
This diff is collapsed.
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