bool FileContainerTemporary::save_temporary() const { xmlpp::Document document; xmlpp::Element *root = document.create_root_node("temporary-container"); root->add_child("container-filename")->set_child_text(container_filename_); xmlpp::Element *files = root->add_child("files"); for(FileMap::const_iterator i = files_.begin(); i != files_.end(); i++) { xmlpp::Element *entry = files->add_child("entry"); entry->add_child("name")->set_child_text(i->second.name); entry->add_child("tmp-basename")->set_child_text(basename(i->second.tmp_filename)); entry->add_child("is-directory")->set_child_text(i->second.is_directory ? "true" : "false"); entry->add_child("is-removed")->set_child_text(i->second.is_removed ? "true" : "false"); } FileSystem::WriteStreamHandle stream = file_system_->get_write_stream( get_temporary_directory() + get_temporary_filename_base()); if (!stream) return false; stream = new ZWriteStream(stream); try { document.write_to_stream_formatted(*stream, "UTF-8"); } catch(...) { synfig::error("FileContainerTemporary::save_temporary(): Caught unknown exception"); return false; } stream.reset(); return true; }
bool synfig::save_canvas(const FileSystem::Identifier &identifier, Canvas::ConstHandle canvas, bool safe) { ChangeLocale change_locale(LC_NUMERIC, "C"); synfig::String tmp_filename(safe ? identifier.filename+".TMP" : identifier.filename); try { assert(canvas); xmlpp::Document document; encode_canvas_toplevel(document.create_root_node("canvas"),canvas); FileSystem::WriteStreamHandle stream = identifier.file_system->get_write_stream(tmp_filename); if (!stream) { synfig::error("synfig::save_canvas(): Unable to open file for write"); return false; } if (filename_extension(identifier.filename) == ".sifz") stream = FileSystem::WriteStreamHandle(new ZWriteStream(stream)); document.write_to_stream_formatted(*stream, "UTF-8"); // close stream stream.reset(); if (safe) { #ifdef _WIN32 // On Win32 platforms, rename() has bad behavior. work around it. char old_file[80]="sif.XXXXXXXX"; mktemp(old_file); identifier.file_system->file_rename(identifier.filename,old_file); if(!identifier.file_system->file_rename(tmp_filename,identifier.filename)) { identifier.file_system->file_rename(old_file,tmp_filename); synfig::error("synfig::save_canvas(): Unable to rename file to correct filename"); return false; } identifier.file_system->file_remove(old_file); #else if(!identifier.file_system->file_rename(tmp_filename, identifier.filename)) { synfig::error("synfig::save_canvas(): Unable to rename file to correct filename"); return false; } #endif } } catch(...) { synfig::error("synfig::save_canvas(): Caught unknown exception"); return false; } return true; }
bool synfig::save_canvas(const FileSystem::Identifier &identifier, Canvas::ConstHandle canvas, bool safe) { ChangeLocale change_locale(LC_NUMERIC, "C"); synfig::String tmp_filename(safe ? identifier.filename+".TMP" : identifier.filename); try { assert(canvas); xmlpp::Document document; encode_canvas_toplevel(document.create_root_node("canvas"),canvas); FileSystem::WriteStreamHandle stream = identifier.file_system->get_write_stream(tmp_filename); if (!stream) { synfig::error("synfig::save_canvas(): Unable to open file for write"); return false; } if (filename_extension(identifier.filename) == ".sifz") stream = FileSystem::WriteStreamHandle(new ZWriteStream(stream)); document.write_to_stream_formatted(*stream, "UTF-8"); // close stream stream.reset(); if (safe) { if(!identifier.file_system->file_rename(tmp_filename, identifier.filename)) { synfig::error("synfig::save_canvas(): Unable to rename file to correct filename"); return false; } } } catch(...) { synfig::error("synfig::save_canvas(): Caught unknown exception"); return false; } return true; }
void DebugSurface::save_to_file(const void *buffer, int width, int height, int pitch, const String &filename, bool overwrite) { // generate filename String actual_filename = overwrite ? filename : FileContainerTemporary::generate_indexed_temporary_filename( FileSystemNative::instance(), filename + ".tga" ); if (buffer == NULL || width <= 0 || height <= 0) { // save empty file for empty surface FileSystemNative::instance() ->get_write_stream(actual_filename); } else { // convert to 32BPP if (pitch == 0) pitch = width * sizeof(Color); PixelFormat pf(PF_BGR|PF_A); size_t total_bytes = width * height * synfig::channels(pf); unsigned char *byte_buffer = (unsigned char*)malloc(total_bytes); if (!byte_buffer) return; // write rows in reverse order (for TGA format) unsigned char *dest(byte_buffer); Gamma gamma; for(int y = height-1; y >= 0; --y) { const Color *src = (const Color *)((const unsigned char*)buffer + pitch*y); for(int x = 0; x < width; ++x, ++src) dest = Color2PixelFormat( src->clamped(), pf, dest, gamma ); } // create file FileSystem::WriteStreamHandle ws = FileSystemNative::instance() ->get_write_stream(actual_filename); // write header unsigned char targa_header[] = { 0, // Length of the image ID field (0 - no ID field) 0, // Whether a color map is included (0 - no colormap) 2, // Compression and color types (2 - uncompressed true-color image) 0, 0, 0, 0, 0, // Color map specification (not need for us) 0, 0, // X-origin 0, 0, // Y-origin (unsigned char)(width & 0xff), // Image width (unsigned char)(width >> 8), (unsigned char)(height & 0xff), // Image height (unsigned char)(height >> 8), 32, // Bits per pixel 0 // Image descriptor (keep zero for capability) }; ws->write_whole_block(targa_header, sizeof(targa_header)); // write data ws->write_whole_block(byte_buffer, total_bytes); free(byte_buffer); } }