SavegameWriter.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (C) 2003-2005 The Pentagram team
00003 
00004 This program is free software; you can redistribute it and/or
00005 modify it under the terms of the GNU General Public License
00006 as published by the Free Software Foundation; either version 2
00007 of the License, or (at your option) any later version.
00008 
00009 This program is distributed in the hope that it will be useful,
00010 but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 GNU General Public License for more details.
00013 
00014 You should have received a copy of the GNU General Public License
00015 along with this program; if not, write to the Free Software
00016 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 */
00018 
00019 #include "pent_include.h"
00020 
00021 #include "SavegameWriter.h"
00022 #include "ODataSource.h"
00023 
00024 // zip API
00025 #include "zip.h"
00026 
00027 // ioapi ODataSource wrapper functions
00028 
00029 static voidpf ods_open(voidpf opaque, const char* filename, int mode);
00030 static uLong ods_read(voidpf opaque, voidpf stream, void* buf, uLong size);
00031 static uLong ods_write(voidpf opaque, voidpf stream,
00032                                            const void* buf, uLong size);
00033 static long ods_tell(voidpf opaque, voidpf stream);
00034 static long ods_seek(voidpf opaque, voidpf stream, uLong offset, int origin);
00035 static int ods_close(voidpf opaque, voidpf stream);
00036 static int ods_error(voidpf opaque, voidpf stream);
00037 
00038 PentZip::zlib_filefunc_def ODS_filefunc_templ = {
00039         ods_open, ods_read, ods_write, ods_tell, ods_seek, ods_close, ods_error, 0
00040 };
00041 
00042 
00043 
00044 SavegameWriter::SavegameWriter(ODataSource* ds_)
00045 {
00046         ds = ds_;
00047 
00048         PentZip::zlib_filefunc_def filefuncs = ODS_filefunc_templ;
00049         filefuncs.opaque = static_cast<void*>(ds);
00050 
00051         PentZip::zipFile zfile = PentZip::zipOpen2("", 0, 0, &filefuncs);
00052         zipfile = static_cast<void*>(zfile);
00053 }
00054 
00055 SavegameWriter::~SavegameWriter()
00056 {
00057         if (ds)
00058                 delete ds;
00059         ds = 0;
00060 }
00061 
00062 bool SavegameWriter::finish()
00063 {
00064         PentZip::zipFile zfile = static_cast<PentZip::zipFile>(zipfile);
00065         zipfile = 0;
00066         if (PentZip::zipClose(zfile, comment.c_str()) != ZIP_OK) return false;
00067 
00068         return true;
00069 }
00070 
00071 
00072 bool SavegameWriter::writeFile(const char* name,
00073                                                            const uint8* data, uint32 size)
00074 {
00075         PentZip::zipFile zfile = static_cast<PentZip::zipFile>(zipfile);
00076         perr << name << ": " << size << std::endl;
00077 
00078         // Because zlib's deflate causes false positives in valgrind,
00079         // check the data to be saved manually, so deflate can be
00080         // suppressed safely.
00081         VALGRIND_CHECK_READABLE(data, size);
00082 
00083         if (PentZip::zipOpenNewFileInZip(zfile, name, 0, 0, 0, 0, 0, 0,
00084                                                                          Z_DEFLATED, Z_BEST_COMPRESSION) != ZIP_OK)
00085                 return false;
00086         
00087         if (PentZip::zipWriteInFileInZip(zfile, data, size) != ZIP_OK)
00088                 return false;
00089 
00090         if (PentZip::zipCloseFileInZip(zfile) != ZIP_OK)
00091                 return false;
00092 
00093         return true;
00094 }
00095 
00096 bool SavegameWriter::writeFile(const char* name, OAutoBufferDataSource* ods)
00097 {
00098         return writeFile(name, ods->getBuf(), ods->getSize());
00099 }
00100 
00101 bool SavegameWriter::writeVersion(uint32 version)
00102 {
00103         uint8 buf[4];
00104         buf[0] = version & 0xFF;
00105         buf[1] = (version >> 8) & 0xFF;
00106         buf[2] = (version >> 16) & 0xFF;
00107         buf[3] = (version >> 24) & 0xFF;
00108         return writeFile("VERSION", buf, 4);
00109 }
00110 
00111 bool SavegameWriter::writeDescription(const std::string& desc)
00112 {
00113         comment = desc;
00114         return true;
00115 }
00116 
00117 
00118 // ------------
00119 
00120 static voidpf ods_open(voidpf opaque, const char* filename, int mode)
00121 {
00122         // write-only, for now
00123 //      if (mode != (ZLIB_FILEFUNC_MODE_WRITE | ZLIB_FILEFUNC_MODE_CREATE))
00124 //              return 0;
00125 
00126         // opaque is actually the ODataSource*
00127         return opaque;
00128 }
00129 
00130 static uLong ods_read(voidpf opaque, voidpf stream, void* buf, uLong size)
00131 {
00132         return 0;
00133 }
00134 
00135 static uLong ods_write(voidpf opaque, voidpf stream,
00136                                            const void* buf, uLong size)
00137 {
00138         ODataSource* ods = static_cast<ODataSource*>(stream);
00139         ods->write(buf, size);
00140         return size;
00141 }
00142 
00143 static long ods_tell(voidpf opaque, voidpf stream)
00144 {
00145         ODataSource* ods = static_cast<ODataSource*>(stream);
00146         return ods->getPos();
00147 }
00148 
00149 static long ods_seek(voidpf opaque, voidpf stream, uLong offset, int origin)
00150 {
00151         ODataSource* ods = static_cast<ODataSource*>(stream);
00152         switch (origin) {
00153         case ZLIB_FILEFUNC_SEEK_CUR:
00154                 ods->skip(offset);
00155                 break;
00156         case ZLIB_FILEFUNC_SEEK_END:
00157                 ods->seek(ods->getSize()+offset);
00158                 break;
00159         case ZLIB_FILEFUNC_SEEK_SET:
00160                 ods->seek(offset);
00161                 break;
00162         default:
00163                 return -1;
00164         }
00165         return 0;
00166 }
00167 
00168 static int ods_close(voidpf opaque, voidpf stream)
00169 {
00170         return 0;
00171 }
00172 
00173 static int ods_error(voidpf opaque, voidpf stream)
00174 {
00175         return 0;
00176 }
00177 

Generated on Fri Jul 27 22:27:33 2007 for pentagram by  doxygen 1.4.7