0

I am trying to find the answer from google, but i am failed.
I am familiar with python, in which we can check the platform-specific directory separator by "os.path.sep".

Is there a way or some constant in XPCOM component that can indicate the path separator?
Or is there a way to normalize the path specific to platform?

For example, user input a file path under window: C:/path1/path2/test.txt
Using nsILocalFile::initWithPath will throw exception if I do not make the path platform valid. The valid path should be "C:\path1\path2\test.txt". So I wonder there is way to make a path valid and can init the nsILocalFile.

6
  • can you elaborate on why you want to know the directory separator? nsiFile has an append function which abstracts os paths
    – skabbes
    Apr 28, 2011 at 5:53
  • Yes, I know that function, but there is some special case we need split the file path that some user input. It would be very helpful if we can know the platform specific path separator for further process of the user input path.
    – winterTTr
    Apr 28, 2011 at 6:06
  • you could probably construct an nsIFile (for an arbitrary file) and compare its path with its parent's path. I think that should work.
    – skabbes
    Apr 28, 2011 at 14:51
  • @winterTTr nsIFile abstracts splitting the file path too, doesn't it?
    – Neil
    May 1, 2011 at 17:51
  • @Neil, yes, but it use static internal separator specific to platform at C level code, and if you give a non-standard path, init a nsIFile will failed, but not an auto-convert action.
    – winterTTr
    May 2, 2011 at 9:59

2 Answers 2

2
/**
 * Returns file path separator under current OS
 */
function getPathSeparator()
{
    var profD = Components.classes["@mozilla.org/file/directory_service;1"].
                getService(Components.interfaces.nsIProperties).
                get("ProfD", Components.interfaces.nsIFile);
    profD.append("abc");
    profD.append("abc");
    var length = profD.path.length;
    return profD.path.substr(length-("abc".length)-1,1);
}
0

You can use nsIIOService.newURI with a file:// URI, and get a nsIFileURL object back, from which you can get an nsIFile object back. You can get the path information from the nsIFile object.

2
  • Yes, you idea is kinda useful, but not meet my requirement. I test the code using such as "Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService).newURI('file:///root/test\\test1.txt' , null , null ).path", the result is "/root/test%5Ctest.txt", but not the "/root/test/test.txt".
    – winterTTr
    Apr 29, 2011 at 5:34
  • Well, why are you mixing the backslash separator with slash separators? The URL that you're constructing is pointing to a file named "test\test1.txt" under the folder /root, not test1.txt under /root/test. The value of the path attribute reflects this.
    – ehsan
    May 16, 2011 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.