Hi,

check this out:
http://objectpainters.com/blog/2007/01/03/where-am-i-relative-paths/

The getPath things works in most cases. If you want a more complex version
that takes more things into account, you can use this:
You will need to replace the RuntimeExceptions with your own error mechanism
(eg traces, regular errors).

 /**
 * Resolves pAssetPath against pBasePath.
 *
 * - if pAssetPath is not a relative path, pAssetPath is returned (eg the
full url)
 * - if pBasePath is an empty path, pAssetPath is returned
 * - if pBasePath is a relative path, an error is thrown
 * - in all other situation a path is returned which may still be or not
valid.
 *
 * @param pAssetPath, a full or relative url
 * @param pBasePath, a full or empty url, this url MAY contain a file as
well, it is stripped automatically
 *
 */
 public static function resolve (pAssetPath:String, pBasePath:String):String
{
  //no base path
  if (pBasePath == null || pBasePath.length == 0) return pAssetPath;
  if (pAssetPath == null) {
    throw new RuntimeException (
     "Assetpath cannot be null.",
     Path, arguments.callee, null, null);
  }

  //file asset path
  if (pAssetPath.indexOf ("http") == 0 || pAssetPath.indexOf ("ftp") == 0 ||
   pAssetPath.indexOf ("rmtp") == 0 || pAssetPath.indexOf ("file") == 0)
return pAssetPath;
  //asset is relative, test basepath for correctness
  if (pBasePath.indexOf ("http") != 0 && pBasePath.indexOf ("ftp") != 0 &&
   pBasePath.indexOf ("rmtp") != 0 && pBasePath.indexOf ("file") != 0) {
    throw new RuntimeException (
     "Basepath is not null and not a full url, but needs to be either empty
or a full url.",
     Path, arguments.callee, null, null);
   }

  //so now we know that pAssetPath is a relative url and pBasePath is a full
url.
  //first normalize both urls so that we are dealing with only one type of
separator
  var lAssetPath:String = pAssetPath.split("\\").join("/<file://%22).join(%22/>
");
  var lBasePath:String = pBasePath.split("\\").join("/<file://%22).join(%22/>
");
  //strip everything after ? to strip parameters from basepath
  if (lBasePath.indexOf("?") > -1) {
   lBasePath = lBasePath.substr (lBasePath.lastIndexOf("?"));
  }
  //check if basepath ends with /, if not check if everything after /
contains a .
  //if it ends with / it is a directory, if it doesnt end with / and
everything after contains a . we assume
  //we are dealing with a file, otherwise a directory
  if (lBasePath.charAt (lBasePath.length-1) != "/") {
   //and the last part contains a . cut it off
   var lLastDir:String = lBasePath.substr (lBasePath.lastIndexOf("/"));
   if (lLastDir.indexOf (".") != -1) {
    //dealing with file
    lBasePath = lBasePath.substr (0, lBasePath.lastIndexOf("/")+1);
   } else {
    //assume the last part was a dir and the trailing slash was forgotten,
so add it
    lBasePath += "/";
   }
  }

  //at this point we have a relative url and full directory path with a
trailing /
  //now create two stacks
  var lAssetStack:Array = lAssetPath.split ("/");
  var lBaseStack:Array = lBasePath.split ("/");

  //remove trailing / from baseStack to provide a correct starting point,
  //our invariant is that each directory 'starts' with a slash and not ends
  lBaseStack.pop();

  //remove any superflous items (pointers to current directory
  //. points to current dir and isnt relative
  //"" points to double slashes or a starting slash, we remove that too
  while (lAssetStack[0] == "." || lAssetStack[0] == "") {
   lAssetStack.shift();
  }

  //remove .. from assetStack AND end of basestack
  while (lAssetStack[0] == "..") {
   lAssetStack.shift();
   lBaseStack.pop();
  }

  return lBaseStack.join("/")+"/"+lAssetStack.join("/");
 }

Usage eg:
xml.load (Path.resolve ("assets/config.xml"), _clip._url));

greetz
JC


On Wed, Jan 30, 2008 at 6:24 AM, Deepanjan Das <[EMAIL PROTECTED]>
wrote:

> Hi,
> You need to keep duplicate files if you want it to work as single and also
> when loaded from main movie.
> Easiest way is to create an xml directory at the place where the main
> movie
> resides and set the path as "xml/1.xml"
>
> also copy this directory in the screens directory
>
> so ths ame path will work for both :)
> Hope this helps
>
> Deepanjan Das
>
> On Jan 30, 2008 10:10 AM, confusticate and bebother these dwarves! <
>  [EMAIL PROTECTED]> wrote:
>
> > Hello Flashcoders,
> >
> > I'm trying to make a main movie ("controller.swf") that loads other
> movies
> > ("screen1.swf", "screen2.swf", etc), which are stored in a subdirectory
> > called "screens".
> >
> > In controller.swf I'm using loadMovie("screens/screenx.swf") to load the
> > movies from the "screens" subdirectory, and this works fine. But the
> > problem
> > is that the movies in the "screens" subdirectory often reference XML
> files
> > (which also live in the "screens" subdirectory). All works well when you
> > play the individual screenx.swf files, obviously, but when you play
> > controller.swf, it is looking for the XML files in the same directory as
> > itself (one directory "up").
> >
> > How can I fix this problem so that playing controller.swf works fine, as
> > well as playing each individual screenx.swf file in the "screens"
> > subdirectory? Is this possible? It seems like such a simple idea, but
> I'm
> > so
> > stumped.
> >
> > I should mention that this project is not actually going online - it is
> > going to be converted to an EXE using Zinc and distributed via CD. So I
> > can't use absolute paths.
> >
> > Please! Heelp! Thanks!
> > _______________________________________________
> > Flashcoders mailing list
> > [email protected]
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> _______________________________________________
> Flashcoders mailing list
> [email protected]
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to