Thursday, March 15, 2018

[PHP] Include a data structure once

Today's job at work was to get some data into GravityForms, a forms tool for WordPress.

So one page has a postcode entry, and the next page has some hidden postcode-specific fields that are used to calculate values later in the form-set, in this case freight costs on goods delivered by
TNT (no relation to the song by AC/DC).

The documentation for GravityForms suggests putting code into functions.php in the active theme. I'm a little wary of doing that given that changes there can work or not work in a rather catastrophic way.

Nevertheless, I ended up coding against the gform_field_input hook
add_filter('gform_field_input', 'update_hidden', 10, 5);
function update_hidden($input, $field, $value, $lead_id, $form_id)
So then the was the issue of how to include the postcode-to-charges array (PCA) (stored in an external php file) once rather than each time gform_field_input fired, which is once per control. With the PCA being just shy of a megabyte in length, I wasn't particularly interested in having it load repeatedly.

The PCA looks like this
return array(
 '0221' => array(
  'BasicChrg' => 9.982, 
  'KgChrg' => 0.5405, 
  'MinChrg' => 15.3755, 
  'RemotAreaChrg' => 0, 
  'ResidChrg' => 5, 
  '0to15Chrg' => 0, 
  '15to199Chrg' => 10, 
  '200to299Chrg' => 30, 
  '300to399Chrg' => 40, 
  '400to499Chrg' => 150, 
  '500to599Chrg' => 250, 
  '600plusChrg' => 300, 
  'FuelSurchrg' => 0.07)
StackOverflow did offer a suggestion on how to deal with this but I had no joy with it. So I went with using include_once. That, however, also had problems. According to the documentation, include_once returns a boolean true when it gets called the second time for the same file. So one must use a temporary variable to hold the result rather than committing it immediately to your PCA variable.

So the code I have ended up with is (some omitted for brevity's sake):
function update_hidden($input, $field, $value, $lead_id, $form_id)
 global $TNTFreightPrices, $PostCode;

 $path = $_SERVER['DOCUMENT_ROOT'] . '/wp-content/uploads/tntfreightprices/TNTFreightPrices.php';
 $tmp = include_once $path;
 if (gettype($tmp) == "array") {
  $TNTFreightPrices = $tmp;
  $PostCode = rgpost('input_52');

 if ($form_id == 8) {
  $BasicChrg = $TNTFreightPrices[$PostCode]['BasicChrg'];

  $fid = $field['id'];
  if ($fid == 53) {
   $input = "<input id='input_8_53' class='gform_hidden' name='input_53' aria-invalid='false' value='$BasicChrg' type='hidden'>";
It remains to be seen whether I stay with $TNTFreightPrices and $PostCode in global space as that's a carryover from other experiments.

It was part fun and part hairloss but at least we're closer to a solution. Probably there are better ways. WooCommerce?

© Copyright Bruce M. Axtens., 2018

Monday, February 12, 2018

[dotnet] core, standard and framework

Riiiiiight, now I get it ... maybe.

Check the original posting on StackOverflow for the full description.

© Copyright Bruce M. Axtens, 2018

Friday, February 09, 2018

[JavaScript] String.prototype

Back in 2011, I started learning JavaScript. I'm still learning but haven't moved beyond ES3, largely because that's the dialect I'm using for most of my server-side scripting. In other words, Microsoft JScript (gasp, shock, horrors, gevalt etc.)

Most of the rest of what I do at the moment is using C#. A while back I went looking for a way to extend my C# programs with scripting and found
ClearScript. I've been using it to great effect for the last 3 years in the vast majority of my work projects. I've even written a JScript-on-steroids which exposes large chunks of C# to JScript and permits the writing of some very powerful scripts.

In the next few postings I'll be discussing some of the interesting things I've discovered. For a lot of folk, it'll be old hat ... so old that mice are living in it. But, who knows, maybe someone will find a use for some of it, or be able to adapt it to newer dialects.


String.prototype.toProperCase = function () {
  return this.replace(/\w\S*/g, function (txt) {
    return String(txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase());
I found this on StackOverflow. It attempts to proper-case (title-case) every word in a string. I don't use it often but it was an interesting find.

String.prototype.after = function (a, including) {
  including = including || false;
  a = this.indexOf(a);
  return -1 === a ? String(this) : String(this.substr(a + (including ? 0 : 1)));
A staple, this gives whatever is after a substring in a string. The substring can be included or excluded for the result.

String.prototype.before = function (a, including) {
  including = including || false;
  a = this.indexOf(a);
  return -1 === a ? String(this) : String(this.substr(0, a + (including ? 1 : 0)));
Another staple, this gives whatever is before a substring in a string. Again, the substring can be included or excluded for the result.

String.prototype.stripChars = function (chars, cnt) {
  var i,
  cnt = cnt || 0;
  var answer = this;
  for (i = 0; i < chars.length; i++) {
    var chr = chars.charAt(i);
    if (cnt === 0) {
      var pat = new RegExp(chr, "g");
      answer = answer.replace(pat, "");
    } else {
      for (j = 0; j < cnt; j++) {
        answer = answer.replace(chr, "");
  return String(answer);
I don't use this often, but it has been useful. It removes each element of chars from the string. If cnt is undefined or zero, all are removed. If cnt is 1 or more, then that many are removed.

String.prototype.between = function (begin, end, including) {
  including = including || false;
  var left = this.indexOf(begin);
  if (left < 0) {
    return String('');
  left += begin.length;
  var right = typeof end === 'undefined' ? this.length : this.indexOf(end, left);
  if (right < 0) {
    return String('');
  return including ? String(begin + this.substring(left, right) + end) : String(this.substring(left, right));
There are various ways of pulling SGML and other markups apart. This one, in various languages, has been in the toolkit for years. This variant permits excluding or including the delimiters. Apart from that, it isn't smart -- "drooling singing".between("droo","ing") gives l

String.prototype.endswith = function (str) {
  return this.indexOf(str) !== -1 && this.indexOf(str) === this.length - str.length;
Returns true if the strings ends with contents of str otherwise false. Has helped with detecting filetypes in filenames.

String.prototype.toSqlString = function () {
  return String("'" + String(this).replace(/'/g, "''") + "'");
This puts a single quote at the start and end of the string and doubles any embedded single quotes. I use this in combination with .questionMarkIs() to format SQL statements.

String.prototype.toArrayOfLines = function () {
  return this.split(/\r\n|\r|\n/g);
Splitting a text file to lines is something I do a lot of. This is the functional variant of code I have in almost every script written over the last 7 years.


String.prototype.questionMarkIs = function (val) {
  return String(this.replace("?", val));
For a while I was doing all kinds of weird things to build SQL queries. Lately I've taken to simply putting question marks in and then replacing them with the value (with or without .toSqlString()).

That should do for now. Enjoy!

© Copyright Bruce M. Axtens, 2018

Thursday, February 08, 2018

[configuration files] dhall

Okay, I know next to nothing about this at the moment but it does look interesting.
dhall is a programmable configuration language that is not Turing-complete. You can think of Dhall as: JSON + functions + types + imports
I can think of some places where this approach to config would be helpful and less dangerous that what I am currently doing.

© Copyright Bruce M. Axtens, 2018

Wednesday, February 07, 2018

[tbas] tbas basic interpreter

In my ample (NOT!) spare time, I'm helping out with a programming language project called tbas. I saw it advertised on comp.lang.basic.misc in September 2017 (yep, usenet is still going strong.)

The author is one Antonio Maschio from Italy.

You can get source, documentation and a CygWin binary from the main site. There are some examples on RosettaCode.

To give you an idea of the flavour of the language, the following is a simple CAESAR cipher implementation.

    LET MSG$ = ""
    LET OFFS = 0
    LINE INPUT "Message"; MSG$
    INPUT "Offset"; OFFS
        TARG(I) = TARG(I) + OFFS

An example of running the above

    >tbas CAESAR.BAS
    Message ? My dog's got fleas
    Offset ? 1

© Copyright Bruce M. Axtens, 2018

Thursday, July 20, 2017

[FreeDOS] The FAST and SOFA compilers

Two recent additions to the FreeDOS stable of development languages are FAST (Fast PC Compiler) and SOFA (Super Optimised Fast Assembler), inventions of one Peter Campbell of New Zealand. Peter developed FAST and SOFA in the mid 1980s and they were used as the main development languages for the Fastbase accounting system. In 2000, Peter rewrote the entire system in Tcl. All use and development of FAST and SOFA stopped at that point.

SOFA is an 8088 (and semi-80386) assembler. The syntax has some similarities to two other shareware assemblers,
A86 and CHASM. SOFA can assemble its own source-code. Its features include quick assembly time (thousands of lines per second), conditional compilation and include files.

FAST was written in SOFA. The language syntax was influenced by Basic, Pascal, C and Assembler. The binaries produced are small and fast

Peter Campbell died in tragic circumstances at Easter 2007, leaving the Fastbase business to his brother, Russell Bell.

The adding of FAST and SOFA to FreeDOS took a little too long: I had heard of the language during the 1990s and had rediscovered it in the
Vetusware abandonware collection. From there I found out about FastBase and made contact with Russell. This was 2009 and it was at that time that I suggested to Russell that SOFA/FAST could be re-released as Open Source software, thus preserving in some way Peter's legacy. Russell promptly gave permission and provided me with the sources. Embarassingly, it has taken me eight years to finalise the process.

SOFA and FAST will appear eventually on
HOPL (The Online Historical Encyclopaedia of Programming Languages). In the meantime, postings regarding the languages can be found on HOPL's Facebook front-end at SOFA and FAST respectively.

[1] Already some FreeDOS utilities have been rewritten in FAST.

© Copyright Bruce M. Axtens, 2017

Wednesday, December 02, 2015

[Browsers] Inconsistent HTML Entities

I fancy that this is old hat to most of you, but I had thought HTML entities cut and dried: An ampersand, a word and a semicolon (reminds me a little of dBase III, actually.) It seems, however, that certain browsers are still unsure as to what constitutes an HTML entity, and where it should end.

One of the languages I use for server-side management uses the ampersand + word + semicolon format to mark where variables may be interpolated into the output stream. So it was with some surprise when it was pointed out to me by a colleague that &currentValue; was being rendered as ¤tValue;.

Being the inquisitive sort, I went to and downloaded all the HTML entity names. After a bit of fiddling about in jQuery, I had the list. After a bit more fiddling, this time in JScript, I ended up with an HTML file showing the entity name, the entity as rendered, and then what happens when you leave off the trailing semicolon and append some other text (in this case 'es'). This is where things get ... well ... unusual. Some HTML entites follow the standard. Others like &curren; and &pound; don't. You enter &poundage, expecting the browser to give &poundage only to have it give you £age. Weirdness abounds. Take &centerdot; which actually renders as ¢erdotes!

So as to give you an idea of how it looks in your browser, I've put the file up in a
jsfiddle . Let me know how you get on and whether your browser is as compliant is it makes out.


© Copyright Bruce M. Axtens, 2015