2.5
[blender.git] / source / blender / blenlib / intern / bpath.c
index 219ca424df870a0d77a50d6655899f6cd7fc50a0..5c8eb5d2aa0ccf8ebc77fbf3a9477a0cc0e8a69a 100644 (file)
@@ -1,14 +1,11 @@
 /**
  *
- * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version. The Blender
- * Foundation also sells licenses for use in proprietary software under
- * the Blender License.  See http://www.blender.org/BL/ for information
- * about this.
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  *
  * Contributor(s): Campbell barton
  *
- * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ * ***** END GPL LICENSE BLOCK *****
  */
 
-#include "BLI_bpath.h"
-#include "BKE_global.h"
+#include "MEM_guardedalloc.h"
+
 #include "DNA_ID.h" /* Library */
 #include "DNA_vfont_types.h"
 #include "DNA_image_types.h"
 #include "DNA_sound_types.h"
 #include "DNA_scene_types.h" /* to get the current frame */
-#include <stdlib.h>
-#include <string.h>
+#include "DNA_sequence_types.h"
+#include "DNA_text_types.h"
 
-#include "BKE_main.h" /* so we can access G.main->*.first */
-#include "BKE_image.h" /* so we can check the image's type */
+#include "BLI_blenlib.h"
+#include "BLI_bpath.h"
 
-#include "blendef.h"
+#include "BKE_global.h"
+#include "BKE_image.h" /* so we can check the image's type */
+#include "BKE_main.h" /* so we can access G.main->*.first */
+#include "BKE_sequence.h"
+#include "BKE_text.h" /* for writing to a textblock */
 #include "BKE_utildefines.h"
 
-/* for writing to a textblock */
-#include "BKE_text.h" 
-#include "BLI_blenlib.h"
-#include "DNA_text_types.h"
+//XXX #include "BIF_screen.h" /* only for wait cursor */
+//
+/* for sequence */
+//XXX #include "BSE_sequence.h"
+//XXX define below from BSE_sequence.h - otherwise potentially odd behaviour
+#define SEQ_HAS_PATH(seq) (seq->type==SEQ_MOVIE || seq->type==SEQ_HD_SOUND || seq->type==SEQ_RAM_SOUND || seq->type==SEQ_IMAGE)
 
 /* path/file handeling stuff */
 #ifndef WIN32
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-
+#include <string.h>
 
 #define FILE_MAX                       240
 
-
 /* TODO - BPATH_PLUGIN, BPATH_SEQ */
 enum BPathTypes {
        BPATH_IMAGE = 0,
        BPATH_SOUND,
        BPATH_FONT,
        BPATH_LIB,
+       BPATH_SEQ,
 
        BPATH_DONE
 };
 
-
 void BLI_bpathIterator_init( struct BPathIterator *bpi ) {
        bpi->type = BPATH_IMAGE;
        bpi->data = NULL;
+       
+       bpi->getpath_callback = NULL;
+       bpi->setpath_callback = NULL;
+       
+       /* Sequencer spesific */
+       bpi->seqdata.totseq = 0;
+       bpi->seqdata.seq = 0;
+       bpi->seqdata.seqar = NULL;
+       bpi->seqdata.scene = NULL;
+       
        BLI_bpathIterator_step(bpi);
 }
 
-char* BLI_bpathIterator_getPath( struct BPathIterator *bpi) {
-       return bpi->path;
+void BLI_bpathIterator_free( struct BPathIterator *bpi ) {
+       if (bpi->seqdata.seqar)
+               MEM_freeN((void *)bpi->seqdata.seqar);
+       bpi->seqdata.seqar = NULL;
+       bpi->seqdata.scene = NULL;
+}
+
+void BLI_bpathIterator_getPath( struct BPathIterator *bpi, char *path) {
+       if (bpi->getpath_callback) {
+               bpi->getpath_callback( bpi, path );
+       } else {
+               strcpy(path, bpi->path); /* warning, we assume 'path' are long enough */
+       }
+}
+
+void BLI_bpathIterator_setPath( struct BPathIterator *bpi, char *path) {
+       if (bpi->setpath_callback) {
+               bpi->setpath_callback( bpi, path );
+       } else {
+               strcpy(bpi->path, path); /* warning, we assume 'path' are long enough */
+       }
 }
-void BLI_bpathIterator_copyPathExpanded( struct BPathIterator *bpi, char *path_expanded) {
-       char *filepath, *libpath;
+
+void BLI_bpathIterator_getPathExpanded( struct BPathIterator *bpi, char *path_expanded) {
+       char *libpath;
        
-       filepath = BLI_bpathIterator_getPath(bpi);
+       BLI_bpathIterator_getPath(bpi, path_expanded);
        libpath = BLI_bpathIterator_getLib(bpi);
        
-       BLI_strncpy(path_expanded, filepath, FILE_MAXDIR*2);
-       
        if (libpath) { /* check the files location relative to its library path */
-               BLI_convertstringcode(path_expanded, libpath, G.scene->r.cfra);
+               BLI_convertstringcode(path_expanded, libpath);
        } else { /* local data, use the blend files path */
-               BLI_convertstringcode(path_expanded, G.sce, G.scene->r.cfra);
+               BLI_convertstringcode(path_expanded, G.sce);
        }
 }
 char* BLI_bpathIterator_getLib( struct BPathIterator *bpi) {
@@ -119,7 +149,7 @@ int BLI_bpathIterator_getPathMaxLen( struct BPathIterator *bpi) {
 }
 
 /* gets the first or the next image that has a path - not a viewer node or generated image */
-static struct Image *ima_getpath__internal(struct Image *ima, int step_next) {
+static struct Image *ima_stepdata__internal(struct Image *ima, int step_next) {
        if (ima==NULL)
                return NULL;
        
@@ -135,7 +165,7 @@ static struct Image *ima_getpath__internal(struct Image *ima, int step_next) {
        return ima;
 }
 
-static struct VFont *vf_getpath__internal(struct VFont *vf, int step_next) {
+static struct VFont *vf_stepdata__internal(struct VFont *vf, int step_next) {
        if (vf==NULL)
                return NULL;
        
@@ -153,7 +183,7 @@ static struct VFont *vf_getpath__internal(struct VFont *vf, int step_next) {
        return vf;
 }
 
-static struct bSound *snd_getpath__internal(struct bSound *snd, int step_next) {
+static struct bSound *snd_stepdata__internal(struct bSound *snd, int step_next) {
        if (snd==NULL)
                return NULL;
        
@@ -171,13 +201,117 @@ static struct bSound *snd_getpath__internal(struct bSound *snd, int step_next) {
        return snd;
 }
 
+static struct Sequence *seq_stepdata__internal(struct BPathIterator *bpi, int step_next) {
+       Sequence *seq;
+       
+       /* Initializing */
+       if (bpi->seqdata.scene==NULL) {
+               bpi->seqdata.scene= G.main->scene.first;
+       }
+       
+       if (step_next) {
+               bpi->seqdata.seq++;
+       }
+       
+       while (bpi->seqdata.scene) {
+               
+               if (bpi->seqdata.scene->ed) {
+                       if (bpi->seqdata.seqar == NULL) {
+                               /* allocate the sequencer array */
+                               seq_array(bpi->seqdata.scene->ed, &bpi->seqdata.seqar, &bpi->seqdata.totseq, 0);                
+                               bpi->seqdata.seq = 0;
+                       }
+                       
+                       if (bpi->seqdata.seq >= bpi->seqdata.totseq) {
+                               seq = NULL;
+                       } else {
+                               seq = bpi->seqdata.seqar[bpi->seqdata.seq];
+                               while (!SEQ_HAS_PATH(seq)) {
+                                       bpi->seqdata.seq++;
+                                       if (bpi->seqdata.seq >= bpi->seqdata.totseq) {
+                                               seq = NULL;
+                                               break;
+                                       }
+                                       seq = bpi->seqdata.seqar[bpi->seqdata.seq];
+                               }
+                       }
+                       if (seq) {
+                               return seq;
+                       } else {
+                               /* keep looking through the next scene, reallocate seq array */
+                               if (bpi->seqdata.seqar) {
+                                       MEM_freeN((void *)bpi->seqdata.seqar);
+                                       bpi->seqdata.seqar = NULL;
+                               }
+                               bpi->seqdata.scene = bpi->seqdata.scene->id.next;
+                       }
+               } else {
+                       /* no seq data in this scene, next */
+                       bpi->seqdata.scene = bpi->seqdata.scene->id.next;
+               }
+       }
+       
+       return NULL;
+}
+
+static void seq_getpath(struct BPathIterator *bpi, char *path) {
+       Sequence *seq = (Sequence *)bpi->data;
+
+       
+       path[0] = '\0'; /* incase we cant get the path */
+       if (seq==NULL) return;
+       if (SEQ_HAS_PATH(seq)) {
+               if (seq->type == SEQ_IMAGE || seq->type == SEQ_MOVIE) {
+                       BLI_strncpy(path, seq->strip->dir, FILE_MAX);
+                       BLI_add_slash(path); /* incase its missing */
+                       if (seq->strip->stripdata) { /* should always be true! */
+                               /* Using the first image is weak for image sequences */
+                               strcat(path, seq->strip->stripdata->name);
+                       } 
+               } else {
+                       /* simple case */
+                       BLI_strncpy(seq->strip->dir, path, sizeof(seq->strip->dir));
+               }
+       }
+}
+
+static void seq_setpath(struct BPathIterator *bpi, char *path) {
+       Sequence *seq = (Sequence *)bpi->data;
+       if (seq==NULL) return; 
+       
+       if (SEQ_HAS_PATH(seq)) {
+               if (seq->type == SEQ_IMAGE || seq->type == SEQ_MOVIE) {
+                       BLI_split_dirfile_basic(path, seq->strip->dir, seq->strip->stripdata->name);
+               } else {
+                       /* simple case */
+                       BLI_strncpy(seq->strip->dir, path, sizeof(seq->strip->dir));
+               }
+       }
+}
+
+static void bpi_type_step__internal( struct BPathIterator *bpi) {
+       bpi->type++; /* advance to the next type */
+       bpi->data = NULL;
+       
+       switch (bpi->type) {
+       case BPATH_SEQ:
+               bpi->getpath_callback = seq_getpath;
+               bpi->setpath_callback = seq_setpath;
+               break;
+       default:
+               bpi->getpath_callback = NULL;
+               bpi->setpath_callback = NULL;
+               break;
+       }
+}
+
 void BLI_bpathIterator_step( struct BPathIterator *bpi) {
        while (bpi->type != BPATH_DONE) {
                
                if  ((bpi->type) == BPATH_IMAGE) {
                        /*if (bpi->data)        bpi->data = ((ID *)bpi->data)->next;*/
-                       if (bpi->data)  bpi->data = ima_getpath__internal( (Image *)bpi->data, 1 ); /* must skip images that have no path */
-                       else                    bpi->data = ima_getpath__internal(G.main->image.first, 0);
+                       if (bpi->data)  bpi->data = ima_stepdata__internal( (Image *)bpi->data, 1 ); /* must skip images that have no path */
+                       else                    bpi->data = ima_stepdata__internal(G.main->image.first, 0);
                        
                        if (bpi->data) {
                                /* get the path info from this datatype */
@@ -192,13 +326,13 @@ void BLI_bpathIterator_step( struct BPathIterator *bpi) {
                                break;
                                
                        } else {
-                               bpi->type+=1; /* advance to the next type */
+                               bpi_type_step__internal(bpi);
                        }
                        
                        
                } else if  ((bpi->type) == BPATH_SOUND) {
-                       if (bpi->data)  bpi->data = snd_getpath__internal( (bSound *)bpi->data, 1 ); /* must skip images that have no path */
-                       else                    bpi->data = snd_getpath__internal(G.main->sound.first, 0);
+                       if (bpi->data)  bpi->data = snd_stepdata__internal( (bSound *)bpi->data, 1 ); /* must skip images that have no path */
+                       else                    bpi->data = snd_stepdata__internal(G.main->sound.first, 0);
                        
                        if (bpi->data) {
                                /* get the path info from this datatype */
@@ -212,14 +346,14 @@ void BLI_bpathIterator_step( struct BPathIterator *bpi) {
                                /* we are done, advancing to the next item, this type worked fine */
                                break;
                        } else {
-                               bpi->type+=1; /* advance to the next type */
+                               bpi_type_step__internal(bpi);
                        }
                        
                        
                } else if  ((bpi->type) == BPATH_FONT) {
                        
-                       if (bpi->data)  bpi->data = vf_getpath__internal( (VFont *)bpi->data, 1 );
-                       else                    bpi->data = vf_getpath__internal( G.main->vfont.first, 0 );
+                       if (bpi->data)  bpi->data = vf_stepdata__internal( (VFont *)bpi->data, 1 );
+                       else                    bpi->data = vf_stepdata__internal( G.main->vfont.first, 0 );
                        
                        if (bpi->data) {
                                /* get the path info from this datatype */
@@ -233,12 +367,10 @@ void BLI_bpathIterator_step( struct BPathIterator *bpi) {
                                /* we are done, advancing to the next item, this type worked fine */
                                break;
                        } else {
-                               bpi->type+=1; /* advance to the next type */
+                               bpi_type_step__internal(bpi);
                        }
                        
-                       
                } else if  ((bpi->type) == BPATH_LIB) {
-                       
                        if (bpi->data)  bpi->data = ((ID *)bpi->data)->next;
                        else                    bpi->data = G.main->library.first;
                        
@@ -254,7 +386,19 @@ void BLI_bpathIterator_step( struct BPathIterator *bpi) {
                                /* we are done, advancing to the next item, this type worked fine */
                                break;
                        } else {
-                               bpi->type+=1; /* advance to the next type */
+                               bpi_type_step__internal(bpi);
+                       }
+               } else if  ((bpi->type) == BPATH_SEQ) {
+                       if (bpi->data)  bpi->data = seq_stepdata__internal( bpi, 1 );
+                       else                    bpi->data = seq_stepdata__internal( bpi, 0 );
+                       if (bpi->data) {
+                               Sequence *seq = (Sequence *)bpi->data;
+                               bpi->lib = NULL;
+                               bpi->name = seq->name+2;
+                               bpi->len = sizeof(seq->strip->stripdata->name);
+                               break;
+                       } else {
+                               bpi_type_step__internal(bpi);
                        }
                }
        }
@@ -283,6 +427,9 @@ static void bpathToText(Text *btxt, struct BPathIterator *bpi)
        case BPATH_LIB:
                txt_insert_buf( btxt, "Library \"" );
                break;
+       case BPATH_SEQ:
+               txt_insert_buf( btxt, "Sequence \"" );
+               break;
        default:
                txt_insert_buf( btxt, "Unknown \"" );
                break;
@@ -295,7 +442,7 @@ static void bpathToText(Text *btxt, struct BPathIterator *bpi)
        }
        txt_insert_buf( btxt, "\" " );
        
-       BLI_bpathIterator_copyPathExpanded(bpi, path_expanded);
+       BLI_bpathIterator_getPathExpanded(bpi, path_expanded);
        
        txt_insert_buf( btxt, path_expanded );
        txt_insert_buf( btxt, "\n" );
@@ -309,15 +456,10 @@ void checkMissingFiles( char *txtname ) {
        
        /* be sure there is low chance of the path being too short */
        char filepath_expanded[FILE_MAXDIR*2]; 
-       char *filepath, *libpath;
-       int files_missing = 0;
        
        BLI_bpathIterator_init(&bpi);
        while (!BLI_bpathIterator_isDone(&bpi)) {
-               filepath = BLI_bpathIterator_getPath(&bpi);
-               libpath = BLI_bpathIterator_getLib(&bpi);
-               
-               BLI_bpathIterator_copyPathExpanded( &bpi, filepath_expanded );
+               BLI_bpathIterator_getPathExpanded( &bpi, filepath_expanded );
                
                if (!BLI_exists(filepath_expanded)) {
                        if (!btxt) {
@@ -327,16 +469,16 @@ void checkMissingFiles( char *txtname ) {
                                }
                        }
                        bpathToText(btxt, &bpi);
-                       files_missing = 1;
                }
                BLI_bpathIterator_step(&bpi);
        }
+       BLI_bpathIterator_free(&bpi);
 }
 
 /* dont log any errors at the moment, should probably do this */
 void makeFilesRelative(char *txtname, int *tot, int *changed, int *failed, int *linked) {
        struct BPathIterator bpi;
-       char *filepath, *libpath;
+       char filepath[FILE_MAX], *libpath;
        
        /* be sure there is low chance of the path being too short */
        char filepath_relative[(FILE_MAXDIR * 2) + FILE_MAXFILE];
@@ -347,7 +489,7 @@ void makeFilesRelative(char *txtname, int *tot, int *changed, int *failed, int *
        
        BLI_bpathIterator_init(&bpi);
        while (!BLI_bpathIterator_isDone(&bpi)) {
-               filepath = BLI_bpathIterator_getPath(&bpi);
+               BLI_bpathIterator_getPath(&bpi, filepath);
                libpath = BLI_bpathIterator_getLib(&bpi);
                
                if(strncmp(filepath, "//", 2)) {
@@ -371,7 +513,7 @@ void makeFilesRelative(char *txtname, int *tot, int *changed, int *failed, int *
                                        (*failed)++;
                                } else {
                                        if(strncmp(filepath_relative, "//", 2)==0) {
-                                               strcpy(filepath, filepath_relative);
+                                               BLI_bpathIterator_setPath(&bpi, filepath_relative);
                                                (*changed)++;
                                        } else {
                                                if (!btxt) {
@@ -389,13 +531,14 @@ void makeFilesRelative(char *txtname, int *tot, int *changed, int *failed, int *
                BLI_bpathIterator_step(&bpi);
                (*tot)++;
        }
+       BLI_bpathIterator_free(&bpi);
 }
 
 /* dont log any errors at the moment, should probably do this -
  * Verry similar to makeFilesRelative - keep in sync! */
 void makeFilesAbsolute(char *txtname, int *tot, int *changed, int *failed, int *linked) {
        struct BPathIterator bpi;
-       char *filepath, *libpath;
+       char filepath[FILE_MAX], *libpath;
        
        /* be sure there is low chance of the path being too short */
        char filepath_absolute[(FILE_MAXDIR * 2) + FILE_MAXFILE];
@@ -406,14 +549,14 @@ void makeFilesAbsolute(char *txtname, int *tot, int *changed, int *failed, int *
        
        BLI_bpathIterator_init(&bpi);
        while (!BLI_bpathIterator_isDone(&bpi)) {
-               filepath = BLI_bpathIterator_getPath(&bpi);
+               BLI_bpathIterator_getPath(&bpi, filepath);
                libpath = BLI_bpathIterator_getLib(&bpi);
                
                if(strncmp(filepath, "//", 2)==0) {
                        if (libpath) { /* cant make absolute if we are library - TODO, LOG THIS */
                                (*linked)++;
                        } else { /* get the expanded path and check it is relative or too long */
-                               BLI_bpathIterator_copyPathExpanded( &bpi, filepath_absolute );
+                               BLI_bpathIterator_getPathExpanded( &bpi, filepath_absolute );
                                BLI_cleanup_file(G.sce, filepath_absolute); /* fix any /foo/../foo/ */
                                /* to be safe, check the length */
                                if (BLI_bpathIterator_getPathMaxLen(&bpi) <= strlen(filepath_absolute)) {
@@ -427,7 +570,7 @@ void makeFilesAbsolute(char *txtname, int *tot, int *changed, int *failed, int *
                                        (*failed)++;
                                } else {
                                        if(strncmp(filepath_absolute, "//", 2)) {
-                                               strcpy(filepath, filepath_absolute);
+                                               BLI_bpathIterator_setPath(&bpi, filepath_absolute);
                                                (*changed)++;
                                        } else {
                                                if (!btxt) {
@@ -445,6 +588,7 @@ void makeFilesAbsolute(char *txtname, int *tot, int *changed, int *failed, int *
                BLI_bpathIterator_step(&bpi);
                (*tot)++;
        }
+       BLI_bpathIterator_free(&bpi);
 }
 
 
@@ -508,28 +652,35 @@ void findMissingFiles(char *str) {
        
        /* be sure there is low chance of the path being too short */
        char filepath_expanded[FILE_MAXDIR*2]; 
-       char *filepath, *libpath;
+       char filepath[FILE_MAX], *libpath;
        int filesize, recur_depth;
        
-       char dirname[FILE_MAX], filename[FILE_MAX], filename_new[FILE_MAX], dummyname[FILE_MAX];
+       char dirname[FILE_MAX], filename[FILE_MAX], filename_new[FILE_MAX];
+       
+       //XXX waitcursor( 1 );
        
-       BLI_split_dirfile(str, dirname, dummyname);
+       BLI_split_dirfile_basic(str, dirname, NULL);
        
        BLI_bpathIterator_init(&bpi);
        
        while (!BLI_bpathIterator_isDone(&bpi)) {
-               filepath = BLI_bpathIterator_getPath(&bpi);
+               BLI_bpathIterator_getPath(&bpi, filepath);
                libpath = BLI_bpathIterator_getLib(&bpi);
                
+               /* Check if esc was pressed because searching files can be slow */
+               /*XXX if (blender_test_break()) {
+                       break;
+               }*/
+               
                if (libpath==NULL) {
                        
-                       BLI_bpathIterator_copyPathExpanded( &bpi, filepath_expanded );
+                       BLI_bpathIterator_getPathExpanded( &bpi, filepath_expanded );
                        
                        if (!BLI_exists(filepath_expanded)) {
                                /* can the dir be opened? */
                                filesize = -1;
                                recur_depth = 0;
-                               BLI_split_dirfile(filepath, dummyname, filename); /* the file to find */
+                               BLI_split_dirfile_basic(filepath, NULL, filename); /* the file to find */
                                
                                findFileRecursive(filename_new, dirname, filename, &filesize, &recur_depth);
                                if (filesize == -1) { /* could not open dir */
@@ -546,11 +697,14 @@ void findMissingFiles(char *str) {
                                                if (G.relbase_valid)
                                                        BLI_makestringcode(G.sce, filename_new);
                                                
-                                               strcpy( BLI_bpathIterator_getPath( &bpi ), filename_new );
+                                               BLI_bpathIterator_setPath( &bpi, filename_new );
                                        }
                                }
                        }
                }
                BLI_bpathIterator_step(&bpi);
        }
+       BLI_bpathIterator_free(&bpi);
+       
+       //XXX waitcursor( 0 );
 }