ObjFW  Diff

Differences From Artifact [ec60fe00a5]:

To Artifact [e4ea0d48b5]:


28
29
30
31
32
33
34

35
36
37
38
39
40
41
# include <sys/mman.h>
#else
# define madvise(addr, len, advise)
#endif

#import "OFString.h"
#import "OFArray.h"

#import "OFFile.h"
#import "OFURL.h"
#import "OFHTTPRequest.h"
#import "OFAutoreleasePool.h"

#import "OFHTTPRequestFailedException.h"
#import "OFInitializationFailedException.h"







>







28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# include <sys/mman.h>
#else
# define madvise(addr, len, advise)
#endif

#import "OFString.h"
#import "OFArray.h"
#import "OFDictionary.h"
#import "OFFile.h"
#import "OFURL.h"
#import "OFHTTPRequest.h"
#import "OFAutoreleasePool.h"

#import "OFHTTPRequestFailedException.h"
#import "OFInitializationFailedException.h"
56
57
58
59
60
61
62
63

64
65
66
67
68
69
70
/* References for static linking */
void _references_to_categories_of_OFString(void)
{
	_OFString_Hashing_reference = 1;
	_OFString_URLEncoding_reference = 1;
	_OFString_XMLEscaping_reference = 1;
	_OFString_XMLUnescaping_reference = 1;
};


static inline int
memcasecmp(const char *first, const char *second, size_t len)
{
	size_t i;

	for (i = 0; i < len; i++) {







<
>







57
58
59
60
61
62
63

64
65
66
67
68
69
70
71
/* References for static linking */
void _references_to_categories_of_OFString(void)
{
	_OFString_Hashing_reference = 1;
	_OFString_URLEncoding_reference = 1;
	_OFString_XMLEscaping_reference = 1;
	_OFString_XMLUnescaping_reference = 1;

}

static inline int
memcasecmp(const char *first, const char *second, size_t len)
{
	size_t i;

	for (i = 0; i < len; i++) {
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710

711
712
713
714
715
716
717
718



719
720
721
722
723
724
725
726
727
728
729
730
731
732
733




























734
735
736


737
738
739
740
741
742
743

	return self;
}

- initWithContentsOfURL: (OFURL*)URL
{
	return [self initWithContentsOfURL: URL
				  encoding: OF_STRING_ENCODING_UTF_8];
}

- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding
{
	OFAutoreleasePool *pool;
	OFHTTPRequest *request;
	OFHTTPRequestResult *result;

	Class c;

	c = isa;
	[self release];

	pool = [[OFAutoreleasePool alloc] init];

	if ([[URL scheme] isEqual: @"file"]) {



		self = [[c alloc] initWithContentsOfFile: [URL path]
						encoding: encoding];
		[pool release];
		return self;
	}

	request = [OFHTTPRequest requestWithURL: URL];
	result = [request perform];

	if ([result statusCode] != 200)
		@throw [OFHTTPRequestFailedException
		    newWithClass: [request class]
		     HTTPRequest: request
		      statusCode: [result statusCode]];





























	self = [[c alloc] initWithCString: (char*)[[result data] cArray]
				 encoding: encoding
				   length: [[result data] count]];


	[pool release];
	return self;
}

- (const char*)cString
{
	return string;







|








>








>
>
>















>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
>
>







696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778

	return self;
}

- initWithContentsOfURL: (OFURL*)URL
{
	return [self initWithContentsOfURL: URL
				  encoding: OF_STRING_ENCODING_AUTODETECT];
}

- initWithContentsOfURL: (OFURL*)URL
	       encoding: (of_string_encoding_t)encoding
{
	OFAutoreleasePool *pool;
	OFHTTPRequest *request;
	OFHTTPRequestResult *result;
	OFMutableString *contentType;
	Class c;

	c = isa;
	[self release];

	pool = [[OFAutoreleasePool alloc] init];

	if ([[URL scheme] isEqual: @"file"]) {
		if (encoding == OF_STRING_ENCODING_AUTODETECT)
			encoding = OF_STRING_ENCODING_UTF_8;

		self = [[c alloc] initWithContentsOfFile: [URL path]
						encoding: encoding];
		[pool release];
		return self;
	}

	request = [OFHTTPRequest requestWithURL: URL];
	result = [request perform];

	if ([result statusCode] != 200)
		@throw [OFHTTPRequestFailedException
		    newWithClass: [request class]
		     HTTPRequest: request
		      statusCode: [result statusCode]];

	if (encoding == OF_STRING_ENCODING_AUTODETECT &&
	    (contentType = [[result headers] objectForKey: @"Content-Type"])) {
		contentType = [[contentType mutableCopy] autorelease];
		[contentType lower];

		if ([contentType hasSuffix: @"encoding=UTF-8"])
			encoding = OF_STRING_ENCODING_UTF_8;
		if ([contentType hasSuffix: @"encoding=iso-8859-1"])
			encoding = OF_STRING_ENCODING_ISO_8859_1;
		if ([contentType hasSuffix: @"encoding=iso-8859-15"])
			encoding = OF_STRING_ENCODING_ISO_8859_15;
		if ([contentType hasSuffix: @"encoding=windows-1252"])
			encoding = OF_STRING_ENCODING_WINDOWS_1252;
	}

	if (encoding == OF_STRING_ENCODING_AUTODETECT) {
		@try {
			self = [[c alloc]
			    initWithCString: (char*)[[result data] cArray]
				   encoding: OF_STRING_ENCODING_UTF_8
				     length: [[result data] count]];
		} @catch (OFInvalidEncodingException *e) {
			self = [[c alloc]
			    initWithCString: (char*)[[result data] cArray]
				   encoding: OF_STRING_ENCODING_ISO_8859_1
				     length: [[result data] count]];
		}
	} else {
		self = [[c alloc] initWithCString: (char*)[[result data] cArray]
					 encoding: encoding
					   length: [[result data] count]];
	}

	[pool release];
	return self;
}

- (const char*)cString
{
	return string;