diff options
author | Ton Roosendaal <ton@blender.org> | 2005-07-27 23:46:06 +0400 |
---|---|---|
committer | Ton Roosendaal <ton@blender.org> | 2005-07-27 23:46:06 +0400 |
commit | 410512e2656007587cc1671c71c37cbc0b5bda4f (patch) | |
tree | 4c09eac0d6de21a38e0d355ea7cc71b46174cf81 /source/blender/blenloader | |
parent | cd6cbda7dba939cec05b820e4a09ad0a5770d2b7 (diff) |
Patch provided by Shaul Kedem: Compressed files are back!
He even made a nice doc in wiki:
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz
Usage: set the option "Compress File" in the main "File" pulldown menu.
This setting is a user-def, meaning it is not changed on reading files.
If you want it default, save it with CTRL+U.
The longest debate went over the file naming convention. Shaul started
with .blend.gz files, which gave issues in Blender because of the code
hanging out everywhere that detects blender files, and that appends the
.blend extension if needed.
Daniel Dunbar proposed to just save it as .blend, and not bother users
with such details. This is indeed the most elegant solution, with as
only drawback that old Blender executables cannot read it.
This drawback isn't very relevant at the moment, since we're heading
towards a release that isn't upward compatible anyway... the recode
going on on Meshes, Modfiers, Armatures, Poses, Actions, NLA already
have upward compatibility issues.
We might check - during the next month(s) - on a builtin system to
warn users in the future when we change things that make a file risky
to read in an older release.
Diffstat (limited to 'source/blender/blenloader')
-rw-r--r-- | source/blender/blenloader/intern/readfile.c | 37 | ||||
-rw-r--r-- | source/blender/blenloader/intern/readfile.h | 4 | ||||
-rw-r--r-- | source/blender/blenloader/intern/writefile.c | 21 |
3 files changed, 55 insertions, 7 deletions
diff --git a/source/blender/blenloader/intern/readfile.c b/source/blender/blenloader/intern/readfile.c index cb37ac1ff01..b916914fd50 100644 --- a/source/blender/blenloader/intern/readfile.c +++ b/source/blender/blenloader/intern/readfile.c @@ -141,6 +141,10 @@ #include "mydevice.h" #include "blendef.h" +#include "zlib.h" + +#include <errno.h> + /* Remark: still a weak point is the newadress() function, that doesnt solve reading from multiple files at the same time @@ -774,6 +778,19 @@ static int fd_read_from_file(FileData *filedata, void *buffer, int size) return (readsize); } +static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size) +{ + int readsize = gzread(filedata->gzfiledes, buffer, size); + + if (readsize < 0) { + readsize = EOF; + } else { + filedata->seek += readsize; + } + + return (readsize); +} + static int fd_read_from_memory(FileData *filedata, void *buffer, int size) { // don't read more bytes then there are available in the buffer @@ -840,6 +857,7 @@ static FileData *filedata_new(void) FileData *fd = MEM_callocN(sizeof(FileData), "FileData"); fd->filedes = -1; + fd->gzfiledes = NULL; /* XXX, this doesn't need to be done all the time, * but it keeps us reentrant, remove once we have @@ -875,7 +893,7 @@ static FileData *blo_decode_and_check(FileData *fd, BlendReadError *error_r) FileData *blo_openblenderfile(char *name, BlendReadError *error_r) { - int file; + gzFile gzfile; char name1[FILE_MAXDIR+FILE_MAXFILE]; /* library files can have stringcodes */ @@ -885,17 +903,16 @@ FileData *blo_openblenderfile(char *name, BlendReadError *error_r) else strcpy(G.sce, name); // global... is set in blender.c setup_app_data too. should be part of Main immediate? - file= open(name1, O_BINARY|O_RDONLY); + gzfile= gzopen(name1, "rb"); - if (file == -1) { + if (NULL == gzfile) { *error_r = BRE_UNABLE_TO_OPEN; return NULL; } else { FileData *fd = filedata_new(); - fd->filedes = file; + fd->gzfiledes = gzfile; BLI_strncpy(fd->filename, name, sizeof(fd->filename)); // now only in use by library append - fd->buffersize = BLI_filesize(file); - fd->read = fd_read_from_file; + fd->read = fd_read_gzip_from_file; return blo_decode_and_check(fd, error_r); } @@ -937,10 +954,16 @@ FileData *blo_openblendermemfile(MemFile *memfile, BlendReadError *error_r) void blo_freefiledata(FileData *fd) { if (fd) { + if (fd->filedes != -1) { close(fd->filedes); } + if (fd->gzfiledes != NULL) + { + gzclose(fd->gzfiledes); + } + if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) { MEM_freeN(fd->buffer); fd->buffer = 0; @@ -971,7 +994,7 @@ void blo_freefiledata(FileData *fd) int BLO_has_bfile_extension(char *str) { - return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")); + return (BLI_testextensie(str, ".ble") || BLI_testextensie(str, ".blend")||BLI_testextensie(str, ".blend.gz")); } /* ************** OLD POINTERS ******************* */ diff --git a/source/blender/blenloader/intern/readfile.h b/source/blender/blenloader/intern/readfile.h index 6389cfaa6f5..efdfa9f76bf 100644 --- a/source/blender/blenloader/intern/readfile.h +++ b/source/blender/blenloader/intern/readfile.h @@ -33,6 +33,8 @@ #ifndef READFILE_H #define READFILE_H +#include "zlib.h" + struct OldNewMap; struct MemFile; @@ -52,6 +54,8 @@ typedef struct FileData { // variables needed for reading from file int filedes; + gzFile gzfiledes; + // now only in use for library appending char filename[FILE_MAXDIR+FILE_MAXFILE]; diff --git a/source/blender/blenloader/intern/writefile.c b/source/blender/blenloader/intern/writefile.c index 61c7911ffca..3ae58e92b03 100644 --- a/source/blender/blenloader/intern/writefile.c +++ b/source/blender/blenloader/intern/writefile.c @@ -167,6 +167,9 @@ Important to know is that 'streaming' has been added to files, for Blender Publi #include "readfile.h" #include "genfile.h" +#include "zlib.h" + +#include <errno.h> /* ********* my write, buffered writing with minimum 50k chunks ************ */ @@ -1607,10 +1610,28 @@ int BLO_write_file(char *dir, int write_flags, char **error_r) close(file); if(!fout) { + if(write_flags & G_FILE_COMPRESS) + { + // compressed files have the same ending as regular files... only from 2.4!!! + + int ret = BLI_gzip(tempname, dir); + + if(-1==ret) { + *error_r= "Failed opening .gz file"; + return 0; + } + if(-2==ret) { + *error_r= "Failed opening .blend file for compression"; + return 0; + } + } + else if(BLI_rename(tempname, dir) < 0) { *error_r= "Can't change old file. File saved with @"; return 0; } + + } else { remove(tempname); |