Exemple #1
0
/**
 * \brief Saves the data into a Lua file.
 * \param[in] file_name Path of the file to save.
 * The file will be encoded in UTF-8.
 * \return \c true in case of success, \c false if the data
 * could not be exported.
 */
bool LuaData::export_to_file(const std::string& file_name) const {

  // Work on a temporary file to keep the initial one intact in case of failure.
  std::string tmp_file_name = file_name + ".solarus_tmp";
  std::ofstream tmp_out(tmp_file_name);
  if (!tmp_out) {
    return false;
  }

  if (!export_to_lua(tmp_out)) {
    std::remove(tmp_file_name.c_str());
    return false;
  }
  tmp_out.flush();
  tmp_out.close();

  std::ifstream in(tmp_file_name);
  if (!in) {
    return false;
  }
  std::ofstream out(file_name);
  if (!out) {
    return false;
  }
  out << in.rdbuf();

  in.close();
  std::remove(tmp_file_name.c_str());
  out.flush();
  return true;
}
Exemple #2
0
/**
 * \brief Saves this object into memory as Lua.
 * \param[out] buffer The buffer to write.
 * Text will be encoded in UTF-8.
 * \return \c true in case of success, \c false if the data
 * could not be exported.
 */
bool LuaData::export_to_buffer(std::string& buffer) const {

  std::ostringstream oss;
  if (!export_to_lua(oss)) {
    return false;
  }

  buffer = oss.str();
  return true;
}
Exemple #3
0
/**
 * \brief Saves the data into a Lua file.
 * \param[in] file_name Path of the file to save.
 * The file will be encoded in UTF-8.
 * \return \c true in case of success, \c false if the data
 * could not be exported.
 */
bool LuaData::export_to_file(const std::string& file_name) const {

  // Work on a temporary file to keep the initial one intact in case of failure.
  std::string tmp_file_name = file_name + ".solarus_tmp";
  std::ofstream out(tmp_file_name);
  if (!out) {
    return false;
  }

  if (!export_to_lua(out)) {
    std::remove(tmp_file_name.c_str());
    return false;
  }

  if (std::rename(tmp_file_name.c_str(), file_name.c_str()) != 0) {
    return false;
  }

  return true;
}