diff --git a/ChangeLog b/ChangeLog index b7d44971e..c07d98d67 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,19 @@ +Thu Feb 12 00:57:00 1999 Manuel Guesdon + + * Source/include/NSString.h: declaration of categories: + NSString (GSString) + NSString(GSTrimming) + NSMutableString (GSString) + NSMutableString (GSTrimming) + * Source/NSString.m: implementation of these categories + * Source/include/NSObject.h: added ASSIGNCOPY + Same as ASSIGN but copy the value + * Source/include/NSURL.h: New class + * Source/include/NSURLHandle.h: New class + * Source/NSURL.m: New class [see the note in the file] + * Source/NSURLHandle.m: New class [see the note in the file] + * GNUMakefile: added NSURL.m and NSURLHandle.m + Fri Feb 12 16:20:00 1999 Richard Frith-Macdonald * Source/NSGeometry.m: Removed most functions - into header diff --git a/Headers/gnustep/base/NSObject.h b/Headers/gnustep/base/NSObject.h index f56ceec14..34c49b3d2 100644 --- a/Headers/gnustep/base/NSObject.h +++ b/Headers/gnustep/base/NSObject.h @@ -259,6 +259,26 @@ if (__value != object) \ } \ }) +/* + * ASSIGNCOPY(object,value) assignes a copy of the value to the object with + * and release operations. + */ +#define ASSIGNCOPY(object,value) ({\ +typeof (value) __value = (value); \ +if (__value != object) \ + { \ + if (__value) \ + { \ + [__value copy]; \ + } \ + if (object) \ + { \ + [object release]; \ + } \ + object = __value; \ + } \ +}) + /* * DESTROY() is a release operation which also sets the object to be * a nil pointer for tidyness - we can't accidentally use a DESTROYED diff --git a/Headers/gnustep/base/NSString.h b/Headers/gnustep/base/NSString.h index 02af45b7e..30f1453fb 100644 --- a/Headers/gnustep/base/NSString.h +++ b/Headers/gnustep/base/NSString.h @@ -319,4 +319,36 @@ compiler warning. @interface NXConstantString : NSGCString @end +#ifndef NO_GNUSTEP +@interface NSString (GSString) +-(NSString*)stringWithoutSuffix:(NSString*)_suffix; +-(NSString*)stringWithoutPrefix:(NSString*)_prefix; +-(NSString*)stringByReplacingString:(NSString*)_replace + withString:(NSString*)_by; +@end + +@interface NSString(GSTrimming) +-(NSString*)stringByTrimmingLeadWhiteSpaces; +-(NSString*)stringByTrimmingTailWhiteSpaces; +-(NSString*)stringByTrimmingWhiteSpaces; + +-(NSString*)stringByTrimmingLeadSpaces; +-(NSString*)stringByTrimmingTailSpaces; +-(NSString*)stringByTrimmingSpaces; +@end + +@interface NSMutableString (GSString) +-(void)removeSuffix:(NSString*)_suffix; +-(void)removePrefix:(NSString*)_prefix; +-(void)replaceString:(NSString*)_replace + withString:(NSString*)_by; +@end + +@interface NSMutableString (GSTrimming) +-(void)trimLeadSpaces; +-(void)trimTailSpaces; +-(void)trimSpaces; +@end +#endif /* NO_GNUSTEP */ + #endif /* __NSString_h_GNUSTEP_BASE_INCLUDE */ diff --git a/Headers/gnustep/base/NSURL.h b/Headers/gnustep/base/NSURL.h new file mode 100644 index 000000000..f9473d4a5 --- /dev/null +++ b/Headers/gnustep/base/NSURL.h @@ -0,0 +1,125 @@ +/* NSURL.h - Class NSURL + Copyright (C) 1999 Free Software Foundation, Inc. + + Written by: Manuel Guesdon + Date: Jan 1999 + + This file is part of the GNUstep Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef _NSURL_h__ + #define _NSURL_h__ + +@class NSURLHandle; +@class NSURL; + +extern NSString* NSURLFileScheme; //file + +//=================================================================================== +@interface NSURL: NSObject +{ + NSString *urlString; + NSURL *baseURL; + void *clients; +}; + ++(id)fileURLWithPath:(NSString*)path; ++(id)URLWithString:(NSString*)URLString; ++(id)URLWithString:(NSString*)URLString + relativeToURL:(NSURL*)baseURL; + +-(id)initWithScheme:(NSString*)_scheme + host:(NSString*)_host + path:(NSString*)_path; + +//Non Standard Function +-(id)initWithScheme:(NSString*)_scheme + host:(NSString*)_host + port:(NSNumber*)_port + path:(NSString*)_path; + +//Do a initWithScheme:NSFileScheme host:nil path:_path +-(id)initFileURLWithPath:(NSString*)_path; + +// urlString is escaped +-(id)initWithString:(NSString*)URLString; + +//URLString!=nil ! +// urlString is escaped +-(id)initWithString:(NSString*)URLString + relativeToURL:(NSURL*)baseURL; + +-(NSString*)description; +-(NSString*)absoluteString; +-(NSString*)relativeString; + +-(NSURL*)baseURL; +-(NSURL*)absoluteURL; + +-(NSString*)scheme; +-(NSString*)resourceSpecifier; + +-(NSString*)host; +-(NSNumber*)port; +-(NSString*)user; +-(NSString*)password; +-(NSString*)path; +-(NSString*)fragment; +-(NSString*)parameterString; +-(NSString*)query; +-(NSString*)relativePath; + +-(BOOL)isFileURL; + +-(NSURL*)standardizedURL; + +//FIXME: delete these fn when NSURL will be validated ++(void)test; ++(void)testPrint:(NSURL*)_url; + +@end + +//=================================================================================== +@interface NSURL (NSURLLoading) +-(NSData*)resourceDataUsingCache:(BOOL)shouldUseCache; + +-(void)loadResourceDataNotifyingClient:(id)client + usingCache:(BOOL)shouldUseCache; + +-(NSURLHandle*)URLHandleUsingCache:(BOOL)shouldUseCache; + +-(BOOL)setResourceData:(NSData*)data; + +-(id)propertyForKey:(NSString*)propertyKey; +-(BOOL)setProperty:(id)property + forKey:(NSString*)propertyKey; + +@end + +//=================================================================================== +@interface NSObject (NSURLClient) +-(void) URL:(NSURL*)sender + resourceDataDidBecomeAvailable:(NSData*)newBytes; + +-(void)URLResourceDidFinishLoading:(NSURL*)sender; +-(void)URLResourceDidCancelLoading:(NSURL*)sender; + +-(void) URL:(NSURL*)sender + resourceDidFailLoadingWithReason:(NSString*)reason; +@end + +#endif //_NSUrl_h__ diff --git a/Headers/gnustep/base/NSURLHandle.h b/Headers/gnustep/base/NSURLHandle.h new file mode 100644 index 000000000..1afaab565 --- /dev/null +++ b/Headers/gnustep/base/NSURLHandle.h @@ -0,0 +1,105 @@ +/* NSURLHandle.h - Class NSURLHandle + Copyright (C) 1999 Free Software Foundation, Inc. + + Written by: Manuel Guesdon + Date: Jan 1999 + + This file is part of the GNUstep Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef _NSURLHandle_h__ + #define _NSURLHandle_h__ + +@class NSURLHandle; +@class NSURL; + +extern NSString *NSHTTPPropertyStatusCodeKey; +extern NSString *NSHTTPPropertyStatusReasonKey; +extern NSString *NSHTTPPropertyServerHTTPVersionKey; +extern NSString *NSHTTPPropertyRedirectionHeadersKey; +extern NSString *NSHTTPPropertyErrorPageDataKey; + +typedef enum +{ + NSURLHandleNotLoaded = 0, + NSURLHandleLoadSucceeded, + NSURLHandleLoadInProgress, + NSURLHandleLoadFailed +} NSURLHandleStatus; + +//=================================================================================== +@protocol NSURLHandleClient +-(void) URLHandle:(NSURLHandle*)sender + resourceDataDidBecomeAvailable:(NSData*)newData; + +-(void)URLHandleResourceDidBeginLoading:(NSURLHandle*)sender; +-(void)URLHandleResourceDidFinishLoading:(NSURLHandle*)sender; +-(void)URLHandleResourceDidCancelLoading:(NSURLHandle*)sender; + +-(void) URLHandle:(NSURLHandle*)sender + resourceDidFailLoadingWithReason:(NSString*)reason; +@end + +//=================================================================================== +@interface NSURLHandle:NSObject +{ + NSMutableArray *clients; + id data; + NSURLHandleStatus status; +}; + ++(void)registerURLHandleClass:(Class)_urlHandleSubclass; ++(Class)URLHandleClassForURL:(NSURL*)_url; + +-(id)initWithURL:(NSURL*)_url + cached:(BOOL)_cached; + +-(NSURLHandleStatus)status; +-(NSString*)failureReason; + +-(void)addClient:(id )_client; +-(void)removeClient:(id )_client; + +-(void)loadInBackground; +-(void)cancelLoadInBackground; + +-(NSData*)resourceData; +-(NSData*)availableResourceData; + +-(void)flushCachedData; + +-(void)backgroundLoadDidFailWithReason:(NSString*)reason; +-(void)didLoadBytes:(NSData*)newData + loadComplete:(BOOL)_loadComplete; + + ++(BOOL)canInitWithURL:(NSURL*)_url; ++(NSURLHandle*)cachedHandleForURL:(NSURL*)_url; + +-(id)propertyForKey:(NSString*)propertyKey; +-(id)propertyForKeyIfAvailable:(NSString*)propertyKey; +-(BOOL)writeProperty:(id)propertyValue + forKey:(NSString*)propertyKey; +-(BOOL)writeData:(NSData*)data; + +-(NSData*)loadInForeground; +-(void)beginLoadInBackground; +-(void)endLoadInBackground; + +@end + +#endif diff --git a/Source/GNUmakefile b/Source/GNUmakefile index ef1d41508..41684c3ed 100644 --- a/Source/GNUmakefile +++ b/Source/GNUmakefile @@ -63,7 +63,8 @@ FILE_AUTHORS = \ "Yoo C. Chung" \ "Richard Frith-Macdonald" \ "Stevo Crvenkovski" \ -"Mike Kienenberger" +"Mike Kienenberger" \ +"Manuel Guesdon" # The GNU source files @@ -330,7 +331,9 @@ NSUser.m \ NSUserDefaults.m \ NSValue.m \ NSZone.m \ -externs.m +externs.m \ +NSURL.m \ +NSURLHandle.m NSVALUE_MFILES = \ NSValue0.m NSValue1.m NSValue2.m NSValue3.m \ @@ -427,7 +430,9 @@ Foundation/NSUserDefaults.h \ Foundation/NSUtilities.h \ Foundation/NSValue.h \ Foundation/NSZone.h \ -Foundation/objc-load.h +Foundation/objc-load.h \ +Foundation/NSURL.h \ +Foundation/NSURLHandle.h UNICODE_HEADERS = \ unicode/caseconv.h \ diff --git a/Source/NSString.m b/Source/NSString.m index cfa588ffa..3d217fc18 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -3499,3 +3499,183 @@ static id parseSfItem(pldata* pld) @end +#ifndef NO_GNUSTEP +//============================================================================== +@implementation NSString (GSTrimming) + +//------------------------------------------------------------------------------ +-(NSString*)stringByTrimmingLeadWhiteSpaces +{ + NSCharacterSet *nonSPSet= [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet]; + NSRange nonSPCharRange = [self rangeOfCharacterFromSet:nonSPSet]; + + if (nonSPCharRange.length>0) + return [self substringFromIndex:nonSPCharRange.location]; + else + return [NSString string]; +}; + +//------------------------------------------------------------------------------ +-(NSString*)stringByTrimmingTailWhiteSpaces +{ + NSCharacterSet *nonSPSet= [[NSCharacterSet whitespaceAndNewlineCharacterSet] invertedSet]; + NSRange nonSPCharRange = [self rangeOfCharacterFromSet:nonSPSet + options:NSBackwardsSearch]; + if (nonSPCharRange.length>0) + return [self substringToIndex:nonSPCharRange.location+1]; + else + return [NSString string]; +}; + +//------------------------------------------------------------------------------ +-(NSString*)stringByTrimmingWhiteSpaces +{ + return [[self stringByTrimmingLeadWhiteSpaces] + stringByTrimmingTailWhiteSpaces]; +};; + +//------------------------------------------------------------------------------ +-(NSString*)stringByTrimmingLeadSpaces +{ + NSMutableString* tmp = [[self mutableCopy] autorelease]; + [tmp trimLeadSpaces]; + // Convert to immuable + return [[tmp copy] autorelease]; +}; + +//------------------------------------------------------------------------------ +-(NSString*)stringByTrimmingTailSpaces +{ + NSMutableString* tmp= [[self mutableCopy] autorelease]; + [tmp trimTailSpaces]; + // Convert to immuable + return [[tmp copy] autorelease]; +}; + +//------------------------------------------------------------------------------ +-(NSString*) stringByTrimmingSpaces +{ + NSMutableString* tmp=[[self mutableCopy] autorelease]; + [tmp trimLeadSpaces]; + [tmp trimTailSpaces]; + // Convert to immuable + return [[tmp copy] autorelease]; +}; + +@end + + +//============================================================================== +@implementation NSMutableString (GSTrimming) + +//------------------------------------------------------------------------------ +-(void)trimLeadSpaces +{ + int location = 0; + int length = [self length]; + while (location0) + [self deleteCharactersInRange:NSMakeRange(0,location)]; +}; + +//------------------------------------------------------------------------------ +-(void)trimTailSpaces +{ + int length = [self length]; + int location = length-1; + + while (location>=0 && isspace([self characterAtIndex:location])) + location--; + + if (location < length-1) + [self deleteCharactersInRange:NSMakeRange((location == 0) ? 0 : location + 1, + length - ((location == 0) ? 0 : location + 1))]; +} + +//------------------------------------------------------------------------------ +-(void)trimSpaces +{ + [self trimLeadSpaces]; + [self trimTailSpaces]; +}; + +@end + +//============================================================================== +@implementation NSString (GSString) + +//------------------------------------------------------------------------------ +-(NSString*)stringWithoutSuffix:(NSString*)_suffix +{ + NSCAssert2([self hasSuffix:_suffix],@"'%@' has not the suffix '%@'",self,_suffix); + return [self substringToIndex:([self length]-[_suffix length])]; +}; + +//------------------------------------------------------------------------------ +-(NSString*)stringWithoutPrefix:(NSString*)_prefix +{ + NSCAssert2([self hasPrefix:_prefix],@"'%@' has not the prefix '%@'",self,_prefix); + return [self substringFromIndex:[_prefix length]]; +}; + +//------------------------------------------------------------------------------ +-(NSString*)stringByReplacingString:(NSString*)_replace + withString:(NSString*)_by +{ + NSRange range=[self rangeOfString:_replace]; + if (range.length>0) + { + NSMutableString* tmp= [[self mutableCopy] autorelease]; + [tmp replaceString:_replace + withString:_by]; + // Convert to immuable + return [[tmp copy] autorelease]; + } + else + return self; +}; + +@end + +//============================================================================== +@implementation NSMutableString (GSString) +//------------------------------------------------------------------------------ +-(void)removeSuffix:(NSString*)_suffix +{ + NSCAssert2([self hasSuffix:_suffix],@"'%@' has not the suffix '%@'",self,_suffix); + [self deleteCharactersInRange:NSMakeRange([self length]-[_suffix length],[_suffix length])]; +}; + +//------------------------------------------------------------------------------ +-(void)removePrefix:(NSString*)_prefix; +{ + NSCAssert2([self hasPrefix:_prefix],@"'%@' has not the prefix '%@'",self,_prefix); + [self deleteCharactersInRange:NSMakeRange(0,[_prefix length])]; +}; + +//------------------------------------------------------------------------------ +-(void)replaceString:(NSString*)_replace + withString:(NSString*)_by +{ + NSRange range=[self rangeOfString:_replace]; + if (range.length>0) + { + int byLen=[_by length]; + do + { + [self replaceCharactersInRange:range + withString:_by]; + range.location+=byLen; + range.length=[self length]-range.location; + range=[self rangeOfString:_replace + options:0 + range:range]; + } + while (range.length>0); + }; +}; +@end + +#endif /* NO_GNUSTEP */ diff --git a/Source/NSURL.m b/Source/NSURL.m new file mode 100644 index 000000000..918e44254 --- /dev/null +++ b/Source/NSURL.m @@ -0,0 +1,612 @@ +/* NSUrl.m - Class NSURL + Copyright (C) 1999 Free Software Foundation, Inc. + + Written by: Manuel Guesdon + Date: Jan 1999 + + This file is part of the GNUstep Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* +Note from Manuel Guesdon: +* I've made some test to compare apple NSURL results +and GNUstep NSURL results but as there this class is not very documented, some +function may be incorrect +* I've put 2 functions to make tests. You can add your own tests +* Some functions are not implemented +*/ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +NSString* NSURLFileScheme=@"file"; + +NSString* NSURLPartKey_host=@"host"; +NSString* NSURLPartKey_port=@"port"; +NSString* NSURLPartKey_user=@"user"; +NSString* NSURLPartKey_password=@"password"; +NSString* NSURLPartKey_path=@"path"; +NSString* NSURLPartKey_fragment=@"fragment"; +NSString* NSURLPartKey_parameterString=@"parameterString"; +NSString* NSURLPartKey_query=@"query"; + +//=================================================================================== +@implementation NSURL + +//----------------------------------------------------------------------------------- ++(id)fileURLWithPath:(NSString*)_path +{ + return [[[NSURL alloc] initFileURLWithPath:_path] + autorelease]; +}; + +//----------------------------------------------------------------------------------- ++(id)URLWithString:(NSString*)_urlString +{ + return [[[NSURL alloc] initWithString:_urlString] + autorelease]; +}; + +//----------------------------------------------------------------------------------- ++(id)URLWithString:(NSString*)_urlString + relativeToURL:(NSURL*)_baseURL +{ + return [[[NSURL alloc] initWithString:_urlString + relativeToURL:_baseURL] + autorelease]; +}; + +//----------------------------------------------------------------------------------- +-(id)initWithScheme:(NSString*)_scheme + host:(NSString*)_host + path:(NSString*)_path +{ + NSString* _urlString=nil; + if (_host) + _urlString=[NSString stringWithFormat:@"%@://%@",_scheme,_host]; + else + _urlString=[NSString stringWithFormat:@"%@:",_scheme]; + if (_path) + _urlString=[_urlString stringByAppendingString:_path]; + self=[self initWithString:_urlString]; + return self; +}; + +//----------------------------------------------------------------------------------- +//Non Standard Function +-(id)initWithScheme:(NSString*)_scheme + host:(NSString*)_host + port:(NSNumber*)_port + path:(NSString*)_path +{ + NSString* tmpHost=nil; + if (_port) + tmpHost=[NSString stringWithFormat:@"%@:%@",_host,_port]; + else + tmpHost=_host; + self=[self initWithScheme:_scheme + host:tmpHost + path:_path]; + return self; +}; + +//----------------------------------------------------------------------------------- +//Do a initWithScheme:NSFileScheme host:nil path:_path +-(id)initFileURLWithPath:(NSString*)_path +{ + self=[self initWithScheme:NSURLFileScheme + host:nil + path:_path]; + return self; +}; + +//----------------------------------------------------------------------------------- +// urlString is escaped +-(id)initWithString:(NSString*)_urlString +{ + self=[self init]; + ASSIGNCOPY(urlString,_urlString); + return self; +}; + +//----------------------------------------------------------------------------------- +//urlString!=nil +// urlString is escaped +-(id)initWithString:(NSString*)_urlString + relativeToURL:(NSURL*)_baseURL +{ + self=[self init]; + ASSIGNCOPY(urlString,_urlString); + ASSIGNCOPY(baseURL,_baseURL); + return self; +}; + +//----------------------------------------------------------------------------------- +-(void)dealloc +{ + DESTROY(urlString); + DESTROY(baseURL); + [super dealloc]; +} + +//----------------------------------------------------------------------------------- +-(id)copyWithZone: (NSZone*)zone +{ + if (NSShouldRetainWithZone(self, zone) == NO) + return [[isa allocWithZone: zone] initWithString:urlString + relativeToURL:baseURL]; + else + return [self retain]; +}; + +//----------------------------------------------------------------------------------- +-(void)encodeWithCoder:(NSCoder*)aCoder +{ + [super encodeWithCoder: aCoder]; + [aCoder encodeObject:urlString]; + [aCoder encodeObject:baseURL]; + //FIXME? clients ? +}; + +//----------------------------------------------------------------------------------- +-(id)initWithCoder: (NSCoder*)aCoder +{ + self = [super initWithCoder: aCoder]; + [aCoder decodeValueOfObjCType: @encode(id) at:&urlString]; + [aCoder decodeValueOfObjCType: @encode(id) at:&baseURL]; + //FIXME? clients ? + return self; +}; + + +//----------------------------------------------------------------------------------- +-(NSString*)description +{ + NSString* dscr=urlString; + if (baseURL) + dscr=[dscr stringByAppendingFormat:@" -- %@",baseURL]; + return dscr; +}; + +//----------------------------------------------------------------------------------- +// Non Standard Function +-(NSString*)baseURLAbsolutePart +{ + if (baseURL) + { + NSString* suffix=[baseURL path]; + NSString* tmp=nil; + if ([baseURL query]) + suffix=[suffix stringByAppendingFormat:@"?%@",[baseURL query]]; + // /test?aa=bb&cc=dd -- http://user:passwd@www.gnustep.org:80/apache + // ==> http://user:passwd@www.gnustep.org:80/ + tmp=[[baseURL absoluteString]stringWithoutSuffix:suffix]; + + // ==> http://user:passwd@www.gnustep.org:80 + if ([tmp hasSuffix:@"/"]) + tmp=[tmp stringWithoutSuffix:@"/"]; + return tmp; + } + else + return @""; +}; + + +//----------------------------------------------------------------------------------- +-(NSString*)absoluteString +{ + NSString* absString=nil; + if (baseURL) + { + // /test?aa=bb&cc=dd -- http://user:passwd@www.gnustep.org:80/apache + // ==> http://user:passwd@www.gnustep.org:80 + absString=[self baseURLAbsolutePart]; + + if ([urlString hasPrefix:@"/"]) + absString=[absString stringByAppendingString:urlString]; + else + absString=[absString stringByAppendingFormat:@"/%@",urlString]; + } + else + absString=urlString; + return absString; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)relativeString +{ + return urlString; +}; + +//----------------------------------------------------------------------------------- +-(NSURL*)baseURL +{ + return baseURL; +}; + +//----------------------------------------------------------------------------------- +-(NSURL*)absoluteURL +{ + if (!baseURL) + return self; + else + return [NSURL URLWithString:[self absoluteString]]; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)scheme +{ + NSString* scheme=nil; + NSString* absoluteString=[self absoluteString]; + NSRange range=[absoluteString rangeOfString:@"://"]; + if (range.length>0) + scheme=[absoluteString substringToIndex:range.location]; + return scheme; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)resourceSpecifier +{ + NSString* absoluteString=[self absoluteString]; + NSRange range=[absoluteString rangeOfString:@"://"]; + if (range.length>0) + return [absoluteString substringFromIndex:range.location+1]; + else + return absoluteString; +}; + +//----------------------------------------------------------------------------------- +//Non Standard Function +-(NSDictionary*)explode +{ + NSMutableDictionary* elements=nil; + NSString* resourceSpecifier=[self resourceSpecifier]; + if ([resourceSpecifier hasPrefix:@"//"]) + { + int index=2; + NSRange range; + elements=[[NSMutableDictionary new] autorelease]; + if (![[self scheme] isEqualToString:NSURLFileScheme]) + { + range=[resourceSpecifier rangeOfString:@"/" + options:0 + range:NSMakeRange(index,[resourceSpecifier length]-index)]; + if (range.length>0) + { + NSString* userPasswordHostPort=[resourceSpecifier substringWithRange:NSMakeRange(index,range.location-index)]; + NSString* userPassword=nil; + NSString* hostPort=nil; + index=range.location; + range=[userPasswordHostPort rangeOfString:@"@"]; + if (range.length>0) + { + if (range.location>0) + userPassword=[userPasswordHostPort substringToIndex:range.location]; + if (range.location+1<[userPasswordHostPort length]) + hostPort=[userPasswordHostPort substringFromIndex:range.location+1]; + } + else + hostPort=userPasswordHostPort; + if (userPassword) + { + range=[userPassword rangeOfString:@":"]; + if (range.length>0) + { + if (range.location>0) + [elements setObject:[userPassword substringToIndex:range.location] + forKey:NSURLPartKey_user]; + if (range.location+1<[userPassword length]) + [elements setObject:[userPassword substringFromIndex:range.location+1] + forKey:NSURLPartKey_password]; + } + else + [elements setObject:userPassword + forKey:NSURLPartKey_user]; + }; + + if (hostPort) + { + range=[hostPort rangeOfString:@":"]; + if (range.length>0) + { + if (range.location>0) + [elements setObject:[hostPort substringToIndex:range.location] + forKey:NSURLPartKey_host]; + if (range.location+1<[hostPort length]) + [elements setObject:[NSNumber valueFromString: + [hostPort substringFromIndex:range.location+1]] + forKey:NSURLPartKey_port]; + } + else + [elements setObject:hostPort + forKey:NSURLPartKey_host]; + }; + }; + } + else + index--; //To Take a / + range=[resourceSpecifier rangeOfString:@"?" + options:0 + range:NSMakeRange(index,[resourceSpecifier length]-index)]; + if (range.length>0) + { + if (range.location>0) + [elements setObject:[resourceSpecifier substringWithRange:NSMakeRange(index,range.location-index)] + forKey:NSURLPartKey_path]; + if (range.location+1<[resourceSpecifier length]) + [elements setObject:[resourceSpecifier substringFromIndex:range.location+1] + forKey:NSURLPartKey_query]; + } + else + [elements setObject:[resourceSpecifier substringFromIndex:index] + forKey:NSURLPartKey_path]; + } + else + { + [NSException raise:NSGenericException + format:@"'%@' is a bad URL",self]; + }; + return elements; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)host +{ + return [[self explode] objectForKey:NSURLPartKey_host]; +}; + +//----------------------------------------------------------------------------------- +-(NSNumber*)port; +{ + return [[self explode] objectForKey:NSURLPartKey_port]; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)user; +{ + return [[self explode] objectForKey:NSURLPartKey_user]; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)password; +{ + return [[self explode] objectForKey:NSURLPartKey_password]; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)path; +{ + return [[self explode] objectForKey:NSURLPartKey_path]; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)fragment; +{ + return [[self explode] objectForKey:NSURLPartKey_fragment]; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)parameterString; +{ + return [[self explode] objectForKey:NSURLPartKey_parameterString]; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)query; +{ + return [[self explode] objectForKey:NSURLPartKey_query]; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)relativePath +{ + //FIXME? + return [self path]; +}; + +//----------------------------------------------------------------------------------- +-(BOOL)isFileURL +{ + return [[self scheme] isEqualToString:NSURLFileScheme]; +}; + +//----------------------------------------------------------------------------------- +-(NSURL*)standardizedURL +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void) URLHandle:(NSURLHandle*)sender + resourceDataDidBecomeAvailable:(NSData*)newData +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)URLHandleResourceDidBeginLoading:(NSURLHandle*)sender +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)URLHandleResourceDidFinishLoading:(NSURLHandle*)sender +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)URLHandleResourceDidCancelLoading:(NSURLHandle*)sender +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void) URLHandle:(NSURLHandle*)sender + resourceDidFailLoadingWithReason:(NSString*)reason +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +//FIXME: delete these fn when NSURL will be validated ++(void)test +{ + NSURL* url2; + NSURL* url3; + NSURL* url=[NSURL URLWithString:@"http://user:passwd@www.gnustep.org:80/apache"]; + url2=[NSURL URLWithString:@"/test?aa=bb&cc=dd" relativeToURL:url]; + url3=[NSURL URLWithString:@"test?aa=bb&cc=dd" relativeToURL:url]; + NSLog(@"===url==="); + [NSURL testPrint:url]; + NSLog(@"===url2==="); + [NSURL testPrint:url2]; + NSLog(@"===url3==="); + [NSURL testPrint:url3]; +}; + ++(void)testPrint:(NSURL*)url +{ + id _baseURL=nil; + id _urlString=nil; + void* _clients=0; + GSGetInstanceVariable(url,@"baseURL",&_baseURL); + GSGetInstanceVariable(url,@"urlString",&_urlString); + GSGetInstanceVariable(url,@"clients",&_clients); + NSLog(@"*BaseURL: %ld",(long)_baseURL); + NSLog(@"*BaseURL: %@",[_baseURL description]); + NSLog(@"*urlString: %@",_urlString); + NSLog(@"*clients: %ld",(long)_clients); + NSLog(@"*host: %@",[url host]); + NSLog(@"*port: %@",[url port]); + NSLog(@"*user: %@",[url user]); + NSLog(@"*password: %@",[url password]); + NSLog(@"*path: %@",[url path]); + NSLog(@"*fragment: %@",[url fragment]); + NSLog(@"*parameterString: %@",[url parameterString]); + NSLog(@"*query: %@",[url query]); + NSLog(@"*relativePath: %@",[url relativePath]); + NSLog(@"*absoluteString: %@",[url absoluteString]); + NSLog(@"*relativeString: %@",[url relativeString]); + NSLog(@"*baseURL: %@",[[url baseURL] description]); + NSLog(@"*absoluteURL: %@",[[url absoluteURL] description]); + NSLog(@"*scheme: %@",[url scheme]); + NSLog(@"*resourceSpecifier: %@",[url resourceSpecifier]); + NSLog(@"*description: %@",[url description]); +}; +@end + +//=================================================================================== +@implementation NSURL (NSURLLoading) + +//----------------------------------------------------------------------------------- +-(NSData*)resourceDataUsingCache:(BOOL)shouldUseCache +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)loadResourceDataNotifyingClient:(id)client + usingCache:(BOOL)shouldUseCache +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(NSURLHandle*)URLHandleUsingCache:(BOOL)shouldUseCache +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(BOOL)setResourceData:(NSData*)data +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(id)propertyForKey:(NSString*)propertyKey +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(BOOL)setProperty:(id)property + forKey:(NSString*)propertyKey; +{ + //FIXME + [self notImplemented: _cmd]; +}; + +@end + +//=================================================================================== +@implementation NSObject (NSURLClient) + +//----------------------------------------------------------------------------------- +-(void) URL:(NSURL*)sender + resourceDataDidBecomeAvailable:(NSData*)newBytes +{ + //FIXME + [self notImplemented: _cmd]; + +}; + +//----------------------------------------------------------------------------------- +-(void)URLResourceDidFinishLoading:(NSURL*)sender +{ + //FIXME + [self notImplemented: _cmd]; + +}; + +//----------------------------------------------------------------------------------- +-(void)URLResourceDidCancelLoading:(NSURL*)sender +{ + //FIXME + [self notImplemented: _cmd]; + +}; + +//----------------------------------------------------------------------------------- +-(void) URL:(NSURL*)sender + resourceDidFailLoadingWithReason:(NSString*)reason +{ + //FIXME + [self notImplemented: _cmd]; +}; + +@end diff --git a/Source/NSURLHandle.m b/Source/NSURLHandle.m new file mode 100644 index 000000000..bf206ecd8 --- /dev/null +++ b/Source/NSURLHandle.m @@ -0,0 +1,206 @@ +/* NSURLHandle.h - Class NSURLHandle + Copyright (C) 1999 Free Software Foundation, Inc. + + Written by: Manuel Guesdon + Date: Jan 1999 + + This file is part of the GNUstep Library. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free + Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +/* +Note from Manuel Guesdon: +* functions are not implemented. If someone has documentation or ideas on +how it should work... +*/ + +#include +#include +#include +#include +#include +#include +#include +#include + +//=================================================================================== +@implementation NSURLHandle + +//----------------------------------------------------------------------------------- ++(void)registerURLHandleClass:(Class)_urlHandleSubclass +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- ++(Class)URLHandleClassForURL:(NSURL*)_url +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(id)initWithURL:(NSURL*)_url + cached:(BOOL)_cached +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(NSURLHandleStatus)status +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(NSString*)failureReason +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)addClient:(id )_client +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)removeClient:(id )_client +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)loadInBackground +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)cancelLoadInBackground +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(NSData*)resourceData +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(NSData*)availableResourceData +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)flushCachedData +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)backgroundLoadDidFailWithReason:(NSString*)reason +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)didLoadBytes:(NSData*)newData + loadComplete:(BOOL)_loadComplete +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- ++(BOOL)canInitWithURL:(NSURL*)_url +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- ++(NSURLHandle*)cachedHandleForURL:(NSURL*)_url +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(id)propertyForKey:(NSString*)propertyKey +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(id)propertyForKeyIfAvailable:(NSString*)propertyKey +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(BOOL)writeProperty:(id)propertyValue + forKey:(NSString*)propertyKey +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(BOOL)writeData:(NSData*)data +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(NSData*)loadInForeground +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)beginLoadInBackground +{ + //FIXME + [self notImplemented: _cmd]; +}; + +//----------------------------------------------------------------------------------- +-(void)endLoadInBackground +{ + //FIXME + [self notImplemented: _cmd]; +}; + +@end