diff --git a/readers/binary.js b/readers/binary.js new file mode 100644 index 0000000..4dc6819 --- /dev/null +++ b/readers/binary.js @@ -0,0 +1,11 @@ +'use strict'; + +var fs = require('fs'); + +exports.load = function(name) { + return fs.readFileSync(name); +}; + +exports.empty = function () { + return null; +}; diff --git a/readers/flat.js b/readers/flat.js new file mode 100644 index 0000000..5e81f64 --- /dev/null +++ b/readers/flat.js @@ -0,0 +1,64 @@ +'use strict'; + +var fs = require('fs'); + +exports.load = function(name, type, options, regex) { + var result = []; + + var data = fs.readFileSync(name, "UTF-8"); + if (type === 'data') { + while (data.length > 0) { + var match = data.match(/^([^\r\n]*)\r?\n?/); + result.push(match[1]); + data = data.slice(match[0].length); + } + return result; + } + + data.split(/\r\n|\r|\n/).forEach( function(line) { + var line_data; + if (regex.comment.test(line)) { return; } + if (regex.blank.test(line)) { return; } + + line_data = regex.line.exec(line); + if (!line_data) { return; } + + result.push(line_data[1].trim()); + }); + + if (result && type !== 'list' && type !== 'data') { + result = result[0]; + if (options && + require('../utils').in_array(result, options.booleans)) { + result = regex.is_truth.test(result); + } + else if (regex.is_integer.test(result)) { + result = parseInt(result, 10); + } + else if (regex.is_float.test(result)) { + result = parseFloat(result); + } + } + + // Return hostname for 'me' if no result + if (/\/me$/.test(name) && !(result && result.length)) { + return [ require('os').hostname() ]; + } + + // For value types with no result + if (!(type && (type === 'list' || type === 'data'))) { + if (!(result && result.length)) { + return null; + } + } + + return result; +}; + +exports.empty = function (options, type) { + if (type) { + if (type === 'flat') { return null; } + if (type === 'value') { return null; } + } + return []; +}; diff --git a/readers/ini.js b/readers/ini.js new file mode 100644 index 0000000..b6b2dc2 --- /dev/null +++ b/readers/ini.js @@ -0,0 +1,120 @@ +'use strict'; + +var fs = require('fs'); + +exports.load = function(name, options, regex) { + var result = { main: {} }; + var current_sect = result.main; + var current_sect_name = 'main'; + this.bool_matches = []; + if (options && options.booleans) { + this.bool_matches = options.booleans.slice(); + } + + // Initialize any booleans + result = this.init_booleans(options, result); + + var match; + var pre = ''; + + fs.readFileSync(name, 'UTF-8') + .split(/\r\n|\r|\n/) + .forEach(function(line) { + if (regex.comment.test(line)) { return; } + if (regex.blank.test(line) ) { return; } + + match = regex.section.exec(line); + if (match) { + if (!result[match[1]]) result[match[1]] = {}; + current_sect = result[match[1]]; + current_sect_name = match[1]; + return; + } + + if (regex.continuation.test(line)) { + pre += line.replace(regex.continuation, ''); + return; + } + + line = pre + line; + pre = ''; + + match = regex.param.exec(line); + if (!match) { + exports.logger( + 'Invalid line in config file \'' + name + '\': ' + line); + } + + if (options && Array.isArray(options.booleans) && + exports.bool_matches.indexOf( + current_sect_name + '.' + match[1]) !== -1) { + current_sect[match[1]] = regex.is_truth.test(match[2]); + var msg = 'Using boolean ' + current_sect[match[1]] + + ' for ' + current_sect_name + '.' + + match[1] + '=' + match[2]; + exports.logger(msg, 'logdebug'); + } + else if (regex.is_integer.test(match[2])) { + current_sect[match[1]] = parseInt(match[2], 10); + } + else if (regex.is_float.test(match[2])) { + current_sect[match[1]] = parseFloat(match[2]); + } + else { + current_sect[match[1]] = match[2]; + } + }); + + return result; +}; + +exports.empty = function (options) { + return this.init_booleans(options, { main: {} }); +}; + +exports.init_booleans = function (options, result) { + if (!options) return result; + if (!Array.isArray(options.booleans)) return result; + + // console.log(options.booleans); + for (var i=0; i