aux-dbf
Version:
DBF for price PSM
133 lines • 5.85 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var DBFReader_1 = require("./DBFReader");
var DBF_MODE_1 = require("./DBF_MODE");
var Column_1 = require("./Column");
var iconv = require("iconv-lite");
var DBF4Reader = /** @class */ (function (_super) {
__extends(DBF4Reader, _super);
function DBF4Reader() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.sizeFieldDescriptior = 32;
_this.sizeHeadLength = 32;
return _this;
}
DBF4Reader.prototype.readHeader = function () {
this.TypeTable = this.file.readInt8(0);
this.DateHeaderY = this.file.readInt8(1) - 100 + 2000;
this.DateHeaderM = this.file.readInt8(2);
this.DateHeaderD = this.file.readInt8(3) % 31;
this.RecordsCount = this.file.readInt32LE(4);
this.HeaderSize = this.file.readInt16LE(8);
this.RecordSize = this.file.readInt16LE(10);
this.Encode = this.file.readInt8(29);
this.ColumnCount =
(this.HeaderSize - this.sizeHeadLength - 1) / this.sizeFieldDescriptior;
if (this.mode == DBF_MODE_1.DBF_MODE.TEST) {
this.printHeader();
}
};
DBF4Reader.prototype.readSignature = function () {
var _this = this;
this.columns = Array(this.ColumnCount)
.fill(new Column_1.Column())
.map(function (_) { return new Column_1.Column(); })
.map(function (column, index) {
var offset = index * _this.sizeHeadLength + _this.sizeFieldDescriptior;
var name = _this.file
.slice(offset, offset + 10)
.filter(function (r) { return r != 0; })
.toString();
var size = _this.file.readUInt8(offset + 16);
var type = _this.readChar(offset + 11);
var decimalPlaces = _this.file.readUInt8(offset + 17);
var isMDX = _this.file.readUInt8(offset + 31).toString() != "0";
column.Add(name, size, type, index, decimalPlaces, isMDX);
return column;
});
if (this.mode == DBF_MODE_1.DBF_MODE.TEST) {
this.printColumns();
}
};
DBF4Reader.prototype.readTable = function () {
var _this = this;
console.time("READ DATA SOURCE");
var array = Array(this.RecordsCount)
.fill(new Array(this.ColumnCount).fill(null))
.map(function (r) {
return new Array(_this.ColumnCount);
});
var startPosition = this.HeaderSize;
var offsetLeft = 0;
for (var column = 0; column < this.ColumnCount; column++) {
var position = startPosition + 1;
if (this.columns[column].getType() == Column_1.COLUMN_TYPE.CHARACTER) {
var size = this.columns[column].getSize();
for (var row = 0; row < this.RecordsCount; row++) {
// TODO
array[row][column] =
//iconv.decode(
this.file.subarray(position + offsetLeft, position + offsetLeft + size);
//, "win1251" ).trim();
position += this.RecordSize;
}
}
else if (this.columns[column].getType() == Column_1.COLUMN_TYPE.NUMBER) {
var size = this.columns[column].getSize();
for (var row = 0; row < this.RecordsCount; row++) {
array[row][column] = +this.file.toString("ascii", position + offsetLeft, position + offsetLeft + size);
position += this.RecordSize;
}
}
else if (this.columns[column].getType() == Column_1.COLUMN_TYPE.FLOAT) {
var size = this.columns[column].getSize();
for (var row = 0; row < this.RecordsCount; row++) {
array[row][column] = +this.file.toString("ascii", position + offsetLeft, position + offsetLeft + size);
position += this.RecordSize;
}
}
else if (this.columns[column].getType() == Column_1.COLUMN_TYPE.DATE) {
for (var row = 0; row < this.RecordsCount; row++) {
array[row][column] = this.readDate(this.file.subarray(position + offsetLeft, position + offsetLeft + 8));
var p = position + offsetLeft;
position += this.RecordSize;
}
}
else {
throw new Error("[X] Непредсказуемый тип колонки");
}
offsetLeft += this.columns[column].getSize();
}
console.timeEnd("READ DATA SOURCE");
return array;
};
DBF4Reader.prototype.readDate = function (buffer) {
var date = (buffer.toString("ascii", 0, 4) +
"-" +
buffer.toString("ascii", 4, 6) +
"-" +
buffer.toString("ascii", 6, 8));
if (/(\d{4}-\d{2}-\d{2})/.test(date)) {
return date;
}
else {
return "0000-00-00";
}
};
return DBF4Reader;
}(DBFReader_1.DBFReader));
exports.DBF4Reader = DBF4Reader;
//# sourceMappingURL=DBF4Reader.js.map