Commit fc8fdf76 authored by Florent Jacquemard's avatar Florent Jacquemard
Browse files

moved files (to v2.2)

refactor main target squant2
parent 7966effc
This diff is collapsed.
......@@ -431,6 +431,8 @@ static int getOption(int c, int option_index)
}
/// control the presence of mandatory option and incompatibilities.
/// @return if check of option passed.
bool checkOptions()
{
bool error = false;
......
......@@ -269,174 +269,6 @@ static struct option long_options[] =
};
// ==========================================================================
// Option handling with getop_long (stdlib)
#define check_index_is(NAME) \
assert(0 == strcmp(long_options[option_index].name, NAME))
static void setOptionArgs(int option_index)
{
TRACE("options: setOptionArgs: {}", option_index);
string _arg;
size_t sepindex;
// long options without abbreviation and with argument
switch(option_index)
{
case 19:
check_index_is("start");
assert (f_start);
d_start = strtod(optarg, NULL);
TRACE("options: setOptionArgs {}: {}", long_options[option_index].name, d_start);
break;
case 20:
check_index_is("end");
assert (f_end);
d_end = strtod(optarg, NULL);
TRACE("options: setOptionArgs {}: {}", long_options[option_index].name, d_end);
break;
case 21:
check_index_is("bars");
assert (f_bars);
_bars = strtol(optarg, NULL, 10);
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name, _bars);
break;
case 22:
check_index_is("pre");
assert (f_pre);
_pre = strtol(optarg, NULL, 10);
TRACE("options: setOptionArgs {}: {}", long_options[option_index].name, _pre);
break;
case 23:
check_index_is("post");
assert (f_post);
_post = strtol(optarg, NULL, 10);
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name,
_post);
break;
case 24:
check_index_is("barsec");
assert (f_barsec);
_barsec = strtod(optarg, NULL);
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name, _barsec);
break;
case 25:
check_index_is("barbeat");
assert (f_barbeat);
_barbeat = strtol(optarg, NULL, 10);
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name, _barsec);
break;
case 26:
check_index_is("ts");
assert (f_timesig);
_arg = string(optarg);
sepindex = _arg.find_last_of("/");
// prefix
// prefix, suffix
_ts = sm::MeterSig(std::stoi(_arg.substr(0, sepindex)),
std::stoi(_arg.substr(sepindex + 1, _arg.size() - sepindex)));
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name, optarg);
break;
default:
; // ignore
}
}
bool checkOptions()
{
bool error = false;
if (f_penalty || f_count || f_proba)
{
if ((f_penalty && f_count) || ((f_penalty || f_count) == f_proba))
{
ERROR("options conflict: more than one schema file type");
error = true;
}
}
else
{
TRACE("options: no schema file type");
}
// set forced weight type value
if (f_penalty)
{
_weight_type = WeightDom::PENALTY;
}
else if (f_count)
{
_weight_type = WeightDom::COUNTING;
}
else if (f_proba)
{
_weight_type = WeightDom::STOCHASTIC;
}
else
{
_weight_type = WeightDom::UNDEF;
}
if (f_in_text == f_in_midi)
{
TRACE("options: f_input={}, f_in_midi={}", f_input, f_in_midi);
if (f_in_midi)
{
ERROR("options conflict: 2 input files");
error = true;
}
else
{
TRACE("options: no input file");
}
}
if (! f_schema)
{
ERROR("missing option: schema file");
error = true;
}
if (f_start && f_end)
{
if (d_end < d_start)
{
ERROR("start date must be before end date");
error = true;
}
if (! f_input)
{
ERROR("options: set start or end date without input");
error = true;
}
}
if (f_bars && f_barsec)
{
ERROR("options bars and barsec are mutually exclusive");
error = true;
}
return error;
}
void usage()
{
std::cout << "Usage: squant2 [options...]";
......@@ -531,50 +363,104 @@ void version()
}
// ==========================================================================
// Option handling with getop_long (stdlib)
#define check_index_is(NAME) \
assert(0 == strcmp(long_options[option_index].name, NAME))
int main(int argc, char** argv)
static void setOptionArgs(int option_index)
{
//clock_t time_start; // chrono
//clock_t time_end;
TRACE("options: setOptionArgs: {}", option_index);
string _arg;
size_t sepindex;
// set for tracing the parsing of options
//
// log levels:
// trace = 0,
// debug = 1,
// info = 2,
// warn = 3,
// err = 4,
// critical = 5,
// off = 6
spd::set_level(spd::level::debug);
// long options without abbreviation and with argument
switch(option_index)
{
case 19:
check_index_is("start");
assert (f_start);
d_start = strtod(optarg, NULL);
TRACE("options: setOptionArgs {}: {}", long_options[option_index].name, d_start);
break;
case 20:
check_index_is("end");
assert (f_end);
d_end = strtod(optarg, NULL);
TRACE("options: setOptionArgs {}: {}", long_options[option_index].name, d_end);
break;
// index of the current option
//int index = 0;
case 21:
check_index_is("bars");
assert (f_bars);
_bars = strtol(optarg, NULL, 10);
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name, _bars);
break;
case 22:
check_index_is("pre");
assert (f_pre);
_pre = strtol(optarg, NULL, 10);
TRACE("options: setOptionArgs {}: {}", long_options[option_index].name, _pre);
break;
//
// read command line options
//
case 23:
check_index_is("post");
assert (f_post);
_post = strtol(optarg, NULL, 10);
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name,
_post);
break;
// character of option
int c;
case 24:
check_index_is("barsec");
assert (f_barsec);
_barsec = strtod(optarg, NULL);
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name, _barsec);
break;
while(1)
{
/* getopt_long stores the option index here. */
int option_index = 0;
case 25:
check_index_is("barbeat");
assert (f_barbeat);
_barbeat = strtol(optarg, NULL, 10);
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name, _barsec);
break;
case 26:
check_index_is("ts");
assert (f_timesig);
_arg = string(optarg);
sepindex = _arg.find_last_of("/");
// prefix
// prefix, suffix
_ts = sm::MeterSig(std::stoi(_arg.substr(0, sepindex)),
std::stoi(_arg.substr(sepindex + 1, _arg.size() - sepindex)));
TRACE("options: setOptionArgs {}: {}",
long_options[option_index].name, optarg);
break;
c = getopt_long_only(argc, argv, "hVv:dtk:i:m:a:",
long_options, &option_index);
default:
; // ignore
}
}
/// @param c character of option
/// @return 0 : continue without error
/// 1 : stop without error
/// > 1 : stop with error
static int getOption(int c, int option_index)
{
// end of the options
if (c == -1)
break;
assert(c != -1);
switch (c)
{
......@@ -585,11 +471,11 @@ int main(int argc, char** argv)
case 'h':
usage();
return(0);
return 1;
case 'V':
version();
return(0);
return 1;
case 'v':
if (optarg)
......@@ -625,19 +511,19 @@ int main(int argc, char** argv)
case 'k':
//k = strtoul(optarg, NULL, 10);
_k = atoi(optarg);
TRACE("option {} : {}",
TRACE("Option {} : {}",
long_options[option_index].name, _k);
if (_k == 0)
{
ERROR("option error: k={} must be > 0", _k);
return(1);
return 2;
}
break;
case 'i':
f_input = true;
_input_file = std::string(optarg);
TRACE("option {}: {}",
TRACE("Option {}: {}",
long_options[option_index].name, _input_file);
// distinguish between MIDI and text input
if (_input_file.size() > 0)
......@@ -666,7 +552,7 @@ int main(int argc, char** argv)
else
{
ERROR("option -i error: no output file name");
return(1);
return 2;
}
break;
......@@ -705,56 +591,131 @@ int main(int argc, char** argv)
ERROR("option -o error: ouput file {}, \
suffix must be .mei or .midi or .mid",
_output_file);
return(1);
return 2;
}
}
else
{
ERROR("option -o error: no output file name");
return(1);
return 2;
}
break;
case 'c':
f_config = true;
_config_file = std::string(optarg);
TRACE("option {}: {}", long_options[option_index].name,
TRACE("Option {}: {}", long_options[option_index].name,
_config_file);
break;
case 'a':
f_schema = true;
_schema_file = std::string(optarg);
TRACE("option {}: {}",
TRACE("Option {}: {}",
long_options[option_index].name, _schema_file);
break;
// missing option argument. Error already signaled by getopt
case ':':
ERROR("option error: missing option argument");
ERROR("option error: missing option argument (ignore)");
break;
// unknown or ambiguous option Error already signaled by getopt
case '?':
WARN("option error: unkown or ambiguous option argument");
ERROR("option error: unkown or ambiguous option argument (ignore)");
break;
// Do nothing for the options that are not flag-managed but without arguments
default:
ERROR("Unknown option {} ignored, use -h for help.", c);
break;
}
return 0;
}
bool checkOptions()
{
bool error = false;
if (f_penalty || f_count || f_proba)
{
if ((f_penalty && f_count) || ((f_penalty || f_count) == f_proba))
{
ERROR("options conflict: more than one schema file type");
error = true;
}
}
else
{
TRACE("options: no schema file type");
}
// set forced weight type value
if (f_penalty)
{
_weight_type = WeightDom::PENALTY;
}
else if (f_count)
{
_weight_type = WeightDom::COUNTING;
}
else if (f_proba)
{
_weight_type = WeightDom::STOCHASTIC;
}
else
{
_weight_type = WeightDom::UNDEF;
}
if (checkOptions())
if (f_in_text == f_in_midi)
{
ERROR("option error. exit");
std::cerr << "option error. exit";
return(1);
TRACE("options: f_input={}, f_in_midi={}", f_input, f_in_midi);
if (f_in_midi)
{
ERROR("options conflict: 2 input files");
error = true;
}
else
{
TRACE("options: no input file");
}
}
INFO("verbosity level = {}", verbosity);
if (! f_schema)
{
ERROR("missing option: schema file");
error = true;
}
if (f_start && f_end)
{
if (d_end < d_start)
{
ERROR("start date must be before end date");
error = true;
}
if (! f_input)
{
ERROR("options: set start or end date without input");
error = true;
}
}
if (f_bars && f_barsec)
{
ERROR("options bars and barsec are mutually exclusive");
error = true;
}
return error;
}
void setVerbosityLevel()
{
switch (verbosity)
{
case 0: spd::set_level(spd::level::off); break;
......@@ -764,9 +725,77 @@ int main(int argc, char** argv)
case 4: spd::set_level(spd::level::info); break;
case 5: spd::set_level(spd::level::debug); break;
case 6: spd::set_level(spd::level::trace); break;
default: spd::set_level(spd::level::trace);
default:
{
WARN("verbosity level {} too high, set to 6 (trace).", verbosity);
spd::set_level(spd::level::trace);
}
}
}
int main(int argc, char** argv)
{
//clock_t time_start; // chrono
//clock_t time_end;
// read command line options
int c; // character of option
while(1)
{
/* getopt_long stores the option index here. */
int option_index = 0;
c = getopt_long_only(argc, argv, "hVv:dtk:i:m:a:",
long_options, &option_index);
// end of the options
if (c == -1)
break;
int ret = getOption(c, option_index);
// if ret ==0 : no problem, continue
// exit silently
if (ret == 1)
return 0;
// exit with error message
else if (ret > 1)
{
ERROR("error in options (code {}), exit", ret);
return ret;
}
}
if (checkOptions())
{
ERROR("option error, exit");
std::cerr << "option error. exit";
return(1);
}
// set verbosity level. log levels:
// trace = 0,
// debug = 1,
// info = 2,
// warn = 3,
// err = 4,
// critical = 5,
// off = 6
INFO("verbosity level = {}", verbosity);
spd::set_level(spd::level::debug); // default
setVerbosityLevel();
// display input/output options
if (f_input) { INFO("input file: {}", _input_file); }
INFO("schema file: {} ({} weight model option)",
_schema_file,
......@@ -775,6 +804,7 @@ int main(int argc, char** argv)
if (f_output) { INFO("output file: {}", _output_file); }
if (f_config) { INFO("config file: {}", _config_file); }
// initialize running environment from INI file
if (f_config)
{
......@@ -796,6 +826,7 @@ int main(int argc, char** argv)
}
}
// adjust options after reading config file
// (possible redundancies between config.ini file and options)
if ((! f_norest) && OPT_NOREST)
......@@ -809,8 +840,8 @@ int main(int argc, char** argv)
f_mono = true;
}
// input schema (global)
// read from file
// read base schema (global) from file
INFO("Reading schema from {}", _schema_file);
_schema = new SWTAFileIn(_schema_file, _weight_type);
......@@ -839,6 +870,8 @@ int main(int argc, char** argv)
}
}
// set domain of weights (cost values)
if (CST_WEIGHT_TYPE == WeightDom::UNDEF)
{
ERROR("no weight type found for {}, abort", _schema_file);
......@@ -861,15 +894,15 @@ int main(int argc, char** argv)
return(4);
}
// TBC remove 0 weighted transitions
// cleaning of base schema [optional]