<commit_before>/*
 * file.js: Simple file storage engine for nconf files
 *
 * (C) 2011, <NAME>
 *
 */
 
var fs = require('fs'),
    util = require('util'),
    Memory = require('./memory').Memory;
 
//
// ### function File (options)
// #### @options {Object} Options for this instance
// Constructor function for the File nconf store, a simple abstraction
// around the Memory store that can persist configuration to disk.
//
var File = exports.File = function (options) {
  if (!options.file) {
    throw new Error ('Missing required option `files`');
  } 

  Memory.call(this, options);

  this.type   = 'file';
  this.file   = options.file;
  this.format = options.format || {
    stringify: function (obj) {
      return JSON.stringify(obj, null, 2)
    },
    parse: JSON.parse
  };
};

// Inherit from the Memory store
util.inherits(File, Memory);

//
// ### function save (value, callback) 
// #### @value {Object} _Ignored_ Left here for consistency
// #### @callback {function} Continuation to respond to when complete.
// Saves the current configuration object to disk at `this.file` 
// using the format specified by `this.format`.
//
File.prototype.save = function (value, callback) {
  if (!callback) {
    callback = value;
    value = null;
  }
  
  fs.writeFile(this.file, this.format.stringify(this.store), function (err) {
    return err ? callback(err) : callback();
  });
};

//
// ### function saveSync (value, callback) 
// #### @value {Object} _Ignored_ Left here for consistency
// #### @callback {function} **Optional** Continuation to respond to when complete.
// Saves the current configuration object to disk at `this.file` 
// using the format specified by `this.format` synchronously.
//
File.prototype.saveSync = function (value, callback) {
  if (!callback) {
    callback = value;
    value = null;
  }
  
  var err;
  try {
    fs.writeFileSync(this.file, this.format.stringify(this.store));
  }
  catch (ex) {
    err = ex;
  }
  
  if (callback) {
    return callback(err);
  }
  
  if (err) {
    throw err;
  }
};

//
// ### function load (callback)
// #### @callback {function} Continuation to respond to when complete.
// Responds with an Object representing all keys associated in this instance.
//
File.prototype.load = function (callback) {
  var self = this;
  fs.readFile(this.file, function (err, data) {
    if (err) {
      return callback(err);
    }
    
    try {
      self.store = self.format.parse(data.toString());
      callback(null, self.store);
    }
    catch (ex) {
      self.store = {};
      callback(ex);
    }
  });
};

//
// ### function load (callback)
// #### @callback {function} **Optional** Continuation to respond to when complete.
// Attempts to load the data stored in `this.file` synchronously and responds appropriately.
//
File.prototype.loadSync = function (callback) {
  var err, data;
  
  try {
    data = fs.readFileSync(this.file, 'utf8');
    this.store = this.format.parse(data);
  }
  catch (ex) {
    err = ex;
  }
  
  if (callback) {<commit_msg>move callback outside of try / catch<commit_after>/*
 * file.js: Simple file storage engine for nconf files
 *
 * (C) 2011, <NAME>
 *
 */
 
var fs = require('fs'),
    util = require('util'),
    Memory = require('./memory').Memory;
 
//
// ### function File (options)
// #### @options {Object} Options for this instance
// Constructor function for the File nconf store, a simple abstraction
// around the Memory store that can persist configuration to disk.
//
var File = exports.File = function (options) {
  if (!options.file) {
    throw new Error ('Missing required option `files`');
  } 

  Memory.call(this, options);

  this.type   = 'file';
  this.file   = options.file;
  this.format = options.format || {
    stringify: function (obj) {
      return JSON.stringify(obj, null, 2)
    },
    parse: JSON.parse
  };
};

// Inherit from the Memory store
util.inherits(File, Memory);

//
// ### function save (value, callback) 
// #### @value {Object} _Ignored_ Left here for consistency
// #### @callback {function} Continuation to respond to when complete.
// Saves the current configuration object to disk at `this.file` 
// using the format specified by `this.format`.
//
File.prototype.save = function (value, callback) {
  if (!callback) {
    callback = value;
    value = null;
  }
  
  fs.writeFile(this.file, this.format.stringify(this.store), function (err) {
    return err ? callback(err) : callback();
  });
};

//
// ### function saveSync (value, callback) 
// #### @value {Object} _Ignored_ Left here for consistency
// #### @callback {function} **Optional** Continuation to respond to when complete.
// Saves the current configuration object to disk at `this.file` 
// using the format specified by `this.format` synchronously.
//
File.prototype.saveSync = function (value, callback) {
  if (!callback) {
    callback = value;
    value = null;
  }
  
  var err;
  try {
    fs.writeFileSync(this.file, this.format.stringify(this.store));
  }
  catch (ex) {
    err = ex;
  }
  
  if (callback) {
    return callback(err);
  }
  
  if (err) {
    throw err;
  }
};

//
// ### function load (callback)
// #### @callback {function} Continuation to respond to when complete.
// Responds with an Object representing all keys associated in this instance.
//
File.prototype.load = function (callback) {
  var self = this;
  fs.readFile(this.file, function (err, data) {
    if (err) {
      return callback(err);
    }
    
    try {
      self.store = self.format.parse(data.toString());
    }
    catch (ex) {
      self.store = {};
      return callback(ex);
    }
    callback(null, self.store);

  });
};

//
// ### function load (callback)
// #### @callback {function} **Optional** Continuation to respond to when complete.
// Attempts to load the data stored in `this.file` synchronously and responds appropriately.
//
File.prototype.loadSync = function (callback) {
  var err, data;
  
  try {
    data = fs.readFileSync(this.file, 'utf8');
    this.store = this.format.parse(data);
  }
  catch (ex) {
    err = ex;
  }
  
  if (callback) {
{'fraction_non_alphanumeric': 0.14899196852974922, 'fraction_numerical': 0.001966890673660056, 'mean_word_length': 2.941860465116279, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3709414', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1881, 'n_words': 582}