Saturday, March 08, 2014

[TCL] Love that Tool Command Language

TCL is just so cool and concise …
cd CSV
set target "snap-[clock format [clock add [clock seconds] -1 hours] -format %Y-%m-%d-%H -gmt 1]"
if {![file exists $target.rar]} then {
    set cmd [list c:/program\ files/winrar/rar.exe m -m5 -s ${target}.rar ${target}-utc.csv]
        exec -- {*}$cmd
}
That goes to the log (called CSV) folder and compresses the previous hour’s worth of log. I run that from a scheduler.

I really like the conciseness of Tcl: clock add [clock seconds] -1 hours, which subtracts an hour from the current time and clock format which takes that and renders it as GMT.

© Copyright Bruce M. Axtens, 2014

Thursday, February 27, 2014

[VBScript] Boosting myself to HIGH process priority

The script gets its own name, then looks through the Win32_Processes for a match with CSCRIPT.EXE and a CommandLine containing that name. Any matches (should be only one, but could be more) are boosted to HIGH process priority.

This demonstrates WMI calls from both VBScript and JScript. Note the need to explicitly define an Enumerator in the JScript version.
Option Explicit

Dim sName
Dim sComputer
Dim oWMI
Dim cProcesses
Dim oProcess

Const HIGH = 256
sComputer = "."
Set oWMI = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
sName = WScript.ScriptName

Set cProcesses = oWMI.ExecQuery("Select * from Win32_Process Where Name = 'cscript.exe' And CommandLine LIKE '%" & sName & "%'")
For Each oProcess In cProcesses
  oProcess.SetPriority(HIGH) 
  WScript.Echo "Boosted myself"
Next
And in JScript
var HIGH = 256;
var sComputer = ".";
var sName = WScript.ScriptName;    

var query = GetObject("winmgmts:\\\\" + sComputer + "\\root\\cimv2")
    .ExecQuery("Select * from Win32_Process " + 
    "Where Name = 'cscript.exe' And CommandLine LIKE '%" + sName + "%'")

// Enumerate WMI objects
var cProcesses = new Enumerator(query);

for ( ; !cProcesses.atEnd(); cProcesses.moveNext()) { 
    var oProcess = cProcesses.item()
    oProcess.Priority = HIGH
    WScript.Echo( "Boosted myself")
}
© Copyright Bruce M. Axtens, 2014

Tuesday, February 25, 2014

[VBScript] How can I display 64 bit double number using VBScript on 32 Bit OS?

"How can I display 64 bit double number using VBScript on 32 Bit OS?" That was the question on StackOverflow back at the end of 2012. And nobody had answered it. What a challenge!

I assumed a pure-VBScript solution was needed, and set about finding and implementing a Very Large Integer class with which I could then implement a Hex64 to integer function. A workable candidate function was found on Rosetta Code in the
Liberty BASIC solution to the Long Multiplication task.

The code for the class is as follows. It's pretty much the same as the original.

Option Explicit
Class VeryLongInteger
  'http://rosettacode.org/wiki/Long_Multiplication#Liberty_BASIC
  Public Function MULTIPLY(Str_A, Str_B)
    Dim signA, signB, sResult, Str_Shift, i, d, Str_T
    signA = 1
    If Left(Str_A,1) = "-" Then 
      Str_A = Mid(Str_A,2)
      signA = -1
    End If
    signB = 1
    If Left(Str_B,1) = "-" Then 
      Str_B = Mid(Str_B,2)
      signB = -1
    End If
    sResult = vbNullString
    Str_T = vbNullString
    Str_shift = vbNullString
    For i = Len(Str_A) To 1 Step -1
      d = CInt(Mid(Str_A,i,1))
      Str_T = MULTBYDIGIT(Str_B, d)
      sResult = ADD(sResult, Str_T & Str_shift)
      Str_shift = Str_shift & "0"
      'print d, Str_T, sResult 
    Next
    If signA * signB < 0 Then sResult = "-" + sResult
    'print sResult
    MULTIPLY = sResult
  End Function
  
  Private Function MULTBYDIGIT(Str_A, d)
    Dim sResult, carry, i, a, c
    'multiply Str_A by digit d
    sResult = vbNullString
    carry = 0
    For i = Len(Str_A) To 1 Step -1
      a = CInt(Mid(Str_A,i,1))
      c = a * d + carry
      carry = c \ 10
      c = c Mod 10
      'print a, c
      sResult = CStr(c) & sResult 
    Next
    If carry > 0 Then sResult = CStr(carry) & sResult
    'print sResult
    MULTBYDIGIT = sResult
  End Function
  
  Public Function ADD(Str_A, Str_B)
    Dim L, sResult, carry, i, a, b, c
    'add Str_A + Str_B, for now only positive
    l = MAX(Len(Str_A), Len(Str_B))
    Str_A=PAD(Str_A,l)
    Str_B=PAD(Str_B,l)
    sResult = vbNullString 'result
    carry = 0
    For i = l To 1 Step -1
      a = CInt(Mid(Str_A,i,1))
      b = CInt(Mid(Str_B,i,1))
      c = a + b + carry
      carry = Int(c/10)
      c = c Mod 10
      'print a, b, c
      sResult = CStr(c) & sResult
    Next
    If carry>0 Then sResult = CStr(carry) & sResult
    'print sResult
    ADD = sResult
  End Function
  
  Private Function Max(a,b)
    If a > b Then
      Max = a
    Else
      Max = b
    End If
  End Function
  
  Private Function pad(a,n)  'pad from right with 0 to length n
    Dim sResult
    sResult = a
    While Len(sResult) < n
      sResult = "0" & sResult
    Wend
    pad = sResult
  End Function
End Class
With that defined I have now all I need to implement a Hex64 function. This I did in two forms:

* A memoized version which precomputes all the relevant powers of 16

Function PrecomputedFromHex64(sHex)
  Dim VLI
  Set VLI = New VeryLongInteger
  
  Dim Sixteen(16)
  Sixteen(0) = "1"
  Sixteen(1) = "16"
  Sixteen(2) = VLI.MULTIPLY(Sixteen(1),"16")
  Sixteen(3) = VLI.MULTIPLY(Sixteen(2),"16")
  Sixteen(4) = VLI.MULTIPLY(Sixteen(3),"16")
  Sixteen(5) = VLI.MULTIPLY(Sixteen(4),"16")
  Sixteen(6) = VLI.MULTIPLY(Sixteen(5),"16")
  Sixteen(7) = VLI.MULTIPLY(Sixteen(6),"16")
  Sixteen(8) = VLI.MULTIPLY(Sixteen(7),"16")
  Sixteen(9) = VLI.MULTIPLY(Sixteen(8),"16")
  Sixteen(10) = VLI.MULTIPLY(Sixteen(9),"16")
  Sixteen(11) = VLI.MULTIPLY(Sixteen(10),"16")
  Sixteen(12) = VLI.MULTIPLY(Sixteen(11),"16")
  Sixteen(13) = VLI.MULTIPLY(Sixteen(12),"16")
  Sixteen(14) = VLI.MULTIPLY(Sixteen(13),"16")
  Sixteen(15) = VLI.MULTIPLY(Sixteen(14),"16")
  
  Dim theAnswer, i, theDigit, theMultiplier, thePower, aPower
  theAnswer = "0"
  aPower = 0
  For i = Len(sHex) To 1 Step -1
    theDigit = UCase(Mid(sHex,i,1))
    theMultiplier = InStr("0123456789ABCDEF",theDigit)-1
    thePower = Sixteen(aPower)
    thePower = VLI.MULTIPLY(CStr(theMultiplier),thePower)
    theAnswer = VLI.ADD(theAnswer,thePower )
    aPower = aPower + 1
  Next
  PrecomputedFromHex64 = theAnswer
End Function
* A non-memoized version which computes the relevant power when it is needed.
Function FromHex64(sHex)
  Dim VLI
  Set VLI = New VeryLongInteger       
  Dim theAnswer, i, theDigit, theMultiplier, thePower, aPower
  theAnswer = "0"
  thePower = "1"
  For i = Len(sHex) To 1 Step -1
    theDigit = UCase(Mid(sHex,i,1))
    theMultiplier = InStr("0123456789ABCDEF",theDigit)-1
    theAnswer = VLI.ADD(theAnswer,VLI.MULTIPLY(thePower,theMultiplier))
    thePower = VLI.MULTIPLY(thePower,"16")
  Next
  FromHex64 = theAnswer
End Function
The memoized version is slightly faster than the non-memoized, despite the overhead of the memoization. If the class/function pair were to be used a lot in a script, one might consider making both the VLI instantiation and the Sixteen() array global and precomputing the array at the beginning of the script rather than on each invocation.

A rough test of the code follows:

Const test = "FFFFFFFFFFFFFFFF" '"41417724EBA8953E"
Dim t, I, S
t=Timer
For I = 1 To 100
  S = FromHex64(test)
Next
WScript.Echo "No memoization", Timer-t

t=Timer
For I = 1 To 100
  S = PrecomputedFromHex64(test)
Next
WScript.Echo "Memoized each time",Timer-t

Function GlobalMemoFromHex64(sHex)  
  Dim theAnswer, i, theDigit, theMultiplier, thePower, aPower
  theAnswer = "0"
  aPower = 0
  For i = Len(sHex) To 1 Step -1
    theDigit = UCase(Mid(sHex,i,1))
    theMultiplier = InStr("0123456789ABCDEF",theDigit)-1
    thePower = Sixteen(aPower)
    thePower = VLI.MULTIPLY(CStr(theMultiplier),thePower)
    theAnswer = VLI.ADD(theAnswer,thePower )
    aPower = aPower + 1
  Next
  GlobalMemoFromHex64 = theAnswer
End Function

Dim VLI
Set VLI = New VeryLongInteger

Dim Sixteen(16)
Sixteen(0) = "1"
Sixteen(1) = "16"
Sixteen(2) = VLI.MULTIPLY(Sixteen(1),"16")
Sixteen(3) = VLI.MULTIPLY(Sixteen(2),"16")
Sixteen(4) = VLI.MULTIPLY(Sixteen(3),"16")
Sixteen(5) = VLI.MULTIPLY(Sixteen(4),"16")
Sixteen(6) = VLI.MULTIPLY(Sixteen(5),"16")
Sixteen(7) = VLI.MULTIPLY(Sixteen(6),"16")
Sixteen(8) = VLI.MULTIPLY(Sixteen(7),"16")
Sixteen(9) = VLI.MULTIPLY(Sixteen(8),"16")
Sixteen(10) = VLI.MULTIPLY(Sixteen(9),"16")
Sixteen(11) = VLI.MULTIPLY(Sixteen(10),"16")
Sixteen(12) = VLI.MULTIPLY(Sixteen(11),"16")
Sixteen(13) = VLI.MULTIPLY(Sixteen(12),"16")
Sixteen(14) = VLI.MULTIPLY(Sixteen(13),"16")
Sixteen(15) = VLI.MULTIPLY(Sixteen(14),"16")

t=Timer
For I = 1 To 100
  S = GlobalMemoFromHex64(test)
Next
WScript.Echo "Global memo",Timer-t
Running the above in VBSEdit a few times under varying conditions gave the following results:
No memoization 2.632813
Memoized each time 1.023438
Global memo 0.328125

No memoization 1.667969
Memoized each time 0.8867188
Global memo 0.2695313

No memoization 1.488281
Memoized each time 0.9335938
Global memo 0.3046875
The global precomputation of the memo array is fastest technique, but if you're only using the function once you'll get by with the non-memo version.

Further optimisations are possible. Compilation, of course, would make it even faster.

Enjoy!
© Copyright Bruce M. Axtens, 2014

Wednesday, September 11, 2013

[Open Source] Github and Bitbucket

The blog's been very quiet lately. Too quiet. But I've been busy.

I got started on
Github, but the only thing I have there is a failed attempt at building a JSON library for the Euphoria programming language.

Most of the more recent stuff is happening on Bitbucket where I have various implementations (javascript v1 and javascript v2 (minimal), euphoria, vb6, php, and c#) of Steve Skiena's integer bignums library and a few other bits and bobs, namely:


All projects are in varying stages of completeness, and looking for users and improvers.


© Copyright Bruce M. Axtens, 2013

Friday, June 21, 2013

[JScript.NET --> C#.NET] Goodbye JScript.NET; Hello C#.NET

I'd better make it official: I've given up on JScript.NET (and to a lesser extent, JScript Classic.) There's just not enough support for it in current tools and IDEs.

So for the past couple of months I've been converting all my server-side javascripts (predominantly in JScript Classic) to C#.

I've also had one of my periodic clean-outs. Am trying to limit myself to a smaller set of programming languages for work and for play. That, and try to catch up on some of the things I've promised to others, like how to do web-scripting in Lhogho.


© Bruce M. Axtens, 2013

Friday, September 21, 2012

[JScript] Another Config library


I've been doing a fair bit of JScript of late, and as I find working with Registry for runtime configuration a bit tiresome, I've cooked up a Config object. It's defined using the prototype approach to object definition. Config files are, in this case, ANSI or Unicode (UTF16LE) files, in the form
key=value
Creating an instance of the object involves passing in the name of the config file. I've hit upon the following technique for tying the config file to the script, using the FileSystemObject and the WScript object.
var oFSO = new ActiveXObject("Scripting.FileSystemObject"),
    sHere = oFSO.GetParentFolderName(WScript.ScriptFullName),
    sConfig = oFSO.BuildPath(sHere, 
        oFSO.GetBaseName(WScript.ScriptName) + ".config");
That way one may always be sure that (for example) Melchizedek.js will have a Melchizedek.config next to it.

First, the main function

function Config(sFile) {
    this.kind = "";
    this.oFSO = new ActiveXObject("Scripting.FileSystemObject");
    this.text = "";
    var resp = [];
    this.file = sFile;
    if (this.oFSO.FileExists(sFile)) {
        resp = function(oFSO, sFilename) {
            var forReading = 1;
            var asUnicode = -1;
            var asANSI = 0;
            var resp = [];
            if (oFSO.FileExists(sFilename)) {
                var handle = oFSO.OpenTextFile(sFilename, forReading, false,
                        asANSI);
                var BOM = handle.Read(2);
                handle.Close();
                if (BOM.charCodeAt(0) == 0xFF && BOM.charCodeAt(1) == 0xFE) {
                    handle = oFSO.OpenTextFile(sFilename, forReading, false,
                            asUnicode);
                    resp[0] = "unicode";
                } else {
                    handle = oFSO.OpenTextFile(sFilename, forReading, false,
                            asANSI);
                    resp[0] = "ansi";
                }
                resp[1] = handle.ReadAll();
                handle.Close();
                return resp;
            } else {
                return resp;
            }
        }(this.oFSO, sFile);
        this.kind = resp[0];
        this.text = resp[1];
        this.name = sFile;
        this.filefound = true;
    } else {
        this.filefound = false;
    }
    return this.filefound;
}
Note that the code, in an attempt to deal appropriately with config files being in ANSI or UTF-16LE format, reads the first two bytes of the config file. If the UTF16LE BOM is there, it reads the file as Unicode. Otherwise, it reads as ANSI.

A few other things are set so that the other methods will work, namely for exists, name and kind.

Config.prototype.name = function() {
        return this.name;
};

Config.prototype.exists = function() {
        return this.filefound;
};

Config.prototype.kind = function () {
        return this.kind;
};
Next come the main workhorses of the object: define, retrieve and save.
Config.prototype.retrieve = function(sName) {
        if (arguments.length > 1) {
                var sDefault = arguments[1];
        } else {
                sDefault = null;
        }

        var re = new RegExp("^" + sName + "=(.*?)$", "m");
        var arr = re.exec(this.text);
        if (arr === null) {
                return sDefault;
        } else {
                return RegExp.$1;
        }
};

Config.prototype.define = function(sName, sValue) {
        var sNew = sName + "=" + sValue;
        var re = new RegExp("^" + sName + "=(.*?)$", "m");
        var arr = re.exec(this.text);
        if (arr === null) {
                this.text = this.text + "\n" + sNew;
        } else {
                this.text = this.text.replace(re, sNew);
        }
};

Config.prototype.save = function() {
        var sFile = "";
        var handle = "";
        // allow for save to a different file
        if (arguments.length > 0) {
                sFile = arguments[0];
        } else {
                sFile = this.file;
        }
        if (this.kind === "ansi") {
                handle = this.oFSO.CreateTextFile(sFile, true, false);
        } else {
                handle = this.oFSO.CreateTextFile(sFile, true, true);                
        }
        handle.Write(this.text);
        handle.Close();
};
retrieve accepts two parameters: the key in the config store, and the default (in the event that the key is not found.) Rather than using a Scripting.Dictionary, the config data is stored as a string, and regular expressions are used to extract and update it.

define accepts two parameters: the key in the config file, and the value to be associated with it. If the key is not found in the stored keys and values, it is appended to it.

save accepts one optional parameter: the name of the file into which to write the stored string of keys and values. If no name is given, the filename given when Config() was first called is used.

The code may be used as follows:

var c = new Config("dog.cfg");
'the define() will either add or update the store to 
'    sound=bark
c.define("sound","bark");
'the save() will write the store to dog.cfg
c.save();

'Config reloads dog.cfg
var c = new Config("dog.cfg");
'if "sound" is a key in dog.cfg, the associated value will be displayed
'otherwise the default.
WScript.Echo(c.retrieve("sound","woof"));

'display the kind (ansi or unicode)
WScript.Echo(c.kind);
'... whether the file exists
WScript.Echo(c.exists());
'... and what its name is
WScript.Echo(c.name);
The result of running the above code (from within SciTE):
>cscript /nologo Config.js
 bark
 unicode
 -1
 dog.cfg
 >Exit code: 0
I have recently pushed this code through Google's Closure Compiler and the difference in code size is significant -- the result is about 50% smaller than the original. I expect it runs faster too, though I haven't bothered to check it out thoroughly.
function Config(a) {
  this.kind = "";
  this.oFSO = new ActiveXObject("Scripting.FileSystemObject");
  this.text = "";
  var b = [];
  this.file = a;
  if(this.oFSO.FileExists(a)) {
    var b = this.oFSO, d = [];
    if(b.FileExists(a)) {
      var c = b.OpenTextFile(a, 1, !1, 0), e = c.Read(2);
      c.Close();
      255 == e.charCodeAt(0) && 254 == e.charCodeAt(1) ? (c = b.OpenTextFile(a, 1, !1, -1), d[0] = "unicode") : (c = b.OpenTextFile(a, 1, !1, 0), d[0] = "ansi");
      d[1] = c.ReadAll();
      c.Close()
    }
    b = d;
    this.kind = b[0];
    this.text = b[1];
    this.name = a;
    this.filefound = !0
  }else {
    this.filefound = !1
  }
  return this.filefound
}
Config.prototype.name = function() {
  return this.name
};
Config.prototype.exists = function() {
  return this.filefound
};
Config.prototype.kind = function() {
  return this.kind
};
Config.prototype.retrieve = function(a) {
  var b = 1 < arguments.length ? arguments[1] : null;
  return null === RegExp("^" + a + "=(.*?)$", "m").exec(this.text) ? b : RegExp.$1
};
Config.prototype.define = function(a, b) {
  var d = a + "=" + b, c = RegExp("^" + a + "=(.*?)$", "m");
  this.text = null === c.exec(this.text) ? this.text + "\n" + d : this.text.replace(c, d)
};
Config.prototype.save = function() {
  var a = "", a = "", a = 0 < arguments.length ? arguments[0] : this.file, a = "ansi" === this.kind ? this.oFSO.CreateTextFile(a, !0, !1) : this.oFSO.CreateTextFile(a, !0, !0);
  a.Write(this.text);
  a.Close()
};

Enjoy!
© Copyright Bruce M. Axtens, 2012

Saturday, July 14, 2012

[JScript.NET] Finally weaned off Scripting.FileSystemObject

Finally removed all reference to Scripting.FileSystemObject. Now we're 99% there with respect to the dotNet-ification of the Access Compress And Repair script.

I say 99% percent because I'm still using ActiveXObject() to connect to the Access.Application object. Apparently, I should be using some other technique.

So here's the code of the application thus far.
import System.Environment;
import System.IO;

var sDatabaseName : String = argNamed("name");
var sDataFolder : String = argNamed("data");
var sTempFolder : String = argNamed("temp");
var sBackupFolder : String = argNamed("backup");

if (  sDatabaseName === "" ) {
 usage();
}

if (  sDataFolder === "" ) {
 usage();
}

if (  sTempFolder  === "" ) {
 usage();
}

if (  sBackupFolder === "" ) {
 usage();
}

var sDatabaseFile : String = System.IO.Path.Combine(sDataFolder, sDatabaseName);
var sBackupFile : String = System.IO.Path.Combine(sBackupFolder, sDatabaseName);
var sTempFile : String = System.IO.Path.Combine(sTempFolder, sDatabaseName);

try {
 File.Delete( sTempFile );
} catch ( e ) {
 //~ print( e.message + ': ' + sTempFile );
}

print("CompactRepair ",sDatabaseFile," to ",sTempFile);
var oACC = new ActiveXObject("Access.Application");
oACC.CompactRepair( sDatabaseFile, sTempFile, true );
var acQuitSaveNone = 2;
oACC.Quit(acQuitSaveNone);

// copy source to backup, overwriting
try {
 File.Delete( sBackupFile );
} catch( e ) {
 //~ print( e.message + ': ' + sBackupFile );
}
print("Moving ",sDatabaseFile," to ",sBackupFile);
File.Move( sDatabaseFile, sBackupFile );

// copy temp to source, overwriting
try {
 File.Delete( sDatabaseFile );
} catch( e ) {
 //~ print( e.message + ': ' + sDatabaseFile );
}
print("Moving ",sTempFile," to ",sDatabaseFile);
File.Move( sTempFile, sDatabaseFile );
System.Environment.Exit(4);

function argNamed( sname : String ) {
 var result : String = "";
 var aCmdline = System.Environment.GetCommandLineArgs();
 var i : short = 1;
 for ( ; i < aCmdline.length; i++ ) {
  if (aCmdline[i].toLowerCase().slice(0, sname.length + 2) == ( "/" + sname.toLowerCase() + ":" )) {
   var inner : String = aCmdline[i].slice( sname.length + 2 ) 
   result = (inner.charAt(0) == '"' ? inner.slice(1,inner.length-1) : inner);
  }
 }
 return result;
}

function usage() {
 var aArgs = System.Environment.GetCommandLineArgs(); 
 print( aArgs[0] );
 print( " /name: /data: /temp: /backup:");
 System.Environment.Exit(1);
}
Enjoy!

© Copyright Bruce M. Axtens, 2012