bool worldfactory::save_world(WORLDPTR world, bool is_conversion)
{
    // if world is NULL then change it to the active_world
    if (!world) {
        world = active_world;
    }
    // if the active_world is NULL then return w/o saving
    if (!world) {
        return false;
    }

    std::ofstream fout;
    std::stringstream woption;

    woption << world->world_path << "/" << WORLD_OPTION_FILE;

    DIR *dir = opendir(world->world_path.c_str());

    if (!dir) {
        // if opendir doesn't work, the *dir pointer is empty.  If we try to close it, it creates a segfault.

#if(defined _WIN32 || defined __WIN32__)
        mkdir(world->world_path.c_str());
#else
        mkdir(world->world_path.c_str(), 0777);
#endif
        dir = opendir(world->world_path.c_str());
    }
    if (!dir) {
        // if opendir doesn't work, the *dir pointer is empty.  If we try to close it, it creates a segfault.

        DebugLog() << "Unable to create or open world[" << world->world_name << "] directory for saving\n";
        return false;
    }
    if (dir)
        closedir(dir); // don't need to keep the directory open

    if (!is_conversion) {
        fout.open(woption.str().c_str());
        if (!fout.is_open()) {
            fout.close();
            return false;
        }
        fout << world_options_header() << std::endl;

        for (std::map<std::string, cOpt>::iterator it = world->world_options.begin(); it != world->world_options.end(); ++it) {
            fout << "#" << it->second.getTooltip() << std::endl;
            fout << "#Default: " << it->second.getDefaultText() << std::endl;
            fout << it->first << " " << it->second.getValue() << std::endl << std::endl;
        }
        fout.close();
    }
    return true;
}
Exemple #2
0
bool worldfactory::save_world(WORLDPTR world, bool is_conversion)
{
    // if world is NULL then change it to the active_world
    if (!world) {
        world = active_world;
    }
    // if the active_world is NULL then return w/o saving
    if (!world) {
        return false;
    }

    std::ofstream fout;
    std::stringstream woption;

    woption << world->world_path << "/" << WORLD_OPTION_FILE;

    if (!assure_dir_exist(world->world_path)) {
        DebugLog( D_ERROR, DC_ALL ) << "Unable to create or open world[" << world->world_name <<
                                    "] directory for saving";
        return false;
    }

    if (!is_conversion) {
        fopen_exclusive(fout, woption.str().c_str());
        if (!fout.is_open()) {
            popup( _( "Could not open the world file %s, check file permissions." ), woption.str().c_str() );
            return false;
        }
        fout << world_options_header() << std::endl;

        for( auto &elem : world->world_options ) {
            fout << "#" << elem.second.getTooltip() << std::endl;
            fout << "#" << elem.second.getDefaultText() << std::endl;
            fout << elem.first << " " << elem.second.getValue() << std::endl << std::endl;
        }
        fclose_exclusive(fout, woption.str().c_str());
        if( fout.fail() ) {
            popup( _( "Failed to save the world file to %s." ), woption.str().c_str() );
        }
    }
    mman->save_mods_list(world);
    return true;
}
bool worldfactory::save_world(WORLDPTR world, bool is_conversion)
{
    // if world is NULL then change it to the active_world
    if (!world) {
        world = active_world;
    }
    // if the active_world is NULL then return w/o saving
    if (!world) {
        return false;
    }

    std::ofstream fout;
    std::stringstream woption;

    woption << world->world_path << "/" << WORLD_OPTION_FILE;

    if (!assure_dir_exist(world->world_path)) {
        DebugLog( D_ERROR, DC_ALL ) << "Unable to create or open world[" << world->world_name << "] directory for saving";
        return false;
    }

    if (!is_conversion) {
        fopen_exclusive(fout, woption.str().c_str());
        if (!fout.is_open()) {
            fout.close();
            return false;
        }
        fout << world_options_header() << std::endl;

        for (auto it = world->world_options.begin();
             it != world->world_options.end(); ++it) {
            fout << "#" << it->second.getTooltip() << std::endl;
            fout << "#Default: " << it->second.getDefaultText() << std::endl;
            fout << it->first << " " << it->second.getValue() << std::endl << std::endl;
        }
        fclose_exclusive(fout, woption.str().c_str());
    }
    mman->save_mods_list(world);
    return true;
}