Replace the Error() object with an error() function which takes fmt

formatspecs, making for much cleaner code. Reformatted everything.

This actually happened in multiple steps but then I corrupted my local
repository and I had to recover from the working tree.
This commit is contained in:
dg
2023-05-09 20:59:44 +00:00
parent bfa0846ad0
commit 466c3c34e5
168 changed files with 5722 additions and 5135 deletions

View File

@@ -3,7 +3,6 @@
#include "lib/fluxsource/fluxsource.pb.h"
#include "fluxsource/fluxsource.h"
#include "proto.h"
#include "fmt/format.h"
#include <fstream>
struct CwfHeader
@@ -14,7 +13,7 @@ struct CwfHeader
uint8_t version; // version of this file
uint8_t clock_rate; // clock rate used: 0, 1, 2 (2 = 28MHz)
uint8_t drive_type; // type of drive
uint8_t tracks; // number of tracks
uint8_t tracks; // number of tracks
uint8_t sides; // number of sides
uint8_t index_mark; // nonzero if index marks are included
uint8_t step; // track stepping interval
@@ -24,7 +23,7 @@ struct CwfHeader
struct CwfTrack
{
uint8_t track; // sequential
uint8_t track; // sequential
uint8_t side;
uint8_t unused[2];
uint8_t length[4]; // little-endian
@@ -33,109 +32,120 @@ struct CwfTrack
class CwfFluxSource : public TrivialFluxSource
{
public:
CwfFluxSource(const CwfFluxSourceProto& config):
_config(config)
CwfFluxSource(const CwfFluxSourceProto& config): _config(config)
{
_if.open(_config.filename(), std::ios::in | std::ios::binary);
if (!_if.is_open())
Error() << fmt::format("cannot open input file '{}': {}",
_config.filename(), strerror(errno));
_if.open(_config.filename(), std::ios::in | std::ios::binary);
if (!_if.is_open())
error("cannot open input file '{}': {}",
_config.filename(),
strerror(errno));
_if.read((char*) &_header, sizeof(_header));
check_for_error();
_if.read((char*)&_header, sizeof(_header));
check_for_error();
if ((_header.file_id[0] != 'C')
|| (_header.file_id[1] != 'W')
|| (_header.file_id[2] != 'S')
|| (_header.file_id[3] != 'F'))
Error() << "input not a CWF file";
if ((_header.file_id[0] != 'C') || (_header.file_id[1] != 'W') ||
(_header.file_id[2] != 'S') || (_header.file_id[3] != 'F'))
error("input not a CWF file");
switch (_header.clock_rate)
{
case 1: _clockRate = 14161000.0; break;
case 2: _clockRate = 28322000.0; break;
default:
Error() << "unsupported clock rate";
}
switch (_header.clock_rate)
{
case 1:
_clockRate = 14161000.0;
break;
case 2:
_clockRate = 28322000.0;
break;
default:
error("unsupported clock rate");
}
std::cout << fmt::format("CWF {}x{} = {} tracks, {} sides\n",
_header.tracks, _header.step, _header.tracks*_header.step, _header.sides);
std::cout << fmt::format("CWF sample clock rate: {} MHz\n", _clockRate / 1e6);
std::cout << fmt::format("CWF {}x{} = {} tracks, {} sides\n",
_header.tracks,
_header.step,
_header.tracks * _header.step,
_header.sides);
std::cout << fmt::format(
"CWF sample clock rate: {} MHz\n", _clockRate / 1e6);
int tracks = _header.tracks*_header.sides;
for (int i=0; i<tracks; i++)
{
CwfTrack trackheader;
_if.read((char*) &trackheader, sizeof(trackheader));
check_for_error();
int tracks = _header.tracks * _header.sides;
for (int i = 0; i < tracks; i++)
{
CwfTrack trackheader;
_if.read((char*)&trackheader, sizeof(trackheader));
check_for_error();
uint32_t length = Bytes(trackheader.length, 4).reader().read_le32() - sizeof(CwfTrack);
unsigned track_number = trackheader.track * _header.step;
uint32_t length =
Bytes(trackheader.length, 4).reader().read_le32() -
sizeof(CwfTrack);
unsigned track_number = trackheader.track * _header.step;
off_t pos = _if.tellg();
_trackOffsets[std::make_pair(track_number, trackheader.side)] = std::make_pair(pos, length);
_if.seekg(pos + length);
}
}
off_t pos = _if.tellg();
_trackOffsets[std::make_pair(track_number, trackheader.side)] =
std::make_pair(pos, length);
_if.seekg(pos + length);
}
}
public:
std::unique_ptr<const Fluxmap> readSingleFlux(int track, int side)
{
const auto& p = _trackOffsets.find(std::make_pair(track, side));
if (p == _trackOffsets.end())
return std::make_unique<const Fluxmap>();
const auto& p = _trackOffsets.find(std::make_pair(track, side));
if (p == _trackOffsets.end())
return std::make_unique<const Fluxmap>();
off_t pos = p->second.first;;
size_t length = p->second.second;
_if.seekg(pos);
off_t pos = p->second.first;
;
size_t length = p->second.second;
_if.seekg(pos);
std::unique_ptr<Fluxmap> fluxmap(new Fluxmap);
uint32_t pending = 0;
bool oldindex = true;
for (size_t cursor = 0; cursor < length; cursor++)
{
uint32_t b = _if.get();
bool index = !!(b & 0x80);
b &= 0x7f;
if (b == 0x7f)
{
pending += 0x7f;
continue;
}
b += pending;
pending = 0;
std::unique_ptr<Fluxmap> fluxmap(new Fluxmap);
uint32_t pending = 0;
bool oldindex = true;
for (size_t cursor = 0; cursor < length; cursor++)
{
uint32_t b = _if.get();
bool index = !!(b & 0x80);
b &= 0x7f;
if (b == 0x7f)
{
pending += 0x7f;
continue;
}
b += pending;
pending = 0;
double interval_us = b * (1e6/_clockRate);
fluxmap->appendInterval(interval_us / US_PER_TICK);
fluxmap->appendPulse();
double interval_us = b * (1e6 / _clockRate);
fluxmap->appendInterval(interval_us / US_PER_TICK);
fluxmap->appendPulse();
if (index && !oldindex)
fluxmap->appendIndex();
oldindex = index;
}
check_for_error();
if (index && !oldindex)
fluxmap->appendIndex();
oldindex = index;
}
check_for_error();
return fluxmap;
return fluxmap;
}
void recalibrate() {}
private:
void check_for_error()
{
if (_if.fail())
Error() << fmt::format("SCP read I/O error: {}", strerror(errno));
}
void check_for_error()
{
if (_if.fail())
error("SCP read I/O error: {}", strerror(errno));
}
private:
const CwfFluxSourceProto& _config;
std::ifstream _if;
CwfHeader _header;
nanoseconds_t _clockRate;
std::map<std::pair<int, int>, std::pair<off_t, size_t>> _trackOffsets;
std::ifstream _if;
CwfHeader _header;
nanoseconds_t _clockRate;
std::map<std::pair<int, int>, std::pair<off_t, size_t>> _trackOffsets;
};
std::unique_ptr<FluxSource> FluxSource::createCwfFluxSource(const CwfFluxSourceProto& config)
std::unique_ptr<FluxSource> FluxSource::createCwfFluxSource(
const CwfFluxSourceProto& config)
{
return std::unique_ptr<FluxSource>(new CwfFluxSource(config));
}