TOMBO source code
Revision | 2e350eca5261869851e1602497dca5901e5998b1 (tree) |
---|---|
Time | 2012-04-10 15:11:33 |
Author | Hirami <tomohisa.hirami@nift...> |
Commiter | Hirami |
Support UTF-8 text with BOM and SJIS.
@@ -1,6 +1,7 @@ | ||
1 | 1 | #import "DetailViewController.h" |
2 | 2 | #import "EditViewController.h" |
3 | 3 | #import "MasterViewController.h" |
4 | +#import "Storage.h" | |
4 | 5 | |
5 | 6 | @interface DetailViewController () |
6 | 7 |
@@ -52,11 +53,8 @@ | ||
52 | 53 | if (item.isNewItem) { |
53 | 54 | self.text.text = @""; |
54 | 55 | } 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) { | |
60 | 58 | self.text.text = @""; |
61 | 59 | } else { |
62 | 60 | self.text.text = note; |
@@ -1,5 +1,6 @@ | ||
1 | 1 | #import "EditViewController.h" |
2 | 2 | #import "MasterViewController.h" |
3 | +#import "Storage.h" | |
3 | 4 | |
4 | 5 | @interface EditViewController () { |
5 | 6 | } |
@@ -36,9 +37,7 @@ | ||
36 | 37 | NSString *noteData; |
37 | 38 | if (self.detailItem && self.detailItem.path) { |
38 | 39 | NSError *error; |
39 | - noteData = [NSString stringWithContentsOfFile:self.detailItem.path | |
40 | - encoding:NSUTF8StringEncoding | |
41 | - error:&error]; | |
40 | + noteData = [Storage load:self.detailItem.path]; | |
42 | 41 | if (error) return; |
43 | 42 | } else { |
44 | 43 | noteData = @""; |
@@ -45,4 +45,9 @@ | ||
45 | 45 | |
46 | 46 | - (FileItem *)newFolder:(NSString *)folder; |
47 | 47 | |
48 | +/* | |
49 | + * Load note. | |
50 | + */ | |
51 | ++(NSString *)load:(NSString *)path; | |
52 | + | |
48 | 53 | @end |
@@ -58,6 +58,17 @@ | ||
58 | 58 | -(BOOL)isTopDir { |
59 | 59 | return [currentDirectory isEqualToString:@"/"]; |
60 | 60 | } |
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 | +} | |
61 | 72 | |
62 | 73 | // save note |
63 | 74 | -(FileItem *)save:(NSString *)note item:(FileItem *)item { |
@@ -91,8 +102,9 @@ | ||
91 | 102 | } |
92 | 103 | |
93 | 104 | // 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]; | |
96 | 108 | |
97 | 109 | return result; |
98 | 110 | } |
@@ -171,4 +183,35 @@ | ||
171 | 183 | return item; |
172 | 184 | } |
173 | 185 | |
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 | + | |
174 | 217 | @end |