UNPKG

aux-dbf

Version:

DBF for price PSM

215 lines 8.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var util_1 = require("util"); var BTree_1 = require("./BTree"); var DBF_MODE_1 = require("./DBF_MODE"); var DBF4Reader_1 = require("./DBF4Reader"); var DBF7Reader_1 = require("./DBF7Reader"); var iconv = require("iconv-lite"); var fs = require("fs"); /** * Проверять, является ли переменная val строкой * @param val * @return {boolean} */ function isString(val) { return (typeof val === "string" || val instanceof String); } /** * DZHIGURDA A. (c) 2017 * OOO "PIKADA-LAB" * Version 0.0.3-DEV */ var DBFLib = /** @class */ (function () { function DBFLib(path) { this.path = path; this.mode = DBF_MODE_1.DBF_MODE.PROD; this.dataSource = []; } DBFLib.prototype.DBFRead = function (clbk) { var _this = this; try { if (this.mode == DBF_MODE_1.DBF_MODE.TEST) { console.log("[*] OpenFile. " + this.path + "\n"); } fs.readFile(this.path, function (err, file) { if (err) { if (err.code === "ENOENT") { console.log(_this.path + " не найден"); if (!util_1.isUndefined(clbk)) clbk(_this.path + " не найден", null); return; } if (!util_1.isUndefined(clbk)) clbk(err, null); return; } if (file.length == 0) return; _this.binFile = file; if (_this.mode == DBF_MODE_1.DBF_MODE.TEST) { console.time("read row"); } _this.checkTableType(); if (_this.TypeTable == 3) { _this.reader = new DBF4Reader_1.DBF4Reader(file); } else if (_this.TypeTable == 4) { _this.reader = new DBF7Reader_1.DBF7Reader(file); } else { throw new Error("Формат DBF не поддерживается"); } _this.reader.mode = _this.mode; _this.reader.readHeader(); _this.reader.readSignature(); _this.col = _this.reader.getColumns(); _this.countColumn = _this.col.length; if (_this.mode == DBF_MODE_1.DBF_MODE.TEST) { console.timeEnd("read row"); } if (!util_1.isUndefined(clbk)) clbk(err, _this); }); } catch (ex) { if (!util_1.isUndefined(clbk)) clbk(ex, null); } }; DBFLib.prototype.readRows = function () { this.dataSource = this.reader.readTable(); this.indexes = new Array(this.Count()).fill(null); }; DBFLib.prototype.Exist = function () { return this.file != null; }; /** Название стобца по номеру */ DBFLib.prototype.getRowName = function (index) { return this.col[index].getTitle(); }; /** Строка по номеру строки и номеру столбца */ DBFLib.prototype.getRow = function (rowIndex, colIndex) { if (rowIndex == null || colIndex == null) { throw "Индекс не определён " + rowIndex + "x" + colIndex; } this.checkRow(rowIndex); if (isString(colIndex)) { colIndex = colIndex.toLowerCase(); var l = 0; for (l = 0; l < this.countColumn; l++) { if (this.col[l].getTitle().toLowerCase() == colIndex) break; } if (l == this.countColumn) throw "Индекс за пределом доступного количества колонок. Доступное значение от 0 до " + (this.col.length - 1); return this.data[rowIndex][l]; } else { if (!this.checkColumn(+colIndex)) throw "Индекс за пределом доступного количества колонок. Доступное значение от 0 до " + (this.col.length - 1); return this.dataSource[rowIndex][colIndex]; } }; DBFLib.prototype.getString = function (rowIndex, colIndex) { if (isString(colIndex)) { colIndex = colIndex.toLowerCase(); var l = 0; for (l = 0; l < this.countColumn; l++) { if (this.col[l].getTitle().toLowerCase() == colIndex) break; } if (l == this.countColumn) throw "Индекс за пределом доступного количества колонок. Доступное значение от 0 до " + (this.col.length - 1); colIndex = l; } if (this.getRow(rowIndex, colIndex) instanceof Buffer) { return iconv.decode(Buffer.from(this.getRow(rowIndex, colIndex), "binary"), "win1251").trim(); } else { this.checkAccess(rowIndex, colIndex); return this.getRow(rowIndex, colIndex).toString(); } }; DBFLib.prototype.getInt = function (rowIndex, colIndex) { if (isString(colIndex)) { colIndex = colIndex.toLowerCase(); var l = 0; for (l = 0; l < this.countColumn; l++) { if (this.col[l].getTitle().toLowerCase() == colIndex) break; } if (l == this.countColumn) throw "Индекс за пределом доступного количества колонок. Доступное значение от 0 до " + (this.col.length - 1); this.checkAccess(rowIndex, l); return +this.dataSource[rowIndex][l]; } else { this.checkAccess(rowIndex, colIndex); return +this.dataSource[rowIndex][colIndex]; } // return +this.data[rowIndex].value[colIndex]; }; DBFLib.prototype.checkAccess = function (rowIndex, colIndex) { if (!this.checkRow(rowIndex)) throw "Индекс за пределом доступного количества строк. Доступное значение от 0 до " + (this.Count() - 1); if (!this.checkColumn(colIndex)) throw "Индекс за пределом доступного количества колонок. Доступное значение от 0 до " + (this.CountColumn() - 1); }; /** Количество строк */ DBFLib.prototype.Count = function () { return this.dataSource.length; }; /** Количество колонок */ DBFLib.prototype.CountColumn = function () { return this.col.length; }; DBFLib.prototype.SetBTree = function (columnIndex) { if (this.mode == DBF_MODE_1.DBF_MODE.TEST) { console.time("add index " + columnIndex); } this.indexes[columnIndex] = new BTree_1.BTree(); for (var i = 0; i < this.Count(); i++) { this.indexes[columnIndex].insert(this.getInt(i, columnIndex), i); } if (this.mode == DBF_MODE_1.DBF_MODE.TEST) { console.timeEnd("add index " + columnIndex); } }; DBFLib.prototype.Search = function (l, value) { var i; if (this.indexes[l]) { // console.log("[*] Whith index"); var index = this.indexes[l].search(value); if (!index) return null; var i_1 = index.Adress; if (!i_1) return null; return i_1; } // console.log("[X] Whith index"); for (var key = 0; key < this.Count(); key++) { if (this.getInt(key, l) == value) { return key; } } return null; }; /// PRIVATE DBFLib.prototype.checkRow = function (index) { return index < this.dataSource.length; }; DBFLib.prototype.checkColumn = function (index) { return index < this.col.length; }; /** * Задаёт байт для определения типа таблицы */ DBFLib.prototype.checkTableType = function () { this.TypeTable = this.binFile.readInt8(0); }; return DBFLib; }()); exports.DBFLib = DBFLib; //# sourceMappingURL=index.js.map