Skip to content

Commit

Permalink
support of dbf files encoded in utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
koszny committed Mar 22, 2017
1 parent 4ed4fbc commit 3310759
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
17 changes: 11 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var TextDecoder = require('text-encoding').TextDecoder;

function dbfHeader(buffer) {
var data = new DataView(buffer);
var out = {};
Expand Down Expand Up @@ -28,9 +30,9 @@ function dbfRowHeader(buffer, headerLen) {
return out;
}

function rowFuncs(buffer, offset, len, type) {
function rowFuncs(buffer, offset, len, type, textDecoder) {
var data = (new Uint8Array(buffer, offset, len));
var textData = String.fromCharCode.apply(this, data).replace(/\0|\s+$/g, '');
var textData = textDecoder.decode(data).replace(/\0|\s+$/g, '');
switch (type) {
case 'N':
case 'F':
Expand All @@ -45,15 +47,15 @@ function rowFuncs(buffer, offset, len, type) {
}
}

function parseRow(buffer, offset, rowHeaders) {
function parseRow(buffer, offset, rowHeaders, textDecoder) {
var out = {};
var i = 0;
var len = rowHeaders.length;
var field;
var header;
while (i < len) {
header = rowHeaders[i];
field = rowFuncs(buffer, offset, header.len, header.dataType);
field = rowFuncs(buffer, offset, header.len, header.dataType, textDecoder);
offset += header.len;
if (typeof field !== 'undefined') {
out[header.name] = field;
Expand All @@ -62,16 +64,19 @@ function parseRow(buffer, offset, rowHeaders) {
}
return out;
}
module.exports = function(buffer) {
module.exports = function(buffer, encoding = 'utf-8') {
var header = dbfHeader(buffer);
var rowHeaders = dbfRowHeader(buffer, header.headerLen - 1);

var offset = ((rowHeaders.length + 1) << 5) + 2;
var recLen = header.recLen;
var records = header.records;
var out = [];

var textDecoder = new TextDecoder(encoding);

while (records) {
out.push(parseRow(buffer, offset, rowHeaders));
out.push(parseRow(buffer, offset, rowHeaders, textDecoder));
offset += recLen;
records--;
}
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
"chai": "^3.3.0",
"jshint": "^2.8.0",
"mocha": "^2.3.3"
},
"dependencies": {
"text-encoding": "^0.6.4"
}
}

0 comments on commit 3310759

Please sign in to comment.