OSDN Git Service

Support UTF-8 text with BOM and SJIS.
authorHirami <tomohisa.hirami@nifty.com>
Tue, 10 Apr 2012 06:11:33 +0000 (15:11 +0900)
committerHirami <tomohisa.hirami@nifty.com>
Tue, 10 Apr 2012 06:11:33 +0000 (15:11 +0900)
iOS/Tombo/Tombo/DetailViewController.m
iOS/Tombo/Tombo/EditViewController.m
iOS/Tombo/Tombo/Storage.h
iOS/Tombo/Tombo/Storage.m

index 3b003c7..0ae33a7 100644 (file)
@@ -1,6 +1,7 @@
 #import "DetailViewController.h"
 #import "EditViewController.h"
 #import "MasterViewController.h"
+#import "Storage.h"
 
 @interface DetailViewController ()
 
     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;
index 2d8af11..b7a891c 100644 (file)
@@ -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 = @"";
index 0060a2b..fa16b56 100644 (file)
@@ -45,4 +45,9 @@
 
 - (FileItem *)newFolder:(NSString *)folder;
 
+/*
+ * Load note.
+ */
++(NSString *)load:(NSString *)path;
+
 @end
index 34c2f43..7ff3c73 100644 (file)
 -(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 {
     }
 
     // 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;
 }
     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