Skip to content
Snippets Groups Projects
Commit c33f52e8 authored by Giovanni La Mura's avatar Giovanni La Mura
Browse files

Use regular expressions to parse input

parent d81ff700
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@
#include <cmath>
#include <cstdio>
#include <fstream>
#include <regex>
#include <string>
#include "../include/List.h"
#include "../include/Parsers.h"
......@@ -59,6 +60,8 @@ GeometryConfiguration* GeometryConfiguration::from_legacy(string file_name) {
int num_lines = 0;
int last_read_line = 0;
string *file_lines;
string str_target, str_num;
smatch m;
try {
file_lines = load_file(file_name, &num_lines);
} catch (exception &ex) {
......@@ -66,19 +69,44 @@ GeometryConfiguration* GeometryConfiguration::from_legacy(string file_name) {
}
int _nsph = 0, _lm = 0, _in_pol = 0, _npnt = 0, _npntts = 0, _isam = 0;
int _li = 0, _le = 0, _mxndm = 0, _iavm = 0;
sscanf(file_lines[last_read_line].c_str(), " %d", &_nsph);
regex re = regex("-?[0-9]+");
str_target = file_lines[last_read_line++];
regex_search(str_target, m, re);
//sscanf(file_lines[last_read_line].c_str(), " %d", &_nsph);
_nsph = stoi(m.str());
if (_nsph == 1) {
sscanf(
file_lines[last_read_line++].c_str(),
" %*d %d %d %d %d %d",
&_lm, &_in_pol, &_npnt, &_npntts, &_isam
);
//sscanf(
// file_lines[last_read_line++].c_str(),
// " %*d %d %d %d %d %d",
// &_lm, &_in_pol, &_npnt, &_npntts, &_isam
//);
for (int ri = 0; ri < 5; ri++) {
str_target = m.suffix().str();
regex_search(str_target, m, re);
if (ri == 0) _lm = stoi(m.str());
if (ri == 1) _in_pol = stoi(m.str());
if (ri == 2) _npnt = stoi(m.str());
if (ri == 3) _npntts = stoi(m.str());
if (ri == 4) _isam = stoi(m.str());
}
} else {
sscanf(
file_lines[last_read_line++].c_str(),
" %*d %d %d %d %d %d %d %d %d",
&_li, &_le, &_mxndm, &_in_pol, &_npnt, &_npntts, &_iavm, &_isam
);
//sscanf(
// file_lines[last_read_line++].c_str(),
// " %*d %d %d %d %d %d %d %d %d",
// &_li, &_le, &_mxndm, &_in_pol, &_npnt, &_npntts, &_iavm, &_isam
//);
for (int ri = 0; ri < 8; ri++) {
str_target = m.suffix().str();
regex_search(str_target, m, re);
if (ri == 0) _li = stoi(m.str());
if (ri == 1) _le = stoi(m.str());
if (ri == 2) _mxndm = stoi(m.str());
if (ri == 3) _in_pol = stoi(m.str());
if (ri == 4) _npnt = stoi(m.str());
if (ri == 5) _npntts = stoi(m.str());
if (ri == 6) _iavm = stoi(m.str());
if (ri == 7) _isam = stoi(m.str());
}
}
double *x, *y, *z;
x = new double[_nsph];
......@@ -90,56 +118,56 @@ GeometryConfiguration* GeometryConfiguration::from_legacy(string file_name) {
z[0] = 0.0;
} else {
for (int i = 0; i < _nsph; i++) {
double sph_x, sph_y, sph_z;
int sph_x_exp, sph_y_exp, sph_z_exp;
sscanf(
file_lines[last_read_line++].c_str(),
" %lf D%d %lf D%d %lf D%d",
&sph_x, &sph_x_exp, &sph_y, &sph_y_exp, &sph_z, &sph_z_exp
);
x[i] = sph_x * pow(10.0, 1.0 * sph_x_exp);
y[i] = sph_y * pow(10.0, 1.0 * sph_y_exp);
z[i] = sph_z * pow(10.0, 1.0 * sph_z_exp);
str_target = file_lines[last_read_line++];
re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?)?[0-9]+");
for (int ri = 0; ri < 3; ri++) {
regex_search(str_target, m, re);
str_num = regex_replace(m.str(), regex("D"), "e");
str_num = regex_replace(str_num, regex("d"), "e");
if (ri == 0) x[i] = stod(str_num);
if (ri == 1) y[i] = stod(str_num);
if (ri == 2) z[i] = stod(str_num);
str_target = m.suffix().str();
}
}
}
double in_th_start, in_th_end, in_th_step, sc_th_start, sc_th_end, sc_th_step;
int in_th_start_exp, in_th_end_exp, in_th_step_exp, sc_th_start_exp, sc_th_end_exp, sc_th_step_exp;
sscanf(
file_lines[last_read_line++].c_str(),
" %lf D%d %lf D%d %lf D%d %lf D%d %lf D%d %lf D%d",
&in_th_start, &in_th_start_exp,
&in_th_step, &in_th_step_exp,
&in_th_end, &in_th_end_exp,
&sc_th_start, &sc_th_start_exp,
&sc_th_step, &sc_th_step_exp,
&sc_th_end, &sc_th_end_exp
);
in_th_start *= pow(10.0, 1.0 * in_th_start_exp);
in_th_step *= pow(10.0, 1.0 * in_th_step_exp);
in_th_end *= pow(10.0, 1.0 * in_th_end_exp);
sc_th_start *= pow(10.0, 1.0 * sc_th_start_exp);
sc_th_step *= pow(10.0, 1.0 * sc_th_step_exp);
sc_th_end *= pow(10.0, 1.0 * sc_th_end_exp);
re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?)?[0-9]+");
str_target = file_lines[last_read_line++];
for (int ri = 0; ri < 6; ri++) {
regex_search(str_target, m, re);
str_num = regex_replace(m.str(), regex("D"), "e");
str_num = regex_replace(str_num, regex("d"), "e");
if (ri == 0) in_th_start = stod(str_num);
if (ri == 1) in_th_step = stod(str_num);
if (ri == 2) in_th_end = stod(str_num);
if (ri == 3) sc_th_start = stod(str_num);
if (ri == 4) sc_th_step = stod(str_num);
if (ri == 5) sc_th_end = stod(str_num);
str_target = m.suffix().str();
}
double in_ph_start, in_ph_end, in_ph_step, sc_ph_start, sc_ph_end, sc_ph_step;
int in_ph_start_exp, in_ph_end_exp, in_ph_step_exp, sc_ph_start_exp, sc_ph_end_exp, sc_ph_step_exp;
sscanf(
file_lines[last_read_line++].c_str(),
" %lf D%d %lf D%d %lf D%d %lf D%d %lf D%d %lf D%d",
&in_ph_start, &in_ph_start_exp,
&in_ph_step, &in_ph_step_exp,
&in_ph_end, &in_ph_end_exp,
&sc_ph_start, &sc_ph_start_exp,
&sc_ph_step, &sc_ph_step_exp,
&sc_ph_end, &sc_ph_end_exp
);
in_ph_start *= pow(10.0, 1.0 * in_ph_start_exp);
in_ph_step *= pow(10.0, 1.0 * in_ph_step_exp);
in_ph_end *= pow(10.0, 1.0 * in_ph_end_exp);
sc_ph_start *= pow(10.0, 1.0 * sc_ph_start_exp);
sc_ph_step *= pow(10.0, 1.0 * sc_ph_step_exp);
sc_ph_end *= pow(10.0, 1.0 * sc_ph_end_exp);
str_target = file_lines[last_read_line++];
for (int ri = 0; ri < 6; ri++) {
regex_search(str_target, m, re);
str_num = regex_replace(m.str(), regex("D"), "e");
str_num = regex_replace(str_num, regex("d"), "e");
if (ri == 0) in_ph_start = stod(str_num);
if (ri == 1) in_ph_step = stod(str_num);
if (ri == 2) in_ph_end = stod(str_num);
if (ri == 3) sc_ph_start = stod(str_num);
if (ri == 4) sc_ph_step = stod(str_num);
if (ri == 5) sc_ph_end = stod(str_num);
str_target = m.suffix().str();
}
int _jwtm;
sscanf(file_lines[last_read_line++].c_str(), " %d", &_jwtm);
re = regex("[0-9]+");
str_target = file_lines[last_read_line++];
regex_search(str_target, m, re);
//sscanf(file_lines[last_read_line].c_str(), " %d", &_nsph);
_jwtm = stoi(m.str());
GeometryConfiguration *conf = new GeometryConfiguration(
_nsph, _lm, _in_pol, _npnt, _npntts, _isam,
_li, _le, _mxndm, _iavm,
......@@ -170,7 +198,6 @@ ScattererConfiguration::ScattererConfiguration(
double x
) {
number_of_spheres = nsph;
scale_vec = scale_vector;
number_of_scales = nxi;
reference_variable_name = variable_name;
iog_vec = iog_vector;
......@@ -183,6 +210,20 @@ ScattererConfiguration::ScattererConfiguration(
exdc = ex;
wp = w;
xip = x;
if (variable_name == "XIV") scale_vec = scale_vector;
else {
scale_vec = new double[number_of_scales]();
const double pi2 = 2.0 * acos(-1.0);
const double evc = 6.5821188e-16;
for (int si = 0; si < number_of_scales; si++) {
double value = scale_vector[si];
if (variable_name.compare("WNS") == 0) value *= (3.0e8 / wp);
else if (variable_name.compare("WLS") == 0) value = pi2 / value * 3.0e8 / wp;
else if (variable_name.compare("PUS") == 0) value /= wp;
else if (variable_name.compare("EVS") == 0) value /= (evc * wp);
scale_vec[si] = value;
}
}
}
ScattererConfiguration::~ScattererConfiguration() {
......@@ -244,7 +285,10 @@ ScattererConfiguration* ScattererConfiguration::from_binary(string file_name, st
ros_vector = new double[nsph]();
rcf_vector = new double*[nsph];
for (int i115 = 1; i115 <= nsph; i115++) {
if (iog[i115 - 1] < i115) continue;
if (iog[i115 - 1] < i115) {
rcf_vector[i115 - 1] = new double[1]();
continue;
}
input.read(reinterpret_cast<char *>(&(nshl_vector[i115 - 1])), sizeof(int));
input.read(reinterpret_cast<char *>(&(ros_vector[i115 - 1])), sizeof(double));
int nsh = nshl_vector[i115 - 1];
......@@ -310,6 +354,8 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
int num_lines = 0;
int last_read_line = 0;
string *file_lines;
regex re;
smatch m;
try {
file_lines = load_file(dedfb_file_name, &num_lines);
} catch (exception &ex) {
......@@ -317,22 +363,38 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
}
int nsph, ies;
int max_ici = 0;
sscanf(file_lines[last_read_line].c_str(), " %d %d", &nsph, &ies);
re = regex("[0-9]+");
string str_target = file_lines[last_read_line];
for (int ri = 0; ri < 2; ri++) {
regex_search(str_target, m, re);
if (ri == 0) nsph = stoi(m.str());
if (ri == 1) ies = stoi(m.str());
str_target = m.suffix().str();
}
if (ies != 0) ies = 1;
double _exdc, _wp, _xip;
int exdc_exp, wp_exp, xip_exp;
str_target = file_lines[++last_read_line];
re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?)?[0-9]+");
for (int ri = 0; ri < 3; ri++) {
regex_search(str_target, m, re);
string str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
if (ri == 0) _exdc = stod(str_number);
if (ri == 1) _wp = stod(str_number);
if (ri == 2) _xip = stod(str_number);
str_target = m.suffix().str();
}
int _idfc, nxi, instpc, insn;
sscanf(
file_lines[++last_read_line].c_str(),
" %lf D%d %lf D%d %lf D%d %d %d %d %d",
&_exdc, &exdc_exp,
&_wp, &wp_exp,
&_xip, &xip_exp,
&_idfc, &nxi, &instpc, &insn
);
_exdc *= pow(10.0, 1.0 * 1.0 * exdc_exp);
_wp *= pow(10.0, 1.0 * wp_exp);
_xip *= pow(10.0, 1.0 * xip_exp);
re = regex("-?[0-9]+");
for (int ri = 0; ri < 4; ri++) {
regex_search(str_target, m, re);
if (ri == 0) _idfc = stoi(m.str());
if (ri == 1) nxi = stoi(m.str());
if (ri == 2) instpc = stoi(m.str());
if (ri == 3) insn = stoi(m.str());
str_target = m.suffix().str();
}
double *variable_vector;
string variable_name;
......@@ -341,23 +403,41 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
variable_name = "XIV";
if (instpc < 1) { // The variable vector is explicitly defined.
double xi;
int xi_exp;
//int xi_exp;
List<double> xi_vector;
sscanf(file_lines[++last_read_line].c_str(), " %9lE D%d", &xi, &xi_exp);
xi *= pow(10.0, 1.0 * xi_exp);
//sscanf(file_lines[++last_read_line].c_str(), " %9lE D%d", &xi, &xi_exp);
str_target = file_lines[++last_read_line];
re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?)?[0-9]+");
regex_search(str_target, m, re);
string str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
xi = stod(str_number);
xi_vector.set(0, xi);
for (int jxi310 = 1; jxi310 < nxi; jxi310++) {
sscanf(file_lines[++last_read_line].c_str(), " %9lE D%d", &xi, &xi_exp);
xi *= pow(10.0, 1.0 * xi_exp);
str_target = file_lines[++last_read_line];
regex_search(str_target, m, re);
str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
xi = stod(str_number);
xi_vector.append(xi);
}
variable_vector = xi_vector.to_array();
} else { // instpc >= 1: the variable vector is defined in steps
double xi, xi_step;
int xi_exp, xi_step_exp;
sscanf(file_lines[++last_read_line].c_str(), " %9lE D%d %9lE D%d", &xi, &xi_exp, &xi_step, &xi_step_exp);
xi *= pow(10.0, 1.0 * xi_exp);
xi_step *= pow(10.0, 1.0 * xi_step_exp);
str_target = file_lines[++last_read_line];
re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?)?[0-9]+");
regex_search(str_target, m, re);
for (int ri = 0; ri < 2; ri++) {
regex_search(str_target, m, re);
string str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
if (ri == 0) xi = stod(str_number);
if (ri == 1) xi_step = stod(str_number);
str_target = m.suffix().str();
}
variable_vector = new double[nxi]();
for (int jxi320 = 0; jxi320 < nxi; jxi320++) {
variable_vector[jxi320] = xi;
......@@ -368,10 +448,16 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
variable_vector = new double[nxi]();
if (instpc == 0) { // The variable vector is explicitly defined
double vs;
int vs_exp;
//int vs_exp;
for (int jxi_r = 0; jxi_r < nxi; jxi_r++) {
sscanf(file_lines[++last_read_line].c_str(), " %lf D%d", &vs, &vs_exp);
vs *= pow(10.0, 1.0 * vs_exp);
//sscanf(file_lines[++last_read_line].c_str(), " %lf D%d", &vs, &vs_exp);
str_target = file_lines[++last_read_line];
re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?)?[0-9]+");
regex_search(str_target, m, re);
string str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
vs = stod(str_number);
variable_vector[jxi_r] = vs;
}
switch (insn) {
......@@ -393,10 +479,20 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
}
} else { // The variable vector needs to be computed in steps
double vs, vs_step;
int vs_exp, vs_step_exp;
sscanf(file_lines[++last_read_line].c_str(), " %lf D%d %lf D%d", &vs, &vs_exp, &vs_step, &vs_step_exp);
vs *= pow(10.0, 1.0 * vs_exp);
vs_step *= pow(10.0, 1.0 * vs_step_exp);
//int vs_exp, vs_step_exp;
//sscanf(file_lines[++last_read_line].c_str(), " %lf D%d %lf D%d", &vs, &vs_exp, &vs_step, &vs_step_exp);
str_target = file_lines[++last_read_line];
re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?)?[0-9]+");
regex_search(str_target, m, re);
for (int ri = 0; ri < 2; ri++) {
regex_search(str_target, m, re);
string str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
if (ri == 0) vs = stod(str_number);
if (ri == 1) vs_step = stod(str_number);
str_target = m.suffix().str();
}
for (int jxi110w = 0; jxi110w < nxi; jxi110w++) {
variable_vector[jxi110w] = vs;
vs += vs_step;
......@@ -441,20 +537,40 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
for (int i113 = 1; i113 <= nsph; i113++) {
int i_val, nsh;
double ros_val;
int ros_val_exp;
if (iog_vector[i113 - 1] < i113) continue;
sscanf(file_lines[++last_read_line].c_str(), " %d %lf D%d", &i_val, &ros_val, &ros_val_exp);
//int ros_val_exp;
if (iog_vector[i113 - 1] < i113) {
rcf_vector[i113 - 1] = new double[1]();
continue;
}
//sscanf(file_lines[++last_read_line].c_str(), " %d %lf D%d", &i_val, &ros_val, &ros_val_exp);
re = regex("[0-9]+");
str_target = file_lines[++last_read_line];
regex_search(str_target, m, re);
i_val = stoi(m.str());
str_target = m.suffix();
re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?)?[0-9]+");
regex_search(str_target, m, re);
string str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
ros_val = stod(str_number);
nshl_vector[i113 - 1] = i_val;
if (max_ici < (i_val + 1) / 2) max_ici = (i_val + 1) / 2;
ros_vector[i113 - 1] = ros_val * pow(10.0, 1.0 * ros_val_exp);
ros_vector[i113 - 1] = ros_val;
nsh = nshl_vector[i113 - 1];
if (i113 == 1) nsh += ies;
rcf_vector[i113 - 1] = new double[nsh]();
for (int ns = 0; ns < nsh; ns++) {
double ns_rcf;
int ns_rcf_exp;
sscanf(file_lines[++last_read_line].c_str(), " %lf D%d", &ns_rcf, &ns_rcf_exp);
rcf_vector[i113 -1][ns] = ns_rcf * pow(10.0, 1.0 * ns_rcf_exp);
//int ns_rcf_exp;
//sscanf(file_lines[++last_read_line].c_str(), " %lf D%d", &ns_rcf, &ns_rcf_exp);
str_target = file_lines[++last_read_line];
regex_search(str_target, m, re);
str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
ns_rcf = stod(str_number);
rcf_vector[i113 - 1][ns] = ns_rcf;
}
}
complex<double> ***dc0m = new complex<double>**[max_ici];
......@@ -464,6 +580,7 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
dc0m[dim1][dim2] = new complex<double>[nxi]();
}
}
re = regex("-?[0-9]+\\.[0-9]+([eEdD][-+]?)?[0-9]+");
for (int jxi468 = 1; jxi468 <= nxi; jxi468++) {
if (_idfc != 0 && jxi468 > 1) continue;
for (int i162 = 1; i162 <= nsph; i162++) {
......@@ -473,11 +590,21 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
if (i162 == 1) ici = ici + ies;
for (int i157 = 0; i157 < ici; i157++) {
double dc0_real, dc0_img;
int dc0_real_exp, dc0_img_exp;
sscanf(file_lines[++last_read_line].c_str(), " (%lf D%d, %lf D%d)", &dc0_real, &dc0_real_exp, &dc0_img, &dc0_img_exp);
dc0_real *= pow(10.0, 1.0 * dc0_real_exp);
dc0_img *= pow(10.0, 1.0 * dc0_img_exp);
dc0m[i157][i162 - 1][jxi468 - 1] = dc0_real + 1i * dc0_img;
//int dc0_real_exp, dc0_img_exp;
//sscanf(file_lines[++last_read_line].c_str(), " (%lf D%d, %lf D%d)", &dc0_real, &dc0_real_exp, &dc0_img, &dc0_img_exp);
str_target = file_lines[++last_read_line];
regex_search(str_target, m, re);
string str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
dc0_real = stod(str_number);
str_target = m.suffix().str();
regex_search(str_target, m, re);
str_number = m.str();
str_number = regex_replace(str_number, regex("D"), "e");
str_number = regex_replace(str_number, regex("d"), "e");
dc0_img = stod(str_number);
dc0m[i157][i162 - 1][jxi468 - 1] = std::complex<double>(dc0_real, dc0_img);
}
}
}
......@@ -498,6 +625,7 @@ ScattererConfiguration* ScattererConfiguration::from_dedfb(string dedfb_file_nam
_wp,
_xip
);
delete[] file_lines;
return config;
}
......@@ -552,12 +680,14 @@ void ScattererConfiguration::write_binary(string file_name, string mode) {
const double two_pi = acos(0.0) * 4.0;
const double evc = 6.5821188e-16;
int max_ici = 0;
bool is_new_vector = false;
if (mode.compare("LEGACY") == 0) { // Legacy mode was chosen.
fstream output;
int ies = (use_external_sphere)? 1 : 0;
double *xi_vec;
if (reference_variable_name.compare("XIV") == 0) xi_vec = scale_vec;
else {
is_new_vector = true;
xi_vec = new double[number_of_scales];
if (reference_variable_name.compare("WNS") == 0) {
for (int i = 0; i < number_of_scales; i++)
......@@ -622,6 +752,7 @@ void ScattererConfiguration::write_binary(string file_name, string mode) {
}
}
}
if (is_new_vector) delete[] xi_vec;
output.close();
}
}
......@@ -793,7 +924,7 @@ void ScattererConfiguration::write_formatted(string file_name) {
for (int jxi476 = 0; jxi476 < number_of_scales; jxi476++) {
double dc0_real = dc0_matrix[ic477 - 1][i478 - 1][jxi476].real();
double dc0_img = dc0_matrix[ic477 - 1][i478 - 1][jxi476].imag();
fprintf(output, "%5d (%12.4lE,%12.4lE)\n", (jxi476 + 1), dc0_real, dc0_img);
fprintf(output, "%5d%12.4lE%12.4lE\n", (jxi476 + 1), dc0_real, dc0_img);
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment