Friday, June 29, 2012

[CMD.EXE] Console weirdness

What do you make of a command line tool that stops working and seems to wait for keyboard input, but the code doesn't have a keyboard input instruction anywhere in it? I just had a batch file (that calls two PHP files and an EXE) sit idle for 2 hours and then start working as soon as I hit a key in its window.

After digging around a bit for a solution, where should I find it but at PerlMonks:

If your cmd.exe windows have "Quick edit" mode enabled, then if you (or somebody) left-clicks in the window (say to bring it to the top of the window stack), then it can select (highlight for copy & paste), one or more characters. A side effect of this is that output to the screen is suspended until the C&P is completed by right-clicking.


And if the program is regularly outputting to the screen, then undisplayed output will accumulate until the internal buffer fills, at which point the next write attempt will block until the select state is ended. Effectively suspending the program. The select state can be ended by right-clicking in the window so completing the copy & paste operation.


It can also be ended by typing any key, which abandons the C&P and allow the blocked write to succeed and the program to continue. Ie. Exactly the symptoms you've described.


The Quick-edit mode is very useful, but it can be confusing if you are not aware of it, or its affects.
        -- Re^3: Perl suddenly pauses for key input
Weird ... can it get any weirder than this?
© Copyright Bruce M. Axtens, 2012

[Batch] Running on Low

Running on LOW priority (or any other priority class) can be done from the command line with the START command, e.g.
START /LOW SomeBatch.cmd
Alternatively, one can embed the START command with the priority class modifier within the batch file itself, viz
@echo off
 setlocal enableextensions
 if %1@==@ (
  start /low %0 running
 goto :eof
 )
 echo This instance is running at LOW priority class
 pause
 exit
One could even make the priority class one of the parameters to the call, e.g.
@echo off
 setlocal enableextensions
 set PC=%1
 if %PC%@==@ (
 echo Please specify priority class as first argument.
 goto :eof
 ) 
 if %2@==@ (
 start /%PC% %0 %1 running
 goto :eof
 )
 echo Supposedly running %PC%
 pause
 exit
The trick is to check, in the first instance, for something in the %1 slot, and in the second, in the %2 slot, with the assumption that 'nothing' means %0 is running for the first time. Note also, that no checking of meaningful priority classes is done. That's left as an exercise for the reader.
© Copyright Bruce M. Axtens, 2012

Thursday, June 28, 2012

[Backup] Genie9 Free Timeline

Gratuitous ad:
The PC equivalent of Apple's TimeMachine: Genie Timeline

[PearlTrees] Programming Languages

I've created a PearlTrees account and have part populated it with some programming language research which you can get to directly here.

Thursday, June 21, 2012

[JScript] The name of the caller


A few postings back I demonstrated how to pull the name of the current function out of arguments.callee. Now to finding out the name of the caller:

function foo() {
 try {
  var myCaller = foo.caller.toString().split(/ /)[1].split(/\(/)[0];
 } catch( e ) {
  myCaller = e.message;
 }
 WScript.Echo(myCaller );

}

function bar() {
 foo();
}

bar();
foo();
The output from all that is
bar
'foo.caller' is null or not an object
So if foo is called from bar then myCaller contains "bar" otherwise an error is raised as the global context doesn't qualify as a "caller". Notice, too, that unlike getting the callee, the caller requires the name of the current function, not arguments.© Copyright Bruce M. Axtens, 2012

[Codeaholic] Well that was intelligent ... NOT!

As you may have noticed, the layout has changed. What you wouldn't have noticed is that my Google Analytics javascript got clobbered in the process. So even if my readership have noticed, I've no record of them having noticed. Smart move, Bruce, real smart move. Oh well, fixed now.
© Copyright Bruce M. Axtens, 2012

Wednesday, June 20, 2012

[Go] Out with JScript, in with Go?

Today I ran a large JScript against a month's worth of hourly REST traffic logs. The logs were parsed and manipulated into a series of tables in an Access database. It took about 12 hours to run. 12 hours ... and on a decently-specced laptop -- a Toshiba Satellite Pro L670.

So I'm looking around for something as flexible as JScript/javascript but which produces native binaries ... and Go seems like a good possibility. Any other suggestions?




I know, I did say 24 hours initially. Frightening the effect of 4 hours sleep on the middle-aged mind.

© Copyright Bruce M. Axtens, 2012

Monday, June 18, 2012

[JScript] Scatter redux

The previous version has a problem: when the recordset has zero records, it falls over. The updated version is below:

function Scatter( o ) {
 var result = {};
 var bEmpty = true;
 var vba = {};
 if ( o.RecordCount > 0 ) {
  bEmpty = false;
  vba = new VBArray( o.GetRows() );
 }
 var fldCount = o.Fields.Count;
 for (var i = 0; i < fldCount; i++ ) {
  result[o.Fields(i).Name] = [];
  if ( ! bEmpty ) {
   for ( var j = vba.lbound(2); j <= vba.ubound(2); j++ ) {
    result[o.Fields(i).Name].push( vba.getItem(i,j) );
   }
  }
 }
 return result;
}


© Copyright Bruce M. Axtens, 2012

Saturday, June 16, 2012

[JScript] Scatter (like the FoxPro function)

On a roll tonight!

Scatter and Gather are a pair of functions in FoxPro and related languages which would take a record and copy it into the memvar space, naming the memvar the same as the field name and storing the field's value. Gather did the opposite.

Now I've got that working in JScript. I hand it a recordset and it creates an object with the field names as property names and the values of each as a list attached to the property. This makes things like this possible:

var X = Scatter( oRS );
 for (var i = 0; i < X.AdvertiserParentFK.length; i++ ) {
  WScript.Echo( i, X.AdvertiserParentFK[i]);
 }
And so here's the function:
function Scatter( o ) {
  var result = {};
  var vba = new VBArray( o.GetRows() );
  var fldCount = o.Fields.Count;
  for (var i = 0; i < fldCount; i++ ) {
   result[o.Fields(i).Name] = [];
   for ( var j = vba.lbound(2); j <= vba.ubound(2); j++ ) {
    result[o.Fields(i).Name].push( vba.getItem(i,j) );
   }
  }
  return result;
 }

More to enjoy! 


© Copyright Bruce M. Axtens, 2012.

[JScript] ADODB GetRows to JScript

StackOverflow to the rescue again. Where would I be without them?

Anyway, I was looking up how to return a GetRows() call on an ADODB recordset to JScript and worked out the following from this posting about returning SafeArrays to JScript


function getRowsToMatrix( o ) {
  var vba = new VBArray( o );
  var dims = vba.dimensions();
  var result = [];
  for ( var i = vba.lbound(2); i <= vba.ubound(2); i++ ) {
   result[i] = [];
   for ( var j = vba.lbound(1); j <= vba.ubound(1); j++ ) {
    result[i].push(vba.getItem(j,i));
   }
  }
  return result;
 }

What I end up with is an array of arrays, which I can address as follows:


var X = getRowsToMatrix( oRS.GetRows() );
var recs = X.length;
var flds = X[0].length;
var lastFldLastRec = X[recs-1][flds-1]; // X is zero-based

Enjoy!



© Copyright Bruce M. Axtens, 2012

[JScript] The name of the current function

This is a plug for StackOverflow, where I spend a fair bit of time.

Today I went looking for Javascript code to give me the name of the currently executing function. I found answers
here and here. And now I have the following in my script:

var fname = arguments.callee.toString().split(/ /)[1].split(/\(/)[0];

Woe betide if callee is ever disabled!


© Copyright Bruce M. Axtens, 2012

Thursday, March 29, 2012

[iPad] Dragon Dictate app

I wrote to Nuance about Dragon Dictate for iPad. We've been having issues getting it to work behind the school's proxy. I had researched what others were saying about it, but thought I'd try and get it from the horse's mouth, so to speak. This is what they said:
Hello,

Please note that the app is not proxy-aware, so if you're using a proxy, you'll need to disable it in order to get traffic through to the app. We do not believe that future versions of the app will change this.

Regards,

Nuance Mobility Support
So if you have a school firewall, and you want to use Dragon Dictate for iPad, start tweaking the proxy, because there's no way Nuance are going to tweak their product.

Wednesday, March 28, 2012

Monday, December 19, 2011

[Lhogho] IsInetOffline? an example of Win32 API wrapping.

Lately I've been having a lot of fun getting my head around the Lhogho programming language. It's a compiled variant of Logo, running on Linux and Windows.

The following code demonstrates wrapping the Win32 API, in this case the IsInetOffline function in URL.DLL. The commented line is taken from a WIN32API.TXT file I found on the internet.

; Private Declare Function InetIsOffline Lib "url.dll" (ByVal dwflags As Long) As Long
make "url_handle libload "url.dll
to InetIsOffline? :dwflags end
external "InetIsOffline? [ U4 InetIsOffline U4] :url_handle

ifelse InetIsOffline? 0 <> 0
 [ print [internet offline] ]
 [ print [internet online] ]

I rather like the notation for defining the call to the DLL itself:
'external' <localsymbol> '[' <returntype> <dllsymbol> <arg1type> ... ']' <libloadvalue>


I've written a few bits and pieces of code which can be found over on the Lhogho forum. Enjoy!

Monday, January 03, 2011

[VB6] Using VBScript's Escape and UnEscape (Take 2)

It's been pointed out to me recently that Escape() doesn't handle embedded quotes and embedded newlines. This is the fix.

I'm using JScript rather than VBScript, so that I can use the String object's fromCharCode() method. I parse the input string, convert the characters to a list of their numeric equivalents for passing to fromCharCode() and then store the result in a JScript variable which is then passed to escape().

Thanks to Anonymous and anjali kalkarni for pointing out the problem.


© Bruce M. Axtens, 2011

Sunday, December 19, 2010

[Javascript] Wildcard string matching / globbing, Take 2.

Okay, I'm convinced: you just can't beat regular expressions. (Okay, SNOBOL4's pattern matching is streets ahead, but there's nothing out there like that for Javascript, AFAIK.)

I've reworked the matchesWild function, caling it grepWild. It takes the wildCard parameter as before but instead of all the substr stuff, it replaces '?' with '.', '*' with '.*' and wraps with '^' and '$'. Then it feeds that into match().

It's simpler, easier on the eyes, and faster too.
© Bruce M. Axtens, 2010

Tuesday, November 30, 2010

[Javascript] Wildcard string matching / globbing

Most Javascript programmers are more than happy with regular expressions. However, there are times when something like wildcards ('?' for one character and '*' for many) would be helpful.

Jack Handy's first and last (it seems) article on CodeProject implemented a wildcard string compare (also known as 'globbing') function in C. Below I present my port of that function to Javascript.

As a newbie Javascript programmer (4 weeks in as of 2010-11-30), I've pleasantly surprised by the expressive power of Javascript. What has caught my eye recently is the power of .prototype., so this implementation of matchesWild() is declared as a prototype extending the String class.

Here's the code, with some commentary:
Apart from the comments, notice the way Javascript adds a method to an object, defining an anonymous function with, in this case, one parameter, and assigning it as a prototype.
The C version did all kinds of interesting things with pointers. Javascript doesn't have that kind of stuff (AFAICT), so I had to do the equivalent with numeric offsets and substr().

Perhaps there are better ways of doing this. I have tried to do this kind of thing before, but this code seems to do a pretty good job of it and hasn't failed thus far.

What should be noted, and perhaps dealt with, is that there is no explicit check for calling the method without a parameter. If one does this, an error is raised. Chrome's V8 raises "TypeError: Cannot call method 'substr' of undefined".
And now some examples (evaluated using macports's JavaScript-C 1.7.0 2007-10-03)
Outputs:
More Javascript coming soon. In the meantime, enjoy.

© Bruce M. Axtens, 2010

Tuesday, November 16, 2010

[iSync] Nokia 5000d-2

After looking around for a while I finally figured out how to get Apple iSync to talk reliably to my Nokia 5000d-2. Most of the other 'solutions' assume that the 5000 is a Series 60 phone. It is in fact a series 40 phone, thus the family.com.nokia.series40.3rdEd.bus.bt.

The image for the phone was downloaded from ericfish.com and copied into the /Applications/iSync.app/Contents/PlugIns/ApplePhoneConduit.syncdevice/Contents/PlugIns/PhoneModelsSync.phoneplugin/Contents/Resources/ folder.

The following code was merged into the MetaClasses.plist file in /Applications/iSync.app/Contents/PlugIns/ApplePhoneConduit.syncdevice/Contents/PlugIns/PhoneModelsSync.phoneplugin/Contents/Resources/

I hope someone finds this helpful. It certainly makes things a bit easier for me. Now all I have to do is figure out how to manage the SMS subsystem. Gammu looks promising, once I figure out the link error. More on that another time.

© Bruce M. Axtens, 2010

Tuesday, September 14, 2010

[VBA] Something like COUNTIFS for Excel 2003

Well, there you go. A colleague sends you a copy of an Excel file and needs a nice formula. So I do the nice formula. Then I try to save the sheet back into the same form in which it arrived:

Clang!

Seems that Excel 2003 doesn't have the nice little COUNTIFS function that I have in my Excel 2007. What to do, what to do, what to do ...

Well, it'd be nice to install Office 07, but in the interim, how about a bit of VBA?

We'll call it RANGEDCOUNT, and it will accept a range of data, and a string being the criteria. The criteria can be
  1. a single value preceded by an operation (>, >=, =, <=, <, #), or

  2. a range of values, the upper and lower bounds separated by a dash.

Right, that's all the declarations over with. Now to check whether we have a range specification or not and what to do if it's not. Notice the 'Instr(2' which checks for an equals sign after the first character, as we're checking to see if we have '>=' or '<=' and don't want to fail on a bare equals
The else fires if we do in fact have a range setting and handles that appropriately.
Next, step through the data and for each element, apply the logic for either a ranged count or an operator count.
Okay, iCount should have the result, so put it in RANGEDCOUNT and end.
Not bad for 15 to 20 minutes work, and would've been faster if I'd remembered how to get data out of Ranges. Now my colleague is happier and I've had a chance to do some something interesting. Granted, it's not particularly fault tolerant, but it's enough for now. © Bruce M. Axtens, 2010