use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender.git] / source / blender / editors / space_file / filelist.c
index a32cfe2..32fe849 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
@@ -48,7 +46,6 @@
 
 #include "BLI_blenlib.h"
 #include "BLI_linklist.h"
-#include "BLI_storage_types.h"
 #include "BLI_threads.h"
 #include "BLI_utildefines.h"
 
@@ -59,6 +56,7 @@
 #include "BKE_context.h"
 #include "BKE_global.h"
 #include "BKE_library.h"
+#include "BKE_icons.h"
 #include "BKE_main.h"
 #include "BKE_report.h"
 #include "BLO_readfile.h"
@@ -243,7 +241,8 @@ static int compare_size(const void *a1, const void *a2)
        else return BLI_natstrcmp(entry1->relname,entry2->relname);
 }
 
-static int compare_extension(const void *a1, const void *a2) {
+static int compare_extension(const void *a1, const void *a2)
+{
        const struct direntry *entry1=a1, *entry2=a2;
        const char *sufix1, *sufix2;
        const char *nil="";
@@ -372,7 +371,11 @@ void filelist_init_icons(void)
        short x, y, k;
        ImBuf *bbuf;
        ImBuf *ibuf;
-       bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_prvicons, datatoc_prvicons_size, IB_rect);
+#ifdef WITH_HEADLESS
+       bbuf = NULL;
+#else
+       bbuf = IMB_ibImageFromMemory((unsigned char*)datatoc_prvicons, datatoc_prvicons_size, IB_rect, "<splash>");
+#endif
        if (bbuf) {
                for (y=0; y<SPECIAL_IMG_ROWS; y++) {
                        for (x=0; x<SPECIAL_IMG_COLS; x++) {
@@ -433,7 +436,7 @@ void folderlist_pushdir(ListBase* folderlist, const char *dir)
 
        // check if already exists
        if(previous_folder && previous_folder->foldername){
-               if(! strcmp(previous_folder->foldername, dir)){
+               if(BLI_path_cmp(previous_folder->foldername, dir)==0){
                        return;
                }
        }
@@ -459,7 +462,7 @@ int folderlist_clear_next(struct SpaceFile *sfile)
 
        // if previous_folder, next_folder or refresh_folder operators are executed it doesn't clear folder_next
        folder = sfile->folders_prev->last;
-       if ((!folder) ||(!strcmp(folder->foldername, sfile->params->dir)))
+       if ((!folder) ||(BLI_path_cmp(folder->foldername, sfile->params->dir) == 0))
                return 0;
 
        // eventually clear flist->folders_next
@@ -469,13 +472,12 @@ int folderlist_clear_next(struct SpaceFile *sfile)
 /* not listbase itself */
 void folderlist_free(ListBase* folderlist)
 {
-       FolderList *folder;
        if (folderlist){
+               FolderList *folder;
                for(folder= folderlist->first; folder; folder= folder->next)
                        MEM_freeN(folder->foldername);
                BLI_freelistN(folderlist);
        }
-       folderlist= NULL;
 }
 
 ListBase *folderlist_duplicate(ListBase* folderlist)
@@ -598,28 +600,6 @@ short filelist_changed(struct FileList* filelist)
        return filelist->changed;
 }
 
-static struct ImBuf * filelist_loadimage(struct FileList* filelist, int index)
-{
-       ImBuf *imb = NULL;
-       int fidx = 0;
-       
-       if ( (index < 0) || (index >= filelist->numfiltered) ) {
-               return NULL;
-       }
-       fidx = filelist->fidx[index];
-       imb = filelist->filelist[fidx].image;
-       if (!imb)
-       {
-               if ( (filelist->filelist[fidx].flags & IMAGEFILE) || (filelist->filelist[fidx].flags & MOVIEFILE) ) {
-                       imb = IMB_thumb_read(filelist->filelist[fidx].path, THB_NORMAL);
-               } 
-               if (imb) {
-                       filelist->filelist[fidx].image = imb;
-               } 
-       }
-       return imb;
-}
-
 struct ImBuf * filelist_getimage(struct FileList* filelist, int index)
 {
        ImBuf* ibuf = NULL;
@@ -686,7 +666,7 @@ struct direntry * filelist_file(struct FileList* filelist, int index)
        return &filelist->filelist[fidx];
 }
 
-int filelist_find(struct FileList* filelist, char *file)
+int filelist_find(struct FileList* filelist, const char *filename)
 {
        int index = -1;
        int i;
@@ -697,7 +677,7 @@ int filelist_find(struct FileList* filelist, char *file)
 
        
        for (i = 0; i < filelist->numfiles; ++i) {
-               if ( strcmp(filelist->filelist[i].relname, file) == 0) {
+               if ( strcmp(filelist->filelist[i].relname, filename) == 0) { /* not dealing with user input so dont need BLI_path_cmp */
                        index = i;
                        break;
                }
@@ -753,7 +733,7 @@ static int file_is_blend_backup(const char *str)
 }
 
 
-static int file_extension_type(char *relname)
+static int file_extension_type(const char *relname)
 {
        if(BLO_has_bfile_extension(relname)) {
                return BLENDERFILE;
@@ -786,7 +766,7 @@ static int file_extension_type(char *relname)
        return 0;
 }
 
-int ED_file_extension_icon(char *relname)
+int ED_file_extension_icon(const char *relname)
 {
        int type= file_extension_type(relname);
        
@@ -798,8 +778,6 @@ int ED_file_extension_icon(char *relname)
                return ICON_FILE_MOVIE;
        else if (type ==  PYSCRIPTFILE)
                return ICON_FILE_SCRIPT;
-       else if (type ==  PYSCRIPTFILE)
-               return ICON_FILE_SCRIPT;
        else if (type ==  SOUNDFILE) 
                return ICON_FILE_SOUND;
        else if (type ==  FTFONTFILE) 
@@ -844,10 +822,10 @@ static void filelist_read_dir(struct FileList* filelist)
        filelist->fidx = NULL;
        filelist->filelist = NULL;
 
-       BLI_getwdN(wdir, sizeof(wdir));  /* backup cwd to restore after */
+       BLI_current_working_dir(wdir, sizeof(wdir));     /* backup cwd to restore after */
 
        BLI_cleanup_dir(G.main->name, filelist->dir);
-       filelist->numfiles = BLI_getdir(filelist->dir, &(filelist->filelist));
+       filelist->numfiles = BLI_dir_contents(filelist->dir, &(filelist->filelist));
 
        if(!chdir(wdir)) {} /* fix warning about not checking return value */
        filelist_setfiletypes(filelist);
@@ -874,13 +852,13 @@ static void filelist_read_library(struct FileList* filelist)
                file = filelist->filelist;
                for(num=0; num<filelist->numfiles; num++, file++) {
                        if(BLO_has_bfile_extension(file->relname)) {
-                               char name[FILE_MAXDIR+FILE_MAXFILE];
+                               char name[FILE_MAX];
                        
                                BLI_strncpy(name, filelist->dir, sizeof(name));
                                strcat(name, file->relname);
                                
                                /* prevent current file being used as acceptable dir */
-                               if (BLI_streq(G.main->name, name)==0) {
+                               if (BLI_path_cmp(G.main->name, name) != 0) {
                                        file->type &= ~S_IFMT;
                                        file->type |= S_IFDIR;
                                }
@@ -913,6 +891,8 @@ void filelist_select_file(struct FileList* filelist, int index, FileSelType sele
                int check_ok = 0; 
                switch (check) {
                        case CHECK_DIRS:
+                               check_ok = S_ISDIR(file->type);
+                               break;
                        case CHECK_ALL:
                                check_ok = 1;
                                break;
@@ -990,7 +970,7 @@ int filelist_islibrary(struct FileList* filelist, char* dir, char* group)
        return BLO_is_a_library(filelist->dir, dir, group);
 }
 
-static int groupname_to_code(char *group)
+static int groupname_to_code(const char *group)
 {
        char buf[32];
        char *lslash;
@@ -1002,13 +982,13 @@ static int groupname_to_code(char *group)
 
        return BKE_idcode_from_name(buf);
 }
-
 void filelist_from_library(struct FileList* filelist)
 {
        LinkNode *l, *names, *previews;
        struct ImBuf* ima;
-       int ok, i, nnames, idcode;
-       char filename[FILE_MAXDIR+FILE_MAXFILE];
+       int ok, i, nprevs, nnames, idcode;
+       char filename[FILE_MAX];
        char dir[FILE_MAX], group[GROUP_MAX];   
        
        /* name test */
@@ -1031,17 +1011,18 @@ void filelist_from_library(struct FileList* filelist)
        
        idcode= groupname_to_code(group);
 
-               // memory for strings is passed into filelist[i].relname
-               // and free'd in freefilelist
-       previews = NULL;
+       /* memory for strings is passed into filelist[i].relname
+        * and free'd in freefilelist */
        if (idcode) {
-               previews= BLO_blendhandle_get_previews(filelist->libfiledata, idcode);
+               previews= BLO_blendhandle_get_previews(filelist->libfiledata, idcode, &nprevs);
                names= BLO_blendhandle_get_datablock_names(filelist->libfiledata, idcode, &nnames);
                /* ugh, no rewind, need to reopen */
                BLO_blendhandle_close(filelist->libfiledata);
                filelist->libfiledata= BLO_blendhandle_from_file(dir, NULL);
                
        } else {
+               previews= NULL;
+               nprevs= 0;
                names= BLO_blendhandle_get_linkable_groups(filelist->libfiledata);
                nnames= BLI_linklist_length(names);
        }
@@ -1064,14 +1045,17 @@ void filelist_from_library(struct FileList* filelist)
                }
        }
        
-       if(previews) {
+       if(previews && (nnames != nprevs)) {
+               printf("filelist_from_library: error, found %d items, %d previews\n", nnames, nprevs);
+       }
+       else if(previews) {
                for (i=0, l= previews; i<nnames; i++, l= l->next) {
                        PreviewImage *img= l->link;
                        
                        if (img) {
-                               unsigned int w = img->w[PREVIEW_MIPMAP_LARGE];
-                               unsigned int h = img->h[PREVIEW_MIPMAP_LARGE];
-                               unsigned int *rect = img->rect[PREVIEW_MIPMAP_LARGE];
+                               unsigned int w = img->w[ICON_SIZE_PREVIEW];
+                               unsigned int h = img->h[ICON_SIZE_PREVIEW];
+                               unsigned int *rect = img->rect[ICON_SIZE_PREVIEW];
 
                                /* first allocate imbuf for copying preview into it */
                                if (w > 0 && h > 0 && rect) {
@@ -1085,7 +1069,7 @@ void filelist_from_library(struct FileList* filelist)
        }
 
        BLI_linklist_free(names, free);
-       if (previews) BLI_linklist_free(previews, (void(*)(void*)) MEM_freeN);
+       if (previews) BLI_linklist_free(previews, BKE_previewimg_freefunc);
 
        filelist_sort(filelist, FILE_SORT_ALPHA);
 
@@ -1119,7 +1103,7 @@ void filelist_from_main(struct FileList *filelist)
        if( filelist->dir[0]==0) {
                
                /* make directories */
-               filelist->numfiles= 23;
+               filelist->numfiles= 24;
                filelist->filelist= (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry));
                
                for(a=0; a<filelist->numfiles; a++) {
@@ -1149,6 +1133,7 @@ void filelist_from_main(struct FileList *filelist)
                filelist->filelist[20].relname= BLI_strdup("Armature");
                filelist->filelist[21].relname= BLI_strdup("Action");
                filelist->filelist[22].relname= BLI_strdup("NodeTree");
+               filelist->filelist[23].relname= BLI_strdup("Speaker");
                filelist_sort(filelist, FILE_SORT_ALPHA);
        }
        else {
@@ -1171,8 +1156,8 @@ void filelist_from_main(struct FileList *filelist)
                
                /* XXXXX TODO: if databrowse F4 or append/link filelist->hide_parent has to be set */
                if (!filelist->hide_parent) filelist->numfiles+= 1;
-               filelist->filelist= (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry));
-               
+               filelist->filelist= filelist->numfiles > 0 ? (struct direntry *)malloc(filelist->numfiles * sizeof(struct direntry)) : NULL;
+
                files = filelist->filelist;
                
                if (!filelist->hide_parent) {
@@ -1194,7 +1179,7 @@ void filelist_from_main(struct FileList *filelist)
                                        if(id->lib==NULL)
                                                files->relname= BLI_strdup(id->name+2);
                                        else {
-                                               files->relname= MEM_mallocN(FILE_MAXDIR+FILE_MAXFILE+32, "filename for lib");
+                                               files->relname= MEM_mallocN(FILE_MAX+32, "filename for lib");
                                                sprintf(files->relname, "%s | %s", id->lib->name, id->name+2);
                                        }
                                        files->type |= S_IFREG;
@@ -1214,10 +1199,10 @@ void filelist_from_main(struct FileList *filelist)
                                        if(idcode == ID_MA || idcode == ID_TE || idcode == ID_LA || idcode == ID_WO || idcode == ID_IM) {
                                                files->flags |= IMAGEFILE;
                                        }
-                                       if(id->lib && fake) sprintf(files->extra, "LF %d", id->us);
-                                       else if(id->lib) sprintf(files->extra, "L    %d", id->us);
-                                       else if(fake) sprintf(files->extra, "F    %d", id->us);
-                                       else sprintf(files->extra, "      %d", id->us);
+                                       if(id->lib && fake) BLI_snprintf(files->extra, sizeof(files->extra), "LF %d", id->us);
+                                       else if(id->lib) BLI_snprintf(files->extra, sizeof(files->extra), "L    %d", id->us);
+                                       else if(fake) BLI_snprintf(files->extra, sizeof(files->extra), "F    %d", id->us);
+                                       else BLI_snprintf(files->extra, sizeof(files->extra), "      %d", id->us);
                                        
                                        if(id->lib) {
                                                if(totlib==0) firstlib= files;