[Bf-committers] zlib patch

Shaul Kedem shaul.kedem at gmail.com
Sat Jul 9 13:02:52 CEST 2005


Hi all,
 First, let me introduce myself; my name is Shaul Kedem, I am 30 and
in the coding/developing/managing business for about 10 years now.

 Sirdude suggested I'll give a go at the zlib stuff, so I did - see
attached diff from current CVS.

 I've also wrote a couple of notes on the wiki, check it out at
http://wiki.blender.org/bin/view.pl/Blenderdev/Blendgz

 intrr, in the course of reviewing the code, suggested that the user
preferences will be added with the compression option as well, if
anyone like to propose where exactly in blender this should be done
I'll gladly hear,

 constructive criticism is welcome,

Regards,
Shaul
-------------- next part --------------
? Hypersrc.pl
? YBtest.tga
? YBtest.xml
? blender
? blenderplayer
? build.log
? config.opts
? config.opts.old.1
? ctags.pl
? doit.sh
? hypersrc
? intrr.txt
? kaito.txt
? obj/2.34
? obj/blender-2.34-linux-glibc2.3.3-i386
? obj/linux-glibc2.3.3-i386
? tools/__init__.pyc
? tools/scons/__init__.pyc
? tools/scons/bs/__init__.pyc
? tools/scons/bs/bs_arc.pyc
? tools/scons/bs/bs_bincopy.pyc
? tools/scons/bs/bs_clean.pyc
? tools/scons/bs/bs_config.pyc
? tools/scons/bs/bs_default.pyc
? tools/scons/bs/bs_dirs.pyc
? tools/scons/bs/bs_globals.pyc
? tools/scons/bs/bs_libs.pyc
? tools/scons/bs/bs_nsis.pyc
Index: source/blender/blenkernel/intern/exotic.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/blenkernel/intern/exotic.c,v
retrieving revision 1.40
diff -u -r1.40 exotic.c
--- source/blender/blenkernel/intern/exotic.c	3 Jul 2005 17:35:31 -0000	1.40
+++ source/blender/blenkernel/intern/exotic.c	8 Jul 2005 14:59:36 -0000
@@ -2399,7 +2399,7 @@
 			read(file, str, 31);
 			close(file);
 
-			if ((*s0 != FORM) && (strncmp(str, "BLEN", 4) != 0)) {
+			if ((*s0 != FORM) && (strncmp(str, "BLEN", 4) != 0) && !BLI_testextensie(name,".blend.gz")) {
 
 				waitcursor(1);
 				
Index: source/blender/blenlib/BLI_blenlib.h
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/blenlib/BLI_blenlib.h,v
retrieving revision 1.13
diff -u -r1.13 BLI_blenlib.h
--- source/blender/blenlib/BLI_blenlib.h	20 May 2005 12:18:11 -0000	1.13
+++ source/blender/blenlib/BLI_blenlib.h	8 Jul 2005 14:59:37 -0000
@@ -256,6 +256,7 @@
 int   BLI_exists(char *file);
 int   BLI_copy_fileops(char *file, char *to);
 int   BLI_rename(char *from, char *to);
+int   BLI_gzip(char *from, char *to);
 int   BLI_delete(char *file, int dir, int recursive);
 int   BLI_move(char *file, char *to);
 int   BLI_touch(char *file);
Index: source/blender/blenlib/intern/fileops.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/blenlib/intern/fileops.c,v
retrieving revision 1.6
diff -u -r1.6 fileops.c
--- source/blender/blenlib/intern/fileops.c	9 Mar 2005 19:45:54 -0000	1.6
+++ source/blender/blenlib/intern/fileops.c	8 Jul 2005 14:59:38 -0000
@@ -55,6 +55,14 @@
 #include "BLI_fileops.h"
 #include "BLI_callbacks.h"
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "BKE_utildefines.h"
+#include "zlib.h"
+#include <errno.h>
+
 /* implementations: */
 char *first_slash(char *string) {
 	char *ffslash, *fbslash;
@@ -301,6 +309,37 @@
 	if (BLI_exists(to))	if(BLI_delete(to, 0, 0)) return 1;
 		
 	return rename(from, to);
+}
+
+/* gzip the file in from and write it to "to". 
+ return -1 if zlib fails, -2 if the originating file does not exist
+ note: will remove the "from" file
+  */
+int BLI_gzip(char *from, char *to) {
+	char buffer[10240];
+	int file;
+	int readsize = 0;
+	
+	gzFile gzfile = gzopen(to,"wb"); 
+	if (NULL == gzfile) return -1;
+	
+	file = open(from,O_BINARY|O_RDONLY);
+	
+	if ( -1 == file ) 	return -2;
+
+	while ( 1 )
+	{
+		readsize = read(file, buffer, 10240);
+		
+		if (readsize <= 0) break;
+		
+		gzwrite(gzfile,buffer,readsize);
+	}
+	
+	gzclose(gzfile);
+	close(file);
+	
+	remove(from);
 }
 
 #endif
Index: source/blender/blenlib/intern/util.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/blenlib/intern/util.c,v
retrieving revision 1.26
diff -u -r1.26 util.c
--- source/blender/blenlib/intern/util.c	10 Jun 2005 13:12:59 -0000	1.26
+++ source/blender/blenlib/intern/util.c	8 Jul 2005 14:59:40 -0000
@@ -121,6 +121,11 @@
 		else if (BLI_strncasecmp(string + len - 6, ".trace", 6) == 0) len -= 6;
 	}
 	
+	if (len > 9) {
+		if (BLI_strncasecmp(string + len - 9, ".blend.gz", 9) == 0) len -= 9;
+	}
+		
+	
 	if (len == len2) {
 		if (len > 4) {
 			/* handle .jf0 en .jf1 for jstreams */
@@ -585,7 +590,7 @@
 }
 
 void BLI_clean(char *path)
-{
+{
 	if(path==0) return;
 #ifdef WIN32
 	if(path && strlen(path)>2) {
@@ -597,7 +602,7 @@
 }
 
 void BLI_char_switch(char *string, char from, char to) 
-{
+{
 	if(string==0) return;
 	while (*string != 0) {
 		if (*string == from) *string = to;
@@ -683,7 +688,7 @@
 		
 	strcat (string, file);
 	
-	/* Push all slashes to the system preferred direction */
+	/* Push all slashes to the system preferred direction */
 	BLI_clean(string);
 }
 
Index: source/blender/blenloader/intern/readfile.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/blenloader/intern/readfile.c,v
retrieving revision 1.136
diff -u -r1.136 readfile.c
--- source/blender/blenloader/intern/readfile.c	6 Jul 2005 05:10:31 -0000	1.136
+++ source/blender/blenloader/intern/readfile.c	8 Jul 2005 14:59:50 -0000
@@ -139,6 +139,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
@@ -772,6 +776,19 @@
 	return (readsize);
 }
 
+static int fd_read_gzip_from_file(FileData *filedata, void *buffer, int size)
+{
+	int readsize = gzread(filedata->filedes, 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
@@ -854,7 +871,7 @@
 
 FileData *blo_openblenderfile(char *name)
 {
-	int file;
+	gzFile file;
 	char name1[FILE_MAXDIR+FILE_MAXFILE];
 	
 	/* library files can have stringcodes */
@@ -864,16 +881,16 @@
 	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);
+	file= gzopen(name1, "rb");
 
-	if (file == -1) {
+	if (NULL == file) {
 		return NULL;
 	} else {
 		FileData *fd = filedata_new();
-		fd->filedes = file;
+		fd->filedes = (int)file;
 		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;
 
 		decode_blender_header(fd);
 
@@ -950,7 +967,7 @@
 {
 	if (fd) {
 		if (fd->filedes != -1) {
-			close(fd->filedes);
+			gzclose(fd->filedes);
 		}
 
 		if (fd->buffer && !(fd->flags & FD_FLAGS_NOT_MY_BUFFER)) {
@@ -983,7 +1000,7 @@
 
 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 ******************* */
Index: source/blender/blenloader/intern/writefile.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/blenloader/intern/writefile.c,v
retrieving revision 1.46
diff -u -r1.46 writefile.c
--- source/blender/blenloader/intern/writefile.c	3 Jul 2005 17:35:31 -0000	1.46
+++ source/blender/blenloader/intern/writefile.c	8 Jul 2005 14:59:53 -0000
@@ -165,6 +165,9 @@
 #include "readfile.h"
 #include "genfile.h"
 
+#include "zlib.h"
+
+#include <errno.h>
 
 /* ********* my write, buffered writing with minimum 50k chunks ************ */
 
@@ -199,7 +202,7 @@
 
 static void writedata_do_write(WriteData *wd, void *mem, int memlen)
 {
-	if (wd->error) return;
+ 	if (wd->error) return;
 
 	/* memory based save */
 	if(wd->current) {
@@ -1577,12 +1580,14 @@
 /* return: success (1) */
 int BLO_write_file(char *dir, int write_flags, char **error_r)
 {
+
 	char userfilename[FILE_MAXDIR+FILE_MAXFILE];
 	char tempname[FILE_MAXDIR+FILE_MAXFILE];
+	char gzname[FILE_MAXDIR+FILE_MAXFILE];
 	int file, fout, write_user_block;
 
 	sprintf(tempname, "%s@", dir);
-
+	
 	file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
 	if(file == -1) {
 		*error_r= "Unable to open";
@@ -1597,10 +1602,27 @@
 	close(file);
 
 	if(!fout) {
+		if(write_flags & G_FILE_COMPRESS)
+		{	
+			// .gz is concatenated to the name in filesel.c:
+			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);
 
Index: source/blender/include/BSE_filesel.h
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/include/BSE_filesel.h,v
retrieving revision 1.8
diff -u -r1.8 BSE_filesel.h
--- source/blender/include/BSE_filesel.h	17 Jul 2003 14:19:55 -0000	1.8
+++ source/blender/include/BSE_filesel.h	8 Jul 2005 14:59:54 -0000
@@ -56,7 +56,8 @@
 void filesel_prevspace(void);
 void free_filesel_spec(char *dir);
 void winqreadfilespace(struct ScrArea *sa, void *spacedata, struct BWinEvent *evt);
-void main_to_filelist(struct SpaceFile *sfile);   
+void main_to_filelist(struct SpaceFile *sfile); 
+void flip_gz_filename(void *arg1_unused, void *arg2_unused);  
 
 void clever_numbuts_filesel(void);
 #endif
Index: source/blender/src/filesel.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/src/filesel.c,v
retrieving revision 1.55
diff -u -r1.55 filesel.c
--- source/blender/src/filesel.c	3 Jul 2005 17:35:37 -0000	1.55
+++ source/blender/src/filesel.c	8 Jul 2005 15:00:19 -0000
@@ -187,7 +187,7 @@
 }
 char *fsmenu_build_menu(void)
 {
-	DynStr *ds= BLI_dynstr_new();
+ 	DynStr *ds= BLI_dynstr_new();
 	FSMenuEntry *fsme;
 	char *menustr;
 
@@ -692,7 +692,7 @@
 static void split_sfile(SpaceFile *sfile, char *s1)
 {
 	char string[FILE_MAXDIR+FILE_MAXFILE], dir[FILE_MAXDIR], file[FILE_MAXFILE];
-
+	
 	strcpy(string, s1);
 
 	BLI_split_dirfile(string, dir, file);
@@ -704,6 +704,7 @@
 		else test_flags_file(sfile);
 	}
 	strcpy(sfile->file, file);
+		
 	BLI_make_file_string(G.sce, sfile->dir, dir, "");
 }
 
@@ -917,6 +918,7 @@
 		linerect(boxcol, x, y);
 	}
 
+	// this is where the little boxes in the file view are being drawn according to the file type
 	if(files->flags & BLENDERFILE) {
 		cpack(0xA0A0);
 		glRects(x-14,  y,  x-8,  y+7);
@@ -1150,8 +1152,9 @@
 	}
 	else loadbutton= 0;
 
-	uiDefBut(block, TEX,	    1,"",	textrct.xmin, filebuty1, textrct.xmax-textrct.xmin-loadbutton, 21, sfile->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
-	uiDefBut(block, TEX,	    2,"",	textrct.xmin, filebuty2, textrct.xmax-textrct.xmin-loadbutton, 21, sfile->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
+	uiDefBut(block, TEX,1,"",	textrct.xmin, filebuty1, textrct.xmax-textrct.xmin-loadbutton, 21, sfile->file, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
+	uiDefBut(block, TEX,2,"",	textrct.xmin, filebuty2, textrct.xmax-textrct.xmin-loadbutton, 21, sfile->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
+
 	if(loadbutton) {
 		uiSetCurFont(block, UI_HELV);
 		uiDefBut(block, BUT,	    5, sfile->title,	textrct.xmax-loadbutton, filebuty2, loadbutton, 21, sfile->dir, 0.0, (float)FILE_MAXFILE-1, 0, 0, "");
@@ -1857,7 +1860,13 @@
 					else {
 						if( strcmp(sfile->file, sfile->filelist[act].relname)) {
 							do_draw= 1;
-							strcpy(sfile->file, sfile->filelist[act].relname);
+							
+							if( (G.fileflags & G_FILE_COMPRESS) && (strncmp(sfile->title, "Save", 4)==0))
+							{
+								sprintf(sfile->file,"%s.gz",sfile->filelist[act].relname);
+							}
+							else 
+								strcpy(sfile->file, sfile->filelist[act].relname);
 						}
 						if(event==MIDDLEMOUSE && sfile->type) filesel_execute(sfile);
 					}
@@ -2552,3 +2561,24 @@
 	}
 }
 
+// add/remove .gz from end of filename - event on the compression button (see header_filesel.c)
+void flip_gz_filename(void *arg1_unused, void *arg2_unused)
+{
+	SpaceFile *sfile;
+	
+	sfile= curarea->spacedata.first;
+	
+	if( (G.fileflags & G_FILE_COMPRESS))	{
+		if( !BLI_testextensie(sfile->file, ".blend.gz")) {
+		strcat(sfile->file, ".gz");
+		scrarea_queue_winredraw(curarea);
+		}
+	}
+	else
+	if(BLI_testextensie(sfile->file, ".blend.gz")) {
+	//remove gz extension
+	
+	*strrchr(sfile->file, '.')=0;
+	scrarea_queue_winredraw(curarea);
+	}
+}
Index: source/blender/src/header_filesel.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/src/header_filesel.c,v
retrieving revision 1.17
diff -u -r1.17 header_filesel.c
--- source/blender/src/header_filesel.c	28 May 2005 16:11:11 -0000	1.17
+++ source/blender/src/header_filesel.c	8 Jul 2005 15:00:20 -0000
@@ -145,7 +145,20 @@
 		uiBlockEndAlign(block);
 	} else if(sfile->type==FILE_BLENDER) {
 		uiDefButI(block, TOGN|BIT|10, B_REDR, "Load UI", xco+=XIC,0,80,YIC, &G.fileflags, 0, 0, 0, 0, "Load the UI setup as well as the scene data");
-		xco+=100;
+		
+	
+		xco+=60;
+						
+		if(strncmp(sfile->title, "Save", 4)==0)
+		{
+		uiBut *bt = uiDefIconButI(block, ICONTOG|BIT|G_FILE_COMPRESS_BIT, 0, ICON_CHECKBOX_DEHLT, xco+=20,0, 15,YIC, &G.fileflags, 0.0, 0.0, 0, 0, "Enable file compression");
+		uiButSetFunc(bt, flip_gz_filename, NULL, NULL);
+		}
+		else
+			xco+=20;
+		
+		xco+=20;
+
 	}
 
 	if(sfile->type==FILE_UNIX) {


More information about the Bf-committers mailing list