From 4a04f7206914a49f5f95adc5eb786237f1a9f547 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sun, 23 Oct 2011 17:52:20 +0000 Subject: remove $Id: tags after discussion on the mailign list: http://markmail.org/message/fp7ozcywxum3ar7n --- source/blender/blenlib/intern/bpath.c | 58 ++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) (limited to 'source/blender/blenlib/intern/bpath.c') diff --git a/source/blender/blenlib/intern/bpath.c b/source/blender/blenlib/intern/bpath.c index 293f824c5fd..2884ea2743a 100644 --- a/source/blender/blenlib/intern/bpath.c +++ b/source/blender/blenlib/intern/bpath.c @@ -1,5 +1,4 @@ /* - * * ***** BEGIN GPL LICENSE BLOCK ***** * * This program is free software; you can redistribute it and/or @@ -952,3 +951,60 @@ void findMissingFiles(Main *bmain, const char *str) //XXX waitcursor( 0 ); } + +/* Run a visitor on a string, replacing the contents of the string as needed. */ +static void rewrite_path(char *path, bpath_visitor visit, void *userdata) { + char pathOut[FILE_MAX]; + if (visit(userdata, path, pathOut)) + BLI_strncpy(path, pathOut, FILE_MAX); +} + +/* Run visitor function 'visit' on all paths contained in 'id'. */ +void bpath_traverse_id(ID *id, bpath_visitor visit, void *userdata) { + Image *ima; + + switch(GS(id->name)) { + case ID_IM: + ima = (Image*)id; + if (ELEM3(ima->source, IMA_SRC_FILE, IMA_SRC_MOVIE, IMA_SRC_SEQUENCE)) + rewrite_path(ima->name, visit, userdata); + break; + case ID_OB: + case ID_SO: + case ID_TXT: + /* TODO: add other ID types e.g. object (modifiers) */ + default: + /* Nothing to do for other IDs that don't contain file paths. */ + break; + } +} + +/* Rewrites a relative path to be relative to the main file - unless the path is + absolute, in which case it is not altered. */ +int bpath_relocate_visitor(void *oldbasepath_v, char *pathIn, char *pathOut) { + /* be sure there is low chance of the path being too short */ + char filepath[(FILE_MAXDIR * 2) + FILE_MAXFILE]; + char *oldbasepath = oldbasepath_v; + + if (strncmp(oldbasepath, "//", 2) == 0) { + printf("Error: old base path '%s' is not absolute.\n", oldbasepath); + return 0; + } + + /* Make referenced file absolute. This would be a side-effect of + BLI_cleanup_file, but we do it explicitely so we know if it changed. */ + BLI_strncpy(filepath, pathIn, FILE_MAX); + if (BLI_path_abs(filepath, oldbasepath)) { + /* Path was relative and is now absolute. Remap. + * Important BLI_cleanup_dir runs before the path is made relative + * because it wont work for paths that start with "//../" */ + BLI_cleanup_file(G.main->name, filepath); + BLI_path_rel(filepath, G.main->name); + BLI_strncpy(pathOut, filepath, FILE_MAX); + return 1; + } + else { + /* Path was not relative to begin with. */ + return 0; + } +} -- cgit v1.2.3