If $native.read fails to open a file as UTF8, it opens it as ascii

This is a temporary solution to allow Atom to open binary files. The ultimate goal would be to have a hex-editor built into atom.
This commit is contained in:
Corey Johnson
2012-11-29 10:34:22 -08:00
parent 0f0ff425f4
commit 02c441e937
3 changed files with 15 additions and 2 deletions

View File

@@ -50,10 +50,16 @@ bool Native::Execute(const CefString& name,
NSString *path = stringFromCefV8Value(arguments[0]);
NSError *error = nil;
NSString *contents = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSStringEncoding *encoding = nil;
NSString *contents = [NSString stringWithContentsOfFile:path usedEncoding:encoding error:&error];
NSError *binaryFileError = nil;
if (error) {
exception = [[error localizedDescription] UTF8String];
contents = [NSString stringWithContentsOfFile:path encoding:NSNonLossyASCIIStringEncoding error:&binaryFileError];
}
if (binaryFileError) {
exception = [[binaryFileError localizedDescription] UTF8String];
}
else {
retval = CefV8Value::CreateString([contents UTF8String]);

BIN
spec/fixtures/binary-file.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 392 B

View File

@@ -1,6 +1,13 @@
fs = require 'fs'
describe "fs", ->
describe ".read(path)", ->
it "return contents of file", ->
expect(fs.read(require.resolve("fixtures/sample.txt"))).toBe "Some text.\n"
it "does not through an exception when the path is a binary file", ->
expect(-> fs.read(require.resolve("fixtures/binary-file.png"))).not.toThrow()
describe ".isFile(path)", ->
fixturesDir = require.resolve('fixtures')