- [#22492] [29159] commit breaks importing of script file that has a reload to self...
authorCampbell Barton <ideasman42@gmail.com>
Sat, 5 Jun 2010 21:19:59 +0000 (21:19 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 5 Jun 2010 21:19:59 +0000 (21:19 +0000)
  broke when including the blend path in the modules filename.

- new function BLI_path_basename(), matches pythons os.path.basename().
  replace a number of cases where BLI_split_dirfile was being used to get the filename only.

source/blender/blenkernel/intern/image.c
source/blender/blenkernel/intern/text.c
source/blender/blenlib/BLI_path_util.h
source/blender/blenlib/intern/bpath.c
source/blender/blenlib/intern/path_util.c
source/blender/editors/space_image/image_ops.c
source/blender/editors/space_sequencer/sequencer_add.c
source/blender/python/generic/bpy_internal_import.c
source/blender/windowmanager/intern/wm_operators.c

index f860f57993030e7d26674e051b19094852b75cf5..ef95139abda44c90ebda8a464d17343eff279e86 100644 (file)
@@ -440,10 +440,8 @@ Image *BKE_add_image_imbuf(ImBuf *ibuf)
 {
        /* on save, type is changed to FILE in editsima.c */
        Image *ima;
-       char filename[sizeof(ibuf->name)];
 
-       BLI_split_dirfile(ibuf->name, NULL, filename);
-       ima= image_alloc(filename, IMA_SRC_FILE, IMA_TYPE_IMAGE);
+       ima= image_alloc(BLI_path_basename(ibuf->name), IMA_SRC_FILE, IMA_TYPE_IMAGE);
 
        if (ima) {
                BLI_strncpy(ima->name, ibuf->name, FILE_MAX);
index f1036b66f699c414686a8ed4bc905c724cd18953..e8328d0e62214c759a6676419f7b2e578ac532d1 100644 (file)
@@ -232,7 +232,6 @@ int reopen_text(Text *text)
        int i, llen, len, res;
        unsigned char *buffer;
        TextLine *tmp;
-       char sfile[FILE_MAXFILE];
        char str[FILE_MAXDIR+FILE_MAXFILE];
        struct stat st;
 
@@ -240,7 +239,6 @@ int reopen_text(Text *text)
        
        BLI_strncpy(str, text->name, FILE_MAXDIR+FILE_MAXFILE);
        BLI_path_abs(str, G.sce);
-       BLI_split_dirfile(str, NULL, sfile);
        
        fp= fopen(str, "r");
        if(fp==NULL) return 0;
@@ -331,19 +329,17 @@ Text *add_text(char *file, const char *relpath)
        unsigned char *buffer;
        TextLine *tmp;
        Text *ta;
-       char sfile[FILE_MAXFILE];
        char str[FILE_MAXDIR+FILE_MAXFILE];
        struct stat st;
 
        BLI_strncpy(str, file, FILE_MAXDIR+FILE_MAXFILE);
        if (relpath) /* can be NULL (bg mode) */
                BLI_path_abs(str, relpath);
-       BLI_split_dirfile(str, NULL, sfile);
        
        fp= fopen(str, "r");
        if(fp==NULL) return NULL;
        
-       ta= alloc_libblock(&G.main->text, ID_TXT, sfile);
+       ta= alloc_libblock(&G.main->text, ID_TXT, BLI_path_basename(str));
        ta->id.us= 1;
 
        ta->lines.first= ta->lines.last= NULL;
index 9bdc6c431e8e225c332fd04348d586d37eb66b74..fb30e991200fd6ed301a0e4142084ccf39bf9dd7 100644 (file)
@@ -53,6 +53,7 @@ void BLI_make_exist(char *dir);
 void BLI_make_existing_file(char *name);
 void BLI_split_dirfile(const char *string, char *dir, char *file);
 void BLI_join_dirfile(char *string, const char *dir, const char *file);
+char *BLI_path_basename(char *path);
 int BKE_rebase_path(char *abs, int abs_size, char *rel, int rel_size, const char *base_dir, const char *src_dir, const char *dest_dir);
 void BLI_getlastdir(const char* dir, char *last, int maxlen);
 int BLI_testextensie(const char *str, const char *ext);
index 9b1d29e6e123dc40f9b6e77e3465d8bcd7eb317c..a56cbb65538b4749fb6348277c7e4cd2db382da2 100644 (file)
@@ -661,7 +661,7 @@ void findMissingFiles(char *basepath, char *str) {
        char filepath[FILE_MAX], *libpath;
        int filesize, recur_depth;
        
-       char dirname[FILE_MAX], filename[FILE_MAX], filename_new[FILE_MAX];
+       char dirname[FILE_MAX], filename_new[FILE_MAX];
        
        //XXX waitcursor( 1 );
        
@@ -686,9 +686,8 @@ void findMissingFiles(char *basepath, char *str) {
                                /* can the dir be opened? */
                                filesize = -1;
                                recur_depth = 0;
-                               BLI_split_dirfile(filepath, NULL, filename); /* the file to find */
                                
-                               findFileRecursive(filename_new, dirname, filename, &filesize, &recur_depth);
+                               findFileRecursive(filename_new, dirname, BLI_path_basename(filepath), &filesize, &recur_depth);
                                if (filesize == -1) { /* could not open dir */
                                        printf("Could not open dir \"%s\"\n", dirname);
                                        return;
index 301b981cdc76d6da7baa56d8fd6b22d0c054a296..f91c38b8271f65f882617cea85e87d6731c68108 100644 (file)
@@ -1172,6 +1172,12 @@ void BLI_join_dirfile(char *string, const char *dir, const char *file)
        }
 }
 
+/* like pythons os.path.basename( ) */
+char *BLI_path_basename(char *path)
+{
+       const char *filename= BLI_last_slash(path);
+       return filename ? filename + 1 : path;
+}
 
 /*
   Produce image export path.
index 8b8b3b1132472d773f5e3694196e12c20941313e..33adc11caa0d1162c18741a34770edcbd60d7112 100644 (file)
@@ -862,7 +862,6 @@ static void save_image_doit(bContext *C, SpaceImage *sima, Scene *scene, wmOpera
                        BKE_image_release_renderresult(scene, ima);
                }
                else if (BKE_write_ibuf(scene, ibuf, path, sima->imtypenr, scene->r.subimtype, scene->r.quality)) {
-                       char *name;
 
                        if(relative)
                                BLI_path_rel(path, G.sce); /* only after saving */
@@ -895,10 +894,8 @@ static void save_image_doit(bContext *C, SpaceImage *sima, Scene *scene, wmOpera
                                        ima->type= IMA_TYPE_IMAGE;
                                }
 
-                               name = BLI_last_slash(path);
-
                                /* name image as how we saved it */
-                               rename_id(&ima->id, name ? name + 1 : path);
+                               rename_id(&ima->id, BLI_path_basename(path));
                        }
                } 
                else
index c1b30ab155a420b0428ccf43f845590b91d5df05..f36461ab7e0cf141fe0fd2e8f7d10d0c6a2337dc 100644 (file)
@@ -421,7 +421,7 @@ static int sequencer_add_image_strip_exec(bContext *C, wmOperator *op)
                RNA_END;
        }
        else {
-               BLI_split_dirfile(seq_load.path, NULL, se->name);
+               BLI_strncpy(se->name, BLI_path_basename(seq_load.path), sizeof(se->name));
                if(seq_load.start_frame < seq_load.end_frame) {
                        seq->endstill= seq_load.end_frame - seq_load.start_frame;
                }
index 6ac634999882a87eb135f3e2771192370a9908ac..01d0f56bf1b9574ae00193d20a07405ea7d147a3 100644 (file)
@@ -34,6 +34,7 @@
 #include "BKE_main.h"
 #include "BKE_global.h" /* grr, only for G.sce */
 #include "BLI_listbase.h"
+#include "BLI_path_util.h"
 #include <stddef.h>
 
 static Main *bpy_import_main= NULL;
@@ -129,8 +130,8 @@ PyObject *bpy_text_import_name( char *name, int *found )
 PyObject *bpy_text_reimport( PyObject *module, int *found )
 {
        Text *text;
-       const char *txtname;
        const char *name;
+       char *filepath;
        char *buf = NULL;
 //XXX  Main *maggie= bpy_import_main ? bpy_import_main:G.main;
        Main *maggie= bpy_import_main;
@@ -143,14 +144,14 @@ PyObject *bpy_text_reimport( PyObject *module, int *found )
        *found= 0;
        
        /* get name, filename from the module itself */
+       if((name= PyModule_GetName(module)) == NULL)
+               return NULL;
 
-       txtname = PyModule_GetFilename( module );
-       name = PyModule_GetName( module );
-       if( !txtname || !name)
+       if((filepath= (char *)PyModule_GetFilename(module)) == NULL)
                return NULL;
 
        /* look up the text object */
-       text= BLI_findstring(&maggie->text, txtname, offsetof(ID, name) + 2);
+       text= BLI_findstring(&maggie->text, BLI_path_basename(filepath), offsetof(ID, name) + 2);
 
        /* uh-oh.... didn't find it */
        if( !text )
index 842e43b98cf3f66c5b5e3193a3ea92299a1e6277..4ab110cc27585916639bec45623b3df284301389 100644 (file)
@@ -1212,10 +1212,7 @@ static uiBlock *wm_block_create_splash(bContext *C, ARegion *ar, void *arg_unuse
        col = uiLayoutColumn(split, 0);
        uiItemL(col, "Recent", 0);
        for(recent = G.recent_files.first, i=0; (i<5) && (recent); recent = recent->next, i++) {
-               char *display_name= BLI_last_slash(recent->filename);
-               if(display_name)        display_name++; /* skip the slash */
-               else                            display_name= recent->filename;
-               uiItemStringO(col, display_name, ICON_FILE_BLEND, "WM_OT_open_mainfile", "path", recent->filename);
+               uiItemStringO(col, BLI_path_basename(recent->filename), ICON_FILE_BLEND, "WM_OT_open_mainfile", "path", recent->filename);
        }
        uiItemS(col);
        uiItemO(col, NULL, ICON_RECOVER_LAST, "WM_OT_recover_last_session");