use BLI_strncpy and BLI_snprintf when the size of the string is known.
[blender.git] / source / blender / editors / space_file / filelist.c
index 4daaea4..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++) {
@@ -597,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;
@@ -685,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;
@@ -696,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) { /* not dealing with user input so dont need BLI_path_cmp */
+               if ( strcmp(filelist->filelist[i].relname, filename) == 0) { /* not dealing with user input so dont need BLI_path_cmp */
                        index = i;
                        break;
                }
@@ -752,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;
@@ -785,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);
        
@@ -841,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);
@@ -871,7 +852,7 @@ 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);
@@ -910,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;
@@ -987,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;
@@ -999,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, nprevs, nnames, idcode;
-       char filename[FILE_MAXDIR+FILE_MAXFILE];
+       char filename[FILE_MAX];
        char dir[FILE_MAX], group[GROUP_MAX];   
        
        /* name test */
@@ -1086,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);
 
@@ -1120,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++) {
@@ -1150,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 {
@@ -1172,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) {
@@ -1195,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;
@@ -1215,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;