Update pipe-ipc.js

This commit is contained in:
Namhyeon Go 2023-09-21 16:53:58 +09:00 committed by GitHub
parent 88482f5ba8
commit 34668ad54e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -93,6 +93,18 @@ function createADO() {
return CreateObject("ADODB.Stream");
}
function openTextFile(filename, iomode) {
return createFSO().OpenTextFile(filename, iomode, true, TristateTrue);
}
function checkFileExists(filename) {
return createFSO().FileExists(filename);;
}
function deleteFile(filename) {
if (checkFileExists(filename)) createFSO().DeleteFile(filename);
}
function AdoConvertToStream(ado, position) {
position = (position !== "undefined" ? position : 0);
@ -109,7 +121,7 @@ function AdoConvertToStream(ado, position) {
function PipeIPC() {
this.path = "data\\.pipe_:pipename";
this.delimiter = '\r\n';
this.delimiter = "\r\n";
this.reader = null;
this.writer = null;
this.recorder = null;
@ -153,7 +165,7 @@ function PipeIPC() {
this.openWriter = function(iomode) {
while (this.writer == null) {
try {
this.writer = createFSO().OpenTextFile(this.path, iomode, true, TristateTrue);
this.writer = openTextFile(this.path, iomode);
} catch (e) {}
}
};
@ -168,7 +180,7 @@ function PipeIPC() {
this.openReader = function() {
while (this.reader == null) {
try {
this.reader = createFSO().OpenTextFile(this.path, ForReading, true, TristateTrue);
this.reader = openTextFile(this.path, ForReading);
} catch (e) {}
}
};
@ -187,14 +199,14 @@ function PipeIPC() {
this.tmpfile = this.savefile + ".tmp";
// read a text from save file
var isExistsSaveFile = createFSO().FileExists(this.savefile);
var isExistsTmpFile = createFSO().FileExists(this.tmpfile);
var isExistsSaveFile = checkFileExists(this.savefile);
var isExistsTmpFile = checkFileExists(this.tmpfile);
while (isExistsSaveFile && !isExistsTmpFile) {
try {
var fso = createFSO().OpenTextFile(this.tmpfile, ForWriting, true, TristateTrue);
var fso = openTextFile(this.tmpfile, ForWriting);
fso.Write(this.readTextFromFile(this.savefile));
fso.Close();
isExistsTmpFile = createFSO().FileExists(this.tmpfile);
isExistsTmpFile = checkFileExists(this.tmpfile);
} catch (e) {
isExistsTmpFile = false;
}
@ -212,7 +224,7 @@ function PipeIPC() {
this.openRecorder = function(iomode) {
while (this.recorder == null) {
try {
this.recorder = createFSO().OpenTextFile(this.tmpfile, iomode, true, TristateTrue);
this.recorder = openTextFile(this.tmpfile, iomode);
} catch (e) {}
}
};
@ -285,8 +297,9 @@ function PipeIPC() {
while (!isCommited) {
try {
// Open a temporary file
var fso = createFSO().OpenTextFile(src, ForReading, true, TristateTrue);
var fso = openTextFile(src, ForReading);
var text = fso.ReadAll();
fso.Close(); // close the file immediately
var sentences = text.split(this.delimiter);
var newSentences = [], str = '';
@ -296,6 +309,19 @@ function PipeIPC() {
newSentences.push(sentences.pop());
}
str = newSentences.reverse().join(this.delimiter);
// Write a new temporary file
var done = false;
while (!done) {
try {
var _fso = openTextFile(src, ForWriting);
_fso.Write(str);
_fso.Close();
done = true;
} catch (e) {
done = false;
}
}
} else {
str = text;
}
@ -310,18 +336,6 @@ function PipeIPC() {
ado.SaveToFile(dst, adSaveCreateOverWrite);
ado.Close();
// Write a new temporary file
/*
if (this.maxSentences > 0) {
var handler = createFSO().OpenTextFile(src, ForWriting, true, TristateTrue);
handler.Write(str);
handler.Close();
}
*/
// Close the temporary file
fso.Close();
// Set a result
isCommited = true;
} catch (e) {
@ -380,13 +394,13 @@ function PipeIPC() {
this.destroy = function() {
this.close();
createFSO().DeleteFile(this.path);
deleteFile(this.path);
};
this.readTextFromFile = function(filename, charset) {
var text = '';
var isFileExists = createFSO().FileExists(filename);
var isFileExists = checkFileExists(filename);
if (isFileExists) {
var isRead = false;
while (!isRead) {
@ -445,7 +459,7 @@ exports.adSaveCreateNotExist = adSaveCreateNotExist;
exports.adSaveCreateOverWrite = adSaveCreateOverWrite;
exports.adModeReadWrite = adModeReadWrite;
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.12";
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.13";
exports.AUTHOR = "abuse@catswords.net";
exports.global = global;
exports.require = require;