mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-09 05:06:04 +00:00
commit
9080a177f1
131
lib/file.js
131
lib/file.js
|
@ -4,25 +4,18 @@
|
||||||
//
|
//
|
||||||
// Common routines. Defines LIB object which contains the API, as well as
|
// Common routines. Defines LIB object which contains the API, as well as
|
||||||
// a global console.log function.
|
// a global console.log function.
|
||||||
|
|
||||||
|
// with the PIPE based IPC (lib/pipe-ipc.js)
|
||||||
//
|
//
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
var LIB = require("lib/std");
|
var LIB = require("lib/std");
|
||||||
|
var PipeIPC = require("lib/pipe-ipc");
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
// Private APIs / Utility functions
|
// Private APIs / Utility functions
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
// StreamTypeEnum
|
|
||||||
// https://learn.microsoft.com/en-us/sql/ado/reference/ado-api/streamtypeenum?view=sql-server-ver16
|
|
||||||
var adTypeBinary = 1;
|
|
||||||
var adTypeText = 2;
|
|
||||||
|
|
||||||
// SaveOptionsEnum
|
|
||||||
// https://learn.microsoft.com/en-us/sql/ado/reference/ado-api/saveoptionsenum?view=sql-server-ver16
|
|
||||||
var adSaveCreateNotExist = 1;
|
|
||||||
var adSaveCreateOverWrite = 2;
|
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
// fileExists
|
// fileExists
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -61,29 +54,9 @@ function fileGet(FN) {
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
function readFile(FN, charset) {
|
function readFile(FN, charset) {
|
||||||
if(typeof(charset) === "undefined") {
|
var pipe = PipeIPC.create();
|
||||||
var FSO = CreateObject("Scripting.FileSystemObject");
|
pipe.setCharset(charset);
|
||||||
var T = null;
|
return pipe.readTextFromFile(FN);
|
||||||
try {
|
|
||||||
var TS = FSO.OpenTextFile(FN, 1);
|
|
||||||
if (TS.AtEndOfStream) return "";
|
|
||||||
T = TS.ReadAll();
|
|
||||||
TS.Close();
|
|
||||||
TS = null;
|
|
||||||
} catch (e) {
|
|
||||||
console.log("ERROR! " + e.number + ", " + e.description + ", FN=" + FN);
|
|
||||||
}
|
|
||||||
FSO = null;
|
|
||||||
return T;
|
|
||||||
} else {
|
|
||||||
var fsT = CreateObject("ADODB.Stream");
|
|
||||||
fsT.CharSet = charset;
|
|
||||||
fsT.Open();
|
|
||||||
fsT.LoadFromFile(FN);
|
|
||||||
T = fsT.ReadText();
|
|
||||||
fsT = null;
|
|
||||||
return T;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -92,60 +65,12 @@ function readFile(FN, charset) {
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
function writeFile(FN, content, charset) {
|
function writeFile(FN, content, charset) {
|
||||||
var Stream_No_UTF8_BOM = function(objStream) {
|
var pipe = PipeIPC.connect("volatile");
|
||||||
var _objStream = CreateObject("ADODB.Stream");
|
pipe.setCharset(charset);
|
||||||
_objStream.Type = 1;
|
pipe.startRecorder(FN, PipeIPC.ForWriting);
|
||||||
_objStream.Mode = 3;
|
pipe.write(content);
|
||||||
_objStream.Open();
|
pipe.destroy();
|
||||||
objStream.Position = 3;
|
return true;
|
||||||
objStream.CopyTo(_objStream);
|
|
||||||
objStream.Flush();
|
|
||||||
//objStream.Close();
|
|
||||||
return _objStream;
|
|
||||||
};
|
|
||||||
var ok = false;
|
|
||||||
|
|
||||||
while (!ok) {
|
|
||||||
// [lib/file] Can not overwrite a file with ADODB.Stream SaveToFile() #32
|
|
||||||
require("lib/shell").exec(["del", FN]);
|
|
||||||
|
|
||||||
// ascii:Scripting.FileSystemObject, unicode:ADODB.Stream
|
|
||||||
if (charset) {
|
|
||||||
console.log("WRITE TO DISK USING ADODB.Stream CHARSET " + charset);
|
|
||||||
try {
|
|
||||||
var fsT = CreateObject("ADODB.Stream");
|
|
||||||
fsT.Type = 2; // save as text/string data.
|
|
||||||
fsT.Charset = charset; // Specify charset For the source text data.
|
|
||||||
fsT.Open();
|
|
||||||
fsT.WriteText(content);
|
|
||||||
if (charset == "utf-8") {
|
|
||||||
Stream_No_UTF8_BOM(fsT).SaveToFile(FN, 2); // save as binary to disk
|
|
||||||
} else {
|
|
||||||
fsT.SaveToFile(FN, 2); // save as binary to disk
|
|
||||||
}
|
|
||||||
fsT.Close();
|
|
||||||
fsT = null;
|
|
||||||
ok = true;
|
|
||||||
} catch (e) {
|
|
||||||
console.log("ADODB.Stream: ERROR! " + e.number + ", " + e.description + ", FN=" + FN);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.log("WRITE TO DISK USING OpenTextFile CHARSET ascii");
|
|
||||||
var FSO = CreateObject("Scripting.FileSystemObject");
|
|
||||||
try {
|
|
||||||
var TS = FSO.OpenTextFile(FN, 2, true, 0); // ascii
|
|
||||||
TS.Write(content);
|
|
||||||
TS.Close();
|
|
||||||
TS = null;
|
|
||||||
ok = true;
|
|
||||||
} catch (e) {
|
|
||||||
console.log("OpenTextFile: ERROR! " + e.number + ", " + e.description + ", FN=" + FN);
|
|
||||||
}
|
|
||||||
FSO = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ok;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -154,7 +79,7 @@ function writeFile(FN, content, charset) {
|
||||||
|
|
||||||
function writeBinaryFile(FN, DATA) {
|
function writeBinaryFile(FN, DATA) {
|
||||||
var BinaryStream = CreateObject("ADODB.Stream");
|
var BinaryStream = CreateObject("ADODB.Stream");
|
||||||
BinaryStream.Type = adTypeBinary;
|
BinaryStream.Type = PipeIPC.adTypeBinary;
|
||||||
BinaryStream.Open();
|
BinaryStream.Open();
|
||||||
BinaryStream.Write(DATA);
|
BinaryStream.Write(DATA);
|
||||||
BinaryStream.SaveToFile(FN, adSaveCreateOverWrite);
|
BinaryStream.SaveToFile(FN, adSaveCreateOverWrite);
|
||||||
|
@ -210,19 +135,26 @@ function deleteFile(FN) {
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
function includeFile(FN) {
|
function includeFile(FN) {
|
||||||
var fso = CreateObject("Scripting.FileSystemObject");
|
eval(readFile(FN));
|
||||||
var fileStream = fso.openTextFile(FN);
|
|
||||||
var fileData = fileStream.readAll();
|
|
||||||
fileStream.Close();
|
|
||||||
eval(fileData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function appendFile(FN, content, charset) {
|
function appendFile(FN, content, charset) {
|
||||||
if (fileExists(FN)) {
|
var pipe = PipeIPC.connect("volatile");
|
||||||
return writeFile(FN, readFile(FN, charset) + content, charset);
|
pipe.setCharset(charset);
|
||||||
} else {
|
pipe.startRecorder(FN, PipeIPC.ForAppending);
|
||||||
return writeFile(FN, content, charset);
|
pipe.write(content);
|
||||||
}
|
pipe.destroy();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
function rotateFile(FN, content, numOfLines, charset) {
|
||||||
|
var pipe = PipeIPC.connect("volatile");
|
||||||
|
pipe.setCharset(charset);
|
||||||
|
pipe.setMaxSentences(numOfLines);
|
||||||
|
pipe.startRecorder(FN, PipeIPC.ForAppending);
|
||||||
|
pipe.write(content);
|
||||||
|
pipe.destroy();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.fileExists = fileExists;
|
exports.fileExists = fileExists;
|
||||||
|
@ -237,7 +169,8 @@ exports.deleteFolder = deleteFolder;
|
||||||
exports.deleteFile = deleteFile;
|
exports.deleteFile = deleteFile;
|
||||||
exports.includeFile = includeFile;
|
exports.includeFile = includeFile;
|
||||||
exports.appendFile = appendFile;
|
exports.appendFile = appendFile;
|
||||||
|
exports.rotateFile = rotateFile;
|
||||||
|
|
||||||
exports.VERSIONINFO = "File Lib (file.js) version 0.2.1";
|
exports.VERSIONINFO = "File Library (file.js) version 0.2.4";
|
||||||
exports.global = global;
|
exports.global = global;
|
||||||
exports.require = global.require;
|
exports.require = global.require;
|
||||||
|
|
|
@ -54,7 +54,6 @@ var CdoUTF_7 = "utf-7";
|
||||||
var CdoUTF_8 = "utf-8";
|
var CdoUTF_8 = "utf-8";
|
||||||
|
|
||||||
function createUUIDv4() {
|
function createUUIDv4() {
|
||||||
sleep(1);
|
|
||||||
var randomize = Math.random;
|
var randomize = Math.random;
|
||||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||||
var r = randomize() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
var r = randomize() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
||||||
|
@ -62,6 +61,14 @@ function createUUIDv4() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createFSO() {
|
||||||
|
return CreateObject("Scripting.FileSystemObject");
|
||||||
|
}
|
||||||
|
|
||||||
|
function createADO() {
|
||||||
|
return CreateObject("ADODB.Stream");
|
||||||
|
}
|
||||||
|
|
||||||
function PipeIPC() {
|
function PipeIPC() {
|
||||||
this.path = "data\\.pipe_:pipename";
|
this.path = "data\\.pipe_:pipename";
|
||||||
this.delimiter = "\r\n";
|
this.delimiter = "\r\n";
|
||||||
|
@ -93,6 +100,9 @@ function PipeIPC() {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.connect = function(pipename, callback) {
|
this.connect = function(pipename, callback) {
|
||||||
|
if (pipename == "volatile") {
|
||||||
|
pipename = createUUIDv4().substring(0, 8);
|
||||||
|
}
|
||||||
this.path = this.path.replace(":pipename", pipename);
|
this.path = this.path.replace(":pipename", pipename);
|
||||||
//this.openWriter();
|
//this.openWriter();
|
||||||
this.openReader();
|
this.openReader();
|
||||||
|
@ -105,7 +115,7 @@ function PipeIPC() {
|
||||||
this.openWriter = function(iomode) {
|
this.openWriter = function(iomode) {
|
||||||
while (this.writer == null) {
|
while (this.writer == null) {
|
||||||
try {
|
try {
|
||||||
this.writer = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.path, iomode, true, TristateTrue);
|
this.writer = createFSO().OpenTextFile(this.path, iomode, true, TristateTrue);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -120,7 +130,7 @@ function PipeIPC() {
|
||||||
this.openReader = function() {
|
this.openReader = function() {
|
||||||
while (this.reader == null) {
|
while (this.reader == null) {
|
||||||
try {
|
try {
|
||||||
this.reader = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.path, ForReading, true, TristateTrue);
|
this.reader = createFSO().OpenTextFile(this.path, ForReading, true, TristateTrue);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -146,7 +156,7 @@ function PipeIPC() {
|
||||||
this.openRecorder = function(iomode) {
|
this.openRecorder = function(iomode) {
|
||||||
while (this.recorder == null) {
|
while (this.recorder == null) {
|
||||||
try {
|
try {
|
||||||
this.recorder = CreateObject("Scripting.FileSystemObject").OpenTextFile(this.tmpfile, iomode, true, TristateTrue);
|
this.recorder = createFSO().OpenTextFile(this.tmpfile, iomode, true, TristateTrue);
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -215,7 +225,7 @@ function PipeIPC() {
|
||||||
|
|
||||||
// define functions
|
// define functions
|
||||||
var StripAdoBOM = function(adoObj) {
|
var StripAdoBOM = function(adoObj) {
|
||||||
var newAdoObj = CreateObject("ADODB.Stream");
|
var newAdoObj = createADO();
|
||||||
newAdoObj.Type = adTypeBinary;
|
newAdoObj.Type = adTypeBinary;
|
||||||
newAdoObj.Mode = adModeReadWrite;
|
newAdoObj.Mode = adModeReadWrite;
|
||||||
newAdoObj.Open();
|
newAdoObj.Open();
|
||||||
|
@ -229,7 +239,7 @@ function PipeIPC() {
|
||||||
while (!isCommited) {
|
while (!isCommited) {
|
||||||
try {
|
try {
|
||||||
// Open a temporary file
|
// Open a temporary file
|
||||||
var fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForReading, true, TristateTrue);
|
var fso = createFSO().OpenTextFile(src, ForReading, true, TristateTrue);
|
||||||
var text = fso.ReadAll();
|
var text = fso.ReadAll();
|
||||||
var sentences = text.split(this.delimiter);
|
var sentences = text.split(this.delimiter);
|
||||||
var newSentences = [], str = '';
|
var newSentences = [], str = '';
|
||||||
|
@ -245,7 +255,7 @@ function PipeIPC() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert UTF-16 BOM to UTF-8
|
// Convert UTF-16 BOM to UTF-8
|
||||||
var ado = CreateObject("ADODB.Stream");
|
var ado = createADO();
|
||||||
ado.Type = adTypeText;
|
ado.Type = adTypeText;
|
||||||
ado.Charset = charset;
|
ado.Charset = charset;
|
||||||
ado.Open();
|
ado.Open();
|
||||||
|
@ -255,12 +265,12 @@ function PipeIPC() {
|
||||||
ado.Close();
|
ado.Close();
|
||||||
|
|
||||||
// Write a new temporary file
|
// Write a new temporary file
|
||||||
/*
|
if (this.maxSentences > 0) {
|
||||||
var _fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForWriting, true, TristateTrue);
|
var handler = createFSO().OpenTextFile(src, ForWriting, true, TristateTrue);
|
||||||
_fso.Write(str);
|
handler.Write(str);
|
||||||
_fso.Close();
|
handler.Close();
|
||||||
*/
|
}
|
||||||
|
|
||||||
// Close the temporary file
|
// Close the temporary file
|
||||||
fso.Close();
|
fso.Close();
|
||||||
|
|
||||||
|
@ -322,19 +332,19 @@ function PipeIPC() {
|
||||||
|
|
||||||
this.destroy = function() {
|
this.destroy = function() {
|
||||||
this.close();
|
this.close();
|
||||||
CreateObject("Scripting.FileSystemObject").DeleteFile(this.path);
|
createFSO().DeleteFile(this.path);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
this.readTextFromFile = function(filename) {
|
this.readTextFromFile = function(filename) {
|
||||||
var text = '';
|
var text = '';
|
||||||
var isLoaded = false;
|
var isLoaded = false;
|
||||||
var isFileExists = CreateObject("Scripting.FileSystemObject").FileExists(filename);
|
var isFileExists = createFSO().FileExists(filename);
|
||||||
if (isFileExists) {
|
if (isFileExists) {
|
||||||
//console.info("File", filename, "exists");
|
//console.info("File", filename, "exists");
|
||||||
while (!isLoaded) {
|
while (!isLoaded) {
|
||||||
try {
|
try {
|
||||||
var ado = CreateObject("ADODB.Stream");
|
var ado = createADO();
|
||||||
ado.CharSet = this.charset;
|
ado.CharSet = this.charset;
|
||||||
ado.Open();
|
ado.Open();
|
||||||
ado.LoadFromFile(filename);
|
ado.LoadFromFile(filename);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user