Overview
Comment: | Try to autodetect the encoding in +[OFString stringWithContentsOfURL:]. |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
1dadc4b4a64c46a9bfce987195f0b429 |
User & Date: | js on 2011-04-25 22:31:01 |
Other Links: | manifest | tags |
Context
2011-04-25
| ||
22:39 | Fix missing release in OFXMLElement. check-in: 74d5765c1f user: js tags: trunk | |
22:31 | Try to autodetect the encoding in +[OFString stringWithContentsOfURL:]. check-in: 1dadc4b4a6 user: js tags: trunk | |
18:49 | Added tag 0.5.2-release for changeset 6702b5c91a13 check-in: b9b4a8e270 user: js tags: trunk | |
Changes
Modified src/OFString.h from [1b480c69df] to [fdc7cb8d4c].
︙ | ︙ | |||
25 26 27 28 29 30 31 | /** * \brief The encoding of a string. */ typedef enum of_string_encoding_t { OF_STRING_ENCODING_UTF_8, OF_STRING_ENCODING_ISO_8859_1, OF_STRING_ENCODING_ISO_8859_15, | | > | 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /** * \brief The encoding of a string. */ typedef enum of_string_encoding_t { OF_STRING_ENCODING_UTF_8, OF_STRING_ENCODING_ISO_8859_1, OF_STRING_ENCODING_ISO_8859_15, OF_STRING_ENCODING_WINDOWS_1252, OF_STRING_ENCODING_AUTODETECT = 0xFF } of_string_encoding_t; #ifdef __cplusplus extern "C" { #endif extern int of_string_check_utf8(const char*, size_t); extern size_t of_string_unicode_to_utf8(of_unichar_t, char*); |
︙ | ︙ | |||
145 146 147 148 149 150 151 | * \param encoding The encoding of the file * \return A new autoreleased OFString */ + stringWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; /** | | > | > > > > | 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | * \param encoding The encoding of the file * \return A new autoreleased OFString */ + stringWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; /** * Creates a new OFString with the contents of the specified URL. * * If the URL's scheme is file, it tries UTF-8 encoding. * * If the URL's scheme is http(s), it tries to detect the encoding from the HTTP * headers. If it could not detect the encoding using the HTTP headers, it tries * UTF-8 and falls back to ISO-8859-1 if that fails. * * \param URL The URL to the contents for the string * \return A new autoreleased OFString */ + stringWithContentsOfURL: (OFURL*)URL; /** |
︙ | ︙ | |||
277 278 279 280 281 282 283 | * \return An initialized OFString */ - initWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; /** * Initializes an already allocated OFString with the contents of the specified | > > | > > > > | 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | * \return An initialized OFString */ - initWithContentsOfFile: (OFString*)path encoding: (of_string_encoding_t)encoding; /** * Initializes an already allocated OFString with the contents of the specified * URL. * * If the URL's scheme is file, it tries UTF-8 encoding. * * If the URL's scheme is http(s), it tries to detect the encoding from the HTTP * headers. If it could not detect the encoding using the HTTP headers, it tries * UTF-8 and falls back to ISO-8859-1 if that fails. * * \param URL The URL to the contents for the string * \return An initialized OFString */ - initWithContentsOfURL: (OFURL*)URL; /** |
︙ | ︙ |
Modified src/OFString.m from [ec60fe00a5] to [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 | /* 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; | < > | 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 | return self; } - initWithContentsOfURL: (OFURL*)URL { return [self initWithContentsOfURL: URL | | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | | | > > | 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; |
︙ | ︙ |