replaced raw pointers by smart pointers for msr2data.cpp.

This commit is contained in:
suter_a 2023-10-13 12:47:33 +02:00
parent 98026c725d
commit ff9245fd28

View File

@ -50,6 +50,7 @@
#include <cstdlib> #include <cstdlib>
#include <limits> #include <limits>
#include <string> #include <string>
#include <memory>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/case_conv.hpp> // for to_lower() in std::string #include <boost/algorithm/string/case_conv.hpp> // for to_lower() in std::string
@ -166,40 +167,6 @@ void msr2data_syntax()
std::cout << std::endl << std::endl; std::cout << std::endl << std::endl;
} }
//--------------------------------------------------------------------------
/**
* <p>Cleaning up some used variables
*
* \param msr2dataHandler pointer to PMsr2Data object
* \param run_vec vector containing the run numbers
* \param arg vector containing the command line arguments
*
*/
void msr2data_cleanup(PMsr2Data *msr2dataHandler, std::vector<unsigned int> &run_vec, std::vector<std::string> &arg)
{
delete msr2dataHandler;
msr2dataHandler = nullptr;
run_vec.clear();
arg.clear();
return;
}
//--------------------------------------------------------------------------
/**
* <p>Cleaning up some used variables
*
* \param msr2dataHandler pointer to PMsr2Data object
* \param arg vector containing the command line arguments
*
*/
void msr2data_cleanup(PMsr2Data *msr2dataHandler, std::vector<std::string> &arg)
{
delete msr2dataHandler;
msr2dataHandler = 0;
arg.clear();
return;
}
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* <p>Checks if only valid options appear in the argument list * <p>Checks if only valid options appear in the argument list
@ -588,8 +555,8 @@ int main(int argc, char *argv[])
run_list += arg[i] + " "; run_list += arg[i] + " ";
} }
// parse run_list string // parse run_list string
PStringNumberList *nl = new PStringNumberList(run_list); std::unique_ptr<PStringNumberList> nl = std::unique_ptr<PStringNumberList>(new PStringNumberList(run_list));
if (nl == 0) { // couldn't invoke object if (nl == nullptr) { // couldn't invoke object
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << ">> msr2data: **ERROR** Couldn't invoke run_list parser object! Quitting now." << std::endl; std::cerr << ">> msr2data: **ERROR** Couldn't invoke run_list parser object! Quitting now." << std::endl;
return 0; return 0;
@ -602,7 +569,6 @@ int main(int argc, char *argv[])
} }
// get run list vector // get run list vector
run_vec = nl->GetList(); run_vec = nl->GetList();
delete nl;
msrExtension = arg[rightbracket + 1]; msrExtension = arg[rightbracket + 1];
@ -696,7 +662,12 @@ int main(int argc, char *argv[])
realOutput = false; realOutput = false;
// create the msr2data-object and set the run numbers according to the runTAG above // create the msr2data-object and set the run numbers according to the runTAG above
PMsr2Data *msr2dataHandler = new PMsr2Data(msrExtension); std::unique_ptr<PMsr2Data> msr2dataHandler = std::unique_ptr<PMsr2Data>(new PMsr2Data(msrExtension));
if (msr2dataHandler == nullptr) {
std::cerr << std::endl;
std::cerr << ">> msr2data: **ERROR** couldn't invoke msr2dataHandler." << std::endl;
return -1;
}
int status; int status;
@ -716,17 +687,14 @@ int main(int argc, char *argv[])
default: default:
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << ">> msr2data: **ERROR** None of the possible run list specifications has been detected! Quitting now..." << std::endl; std::cerr << ">> msr2data: **ERROR** None of the possible run list specifications has been detected! Quitting now..." << std::endl;
msr2data_cleanup(msr2dataHandler, run_vec, arg);
return 0; return 0;
} }
if (status == 1) { if (status == 1) {
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << ">> msr2data: **ERROR** The run numbers are out of range! Quitting..." << std::endl; std::cerr << ">> msr2data: **ERROR** The run numbers are out of range! Quitting..." << std::endl;
msr2data_cleanup(msr2dataHandler, run_vec, arg);
return status; return status;
} else if (status == -1) { } else if (status == -1) {
msr2data_cleanup(msr2dataHandler, run_vec, arg);
return status; return status;
} }
@ -742,12 +710,10 @@ int main(int argc, char *argv[])
if (temp == -2) { if (temp == -2) {
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << ">> msr2data: **ERROR** More than one fitting options are specified! Quitting..." << std::endl; std::cerr << ">> msr2data: **ERROR** More than one fitting options are specified! Quitting..." << std::endl;
msr2data_cleanup(msr2dataHandler, arg);
return temp; return temp;
} else if (temp == -3) { } else if (temp == -3) {
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << ">> msr2data: **ERROR** The given template has not a valid run number! Quitting..." << std::endl; std::cerr << ">> msr2data: **ERROR** The given template has not a valid run number! Quitting..." << std::endl;
msr2data_cleanup(msr2dataHandler, arg);
return temp; return temp;
} }
@ -775,7 +741,6 @@ int main(int argc, char *argv[])
if (temp == -3) { if (temp == -3) {
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << ">> msr2data: **ERROR** The given template has not a valid run number! Quitting..." << std::endl; std::cerr << ">> msr2data: **ERROR** The given template has not a valid run number! Quitting..." << std::endl;
msr2data_cleanup(msr2dataHandler, arg);
return temp; return temp;
} }
} }
@ -801,7 +766,6 @@ int main(int argc, char *argv[])
} }
if (status) { if (status) {
msr2data_cleanup(msr2dataHandler, arg);
return status; return status;
} }
@ -810,7 +774,6 @@ int main(int argc, char *argv[])
if(status) { if(status) {
std::cerr << std::endl; std::cerr << std::endl;
std::cerr << ">> msr2data: **ERROR** At least one given run number is out of range! Quitting..." << std::endl; std::cerr << ">> msr2data: **ERROR** At least one given run number is out of range! Quitting..." << std::endl;
msr2data_cleanup(msr2dataHandler, arg);
return status; return status;
} }
@ -820,7 +783,7 @@ int main(int argc, char *argv[])
// 1 - write header explicitly (even if the file is present already) // 1 - write header explicitly (even if the file is present already)
// 2 - write header automatically if a new file is created and do not if the data is appended to an existing file // 2 - write header automatically if a new file is created and do not if the data is appended to an existing file
std::fstream *fileOutput = nullptr; std::unique_ptr<std::fstream> fileOutput;
if (realOutput) { if (realOutput) {
// check the arguments for the "header" and "noheader" options // check the arguments for the "header" and "noheader" options
if (!msr2data_useOption(arg, "header")) { if (!msr2data_useOption(arg, "header")) {
@ -838,7 +801,7 @@ int main(int argc, char *argv[])
// delete old db/data file if the "new" option is given // delete old db/data file if the "new" option is given
if (!msr2data_useOption(arg, "new")) { if (!msr2data_useOption(arg, "new")) {
fileOutput = new std::fstream; fileOutput = std::unique_ptr<std::fstream>(new std::fstream);
fileOutput->open(outputFile.c_str(), std::ios::in); fileOutput->open(outputFile.c_str(), std::ios::in);
if (fileOutput->is_open()) { if (fileOutput->is_open()) {
std::cout << std::endl << ">> msr2data: **INFO** Deleting output file " << outputFile << std::endl; std::cout << std::endl << ">> msr2data: **INFO** Deleting output file " << outputFile << std::endl;
@ -848,8 +811,6 @@ int main(int argc, char *argv[])
} else { } else {
std::cout << std::endl << ">> msr2data: **INFO** Ignoring the 'new' option since " << outputFile << " does not exist yet." << std::endl; std::cout << std::endl << ">> msr2data: **INFO** Ignoring the 'new' option since " << outputFile << " does not exist yet." << std::endl;
} }
delete fileOutput;
fileOutput = 0;
if (writeHeader == 2) { if (writeHeader == 2) {
writeHeader = 1; writeHeader = 1;
} }
@ -868,7 +829,6 @@ int main(int argc, char *argv[])
if (!success) { if (!success) {
std::cerr << std::endl << ">> msr2data: **ERROR** Input file generation has not been successful! Quitting..." << std::endl; std::cerr << std::endl << ">> msr2data: **ERROR** Input file generation has not been successful! Quitting..." << std::endl;
msr2data_cleanup(msr2dataHandler, arg);
return -1; return -1;
} }
} }
@ -922,7 +882,6 @@ int main(int argc, char *argv[])
// write DB or dat file // write DB or dat file
status = msr2dataHandler->WriteOutput(outputFile, param_vec, db, writeHeader, !setNormalMode, counter); status = msr2dataHandler->WriteOutput(outputFile, param_vec, db, writeHeader, !setNormalMode, counter);
if (status == -1) { if (status == -1) {
msr2data_cleanup(msr2dataHandler, arg);
return status; return status;
} }
++counter; ++counter;
@ -959,7 +918,6 @@ int main(int argc, char *argv[])
if (!success) { if (!success) {
std::cerr << std::endl << ">> msr2data: **ERROR** Input file generation has not been successful! Quitting..." << std::endl; std::cerr << std::endl << ">> msr2data: **ERROR** Input file generation has not been successful! Quitting..." << std::endl;
msr2data_cleanup(msr2dataHandler, arg);
return -1; return -1;
} }
} }
@ -997,7 +955,6 @@ int main(int argc, char *argv[])
// if the msr-file cannot be read, write no output but proceed to the next run // if the msr-file cannot be read, write no output but proceed to the next run
status = msr2dataHandler->WriteOutput("none", param_vec, db, writeHeader); status = msr2dataHandler->WriteOutput("none", param_vec, db, writeHeader);
if (status == -1) { if (status == -1) {
msr2data_cleanup(msr2dataHandler, arg);
return status; return status;
} else { } else {
continue; continue;
@ -1012,7 +969,6 @@ int main(int argc, char *argv[])
// write DB or dat file // write DB or dat file
status = msr2dataHandler->WriteOutput(outputFile, param_vec, db, writeHeader); status = msr2dataHandler->WriteOutput(outputFile, param_vec, db, writeHeader);
if (status == -1) { if (status == -1) {
msr2data_cleanup(msr2dataHandler, arg);
return status; return status;
} }
} }
@ -1024,7 +980,7 @@ int main(int argc, char *argv[])
// Unfortunately, there are also problems with boost::filesystem::exists(outputFile) // Unfortunately, there are also problems with boost::filesystem::exists(outputFile)
// Therefore, first try to open the file for reading and if this works, write to it - not clean but it works // Therefore, first try to open the file for reading and if this works, write to it - not clean but it works
if (realOutput) { if (realOutput) {
fileOutput = new std::fstream; fileOutput = std::unique_ptr<std::fstream>(new std::fstream);
fileOutput->open(outputFile.c_str(), std::ios::in); fileOutput->open(outputFile.c_str(), std::ios::in);
if (fileOutput->is_open()) { if (fileOutput->is_open()) {
fileOutput->close(); fileOutput->close();
@ -1040,8 +996,6 @@ int main(int argc, char *argv[])
std::cerr << std::endl << ">> msr2data: **WARNING** No output has been written to the file " << outputFile << "!"; std::cerr << std::endl << ">> msr2data: **WARNING** No output has been written to the file " << outputFile << "!";
std::cerr << std::endl << ">> msr2data: **WARNING** Please check the range of runs and the specified options!" << std::endl; std::cerr << std::endl << ">> msr2data: **WARNING** Please check the range of runs and the specified options!" << std::endl;
} }
delete fileOutput;
fileOutput = 0;
} }
if (!arg.empty()) { if (!arg.empty()) {
@ -1052,8 +1006,6 @@ int main(int argc, char *argv[])
std::cout << std::endl; std::cout << std::endl;
} }
msr2data_cleanup(msr2dataHandler, arg);
std::cout << std::endl << ">> msr2data: done ..." << std::endl; std::cout << std::endl << ">> msr2data: done ..." << std::endl;
return 1; return 1;