• R/O
  • HTTP
  • SSH
  • HTTPS

Tombo: Commit

TOMBO source code


Commit MetaInfo

Revision2e350eca5261869851e1602497dca5901e5998b1 (tree)
Time2012-04-10 15:11:33
AuthorHirami <tomohisa.hirami@nift...>
CommiterHirami

Log Message

Support UTF-8 text with BOM and SJIS.

Change Summary

Incremental Difference

--- a/iOS/Tombo/Tombo/DetailViewController.m
+++ b/iOS/Tombo/Tombo/DetailViewController.m
@@ -1,6 +1,7 @@
11 #import "DetailViewController.h"
22 #import "EditViewController.h"
33 #import "MasterViewController.h"
4+#import "Storage.h"
45
56 @interface DetailViewController ()
67
@@ -52,11 +53,8 @@
5253 if (item.isNewItem) {
5354 self.text.text = @"";
5455 } else {
55- NSError *error;
56- NSString *note = [NSString stringWithContentsOfFile:item.path
57- encoding:NSUTF8StringEncoding
58- error:&error];
59- if (error) {
56+ NSString *note = [Storage load:item.path];
57+ if (note == nil) {
6058 self.text.text = @"";
6159 } else {
6260 self.text.text = note;
--- a/iOS/Tombo/Tombo/EditViewController.m
+++ b/iOS/Tombo/Tombo/EditViewController.m
@@ -1,5 +1,6 @@
11 #import "EditViewController.h"
22 #import "MasterViewController.h"
3+#import "Storage.h"
34
45 @interface EditViewController () {
56 }
@@ -36,9 +37,7 @@
3637 NSString *noteData;
3738 if (self.detailItem && self.detailItem.path) {
3839 NSError *error;
39- noteData = [NSString stringWithContentsOfFile:self.detailItem.path
40- encoding:NSUTF8StringEncoding
41- error:&error];
40+ noteData = [Storage load:self.detailItem.path];
4241 if (error) return;
4342 } else {
4443 noteData = @"";
--- a/iOS/Tombo/Tombo/Storage.h
+++ b/iOS/Tombo/Tombo/Storage.h
@@ -45,4 +45,9 @@
4545
4646 - (FileItem *)newFolder:(NSString *)folder;
4747
48+/*
49+ * Load note.
50+ */
51++(NSString *)load:(NSString *)path;
52+
4853 @end
--- a/iOS/Tombo/Tombo/Storage.m
+++ b/iOS/Tombo/Tombo/Storage.m
@@ -58,6 +58,17 @@
5858 -(BOOL)isTopDir {
5959 return [currentDirectory isEqualToString:@"/"];
6060 }
61+- (void)saveDataWithBOM:(NSString *)note file:(NSString *)path {
62+ const char *noteBytes = [note cStringUsingEncoding:NSUTF8StringEncoding];
63+ NSMutableData *data = [[NSMutableData alloc] initWithLength:strlen(noteBytes) + 3];
64+ char *buf = (char *)[data mutableBytes];
65+ strcpy(buf + 3, noteBytes);
66+ *(char*)(buf + 0) = 0xEF;
67+ *(char*)(buf + 1) = 0xBB;
68+ *(char*)(buf + 2) = 0xBF;
69+
70+ [data writeToFile:path atomically:YES];
71+}
6172
6273 // save note
6374 -(FileItem *)save:(NSString *)note item:(FileItem *)item {
@@ -91,8 +102,9 @@
91102 }
92103
93104 // Save note.
94- NSError *error = nil;
95- [note writeToFile:result.path atomically:YES encoding:NSUTF8StringEncoding error:&error];
105+// NSError *error = nil;
106+// [note writeToFile:result.path atomically:YES encoding:NSUTF8StringEncoding error:&error];
107+ [self saveDataWithBOM:note file:result.path];
96108
97109 return result;
98110 }
@@ -171,4 +183,35 @@
171183 return item;
172184 }
173185
186++(NSString *)load:(NSString *)path {
187+ NSData *data = [NSData dataWithContentsOfFile:path];
188+ const char *header = [data bytes];
189+ if ([data length] > 3 &&
190+ *header == 0xEF && *(header + 1) == 0xBB && *(header + 2) == 0xBF) {
191+ // BOM exists. UTF-8.
192+ NSString *note = [[NSString alloc] initWithBytes:[data bytes] + 3
193+ length:[data length] - 3
194+ encoding:NSUTF8StringEncoding];
195+ return note;
196+ }
197+
198+ NSString *note;
199+
200+ if ([[[NSLocale currentLocale] localeIdentifier] isEqualToString:@"ja_JP"]) {
201+ note = [[NSString alloc] initWithBytes:[data bytes]
202+ length:[data length]
203+ encoding:NSShiftJISStringEncoding];
204+ if (note) return note;
205+ }
206+
207+ // UTF-8
208+ note = [[NSString alloc] initWithBytes:[data bytes]
209+ length:[data length]
210+ encoding:NSUTF8StringEncoding];
211+ if (note) return note;
212+
213+ // encode UTF-8 fail.
214+ return @"";
215+}
216+
174217 @end
Show on old repository browser