Skip to content

Posts from the ‘Code’ Category

30
Dec

LINQ Audit Trail v2 – DoddleAudit

This looked like a link that was fairly re-post worthy. This is practically a must for any .NET + LINQ + MSSQL developer out there. Very neat way of doing audit trails in MSSQL with LINQ and C#.

A couple months ago I wrote a basic set of extension methods to handle automatic auditing in LINQ to SQL. Well I have received a large number of emails regarding this particular project so I have decided to focus on cleaning up my v2 API and releasing it on CodePlex. There was a lot of room for improvement from version 1 and today I am going to post the all new LINQ Audit Trail code. This new version is significantly enhanced in the previous version.

via LINQ Audit Trail v2 – DoddleAudit.

6
Dec

Gripes about Torque Game Engine on Windows 7

So for my Bachelors degree, we’re using the Torque Game Engine to learn how to create assets and write basic scripts for a game engine… effectively creating a whole game. I’ve used Torque lightly in the past, on Windows XP but never on Vista. Now that I’ve gone Windows 7 I’ve had nothing but problems with Torque. So far, just today, in the last 4 hours, it crashed on me 6 times. No pattern to it either, it usually happens when I just simply try to do something. In one case it crashed when I removed a dynamic property, another case it crashed when I placed an entity inside of a sim group. I’ve also experienced crashes in map2dif.exe as well. So far the Torque Constructor has been fairly solid, though I don’t like the interface at all, but the Torque engine itself has been absolutely terrible.

I really wish my college would use Unity or even UDK for teaching game development, Torque seems like an absolute joke. Of course, they haven’t tried out the new Torque 3D engine yet… they’re still on the archaic “Torque Game Engine” which is probably part of the problem.

Anyway, just felt like bitching there for a second about my Torque woes… its been a total nightmare trying to get this assignment done before the end of the 7th week of class (which is next week).

6
Nov

A few handy Javascript Methods

So I’ve been doing a LOT of Javascript programming in the last few days and I ran into a few limitations of the language or constructs within the language. For example, certain versions of IE do not support the Array.indexOf() method. Another problem I ran into is that the Javascript language does not contain a method for performing deep copies on objects. So after some Googling I found a few ways around the aforementioned problems. The first one (indexOf) is as follows:

Array.prototype.findObject = (
    !Array.indexOf ? function (o) {
        var l = this.length + 1;
        while (l–) {
            if (this[l - 1] === o) return l – 1;
        }
        return -1;
    } : function (o) { return (this.indexOf(o) !== -1); }
);

The above code will add the ‘findObject’ method to your Array objects. Then you can use that instead of indexOf() and be guaranteed that it will work regardless if the browser supports indexOf or not.

The second one (deep copying) can be circumvented by creating a helper method which will perform the copy for you:

var ObjectHandler = {
    //public method
    getCloneOfObject: function(oldObject) {
        var tempClone = {};

        if (typeof(oldObject) == "object")
            for (prop in oldObject)
                // for array use private method getCloneOfArray
                if ((typeof(oldObject[prop]) == "object") &&
                                (oldObject[prop]).__isArray)
                    tempClone[prop] = this.getCloneOfArray(oldObject[prop]);
                // for object make recursive call to getCloneOfObject
                else if (typeof(oldObject[prop]) == "object")
                    tempClone[prop] = this.getCloneOfObject(oldObject[prop]);
                // normal (non-object type) members
                else
                    tempClone[prop] = oldObject[prop];

        return tempClone;
    },

    //private method (to copy array of objects) – getCloneOfObject will use this internally
    getCloneOfArray: function(oldArray) {
        var tempClone = [];

        for (var arrIndex = 0; arrIndex <= oldArray.length; arrIndex++)
            if (typeof(oldArray[arrIndex]) == "object")
                tempClone.push(this.getCloneOfObject(oldArray[arrIndex]));
            else
                tempClone.push(oldArray[arrIndex]);

        return tempClone;
    }
};

Just thought I’d share those with everyone. Credit for the majority of the deep copy code goes to http://blog.pramatiservices.com/deep-copy-in-javascript/ and credit for the findObject code goes to someone other than me, but I didn’t bookmark the link so I can’t find it again. Sorry :(

27
Oct

FIX: When you undock some windows or change the window layout in the Visual Studio 2008 Service Pack 1 IDE, the IDE crashes

FIX: When you undock some windows or change the window layout in the Visual Studio 2008 Service Pack 1 IDE, the IDE crashes.

I just thought I re-post this incase anybody stumbled upon my blog before they landed on the Microsoft page that had the fix for IDE crashes. I experienced this only one time when I was moving/undocking windows.

Enjoy.

26
Oct

Helpful Extension Methods to the System.String Class in C#

I’ve been doing a decent amount of string manipulation lately and decided to combine that with my newfound knowledge about .NET extension methods. If you don’t know what extension methods are, they’re basically a way to extend a class without needing the source code to that particular class–hence why I can extend the System.String type by simply writing another class. The key is with the parameters of your methods. Here’s the basic gist of it:

public static {Your Return Type} {Your Method Name} ( this {Type/Class you want to extend} {Parameter Name} )
{
     /* Your method body */
}

 

So the key here is the “this” keyword before the type identifier in the parameter list of the method.

I’ve wrapped all of my extension methods into a single class simply called “ExtensionMethods” and added it to my project. The namespace doesn’t matter, so I haven’t included it in this example. You can either use the global namespace (though I don’t recommend it) or just create your own namespace called whatever you want.

Without further adue, here’s the code:

public static class ExtensionMethods
{
    /// <summary>
    /// Includes the trailing path delimiter.
    /// </summary>
    /// <param name="InputPath">The input path.</param>
    /// <returns>The input path including a trailing path delimiter if it doesn't have one</returns>
    public static string IncludeTrailingPathDelimiter(this string InputPath)
    {
        if (!InputPath.EndsWith(Path.DirectorySeparatorChar.ToString()) &&
            !InputPath.EndsWith(Path.AltDirectorySeparatorChar.ToString()))
            return InputPath + Path.DirectorySeparatorChar;
        return InputPath;
    }

    /// <summary>
    /// Asserts the trailing path delimiter.
    /// </summary>
    /// <param name="InputPath">The input path.</param>
    public static void AssertTrailingPathDelimiter(this string InputPath)
    {
        InputPath = InputPath.IncludeTrailingPathDelimiter();
    }

    /// <summary>
    /// Trims the extension from a file name.
    /// </summary>
    /// <param name="InputFile">The input file.</param>
    public static void TrimExtension(this string InputFile)
    {
        int start, count;
        if ((start = InputFile.LastIndexOf(".")) > 0)
        {
            count = InputFile.Length – start;
            InputFile = InputFile.Remove(start, count);
        }
    }

    /// <summary>
    /// Gets a file extension of a filename.
    /// </summary>
    /// <param name="InputFile">The input file.</param>
    /// <returns>The file extension</returns>
    public static string FileExtension(this string InputFile)
    {
        int start, count;
        if ((start = InputFile.LastIndexOf(".")) > 0)
        {
            count = InputFile.Length – start;
            return InputFile.Substring(start, count);
        }
        return string.Empty;
    }

}

 

Now whenever you use a System.String object, you’ll be able to simply call these methods as if they were part of the System.String class. IE: string MyFileExtension = MyFilePath.FileExtension();

I’m sure I’ll add on to this as I go along.

1
Aug

TI-83 Addition

I’ve added a section to the site called “TI-83″ which contains just a couple of programs I’ve written for my graphing calculator over the years. A lot of these I wrote when I was in high school to assist me on quizzes and homework for algebra II and pre-calc. I’ve been finding myself relying more and more on my graphing calculator recently to solve complex equations–most recently was the Bernoulli Trials equation for my Discreet Mathematics class. Last session in school I damn near wrote a program that solved systems of equations using the Gauss-Jordan method, but then I realized that my graphing calculator already did this for me, so a program was not needed.

They’re all pretty small, but they might be worth grabbing. You’ll have to use your best judgement on using special math characters since keyboards and HTML don’t have them available for input/display. So for example instead of the square root sign that would normally be in a program or on a graphing calculator, I used “sqrt” and for something like the Greek letter Theta, i used <THETA> or (THETA). So the programs won’t port DIRECTLY into your calculator, you’ll have to use your discretion to make any modifications necessary.

Enjoy! *pushes glasses back up on nose with a single finger*

18
Jul

Templatize Tool

I recently wrote up a template engine tool called Templatize. It started as a tool for my girlfriend so she could quickly generate HTML pages from CSV rows for use on eBay, but then quickly grew into a more robust template tool.

Basically it takes a set of input data, containing multiple entities (rows), each with multiple properties (columns), formatted from some type of delimited text file. Then it takes in a master template file and spits out multiple “data-injected” versions of that template.

View the full page here.

2
Jul

CTask-Lite on GitHub

Alright, I’ve finally put my open-source project website woes to rest. I successfully published the first version of CTask-Lite out on GitHub. It can be found here: http://github.com/cdunlap/ctask-lite

CTask-Lite is an easy to install, configure, theme, and use task management system. It is aimed at tiny teams working on ‘open’ projects. What I mean by open is that they don’t care who sees what (because there is no user/role management capability in it).

The Wiki for CTask-Lite is here:  http://wiki.github.com/cdunlap/ctask-lite

Spread the word! Download it, use it, mangle it, hack it, fork it, etc.

2
Jul

Why is open source project hosting annoying and not playing nice with me?

I wrote a neat little web-based task system in PHP, jQuery, and SQLite last night and wanted a place to put it so I could give it to the world (and maintain my source code).

I checked out Google code, where I have placed several projects of mine. I successfully created a project, but when I’d try to check in the code, the SVN server wasn’t available. At the same time, I’d try to click the “Source” tab on the project that I just created and I’d get an HTTP 500 from Google (which is incredibly rare). So I marked that project for deletion from Google Code.

Then I went over to Source Forge. Source Forge is having issues. I couldn’t get their site to load at the time that I tried, again, HTTP 500 errors. Though I did get an email from them a few days ago saying they were going to be pushing a new UI to their site soon. The email said the service interruptions were only going to be for a few minutes at the most. Lets just say it was more than a few minutes, so I scrapped that idea.

Then I remembered a guy at work recommended GitHub. I’m really new to Git and so far I don’t like it. I find it more difficult to use than SVN–though it is faster by far. So I get the code put up on GitHub just fine, but now I can’t edit the repository details in the admin area. Every time I try to change the project website or description it responds with “Name is already taken”. Ok, minor annoyance. Ignoring that, I went over to the downloads area to upload my ZIP containing the final v1.0 build of the project. It gets 100% done and then I get a HTTP 500 from GitHub. I try to go back to the project page but now I’m getting a HTTP 500 error anytime I try to do anything with my project. Not all of GitHub… my project page. I seem to be the only one affected by this.

So, I’m a bit frustrated at the moment. I guess I’ll start working on one of my closed-source projects and call it a night.

22
Jun

1 little 2 little 3 little endians… er wait, is it 3 little 2 little 1 little endians?

Well, it depends on what encoding you’re using.

Lately I’ve been in “endian-ness” hell on a project I’m working on. Well, not really a “hell”, more of a hurdle that can easily be overcome. A friend of mine recently asked me the difference between the two binary encoding strategies and when I went to explain it to him (via cell phone txt messaging) I quickly realized that I will run up both our bills pretty quickly by doing so. So here’s the quick rundown, and an example as it applies to the project I am working on.

Big Endian means that value is stored “from left to right”. The most significant values (bytes) are stored in memory with the lowest address first. This is probably what many people are familiar with, especially those in the networking field.  Here’s an example of data stored as Big Endian:

Take for instance the 2-byte value 1325, equivalent to a short or unsigned short. Stored in Big Endian encoding, the value would look like this in binary: 00000101 00101101. Makes sense right?

Now here is the same 2-byte value, 1325, in Little Endian encoding: 00101101 00000101. The bytes are flipped and thus to make any sense of it, you must read the last set of bits first.

So for simplicity’s sake, Big Endian values are stored like so:

<byte1> <byte2> <byte3> <byte4> ... <byten>

Little Endian values are stored like so:

<byten> ... <byte4> <byte3> <byte2> <byte1>

Network devices use Big Endian, it is considered the network standard, or “network encoding”. Endian-ness varies from CPU to CPU depending on its architecture. Power Macs use Big Endian, network devices use Big Endian. Intel PC’s (and Intel Macs) use Little Endian. What does that mean? Well, it means that if a program running on a Power Mac sends data to a PC, the PC may not understand it correctly without some type of encoding change taking place.

In my situation, I’m reading a series of values off of the network, all coming in via a byte stream. So the client will send the following packet for example:

4 bytes, 2 bytes, 1 byte, … and some others but I won’t get into them

So when I read the first four bytes off the network (which uses Big Endian encoding, remember?) I must flip the entire set of 4 bytes before my Little Endian PC can understand it.  Same goes for the next 2 bytes. The single byte obviously doesn’t need to be flipped with anything, it stands alone. So an incoming set of data may look like:

0x1A 0x2B 0x3C 0x4D 0x2A 0x2B 0xAD

Now I must flip each set of values byte by byte (but not invert the entire thing; this is a packet, containing multiple values).

In Little Endian, the same set of data reads:

0x4D 0x3C 0x2B 0x1A 0x2B 0x2A 0xAD

Confused yet? You’ll figure it out. Here’s some reading that should help: http://en.wikipedia.org/wiki/Endianness. It even gets into the origins of the word, which is pretty interesting. Here’s a little tool that may help you understand the pattern: http://dlnova.com/reverseendian.htm

Get Adobe Flash playerPlugin by wpburn.com wordpress themes