[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [43098] branches/asset-browser/source/ blender: file browser: CleanUp work
Andrea Weikert
elubie at gmx.net
Tue Jan 3 13:41:43 CET 2012
Revision: 43098
http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=43098
Author: elubie
Date: 2012-01-03 12:41:41 +0000 (Tue, 03 Jan 2012)
Log Message:
-----------
file browser: CleanUp work
* removed static storage for the directory
* removed unneeded parameter relname which allowed to call BLI_dir_contents with relative path, was never used
* made functions used for reading blender library static
Modified Paths:
--------------
branches/asset-browser/source/blender/blenlib/BLI_fileops.h
branches/asset-browser/source/blender/blenlib/CMakeLists.txt
branches/asset-browser/source/blender/blenlib/intern/storage.c
branches/asset-browser/source/blender/editors/interface/interface_icons.c
branches/asset-browser/source/blender/editors/space_file/filelist.c
branches/asset-browser/source/blender/editors/space_file/filelist.h
Modified: branches/asset-browser/source/blender/blenlib/BLI_fileops.h
===================================================================
--- branches/asset-browser/source/blender/blenlib/BLI_fileops.h 2012-01-03 12:40:51 UTC (rev 43097)
+++ branches/asset-browser/source/blender/blenlib/BLI_fileops.h 2012-01-03 12:41:41 UTC (rev 43098)
@@ -61,7 +61,10 @@
double BLI_dir_free_space(const char *dir);
char *BLI_current_working_dir(char *dir, const int maxlen);
-unsigned int BLI_dir_contents(const char *dir, struct direntry **filelist);
+/* allocates memory that needs to be freed once struct direntry **files is no longer needed
+ use BLI_dir_dispose for this! */
+void BLI_dir_contents(const char *dirname, struct direntry **files, int* num_files);
+void BLI_dir_dispose(struct direntry *filelist, unsigned int num_files);
/* Files */
Modified: branches/asset-browser/source/blender/blenlib/CMakeLists.txt
===================================================================
--- branches/asset-browser/source/blender/blenlib/CMakeLists.txt 2012-01-03 12:40:51 UTC (rev 43097)
+++ branches/asset-browser/source/blender/blenlib/CMakeLists.txt 2012-01-03 12:41:41 UTC (rev 43098)
@@ -27,6 +27,7 @@
.
../blenkernel
../blenloader
+ ../imbuf
../gpu
../makesdna
../../../intern/ghost
Modified: branches/asset-browser/source/blender/blenlib/intern/storage.c
===================================================================
--- branches/asset-browser/source/blender/blenlib/intern/storage.c 2012-01-03 12:40:51 UTC (rev 43097)
+++ branches/asset-browser/source/blender/blenlib/intern/storage.c 2012-01-03 12:41:41 UTC (rev 43098)
@@ -93,12 +93,11 @@
#include "BKE_utildefines.h"
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+
/* vars: */
-static int totnum,actnum;
-static struct direntry *files;
-static struct ListBase dirbase_={NULL, NULL};
-static struct ListBase *dirbase = &dirbase_;
/* can return NULL when the size is not big enough */
char *BLI_current_working_dir(char *dir, const int maxncpy)
@@ -195,22 +194,19 @@
#endif
}
-static void bli_builddir(const char *dirname, const char *relname)
+static void bli_adddirstrings(struct direntry* files, int num_files);
+
+void BLI_dir_contents(const char *dirname, struct direntry **files, int *num_files)
{
+ int totnum= 0,actnum= 0;
+ struct ListBase dirbase_={NULL, NULL};
+ struct ListBase *dirbase = &dirbase_;
struct dirent *fname;
struct dirlink *dlink;
- int rellen, newnum = 0;
+ int newnum = 0;
char buf[256];
DIR *dir;
- strcpy(buf,relname);
- rellen=strlen(relname);
-
- if (rellen){
- buf[rellen]='/';
- rellen++;
- }
-
if (chdir(dirname) == -1){
perror(dirname);
return;
@@ -220,7 +216,7 @@
while ((fname = (struct dirent*) readdir(dir)) != NULL) {
dlink = (struct dirlink *)malloc(sizeof(struct dirlink));
if (dlink){
- strcpy(buf+rellen,fname->d_name);
+ strcpy(buf,fname->d_name);
dlink->name = BLI_strdup(buf);
BLI_addhead(dirbase,dlink);
newnum++;
@@ -229,37 +225,25 @@
if (newnum){
- if(files) {
- void *tmp= realloc(files, (totnum+newnum) * sizeof(struct direntry));
- if(tmp) {
- files= (struct direntry *)tmp;
- }
- else { /* realloc fail */
- free(files);
- files= NULL;
- }
- }
-
- if(files==NULL)
- files=(struct direntry *)malloc(newnum * sizeof(struct direntry));
+ (*files)=(struct direntry *)MEM_mallocN(newnum * sizeof(struct direntry), "directory files");
- if (files){
+ if (*files){
dlink = (struct dirlink *) dirbase->first;
while(dlink){
- memset(&files[actnum], 0 , sizeof(struct direntry));
- files[actnum].relname = dlink->name;
- files[actnum].path = BLI_strdupcat(dirname, dlink->name);
+ memset(&(*files)[actnum], 0 , sizeof(struct direntry));
+ (*files)[actnum].relname = dlink->name;
+ (*files)[actnum].path = BLI_strdupcat(dirname, dlink->name);
// use 64 bit file size, only needed for WIN32 and WIN64.
// Excluding other than current MSVC compiler until able to test.
#if (defined(WIN32) || defined(WIN64)) && (_MSC_VER>=1500)
- _stat64(dlink->name,&files[actnum].s);
+ _stat64(dlink->name,&(*files)[actnum].s);
#elif defined(__MINGW32__)
_stati64(dlink->name,&files[actnum].s);
#else
stat(dlink->name,&files[actnum].s);
#endif
- files[actnum].type=files[actnum].s.st_mode;
- files[actnum].flags = 0;
+ (*files)[actnum].type=(*files)[actnum].s.st_mode;
+ (*files)[actnum].flags = 0;
totnum++;
actnum++;
dlink = dlink->next;
@@ -270,7 +254,7 @@
}
BLI_freelist(dirbase);
- if (files) qsort(files, actnum, sizeof(struct direntry), (int (*)(const void *,const void*))bli_compare);
+ if ((*files)) qsort((*files), actnum, sizeof(struct direntry), (int (*)(const void *,const void*))bli_compare);
} else {
printf("%s empty directory\n",dirname);
}
@@ -279,9 +263,12 @@
} else {
printf("%s non-existant directory\n",dirname);
}
+
+ bli_adddirstrings((*files), actnum);
+ *num_files= actnum;
}
-static void bli_adddirstrings(void)
+static void bli_adddirstrings(struct direntry* files, int num_files)
{
char datum[100];
char buf[512];
@@ -298,7 +285,7 @@
struct tm *tm;
time_t zero= 0;
- for(num=0, file= files; num<actnum; num++, file++){
+ for(num=0, file= files; num<num_files; num++, file++){
#ifdef WIN32
mode = 0;
BLI_strncpy(file->mode1, types[0], sizeof(file->mode1));
@@ -389,30 +376,27 @@
}
}
-unsigned int BLI_dir_contents(const char *dirname, struct direntry **filelist)
+void BLI_dir_dispose(struct direntry *filelist, unsigned int num_files)
{
- // reset global variables
- // memory stored in files is free()'d in
- // filesel.c:freefilelist()
-
- actnum = totnum = 0;
- files = NULL;
-
- bli_builddir(dirname,"");
- bli_adddirstrings();
-
- if (files) {
- *(filelist) = files;
- } else {
- // keep blender happy. Blender stores this in a variable
- // where 0 has special meaning.....
- *(filelist) = files = malloc(sizeof(struct direntry));
+ int i;
+ struct direntry *files = filelist;
+ for (i = 0; i < num_files; ++i) {
+ if (files[i].image) {
+ IMB_freeImBuf(files[i].image);
+ }
+ files[i].image = NULL;
+ if (files[i].relname)
+ MEM_freeN(files[i].relname);
+ if (files[i].path)
+ MEM_freeN(files[i].path);
+ files[i].relname = NULL;
+ if (files[i].string)
+ MEM_freeN(files[i].string);
+ files[i].string = NULL;
}
-
- return(actnum);
+ MEM_freeN(files);
}
-
size_t BLI_file_descriptor_size(int file)
{
struct stat buf;
Modified: branches/asset-browser/source/blender/editors/interface/interface_icons.c
===================================================================
--- branches/asset-browser/source/blender/editors/interface/interface_icons.c 2012-01-03 12:40:51 UTC (rev 43097)
+++ branches/asset-browser/source/blender/editors/interface/interface_icons.c 2012-01-03 12:41:41 UTC (rev 43098)
@@ -610,7 +610,8 @@
back to old value afterwards */
if(!BLI_current_working_dir(olddir, sizeof(olddir)))
restoredir = 0;
- totfile = BLI_dir_contents(icondir, &dir);
+ BLI_dir_contents(icondir, &dir, &totfile);
+
if (restoredir && !chdir(olddir)) {} /* fix warning about checking return value */
for(i=0; i<totfile; i++) {
@@ -660,14 +661,7 @@
}
/* free temporary direntry structure that's been created by BLI_dir_contents() */
- i= totfile-1;
-
- for(; i>=0; i--){
- MEM_freeN(dir[i].relname);
- MEM_freeN(dir[i].path);
- if (dir[i].string) MEM_freeN(dir[i].string);
- }
- free(dir);
+ BLI_dir_dispose(dir, totfile);
dir= NULL;
}
Modified: branches/asset-browser/source/blender/editors/space_file/filelist.c
===================================================================
--- branches/asset-browser/source/blender/editors/space_file/filelist.c 2012-01-03 12:40:51 UTC (rev 43097)
+++ branches/asset-browser/source/blender/editors/space_file/filelist.c 2012-01-03 12:41:41 UTC (rev 43098)
@@ -45,6 +45,7 @@
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
+#
#include "BLI_linklist.h"
#include "BLI_threads.h"
#include "BLI_utildefines.h"
@@ -526,8 +527,6 @@
void filelist_free(struct FileList* filelist)
{
- int i;
-
if (!filelist) {
printf("Attempting to delete empty filelist.\n");
return;
@@ -538,28 +537,11 @@
filelist->fidx = NULL;
}
- for (i = 0; i < filelist->numfiles; ++i) {
- if (filelist->filelist[i].image) {
- IMB_freeImBuf(filelist->filelist[i].image);
- }
- filelist->filelist[i].image = NULL;
- if (filelist->filelist[i].relname)
- MEM_freeN(filelist->filelist[i].relname);
- if (filelist->filelist[i].path)
- MEM_freeN(filelist->filelist[i].path);
- filelist->filelist[i].relname = NULL;
- if (filelist->filelist[i].string)
- MEM_freeN(filelist->filelist[i].string);
- filelist->filelist[i].string = NULL;
- }
+ BLI_dir_dispose(filelist->filelist, filelist->numfiles);
filelist->numfiles = 0;
- free(filelist->filelist);
filelist->filelist = NULL;
- filelist->filter = 0;
- filelist->filter_glob[0] = '\0';
filelist->numfiltered =0;
- filelist->hide_dot =0;
}
void filelist_freelib(struct FileList* filelist)
@@ -825,13 +807,19 @@
BLI_current_working_dir(wdir, sizeof(wdir)); /* backup cwd to restore after */
BLI_cleanup_dir(G.main->name, filelist->dir);
- filelist->numfiles = BLI_dir_contents(filelist->dir, &(filelist->filelist));
+
+ filelist_free(filelist);
+ BLI_dir_contents(filelist->dir, &filelist->filelist, &filelist->numfiles);
+
if(!chdir(wdir)) {} /* fix warning about not checking return value */
filelist_setfiletypes(filelist);
filelist_filter(filelist);
}
+static void filelist_from_main(struct FileList* filelist);
+static void filelist_from_library(struct FileList* filelist);
+
static void filelist_read_main(struct FileList* filelist)
{
if (!filelist) return;
@@ -983,7 +971,7 @@
return BKE_idcode_from_name(buf);
}
-void filelist_from_library(struct FileList* filelist)
+static void filelist_from_library(struct FileList* filelist)
{
LinkNode *l, *names, *previews;
struct ImBuf* ima;
@@ -1084,7 +1072,7 @@
filelist->hide_parent = hide;
}
-void filelist_from_main(struct FileList *filelist)
+static void filelist_from_main(struct FileList *filelist)
{
ID *id;
struct direntry *files, *firstlib = NULL;
Modified: branches/asset-browser/source/blender/editors/space_file/filelist.h
===================================================================
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list