add an option to musrfit which allows to replace to musrfit_startup.xml by the default one.

This commit is contained in:
2023-11-11 18:26:25 +01:00
parent f72a213bd8
commit df926a4d66
3 changed files with 53 additions and 27 deletions

View File

@@ -91,7 +91,7 @@ int parseXmlFile(TSAXParser *saxParser, const char *startup_path_name)
/**
* <p>Constructor. Check if the musrfit_startup.xml file is found in some standard search paths
*/
PStartupHandler::PStartupHandler()
PStartupHandler::PStartupHandler(bool reset_startup_file)
{
fStartupFileFound = false;
fStartupFilePath = "";
@@ -145,6 +145,17 @@ PStartupHandler::PStartupHandler()
}
}
// musrfit_startup.xml found. Check if it should be rewritten
if (fStartupFileFound && reset_startup_file) {
std::cout << std::endl;
std::cout << ">> Will only reset the file: '" << fStartupFilePath.Data() << "'."<< std::endl;
std::cout << std::endl;
if (!WriteDefaultStartupFile(reset_startup_file)) {
std::cerr << std::endl << "**ERROR** couldn't re-write " << fStartupFilePath.Data() << "." << std::endl;
return;
}
}
// if musrfit_startup.xml is still not found, will create a default one
if (!fStartupFileFound) {
std::cout << std::endl << "**INFO** no musrfit_startup.xml file found, will write a default one." << std::endl;
@@ -617,33 +628,37 @@ Bool_t PStartupHandler::StartupFileExists(Char_t *fln)
//--------------------------------------------------------------------------
// WriteDefaultStartupFile
//--------------------------------------------------------------------------
Bool_t PStartupHandler::WriteDefaultStartupFile()
Bool_t PStartupHandler::WriteDefaultStartupFile(bool reset_startup_file)
{
// get home
Char_t startup_path_name[256];
Char_t *home = nullptr;
home = getenv("HOME");
if (home == nullptr) {
std::cerr << std::endl << "**ERROR** couldn't obtain $HOME." << std::endl;
return false;
}
// first check that $HOME/.musrfit exists and if NOT create it
struct stat info;
snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit", home);
if (!stat(startup_path_name, &info)) {
if (!(info.st_mode & S_IFDIR))
return false;
} else {
if (mkdir(startup_path_name, 0777)) {
std::cerr << std::endl << "**ERROR** couldn't create '" << startup_path_name << "'" << std::endl;
if (reset_startup_file) { // reset the found
snprintf(startup_path_name, sizeof(startup_path_name), "%s", fStartupFilePath.Data());
} else { // no musrfit_startup.xml found, hence write default under $HOME/.musrfit
// get home
Char_t *home = nullptr;
home = getenv("HOME");
if (home == nullptr) {
std::cerr << std::endl << "**ERROR** couldn't obtain $HOME." << std::endl;
return false;
}
}
// set path-name for musrfit_startup.xml
snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit/musrfit_startup.xml", home);
// first check that $HOME/.musrfit exists and if NOT create it
struct stat info;
snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit", home);
if (!stat(startup_path_name, &info)) {
if (!(info.st_mode & S_IFDIR))
return false;
} else {
if (mkdir(startup_path_name, 0777)) {
std::cerr << std::endl << "**ERROR** couldn't create '" << startup_path_name << "'" << std::endl;
return false;
}
}
// set path-name for musrfit_startup.xml
snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit/musrfit_startup.xml", home);
}
std::ofstream fout(startup_path_name, std::ofstream::out);
if (!fout.is_open()) {