Improved handling of line endings and spaces in calkit files

This commit is contained in:
Jan Käberich 2020-11-01 12:09:56 +01:00
parent a5c9f1e3d3
commit 3976db8f9d
2 changed files with 48 additions and 36 deletions

View File

@ -89,6 +89,12 @@ void Calkit::toFile(std::string filename)
file.close(); file.close();
} }
static QString readLine(ifstream &file) {
string line;
getline(file, line);
return QString::fromStdString(line).simplified();
}
Calkit Calkit::fromFile(std::string filename) Calkit Calkit::fromFile(std::string filename)
{ {
Calkit c; Calkit c;
@ -97,50 +103,50 @@ Calkit Calkit::fromFile(std::string filename)
if(!file.is_open()) { if(!file.is_open()) {
throw runtime_error("Unable to open file"); throw runtime_error("Unable to open file");
} }
file >> c.open_measurements; c.open_measurements = readLine(file).toInt();
file >> c.short_measurements; c.short_measurements = readLine(file).toInt();
file >> c.load_measurements; c.load_measurements = readLine(file).toInt();
file >> c.through_measurements; c.through_measurements = readLine(file).toInt();
file >> c.open_Z0; c.open_Z0 = readLine(file).toDouble();
file >> c.open_delay; c.open_delay = readLine(file).toDouble();
file >> c.open_loss; c.open_loss = readLine(file).toDouble();
file >> c.open_C0; c.open_C0 = readLine(file).toDouble();
file >> c.open_C1; c.open_C1 = readLine(file).toDouble();
file >> c.open_C2; c.open_C2 = readLine(file).toDouble();
file >> c.open_C3; c.open_C3 = readLine(file).toDouble();
file >> c.short_Z0; c.short_Z0 = readLine(file).toDouble();
file >> c.short_delay; c.short_delay = readLine(file).toDouble();
file >> c.short_loss; c.short_loss = readLine(file).toDouble();
file >> c.short_L0; c.short_L0 = readLine(file).toDouble();
file >> c.short_L1; c.short_L1 = readLine(file).toDouble();
file >> c.short_L2; c.short_L2 = readLine(file).toDouble();
file >> c.short_L3; c.short_L3 = readLine(file).toDouble();
file >> c.load_Z0; c.load_Z0 = readLine(file).toDouble();
file >> c.through_Z0; c.through_Z0 = readLine(file).toDouble();
file >> c.through_delay; c.through_delay = readLine(file).toDouble();
file >> c.through_loss; c.through_loss = readLine(file).toDouble();
if(c.open_measurements) { if(c.open_measurements) {
file >> c.open_file; c.open_file = readLine(file).toStdString();
file >> c.open_Sparam; c.open_Sparam = readLine(file).toInt();
} }
if(c.short_measurements) { if(c.short_measurements) {
file >> c.short_file; c.short_file = readLine(file).toStdString();
file >> c.short_Sparam; c.short_Sparam = readLine(file).toInt();
} }
if(c.load_measurements) { if(c.load_measurements) {
file >> c.load_file; c.load_file = readLine(file).toStdString();
file >> c.load_Sparam; c.load_Sparam = readLine(file).toInt();
} }
if(c.through_measurements) { if(c.through_measurements) {
file >> c.through_file; c.through_file = readLine(file).toStdString();
file >> c.through_Sparam1; c.through_Sparam1 = readLine(file).toInt();
file >> c.through_Sparam2; c.through_Sparam2 = readLine(file).toInt();
} }
file >> c.TRL_through_Z0; c.TRL_through_Z0 = readLine(file).toDouble();
file >> c.TRL_reflection_short; c.TRL_reflection_short = readLine(file).toDouble();
file >> c.TRL_line_delay; c.TRL_line_delay = readLine(file).toDouble();
file >> c.TRL_line_minfreq; c.TRL_line_minfreq = readLine(file).toDouble();
file >> c.TRL_line_maxfreq; c.TRL_line_maxfreq = readLine(file).toDouble();
file.close(); file.close();
return c; return c;
} }

View File

@ -117,6 +117,12 @@ CalkitDialog::CalkitDialog(Calkit &c, QWidget *parent) :
connect(ui->buttonBox->button(QDialogButtonBox::Save), &QPushButton::clicked, [=](){ connect(ui->buttonBox->button(QDialogButtonBox::Save), &QPushButton::clicked, [=](){
auto filename = QFileDialog::getSaveFileName(this, "Save calibration kit coefficients", "", "Calibration kit files (*.calkit)", nullptr, QFileDialog::DontUseNativeDialog); auto filename = QFileDialog::getSaveFileName(this, "Save calibration kit coefficients", "", "Calibration kit files (*.calkit)", nullptr, QFileDialog::DontUseNativeDialog);
if(filename.length() > 0) { if(filename.length() > 0) {
// strip any potential file name extension and set default
auto dotPos = filename.lastIndexOf('.');
if(dotPos >= 0) {
filename.truncate(dotPos);
}
filename.append(".calkit");
parseEntries(); parseEntries();
ownKit.toFile(filename.toStdString()); ownKit.toFile(filename.toStdString());
} }