<commit_before>/*jshint globalstrict:true, node:true*/

"use strict";

var fs = require("fs-extra");
var isbinaryfile = require("isbinaryfile");
var jschardet = require("jschardet");
var stripBom = require("strip-bom");
var trash = require("trash");
var utils = require("../utils");
var remote = require("remote");
var dialog = remote.require("dialog");

/*
    additions to native fs provided by node
    to support functionality required by brackets
*/

var fsAdditions = module.exports = {};

fsAdditions.isBinaryFile = function (filename, callback) {
    isbinaryfile(filename, callback);
};

fsAdditions.isBinaryFileSync = function (filename) {
    return isbinaryfile(filename);
};

fsAdditions.isEncodingSupported = function (encoding) {
    return ["ascii", "utf-8", "utf8"].indexOf(encoding.toLowerCase()) !== -1;
};

fsAdditions.isNetworkDrive = function (path, callback) {
    // TODO: implement
    process.nextTick(function () {
        callback(null, false);
    });
};

fsAdditions.moveToTrash = function (path, callback) {
    // trash expects an array of files which is inconsistent with fs-extra apis
    trash(Array.isArray(path) ? path : [path], callback);
};

fsAdditions.readTextFile = function (filename, encoding, callback) {
    if (typeof encoding === "function") {
        callback = encoding;
        encoding = "utf-8";
    } else if (typeof encoding !== "string") {
        throw new TypeError("encoding must be a string");
    } else if (!fsAdditions.isEncodingSupported(encoding)) {
        throw new TypeError("encoding is not supported: " + encoding);
    }
    // isbinaryfile check first because it checks first 1000 bytes of a file
    // so we don't load whole file if it's binary
    isbinaryfile(filename, function(err, isBinary) {
        if (err) {
            return callback(err);
        }
        if (isBinary) {
            err = new Error("ECHARSET: file is a binary file");
            err.code = "ECHARSET";
            return callback(err);
        }
        fs.readFile(filename, function (err, buffer) {
            if (err) {
                return callback(err);
            }
            if (buffer.length) {
                var chardet = jschardet.detect(buffer);
                if (!fsAdditions.isEncodingSupported(chardet.encoding)) {
                    err = new Error("ECHARSET: unsupported encoding: " + chardet.encoding);
                    err.code = "ECHARSET";
                    return callback(err);
                }
            }
            callback(null, stripBom(buffer.toString(encoding)));
        });
    });
};<commit_msg>update charset error to include file name<commit_after>/*jshint globalstrict:true, node:true*/

"use strict";

var fs = require("fs-extra");
var isbinaryfile = require("isbinaryfile");
var jschardet = require("jschardet");
var stripBom = require("strip-bom");
var trash = require("trash");
var utils = require("../utils");
var remote = require("remote");
var dialog = remote.require("dialog");

/*
    additions to native fs provided by node
    to support functionality required by brackets
*/

var fsAdditions = module.exports = {};

fsAdditions.isBinaryFile = function (filename, callback) {
    isbinaryfile(filename, callback);
};

fsAdditions.isBinaryFileSync = function (filename) {
    return isbinaryfile(filename);
};

fsAdditions.isEncodingSupported = function (encoding) {
    return ["ascii", "utf-8", "utf8"].indexOf(encoding.toLowerCase()) !== -1;
};

fsAdditions.isNetworkDrive = function (path, callback) {
    // TODO: implement
    process.nextTick(function () {
        callback(null, false);
    });
};

fsAdditions.moveToTrash = function (path, callback) {
    // trash expects an array of files which is inconsistent with fs-extra apis
    trash(Array.isArray(path) ? path : [path], callback);
};

fsAdditions.readTextFile = function (filename, encoding, callback) {
    if (typeof encoding === "function") {
        callback = encoding;
        encoding = "utf-8";
    } else if (typeof encoding !== "string") {
        throw new TypeError("encoding must be a string");
    } else if (!fsAdditions.isEncodingSupported(encoding)) {
        throw new TypeError("encoding is not supported: " + encoding);
    }
    // isbinaryfile check first because it checks first 1000 bytes of a file
    // so we don't load whole file if it's binary
    isbinaryfile(filename, function(err, isBinary) {
        if (err) {
            return callback(err);
        }
        if (isBinary) {
            err = new Error("ECHARSET: file is a binary file: " + filename);
            err.code = "ECHARSET";
            return callback(err);
        }
        fs.readFile(filename, function (err, buffer) {
            if (err) {
                return callback(err);
            }
            if (buffer.length) {
                var chardet = jschardet.detect(buffer);
                if (!fsAdditions.isEncodingSupported(chardet.encoding)) {
                    err = new Error("ECHARSET: unsupported encoding " + chardet.encoding +
                                    " in file: " + filename);
                    err.code = "ECHARSET";
                    return callback(err);
                }
            }
            callback(null, stripBom(buffer.toString(encoding)));
        });
    });
};
{'fraction_non_alphanumeric': 0.12584777694046723, 'fraction_numerical': 0.003014318010550113, 'mean_word_length': 2.5799055967633175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2617180', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1450, 'n_words': 404}