Google Analytics – Updated

Google has updated Analytics, and the feature that made this most obvious was the date range picker.  Before you even open it you can see a new Comparing to: Site ? text.
Analytics date picker change 1
On opening the data picker, they now let you choose today in New Zealand time.

Analytics date picker change 2
No more editing the URL to see what is happening now! This is quite useful when I get comments to see how people found my site etc.

I will have to explore to see if I find more features, but for now, my #1 missing feature has been solved.

Installing Visual Studio 2008

I have finished now, but I have to say there was something creepy about the two male photos used. One looked too happy and the other too smooth.  What made it worse was that it kept changing and my attention would be pulled to smug guy or happy guy.

Visual Studio 2008 - Happy Guy Visual Studio 2008 - Smooth Guy

I’m not looking forward to having to do a repair, luckily I did a full install, so there are no extra features that I will have to install later……

Pascal 6-byte real to IEEE 8-byte double

A while ago I mentioned that System.BitConverter.Int64BitsToDouble would be useful for converting Pascal 6-byte real’s to IEEE 8-byte double’s.  Here is my C# .Net code to do just that.

My original code was based on Richard Biffl’s BPREAL.C.  My original code used System.Runtime.InteropServices.StructLayout( LayoutKind.Explicit ) attribute to make a C++ union data type.  The new code now uses a long (int64) and System.BitConverter.Int64BitsToDouble to do the dirty work.

I originally had my real’s loaded into registers CX, SI, and DI, so that is why the name/order of my functions parameters also.  I knew I had it correct when two of the original values turned out to be 2*PI and 100.0

Here’s the code, the old code is in class PascalReal and the newer version in class PascalRealNicer:

using System;
using System.Runtime.InteropServices;

namespace RealToDouble
{
    class MainProgram
    {
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("{0}", PascalReal.ToDouble(new ushort[] { /*cx*/0xdc83, /*si*/0xcf80, /*di*/0x490F })); // 2 * PI
            Console.WriteLine("{0}", PascalReal.ToDouble(new ushort[] { /*cx*/0x0087, /*si*/0x0000, /*di*/0x4800 })); // 100.0
            Console.WriteLine("{0}", PascalReal.ToDouble(new ushort[] { /*cx*/0x0084, /*si*/0x0000, /*di*/0xa000 })); // -10.0
            Console.WriteLine("{0}", PascalReal.ToDouble(new ushort[] { /*cx*/0x0084, /*si*/0x0000, /*di*/0x0000 })); // 8.0
            Console.WriteLine("{0}", PascalReal.ToDouble(new ushort[] { /*cx*/0x0083, /*si*/0x0000, /*di*/0x0000 })); // 2.0
            Console.WriteLine("{0}", PascalReal.ToDouble(new ushort[] { /*cx*/0x0000, /*si*/0x0000, /*di*/0x0000 })); // 0.0

            Console.WriteLine();

            Console.WriteLine("{0}", PascalRealNicer.ToDouble( /*cx*/0xdc83, /*si*/0xcf80, /*di*/0x490F)); // 2 * PI
            Console.WriteLine("{0}", PascalRealNicer.ToDouble( /*cx*/0x0087, /*si*/0x0000, /*di*/0x4800)); // 100.0
            Console.WriteLine("{0}", PascalRealNicer.ToDouble( /*cx*/0x0084, /*si*/0x0000, /*di*/0xa000)); // -10.0
            Console.WriteLine("{0}", PascalRealNicer.ToDouble( /*cx*/0x0084, /*si*/0x0000, /*di*/0x0000)); // 8.0
            Console.WriteLine("{0}", PascalRealNicer.ToDouble( /*cx*/0x0083, /*si*/0x0000, /*di*/0x0000)); // 2.0
            Console.WriteLine("{0}", PascalRealNicer.ToDouble( /*cx*/0x0000, /*si*/0x0000, /*di*/0x0000)); // 0.0

            Console.ReadKey();
        }
    }

    class PascalReal
    {
        [ StructLayout( LayoutKind.Explicit )]
        public struct doublearray
        {
            [ FieldOffset( 0 )]
            public ushort a0;
            [ FieldOffset( 2 )]
            public ushort a1;
            [ FieldOffset( 4 )]
            public ushort a2;
            [ FieldOffset( 6 )]
            public ushort a3;

            [ FieldOffset( 0 )]
            public double d;
        }

        public static double ToDouble( ushort[] r )
        {
            doublearray da;

            da.d = 0.0;
            da.a0 = 0;
            da.a1 = 0;
            da.a2 = 0;
            da.a3 = 0;

            ushort x = (ushort)(r[0] & 0x00FF);  /* Real biased exponent in x */
            /* when exponent is 0, value is 0.0 */
            if (x == 0)
                da.d = 0.0;
            else
            {
                da.a3 = (ushort)(((x + 894) << 4) |  /* adjust exponent bias */
                    (r[2] & 0x8000) |  /* sign bit */
                    ((r[2] & 0x7800) >> 11));  /* begin significand */
                da.a2 |= (ushort)((r[2] << 5) |  /* continue shifting significand */
                    (r[1] >> 11));
                da.a1 |= (ushort)((r[1] << 5) |
                    (r[0] >> 11));
                da.a0 |= (ushort)((r[0] & 0xFF00) << 5); /* mask real's exponent */

            }
            return da.d;

        }
    }

    class PascalRealNicer
    {
        public static double ToDouble(ushort cx, ushort si, ushort di)
        {
            long double64 = 0;
            int exp = cx & 0x00ff;

            if (exp != 0)
            {
                // sign
                double64 = (long)(di & 0x8000) << 48;
                // biased exponent
                double64 |= (long)(exp + 894) << 52;
                // mantissa
                double64 |= (long)(di & 0x7FFF) << 37;
                double64 |= (long)si << 21;
                double64 |= (long)(cx & 0xFF00) << 5;
            }

            return System.BitConverter.Int64BitsToDouble(double64);
        }
    }
}

Compiling and Running code at runtime

I have wanted to know how to compile and run code at runtime for a long time. I have now got a working solution, so it’s time to share.  I might even learn a better way.

So here is the C# 2.0 code, no extra references are required.

using System;
using System.Collections.Generic;
using System.Text;

using System.CodeDom.Compiler;
using System.IO;
using Microsoft.CSharp;
using System.Reflection;

namespace DynaCode
{
    class Program
    {
        static string[] code = {
            "using System;"+
            "namespace DynaCore"+
            "{"+
            "   public class DynaCore"+
            "   {"+
            "       static public int Main(string str)"+
            "       {"+
            "           return str.Length;"+
            "       }"+
            "   }"+
            "}"};

        static void Main(string[] args)
        {
            CompileAndRun(code);

            Console.ReadKey();
        }

        static void CompileAndRun(string[] code)
        {
            CompilerParameters CompilerParams = new CompilerParameters();
            string outputDirectory = Directory.GetCurrentDirectory();

            CompilerParams.GenerateInMemory = true;
            CompilerParams.TreatWarningsAsErrors = false;
            CompilerParams.GenerateExecutable = false;
            CompilerParams.CompilerOptions = "/optimize";

            string[] references = { "System.dll" };
            CompilerParams.ReferencedAssemblies.AddRange(references);

            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerResults compile = provider.CompileAssemblyFromSource(CompilerParams, code);

            if (compile.Errors.HasErrors)
            {
                string text = "Compile error: ";
                foreach (CompilerError ce in compile.Errors)
                {
                    text += "rn" + ce.ToString();
                }
                throw new Exception(text);
            }

            //ExpoloreAssembly(compile.CompiledAssembly);

            Module module = compile.CompiledAssembly.GetModules()[0];
            Type mt = null;
            MethodInfo methInfo = null;

            if (module != null)
            {
                mt = module.GetType("DynaCore.DynaCore");
            }

            if (mt != null)
            {
                methInfo = mt.GetMethod("Main");
            }

            if (methInfo != null)
            {
                Console.WriteLine(methInfo.Invoke(null, new object[] { "here in dyna code" }));
            }
        }

        static void ExpoloreAssembly(Assembly assembly)
        {
            Console.WriteLine("Modules in the assembly:");
            foreach (Module m in assembly.GetModules())
            {
                Console.WriteLine("{0}", m);

                foreach (Type t in m.GetTypes())
                {
                    Console.WriteLine("t{0}", t.Name);

                    foreach (MethodInfo mi in t.GetMethods())
                    {
                        Console.WriteLine("tt{0}", mi.Name);
                    }
                }
            }
        }
    }
}

The foundation of this code was a Dynamically Compiling C# blog post for IronPython.

It took me ages to realise that the string[] that CompileAssemblyFromSource consumes is a single string for each block (file) worth of C# code, not for each line.  New lines and space do not affect the compiler.

I then wasted ages finding my method to call via Invoke.  I have not come up with a good solution yet.  Currently my code is calling a static method, as this is what I need.

The last pain was that Invoke takes an array of objects, but it then maps the array to the method being called, so the method needs to have the signature of the items in the object array. I thought I could just handle object[] as the method input.  This makes sense in hind sight, but at the time it was very painful.

Now that I have working code, and know what to Google for, I have found other posts that go into great detail, like Rick Strahl’s 2002 Dynamically executing code in .Net. I feel like I’ve been under a rock.