Update lib/pipe-ipc, lib/xml

This commit is contained in:
Namhyeon Go 2024-06-10 07:13:45 +09:00
parent 46eb8e6609
commit 14e87fbe63
2 changed files with 64 additions and 65 deletions

View File

@ -94,46 +94,73 @@ function makeProbabilityBit(p) {
return !( p > 0.0 ? ( (randomize() / p) > 1.0 ) : true) ? 1 : 0;
}
function createFSO() {
return CreateObject("Scripting.FileSystemObject");
}
function createADO() {
return CreateObject("ADODB.Stream");
function makeInterface(i) {
return CreateObject(["Scripting.FileSystemObject", "ADODB.Stream"][i]);
}
function openTextFile(filename, iomode) {
return createFSO().OpenTextFile(filename, iomode, true, TristateTrue);
return makeInterface(0).OpenTextFile(filename, iomode, true, TristateTrue);
}
function checkFileExists(filename) {
return createFSO().FileExists(filename);;
return makeInterface(0).FileExists(filename);;
}
function deleteFile(filename) {
if (checkFileExists(filename)) createFSO().DeleteFile(filename);
if (checkFileExists(filename)) makeInterface(0).DeleteFile(filename);
}
function getFileSize(filename) {
try {
return createFSO().GetFile(filename).Size;
return makeInterface(0).GetFile(filename).Size;
} catch (e) {
return -1;
}
}
function AdoConvertToStream(ado, position) {
position = (position !== "undefined" ? position : 0);
function Converter() {
this.value = null;
var newAdo = createADO();
newAdo.Type = adTypeBinary;
newAdo.Mode = adModeReadWrite;
newAdo.Open();
ado.Position = position;
ado.CopyTo(newAdo);
ado.Flush();
ado.Close();
return newAdo;
this.from = function(value) {
this.value = value;
};
this.getBinaryFromText = function() {
var stream = makeInterface(1);
stream.Type = adTypeText;
stream.CharSet = CdoCharset.CdoUTF_8;
stream.Open();
stream.WriteText(this.value);
stream.Position = 0;
stream.Type = adTypeBinary;
return stream.Read();
};
this.getTextFromBinary = function() {
var stream = makeInterface(1);
stream.Type = adTypeBinary;
stream.Open();
stream.Write(this.value);
stream.Position = 0;
stream.Type = adTypeText;
stream.CharSet = CdoCharset.CdoUTF_8;
return stream.ReadText();
};
this.repositionObject = function(stream, position) {
position = (position !== "number" ? 0 : position);
var _stream = makeInterface(1);
_stream.Type = adTypeBinary;
_stream.Mode = adModeReadWrite;
_stream.Open();
stream.Position = position;
stream.CopyTo(_stream);
stream.Flush();
stream.Close();
return _stream;
};
}
function PipeIPC() {
@ -368,14 +395,14 @@ function PipeIPC() {
}
// Convert UTF-16 BOM to a character set
var ado = createADO();
ado.Type = adTypeText;
ado.Charset = charset;
ado.Open();
ado.WriteText(str);
ado = AdoConvertToStream(ado, 3);
ado.SaveToFile(dst, adSaveCreateOverWrite);
ado.Close();
var stream = makeInterface(1);
stream.Type = adTypeText;
stream.Charset = charset;
stream.Open();
stream.WriteText(str);
stream = (new Converter()).repositionObject(stream, 3);
stream.SaveToFile(dst, adSaveCreateOverWrite);
stream.Close();
// Set a result
isCommited = true;
@ -463,7 +490,7 @@ function PipeIPC() {
var isRead = false;
while (!isRead) {
try {
var ado = createADO();
var ado = makeInterface(1);
ado.Charset = charset;
ado.Open();
ado.LoadFromFile(filename);
@ -504,6 +531,7 @@ exports.connect = function(path, callback) {
return pipe.connect(path, callback);
};
exports.Converter = Converter;
exports.UUIDv4 = UUIDv4;
exports.CRC32 = CRC32;
@ -521,7 +549,7 @@ exports.adSaveCreateNotExist = adSaveCreateNotExist;
exports.adSaveCreateOverWrite = adSaveCreateOverWrite;
exports.adModeReadWrite = adModeReadWrite;
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.22";
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.23";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = require;

View File

@ -1,37 +1,8 @@
////////////////////////////////////////////////////////////////////////
// XML API
// XML interface
////////////////////////////////////////////////////////////////////////
var FILE = require("lib/file");
var adTypeText = 2;
var adTypeBinary = 1;
var StreamConvert = function(data) {
this.data = data;
this.toBinary = function() {
var BinaryStream = CreateObject("ADODB.Stream");
BinaryStream.Type = adTypeText;
BinaryStream.CharSet = FILE.CdoCharset.CdoUTF_8;
BinaryStream.Open();
BinaryStream.WriteText(this.data);
BinaryStream.Position = 0;
BinaryStream.Type = adTypeBinary;
BinaryStream.Position = 0;
return BinaryStream.Read();
};
this.toString = function() {
var BinaryStream = CreateObject("ADODB.Stream");
BinaryStream.Type = adTypeBinary;
BinaryStream.Open();
BinaryStream.Write(this.data);
BinaryStream.Position = 0;
BinaryStream.Type = adTypeText;
BinaryStream.CharSet = FILE.CdoCharset.CdoUTF_8;
return BinaryStream.ReadText();
};
};
var PipeIPC = require("lib/pipe-ipc");
var XMLObject = function(dom) {
this.filename = null;
@ -120,7 +91,7 @@ var XMLObject = function(dom) {
var node = dom.createElement("XMLNode");
node.dataType = type;
node.nodeTypedValue = (new StreamConvert(value)).toBinary();
node.nodeTypedValue = (new PipeIPC.Converter()).from(value).getBinaryFromText();
return node.text;
} catch (e) {
@ -136,7 +107,7 @@ var XMLObject = function(dom) {
node.dataType = type;
node.text = value;
return (new StreamConvert(node.nodeTypedValue)).toString();
return (new PipeIPC.Converter()).from(node.nodeTypedValue).getTextFromBinary();
} catch (e) {
console.error("XMLObject->decode():", e.message);
}
@ -164,6 +135,6 @@ exports.load = load;
exports.encode = encode;
exports.decode = decode;
exports.VERSIONINFO = "XML interface (xml.js) version 0.2.3";
exports.VERSIONINFO = "XML interface (xml.js) version 0.2.4";
exports.global = global;
exports.require = global.require;