mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-05-09 13:16:05 +00:00
Update pipe-ipc.js
This commit is contained in:
parent
145be2a894
commit
4a3d044f02
|
@ -54,9 +54,8 @@ var CdoUTF_7 = "utf-7";
|
||||||
var CdoUTF_8 = "utf-8";
|
var CdoUTF_8 = "utf-8";
|
||||||
|
|
||||||
function PipeIPC() {
|
function PipeIPC() {
|
||||||
var EOL = "\r\n";
|
|
||||||
|
|
||||||
this.path = "data\\.pipe_:pipename";
|
this.path = "data\\.pipe_:pipename";
|
||||||
|
this.delimiter = "\r\n";
|
||||||
this.reader = null;
|
this.reader = null;
|
||||||
this.writer = null;
|
this.writer = null;
|
||||||
this.recorder = null;
|
this.recorder = null;
|
||||||
|
@ -65,6 +64,7 @@ function PipeIPC() {
|
||||||
this.charset = CdoUTF_8;
|
this.charset = CdoUTF_8;
|
||||||
this.lastReadTime = -1;
|
this.lastReadTime = -1;
|
||||||
this.lastWriteTime = -1;
|
this.lastWriteTime = -1;
|
||||||
|
this.maxSentences = 0;
|
||||||
|
|
||||||
this.getCurrentTime = function() {
|
this.getCurrentTime = function() {
|
||||||
return new Date().getTime();
|
return new Date().getTime();
|
||||||
|
@ -74,6 +74,14 @@ function PipeIPC() {
|
||||||
this.charset = charset;
|
this.charset = charset;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.setDelimiter = function(delimiter) {
|
||||||
|
this.delimiter = delimiter;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.setMaxSentences = function(maxSentences) {
|
||||||
|
this.maxSentences = maxSentences;
|
||||||
|
};
|
||||||
|
|
||||||
this.connect = function(pipename, callback) {
|
this.connect = function(pipename, callback) {
|
||||||
this.path = this.path.replace(":pipename", pipename);
|
this.path = this.path.replace(":pipename", pipename);
|
||||||
//this.openWriter();
|
//this.openWriter();
|
||||||
|
@ -141,7 +149,7 @@ function PipeIPC() {
|
||||||
};
|
};
|
||||||
|
|
||||||
this._write = function(message) {
|
this._write = function(message) {
|
||||||
this.writer.Write(message + EOL);
|
this.writer.Write(message + this.delimiter);
|
||||||
};
|
};
|
||||||
|
|
||||||
this.write = function(message) {
|
this.write = function(message) {
|
||||||
|
@ -172,11 +180,12 @@ function PipeIPC() {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.record = function(message) {
|
this.record = function(message) {
|
||||||
|
var _this = this; // Because of the variable scope
|
||||||
var commit = function(src, dst, charset) {
|
var commit = function(src, dst, charset) {
|
||||||
// Open a temporary file
|
var isCommited = false;
|
||||||
var fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForReading, true, TristateTrue);
|
|
||||||
var str = fso.ReadAll();
|
// define functions
|
||||||
var StripBOM = function(adoObj) {
|
var StripAdoBOM = function(adoObj) {
|
||||||
var newAdoObj = CreateObject("ADODB.Stream");
|
var newAdoObj = CreateObject("ADODB.Stream");
|
||||||
newAdoObj.Type = adTypeBinary;
|
newAdoObj.Type = adTypeBinary;
|
||||||
newAdoObj.Mode = adModeReadWrite;
|
newAdoObj.Mode = adModeReadWrite;
|
||||||
|
@ -184,20 +193,52 @@ function PipeIPC() {
|
||||||
adoObj.Position = 3;
|
adoObj.Position = 3;
|
||||||
adoObj.CopyTo(newAdoObj);
|
adoObj.CopyTo(newAdoObj);
|
||||||
adoObj.Flush();
|
adoObj.Flush();
|
||||||
//adoObj.Close();
|
adoObj.Close();
|
||||||
return newAdoObj;
|
return newAdoObj;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
while (!isCommited) {
|
||||||
|
try {
|
||||||
|
// Open a temporary file
|
||||||
|
var fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForReading, true, TristateTrue);
|
||||||
|
var text = fso.ReadAll();
|
||||||
|
var sentences = text.split(_this.delimiter);
|
||||||
|
var newSentences = [], str = '';
|
||||||
|
|
||||||
|
// if enabled "maxSentences" feature
|
||||||
|
if (_this.maxSentences > 0) {
|
||||||
|
while (sentences.length > 0 && newSentences.length < _this.maxSentences) {
|
||||||
|
newSentences.push(sentences.pop());
|
||||||
|
}
|
||||||
|
str = newSentences.reverse().join(_this.delimiter);
|
||||||
|
} else {
|
||||||
|
str = text;
|
||||||
|
}
|
||||||
|
|
||||||
// Convert UTF-16 BOM to UTF-8
|
// Convert UTF-16 BOM to UTF-8
|
||||||
var ado = CreateObject("ADODB.Stream");
|
var ado = CreateObject("ADODB.Stream");
|
||||||
ado.Type = adTypeText;
|
ado.Type = adTypeText;
|
||||||
ado.Charset = charset;
|
ado.Charset = charset;
|
||||||
ado.Open();
|
ado.Open();
|
||||||
ado.WriteText(str);
|
ado.WriteText(str);
|
||||||
StripBOM(ado).SaveToFile(dst, adSaveCreateOverWrite);
|
ado = StripAdoBOM(ado);
|
||||||
//ado.SaveToFile(savefile, adSaveCreateOverWrite);
|
ado.SaveToFile(dst, adSaveCreateOverWrite);
|
||||||
ado.Close();
|
ado.Close();
|
||||||
|
|
||||||
|
/*
|
||||||
|
// Write a new temporary file
|
||||||
|
var _fso = CreateObject("Scripting.FileSystemObject").OpenTextFile(src, ForWriting, true, TristateTrue);
|
||||||
|
_fso.Write(str);
|
||||||
|
_fso.Close();
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Set a result
|
||||||
|
isCommited = true;
|
||||||
|
} catch (e) {
|
||||||
|
isCommited = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Close a temporary file
|
// Close a temporary file
|
||||||
fso.Close();
|
fso.Close();
|
||||||
};
|
};
|
||||||
|
@ -264,7 +305,6 @@ function PipeIPC() {
|
||||||
this.closeReader();
|
this.closeReader();
|
||||||
this.closeRecorder();
|
this.closeRecorder();
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.create = function() {
|
exports.create = function() {
|
||||||
|
@ -278,8 +318,9 @@ exports.connect = function(path, callback) {
|
||||||
exports.ForReading = ForReading;
|
exports.ForReading = ForReading;
|
||||||
exports.ForWriting = ForWriting;
|
exports.ForWriting = ForWriting;
|
||||||
exports.ForAppending = ForAppending;
|
exports.ForAppending = ForAppending;
|
||||||
|
exports.CdoUTF_8 = CdoUTF_8;
|
||||||
|
|
||||||
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.4";
|
exports.VERSIONINFO = "PIPE-based IPC Module (pipe-ipc.js) version 0.1.5";
|
||||||
exports.AUTHOR = "abuse@catswords.net";
|
exports.AUTHOR = "abuse@catswords.net";
|
||||||
exports.global = global;
|
exports.global = global;
|
||||||
exports.require = require;
|
exports.require = require;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user