From 2e350eca5261869851e1602497dca5901e5998b1 Mon Sep 17 00:00:00 2001 From: Hirami Date: Tue, 10 Apr 2012 15:11:33 +0900 Subject: [PATCH] Support UTF-8 text with BOM and SJIS. --- iOS/Tombo/Tombo/DetailViewController.m | 8 +++--- iOS/Tombo/Tombo/EditViewController.m | 5 ++-- iOS/Tombo/Tombo/Storage.h | 5 ++++ iOS/Tombo/Tombo/Storage.m | 47 ++++++++++++++++++++++++++++++++-- 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/iOS/Tombo/Tombo/DetailViewController.m b/iOS/Tombo/Tombo/DetailViewController.m index 3b003c7..0ae33a7 100644 --- a/iOS/Tombo/Tombo/DetailViewController.m +++ b/iOS/Tombo/Tombo/DetailViewController.m @@ -1,6 +1,7 @@ #import "DetailViewController.h" #import "EditViewController.h" #import "MasterViewController.h" +#import "Storage.h" @interface DetailViewController () @@ -52,11 +53,8 @@ if (item.isNewItem) { self.text.text = @""; } else { - NSError *error; - NSString *note = [NSString stringWithContentsOfFile:item.path - encoding:NSUTF8StringEncoding - error:&error]; - if (error) { + NSString *note = [Storage load:item.path]; + if (note == nil) { self.text.text = @""; } else { self.text.text = note; diff --git a/iOS/Tombo/Tombo/EditViewController.m b/iOS/Tombo/Tombo/EditViewController.m index 2d8af11..b7a891c 100644 --- a/iOS/Tombo/Tombo/EditViewController.m +++ b/iOS/Tombo/Tombo/EditViewController.m @@ -1,5 +1,6 @@ #import "EditViewController.h" #import "MasterViewController.h" +#import "Storage.h" @interface EditViewController () { } @@ -36,9 +37,7 @@ NSString *noteData; if (self.detailItem && self.detailItem.path) { NSError *error; - noteData = [NSString stringWithContentsOfFile:self.detailItem.path - encoding:NSUTF8StringEncoding - error:&error]; + noteData = [Storage load:self.detailItem.path]; if (error) return; } else { noteData = @""; diff --git a/iOS/Tombo/Tombo/Storage.h b/iOS/Tombo/Tombo/Storage.h index 0060a2b..fa16b56 100644 --- a/iOS/Tombo/Tombo/Storage.h +++ b/iOS/Tombo/Tombo/Storage.h @@ -45,4 +45,9 @@ - (FileItem *)newFolder:(NSString *)folder; +/* + * Load note. + */ ++(NSString *)load:(NSString *)path; + @end diff --git a/iOS/Tombo/Tombo/Storage.m b/iOS/Tombo/Tombo/Storage.m index 34c2f43..7ff3c73 100644 --- a/iOS/Tombo/Tombo/Storage.m +++ b/iOS/Tombo/Tombo/Storage.m @@ -58,6 +58,17 @@ -(BOOL)isTopDir { return [currentDirectory isEqualToString:@"/"]; } +- (void)saveDataWithBOM:(NSString *)note file:(NSString *)path { + const char *noteBytes = [note cStringUsingEncoding:NSUTF8StringEncoding]; + NSMutableData *data = [[NSMutableData alloc] initWithLength:strlen(noteBytes) + 3]; + char *buf = (char *)[data mutableBytes]; + strcpy(buf + 3, noteBytes); + *(char*)(buf + 0) = 0xEF; + *(char*)(buf + 1) = 0xBB; + *(char*)(buf + 2) = 0xBF; + + [data writeToFile:path atomically:YES]; +} // save note -(FileItem *)save:(NSString *)note item:(FileItem *)item { @@ -91,8 +102,9 @@ } // Save note. - NSError *error = nil; - [note writeToFile:result.path atomically:YES encoding:NSUTF8StringEncoding error:&error]; +// NSError *error = nil; +// [note writeToFile:result.path atomically:YES encoding:NSUTF8StringEncoding error:&error]; + [self saveDataWithBOM:note file:result.path]; return result; } @@ -171,4 +183,35 @@ return item; } ++(NSString *)load:(NSString *)path { + NSData *data = [NSData dataWithContentsOfFile:path]; + const char *header = [data bytes]; + if ([data length] > 3 && + *header == 0xEF && *(header + 1) == 0xBB && *(header + 2) == 0xBF) { + // BOM exists. UTF-8. + NSString *note = [[NSString alloc] initWithBytes:[data bytes] + 3 + length:[data length] - 3 + encoding:NSUTF8StringEncoding]; + return note; + } + + NSString *note; + + if ([[[NSLocale currentLocale] localeIdentifier] isEqualToString:@"ja_JP"]) { + note = [[NSString alloc] initWithBytes:[data bytes] + length:[data length] + encoding:NSShiftJISStringEncoding]; + if (note) return note; + } + + // UTF-8 + note = [[NSString alloc] initWithBytes:[data bytes] + length:[data length] + encoding:NSUTF8StringEncoding]; + if (note) return note; + + // encode UTF-8 fail. + return @""; +} + @end -- 2.11.0