cocoa - NSURL: Display Relative URLs? -
has come across solution or addition nsurl make relative file url based on this:
nsurl * = [nsurl urlwithstring:@"/users/me/scripts/python/test.py"]; nsurl * = [nsurl urlwithstring:@"/users/me/new-scripts/python/test.py"]; nsurl * rel = [nsurl urlfrom:from to:to]; nslog(@"%@",rel); --> ../../new-scripts/python/test.py
and i'm looking able take relative path "../../new-scripts/python/test.py" , combine absolute url new url.
[nsurl urlwithstring:@"../../new-scripts/python/test.py" relativetourl:[nsurl fileurlwithpath:@"/users/me/scripts/python/test.py"]];
haven't come across on google, , trying implement myself can end having lot intricacies correctness. wondering if apple or or if c/posix functions exist already?
thanks!
i came accross https://github.com/karelia/ksfileutilities, doesn't work file urls. wrote this. http://pastebin.com/fkyjvwpu
#import <foundation/foundation.h> nsstring * relativize(nsurl * to, nsurl * from, bool fromisdir); int main(int argc, const char * argv[]) { @autoreleasepool { //nsurl * u = [nsurl urlwithstring:@"../../scripts" relativetourl:[nsurl fileurlwithpath:@"/users/aaronsmith/development/aaron/"]]; //nslog(@"%@",u.absolutestring); //nslog(@"%@",[nsurl urlwithstring:@"../../scripts.py" relativetourl:[nsurl fileurlwithpath:@"/users/aaronsmith/scipts/"]]); nsurl * = null; nsurl * = null; = [nsurl fileurlwithpath:@"/users/aaronsmith/uverse/upload-issue.py"]; = [nsurl fileurlwithpath:@"/users/aaronsmith/scripts.scrpt"]; assert([relativize(to,from,false) isequaltostring:@"./uverse/upload-issue.py"]); = [nsurl fileurlwithpath:@"/users/aaronsmith/uverse/test/upload-issue.py"]; = [nsurl fileurlwithpath:@"/users/aaronsmith/"]; assert([relativize(to, from, true) isequaltostring:@"./uverse/test/upload-issue.py"]); relativize(to,from,true); = [nsurl fileurlwithpath:@"/users/aaronsmith/uverse/test/upload-issue.py"]; = [nsurl fileurlwithpath:@"/users/aaronsmith/example/scripts/"]; assert([relativize(to,from,true) isequaltostring:@"../../uverse/test/upload-issue.py"]); = [nsurl fileurlwithpath:@"/users/aaronsmith/uverse/test/scripts/stuff/upload-issue.py"]; = [nsurl fileurlwithpath:@"/users/johnson/scripts/"]; assert([relativize(to,from,true) isequaltostring:@"../../aaronsmith/uverse/test/scripts/stuff/upload-issue.py"]); = [nsurl urlwithstring:@"/users/aaronsmith/upload-issue.py"]; = [nsurl urlwithstring:@"/users/aaronsmith/"]; assert([relativize(to,from,true) isequaltostring:@"./upload-issue.py"]); = [nsurl urlwithstring:@"/users/aaronsmith/upload-issue.py"]; = [nsurl urlwithstring:@"/users/aaronsmith/scripts.scrpt"]; assert([relativize(to,from,false) isequaltostring:@"./upload-issue.py"]); = [nsurl urlwithstring: @"/users/aaronsmith/uverse/test/upload-issue.py"]; = [nsurl urlwithstring:@"/users/aaronsmith/test/whatever/scripts/script.scrpt"]; assert([relativize(to,from,false) isequaltostring:@"../../../uverse/test/upload-issue.py"]); = [nsurl urlwithstring: @"/users/aaronsmith/uverse/test/upload-issue.py"]; = [nsurl urlwithstring:@"/users/aaronsmith/test/whatever/scripts/"]; assert([relativize(to,from,true) isequaltostring:@"../../../uverse/test/upload-issue.py"]); = [nsurl urlwithstring:@"/users/aaronsmith/uverse/test/upload-issue.py"]; = [nsurl urlwithstring:@"/users/aaronsmith/uverse/test/scripts.scrpt"]; assert([relativize(to, from, false) isequaltostring:@"./upload-issue.py"]); = [nsurl urlwithstring:@"/users/aaronsmith/upload-issue.py"]; = [nsurl urlwithstring:@"/users/aaronsmith/uverse/test/scripts.scrpt"]; assert([relativize(to, from, false) isequaltostring:@"../../upload-issue.py"]); } return 0; } nsstring * relativize(nsurl * to, nsurl * from, bool fromisdir) { nsstring * tostring = [[to absolutestring] stringbystandardizingpath]; nsmutablearray * topieces = [nsmutablearray arraywitharray:[tostring pathcomponents]]; nsstring * fromstring = [[from absolutestring] stringbystandardizingpath]; nsmutablearray * frompieces = [nsmutablearray arraywitharray:[fromstring pathcomponents]]; nsmutablestring * relpath = [nsmutablestring string]; nsstring * totrimmed = tostring; nsstring * topiece = null; nsstring * fromtrimmed = fromstring; nsstring * frompiece = null; nsmutablearray * parents = [nsmutablearray array]; nsmutablearray * pieces = [nsmutablearray array]; if(topieces.count >= frompieces.count) { nsuinteger tocount = topieces.count; while(tocount > frompieces.count) { topiece = [totrimmed lastpathcomponent]; totrimmed = [totrimmed stringbydeletinglastpathcomponent]; [pieces insertobject:topiece atindex:0]; tocount--; } while(![fromtrimmed isequaltostring:totrimmed]) { topiece = [totrimmed lastpathcomponent]; totrimmed = [totrimmed stringbydeletinglastpathcomponent]; frompiece = [fromtrimmed lastpathcomponent]; fromtrimmed = [fromtrimmed stringbydeletinglastpathcomponent]; if(![topiece isequaltostring:frompiece]) { if(![frompiece isequaltostring:[frompiece lastpathcomponent]] || fromisdir) { [parents addobject:@".."]; } [pieces insertobject:topiece atindex:0]; } } } else { nsuinteger fromcount = frompieces.count; while(fromcount > topieces.count) { frompiece = [fromtrimmed lastpathcomponent]; fromtrimmed = [fromtrimmed stringbydeletinglastpathcomponent]; if(![frompiece isequaltostring:[fromstring lastpathcomponent]] || fromisdir) { [parents addobject:@".."]; } fromcount--; } while(![totrimmed isequaltostring:fromtrimmed]) { topiece = [totrimmed lastpathcomponent]; totrimmed = [totrimmed stringbydeletinglastpathcomponent]; frompiece = [fromtrimmed lastpathcomponent]; fromtrimmed = [fromtrimmed stringbydeletinglastpathcomponent]; [parents addobject:@".."]; [pieces insertobject:topiece atindex:0]; } } [relpath appendstring:[parents componentsjoinedbystring:@"/"]]; if(parents.count > 0) [relpath appendstring:@"/"]; else [relpath appendstring:@"./"]; [relpath appendstring:[pieces componentsjoinedbystring:@"/"]]; nslog(@"%@",relpath); return relpath; }
Comments
Post a Comment