replaced raw pointers by smart pointers for dump_header.cpp.

This commit is contained in:
suter_a 2023-10-13 12:33:47 +02:00
parent bad6812b2b
commit 4d52fa778b

View File

@ -43,6 +43,7 @@
#include <sstream>
#include <string>
#include <vector>
#include <memory>
#include <boost/algorithm/string.hpp>
@ -145,7 +146,7 @@ int dump_header_root(const std::string fileName, const bool summary, const bool
fileType = DH_LEM_ROOT;
}
TMusrRunHeader *header{nullptr};
std::unique_ptr<TMusrRunHeader> header;
bool ok;
Int_t ival;
@ -154,7 +155,7 @@ int dump_header_root(const std::string fileName, const bool summary, const bool
TLemRunHeader* runHeader = dynamic_cast<TLemRunHeader*>(folder->FindObjectAny("TLemRunHeader"));
// check if run header is valid
if (!runHeader) {
if (runHeader == nullptr) {
std::cerr << std::endl << "**ERROR** Couldn't obtain run header info from ROOT file " << fileName << std::endl;
f.Close();
return 1;
@ -197,12 +198,10 @@ int dump_header_root(const std::string fileName, const bool summary, const bool
std::cout << std::endl << "First Good Bin : " << timeZero[0];
std::cout << std::endl << "Last Good Bin : " << runHeader->GetNChannels()-1;
std::cout << std::endl << "-------------------" << std::endl << std::endl;
delete runHeader;
} else { // MusrRoot
// invoke the MusrRoot header object
header = new TMusrRunHeader(fileName.c_str(), true); // read quite
if (header == 0) {
header = std::unique_ptr<TMusrRunHeader>(new TMusrRunHeader(fileName.c_str(), true)); // read quite
if (header == nullptr) {
std::cerr << std::endl << "**ERROR** Couldn't invoke MusrRoot RunHeader in file:" << fileName;
std::cerr << std::endl;
f.Close();
@ -306,9 +305,6 @@ int dump_header_root(const std::string fileName, const bool summary, const bool
f.Close();
if (header)
delete header;
return 0;
}
@ -324,7 +320,7 @@ int dump_header_root(const std::string fileName, const bool summary, const bool
int dump_header_nexus(const std::string fileName, const bool counts) {
#ifdef PNEXUS_ENABLED
PNeXus *nxs_file = new PNeXus(fileName.c_str());
std::unique_ptr<PNeXus> nxs_file = std::unique_ptr<PNeXus>(new PNeXus(fileName.c_str()));
if (nxs_file == nullptr) {
std::cerr << std::endl;
@ -341,9 +337,6 @@ int dump_header_nexus(const std::string fileName, const bool counts) {
std::cerr << std::endl;
return 1;
}
if (nxs_file)
delete nxs_file;
#else
std::cout << std::endl << "NeXus not enabled, hence the header information cannot be dumped." << std::endl << std::endl;
#endif
@ -937,40 +930,22 @@ int main(int argc, char *argv[])
// invoke the startup handler in order to get the default search paths to the data files
// read startup file
char startup_path_name[128];
TSAXParser *saxParser = new TSAXParser();
PStartupHandler *startupHandler = new PStartupHandler();
std::unique_ptr<TSAXParser> saxParser = std::unique_ptr<TSAXParser>(new TSAXParser());
std::unique_ptr<PStartupHandler> startupHandler = std::unique_ptr<PStartupHandler>(new PStartupHandler());
if (!startupHandler->StartupFileFound()) {
std::cerr << std::endl << ">> musrfit **WARNING** couldn't find " << startupHandler->GetStartupFilePath().Data();
std::cerr << std::endl;
// clean up
if (saxParser) {
delete saxParser;
saxParser = nullptr;
}
if (startupHandler) {
delete startupHandler;
startupHandler = nullptr;
}
} else {
strcpy(startup_path_name, startupHandler->GetStartupFilePath().Data());
saxParser->ConnectToHandler("PStartupHandler", startupHandler);
saxParser->ConnectToHandler("PStartupHandler", startupHandler.get());
//status = saxParser->ParseFile(startup_path_name);
// parsing the file as above seems to lead to problems in certain environments;
// use the parseXmlFile function instead (see PStartupHandler.cpp for the definition)
int status = parseXmlFile(saxParser, startup_path_name);
int status = parseXmlFile(saxParser.get(), startup_path_name);
// check for parse errors
if (status) { // error
std::cerr << std::endl << ">> musrfit **WARNING** Reading/parsing musrfit_startup.xml failed.";
std::cerr << std::endl;
// clean up
if (saxParser) {
delete saxParser;
saxParser = nullptr;
}
if (startupHandler) {
delete startupHandler;
startupHandler = nullptr;
}
}
}
@ -1034,15 +1009,5 @@ int main(int argc, char *argv[])
dump_header_wkm(pathFln);
}
// cleanup
if (saxParser) {
delete saxParser;
saxParser = nullptr;
}
if (startupHandler) {
delete startupHandler;
startupHandler = nullptr;
}
return 0;
}