[Bf-committers] small filesel.c (cleanup) patch

Daniel Fairhead bf-committers@blender.org
Mon, 17 May 2004 18:17:42 +0300


This is a multi-part message in MIME format.

--Multipart=_Mon__17_May_2004_18_17_42_+0300_y_wFIGvZLGg=tlKX
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit

Hi all,

just a small patch to replace something which annoyed me in filesel.c:
on windows a directory break is "\\" and on everything else it is
"/", and so there were two chunks of code which were duplicated with
#ifdef WIN32s. not good. Now it uses the same code, by defining at
the top of the file DIRBR which is correct for each one.

I know filesel.c is "horrible old code, that needs completely re-writing",
and I should work on more useful things, but this has been itching me 
for ages. :-)

Comments?

Dan

--Multipart=_Mon__17_May_2004_18_17_42_+0300_y_wFIGvZLGg=tlKX
Content-Type: text/x-patch;
 name="filesel-patch.diff"
Content-Disposition: attachment;
 filename="filesel-patch.diff"
Content-Transfer-Encoding: 7bit

Index: filesel.c
===================================================================
RCS file: /cvsroot/bf-blender/blender/source/blender/src/filesel.c,v
retrieving revision 1.35
diff -u -B -r1.35 filesel.c
--- filesel.c	25 Apr 2004 15:12:45 -0000	1.35
+++ filesel.c	17 May 2004 15:16:00 -0000
@@ -115,6 +115,14 @@
 #include <sys/param.h>
 #endif
 
+#ifdef WIN32
+	#define DIRBR	'\\'
+	#define DIRBRS	"\\"
+#else
+	#define DIRBR	'/'
+	#define DIRBRS	"/"
+#endif
+
 #define FILESELHEAD		60
 #define FILESEL_DY		16
 
@@ -404,87 +412,52 @@
  * excess file entry stuff (like /tmp/../tmp/../)
  */
 
+/* Should this be in blenlib? - madprof */
+
 void checkdir(char *dir)
 {
 	short a;
-	char *start, *eind;
+	char *start, *end;
 	char tmp[FILE_MAXDIR+FILE_MAXFILE];
 
 	BLI_make_file_string(G.sce, tmp, dir, "");
 	strcpy(dir, tmp);
 	
-#ifdef WIN32
 	if(dir[0]=='.') {	/* happens for example in FILE_MAIN */
-		dir[0]= '\\';
+		dir[0]= DIRBR;
 		dir[1]= 0;
 		return;
-	}	
-
-	while (start = strstr(dir, "\\..\\")) {
-		eind = start + strlen("\\..\\") - 1;
-		a = start-dir-1;
-		while (a>0) {
-			if (dir[a] == '\\') break;
-			a--;
-		}
-		strcpy(dir+a,eind);
 	}
 
-	while (start = strstr(dir,"\\.\\")){
-		eind = start + strlen("\\.\\") - 1;
-		strcpy(start,eind);
-	}
-
-	while (start = strstr(dir,"\\\\" )){
-		eind = start + strlen("\\\\") - 1;
-		strcpy(start,eind);
-	}
-
-	if(a = strlen(dir)){				/* remove the '\\' at the end */
-		while(a>0 && dir[a-1] == '\\'){
-			a--;
-			dir[a] = 0;
-		}
-	}
-
-	strcat(dir, "\\");
-#else	
-	if(dir[0]=='.') {	/* happens, for example in FILE_MAIN */
-		dir[0]= '/';
-		dir[1]= 0;
-		return;
-	}	
-	
-	while ( (start = strstr(dir, "/../")) ) {
-		eind = start + strlen("/../") - 1;
+	while (start = strstr(dir, DIRBRS ".." DIRBRS)) {
+		end = start + strlen(DIRBRS ".." DIRBRS) - 1;
 		a = start-dir-1;
 		while (a>0) {
-			if (dir[a] == '/') break;
+			if (dir[a] == DIRBR) break;
 			a--;
 		}
-		strcpy(dir+a,eind);
+		strcpy(dir+a,end);
 	}
 
-	while ( (start = strstr(dir,"/./")) ){
-		eind = start + strlen("/./") - 1;
-		strcpy(start,eind);
+	while (start = strstr(dir,DIRBRS "." DIRBRS)){
+		end = start + strlen(DIRBRS "." DIRBRS) - 1;
+		strcpy(start,end);
 	}
 
-	while ( (start = strstr(dir,"//" )) ){
-		eind = start + strlen("//") - 1;
-		strcpy(start,eind);
+	while (start = strstr(dir,DIRBRS DIRBRS)){
+		end = start + strlen(DIRBRS DIRBRS) - 1;
+		strcpy(start,end);
 	}
 
-	if( (a = strlen(dir)) ){				/* remove all '/' at the end */
-		while(dir[a-1] == '/'){
+	if(a = strlen(dir)){				/* remove the '\\' or '/' at the end */
+		while(a>0 && dir[a-1] == DIRBR){
 			a--;
 			dir[a] = 0;
-			if (a<=0) break;
 		}
 	}
 
-	strcat(dir, "/");
-#endif
+	strcat(dir, DIRBRS);
+
 }
 
 void test_flags_file(SpaceFile *sfile)
@@ -729,45 +702,24 @@
 
 	dir= sfile->dir;
 	
-#ifdef WIN32
-	if(a = strlen(dir)) {				/* remove all '/' at the end */
-		while(dir[a-1] == '\\') {
+	if(a = strlen(dir)) {				/* remove all '/' or '\\' at the end */
+		while(dir[a-1] == DIRBR) {
 			a--;
 			dir[a] = 0;
 			if (a<=0) break;
 		}
 	}
-	if(a = strlen(dir)) {				/* then remove all until '/' */
-		while(dir[a-1] != '\\') {
+	if(a = strlen(dir)) {				/* then remove all until '/' or '\\'*/
+		while(dir[a-1] != DIRBR) {
 			a--;
 			dir[a] = 0;
 			if (a<=0) break;
 		}
 	}
 	if (a = strlen(dir)) {
-		if (dir[a-1] != '\\') strcat(dir,"\\");
-	}
-	else if(sfile->type!=FILE_MAIN) strcpy(dir,"\\");
-#else
-	if( (a = strlen(dir)) ) {				/* remove all '/' at the end */
-		while(dir[a-1] == '/') {
-			a--;
-			dir[a] = 0;
-			if (a<=0) break;
-		}
-	}
-	if( (a = strlen(dir)) ) {				/* then remove until '/' */
-		while(dir[a-1] != '/') {
-			a--;
-			dir[a] = 0;
-			if (a<=0) break;
-		}
+		if (dir[a-1] != DIRBR) strcat(dir,DIRBRS);
 	}
-	if ( (a = strlen(dir)) ) {
-		if (dir[a-1] != '/') strcat(dir,"/");
-	}
-	else if(sfile->type!=FILE_MAIN) strcpy(dir,"/");
-#endif
+	else if(sfile->type!=FILE_MAIN) strcpy(dir,DIRBRS);
 	
 	/* to be sure */
 	BLI_make_exist(sfile->dir);
@@ -2141,11 +2093,7 @@
 		case SLASHKEY:
 			if(sfile->type==FILE_MAIN) break;
 
-#ifdef WIN32
-			strcpy(sfile->dir, "\\");
-#else
-			strcpy(sfile->dir, "/");
-#endif
+			strcpy(sfile->dir, DIRBRS);
 			freefilelist(sfile);
 			sfile->ofs= 0;
 			do_draw= 1;

--Multipart=_Mon__17_May_2004_18_17_42_+0300_y_wFIGvZLGg=tlKX--