advgetopt/conf_file.cpp
/** \brief Save the configuration file.
 *
 * This function saves the current data from this configuration file to
 * the file. It overwrites the existing file.
 *
 * Note that when you load the configuration, you may get data from
 * many different configuration files. This very file will only
 * include the data that was loaded from this file, though, and whatever
 * modifications you made.
 *
 * If the conf is not marked as modified, the function returns immediately
 * with true.
 *
 * \param[in] create_backup  Whether to create a backup or not.
 *
 * \return true if the save worked as expected.
 */
\brief Save the configuration file.
This function saves the current data from this configuration file to
the file. It overwrites the existing file.

Note that when you load the configuration, you may get data from
many different configuration files. This very file will only
include the data that was loaded from this file, though, and whatever
modifications you made.

If the conf is not marked as modified, the function returns immediately
with true.

\param[in] create_backup  Whether to create a backup or not.

\return true if the save worked as expected.
\ brief Save the configuration file . This function saves the current data from this configuration file to the file . It overwrites the existing file . Note that when you load the configuration you may get data from many different configuration files . This very file will only include the data that was loaded from this file though and whatever modifications you made . If the conf is not marked as modified the function returns immediately with true . \ param [ in ] create_backup Whether to create a backup or not . \ return true if the save worked as expected .
bool conf_file::save_configuration(bool create_backup)
{
    if(f_modified)
    {
        if(create_backup)
        {
             TODO: offer means to set the backup extension
            std::string const backup_filename(f_setup.get_filename() + ".bak");
            if(unlink(backup_filename.c_str()) != 0
            && errno != ENOENT)
            {
                f_errno = errno;    LCOV_EXCL_LINE
                return false;       LCOV_EXCL_LINE
            }
            if(rename(f_setup.get_filename().c_str(), backup_filename.c_str()) != 0)
            {
                f_errno = errno;    LCOV_EXCL_LINE
                return false;       LCOV_EXCL_LINE
            }
        }
         save parameters to file
        std::ofstream conf;
        conf.open(f_setup.get_filename().c_str());
        if(!conf.is_open())
        {
            f_errno = errno;    LCOV_EXCL_LINE
            return false;       LCOV_EXCL_LINE
        }
        time_t const now(time(nullptr));
        tm t;
        gmtime_r(&now, &t);
        char str_date[16];
        strftime(str_date, sizeof(str_date), "%Y/%m/%d", &t);
        char str_time[16];
        strftime(str_time, sizeof(str_time), "%H:%M:%S", &t);
         header warning with date & time
        conf << "# This file was auto-generated by snap_config.cpp on " << str_date << " at " << str_time << "." << std::endl
             << "# Making modifications here is likely safe unless the tool handling this" << std::endl
             << "# configuration file is actively working on it while you do the edits." << std::endl;
        for(auto p : f_parameters)
        {
            conf << p.first << "=";
             prevent saving \r and \n characters as is when part of the
             value; also double \ otherwise reading those back would fail
            std::string value(p.second);
            boost::replace_all(value, "\\", "\\\\");
            boost::replace_all(value, "\r", "\\r");
            boost::replace_all(value, "\n", "\\n");
            boost::replace_all(value, "\t", "\\t");
            conf << value << std::endl;
            if(!conf)
            {
                return false;    LCOV_EXCL_LINE
            }
        }
         it all worked, it's considered saved now
        f_modified = false;
    }
    return true;
}
bool conf_file :: save_configuration ( bool create_backup ) { if ( f_modified ) { if ( create_backup ) { std :: string const backup_filename ( f_setup . get_filename ( ) + " " ) ; if ( unlink ( backup_filename . c_str ( ) ) != 0 && errno != ENOENT ) { f_errno = errno ; return false ; } if ( rename ( f_setup . get_filename ( ) . c_str ( ) , backup_filename . c_str ( ) ) != 0 ) { f_errno = errno ; return false ; } } std :: ofstream conf ; conf . open ( f_setup . get_filename ( ) . c_str ( ) ) ; if ( ! conf . is_open ( ) ) { f_errno = errno ; return false ; } time_t const now ( time ( nullptr ) ) ; tm t ; gmtime_r ( & now , & t ) ; char str_date [ 16 ] ; strftime ( str_date , sizeof ( str_date ) , " " , & t ) ; char str_time [ 16 ] ; strftime ( str_time , sizeof ( str_time ) , " " , & t ) ; conf << " " << str_date << " " << str_time << " " << std :: endl << " " << std :: endl << " " << std :: endl ; for ( auto p : f_parameters ) { conf << p . first << " " ; std :: string value ( p . second ) ; boost :: replace_all ( value , " \\ " , " \\ \\ " ) ; boost :: replace_all ( value , " \r " , " \\ " ) ; boost :: replace_all ( value , " \n " , " \\ " ) ; boost :: replace_all ( value , " \t " , " \\ " ) ; conf << value << std :: endl ; if ( ! conf ) { return false ; } } f_modified = false ; } return true ; }
\brief Save the configuration file.
\ brief Save the configuration file .
// create backup? // // TODO: offer means to set the backup extension // // LCOV_EXCL_LINE // LCOV_EXCL_LINE // LCOV_EXCL_LINE // LCOV_EXCL_LINE // save parameters to file // // LCOV_EXCL_LINE // LCOV_EXCL_LINE // header warning with date & time // // prevent saving \r and \n characters as is when part of the // value; also double \ otherwise reading those back would fail // // LCOV_EXCL_LINE // it all worked, it's considered saved now //