Code Rookie Mistake
Posted in Code
November 6th, 2008 —6:56 pm

Today I found I made a rookie mistake in my code project at work. I’ve been tracking down an error for an hour or so when I finally realized it was due to an uninitialized pointer. I feel so stupid. It shouldn’t have happened.

Tags:
No Comments »
Code Linking Error
Posted in Code
October 21st, 2008 —5:31 pm

I just solved an annoying linking error in Linux. I have a program I’m writing that has two subdirectories in its source directory, and inside those two directories, two static libraries are built. In the parent directory, all objects are built fine, and everything’s shiny until it tries to link the final binary, when I get all these undefined reference to `class::class()`, etc. The constructor, destructor, and other functions seem to have disappeared when I link the library to the main application!

Using nm I can see that the library has those symbols defined! What’s going on?

Turns out it was a problem with my Makefile where I set up the linking. I was adding the link path and libraries before the object files of the parent directory. I didn’t think there was a problem with this as long as everything was on the same command line. Well it looks like I was wrong. When I changed my linking from

g++ -o program_name -Lpath1 -Lpath2 -lstaticlib1 -lstaticlib2 object1.o object2.o ...

to

g++ -o program_name object1.o object2.o ... -Lpath1 -Lpath2 -lstaticlib1 -lstaticlib2

everything worked perfectly! Sheesh, picky linker!

PS Technically I’m using Makefile variables, so it’s easier to read. The actual line looks like this:
$(CC) -o program_name $(OBJ) $(LINK)
but I expanded them for clarity.

Tags:
No Comments »
Code Code Day
Posted in Code
October 6th, 2008 —9:11 pm

I had a very productive day at work today. It’s not often I get to work on real coding projects, but right now I have a big one looming over me. Though challenging, I find this one of my favorite parts of my job. I started with nothing today, but ended the day with a good database, a server program with excellent security, and probably 25% or so of the access functionality it needs to have—all in one day! Now that the groundwork is laid, I should be able to accomplish even more tomorrow, and I’m looking forward to it.

No Comments »
Code DIY File System
Posted in Code
September 2nd, 2008 —4:47 pm

I just ran across this how-to article on writing your own filesystem. I had no idea it was this easy. Requires Linux.

No Comments »
Code Text Formatting
Posted in Code
August 14th, 2008 —12:49 am

I just remembered to make a note of the way I’m converting text from the Gutenberg Project to a usable format. When I get a book in ANSI text format, it has been formatted to a certain width, delimited with newlines. To properly format this in a modern word processor, you need to remove a single instance of a newline, and replace a double newline with a single one. It’s quite simple to do with the right tools:
tr '\n' '+' < original.file > out.file
cat out.file | sed ’s/++/\n/g; s/+/ /g’ > final.file

This quite nicely reformats the plain text into single-line paragraphs. I use the + character because it doesn’t normally appear in this type of text. YMMV; you may want to pick a different one.

No Comments »
Code I Just Don’t Get It
Posted in Code
May 6th, 2008 —4:51 pm

I’m trying to wrap my head around a nasty MS-SQL one-to-many JOIN problem and I just can’t get it to work out.

Basically, I have table a and table b, where a.index = b.index. b can have multiple entries per a.index, but I only care about the first match.

What I want to do is get something like this:
SELECT a.field1, a.field2, b.field
FROM a
JOIN b ON a.index = b.index
WHERE a.field1='this'
...etc...

But for every additional entry in b, I get another row for a. The only field guaranteed to be unique in b is a uniqueid field. It’s hard to explain because the whole thing is a complicated mess.

Basically, a is a table of invoices, and b is a table of products added to the invoice. Most of the time, we have one product per invoice, but not always. I only care about seeing the first match. I thought a MIN() or MAX() would get me where I need to be, but it doesn’t work—the only thing that’s ever different in b is the uniqueid.

It’d be great if you could just JOIN TOP 1 in MSSQL but you can’t. Any ideas?

9:05pm Update: I just had a thought about this: how about JOINing to a view that is just a simple SELECT DISTINCT a.uniqueid FROM b? I’ll have to try this out tomorrow…

Tags:
No Comments »
Code Blog Changes
Posted in Code
April 3rd, 2008 —5:59 pm

I found out some interesting changes in my recent upgrade of WordPress. They’ve added drop-down controls for Archives and Categories, something I wrote widgets for quite a while back. They seem to work well with one exception—the Links still aren’t XHTML 1.0 Strict compliant! Since those are the standards I hold my blog up to, I will be maintaining my strict_links widget as well.

Tags:
No Comments »
Code It’s Been a SQL Day
Posted in Code
January 15th, 2008 —5:42 pm

Today was SQL day. I’ve been working in databases and with scripts that manipulate SQL databases all day long.

I suspect tomorrow will be very similar to today.

And, for the first time, I found a need for using NOT EXISTS syntax in MS SQL.

No Comments »
Code Dodging Bullets
Posted in Code
January 11th, 2008 —11:41 am

I had a meeting at 10am this morning to discuss online training for our software, and how to handle signing up, payments, scheduling classes, etc for it.

The phrase that scared me the most was just duplicate the whole interface we use for classroom training, but in the end we decided to start a little smaller (thank goodness, I hate duplicating code, even scripting/database stuff) and just let the sales staff handle scheduling and sign-ups on an internal calendar. When the program gets off the ground, and if it becomes popular, then we’ll revisit the online sign-up process and its requisite code.

I think that’s one of the very few times I left a meeting with absolutely no work to do. Not that I’m complaining…

No Comments »
Code Wordpress Plugin for Google Analytics
Posted in Code
October 24th, 2007 —11:42 am

The WordPress plugin for Google Analytics by Rich Boakes works great except for one small part: it rewrites a link under comment_author_link and doesn’t add a space between elements in the a tag, which causes a validation problem with XHTML.

It’s simple to add a space on the proper line (272 in v0.68), but I created a diff file you can use to patch it. Hopefully the author updates the file and nobody else has to deal with this problem.

Get the diff file, copy it into your plugins directory where the googleanalytics.php file is, then run the following command:

patch -p1 < googleanalytics.diff

If this is too confusing for you, or you use Windows, change the line (272) from:

return $matches[1] . "\"" . $matches[2] . "\"" . $coolBit . $matches[3];

to

return $matches[1] . "\"" . $matches[2] . "\" " . $coolBit . $matches[3];
Tags:
No Comments »
CodeTech PHP Sessions
Posted in Code, Tech
September 20th, 2007 —6:22 pm

I’ve been working on some areas of PHP that I’m not quite as familiar with lately, getting ready to take the Zend PHP 5 certification exam.

Today I played around a little with sessions. I was able to use them to avoid the annoying browser history “this page was generated from POST data, do you want to resend the POST data?” problem when you go back to a page where login information had been authenticated.

The trick? I use header("Location: /path/to/logged-in-home.php"); after successful authentication, but otherwise display a page with error messages. It’s a small problem, but it’s nice to do away with it.

No Comments »
Code Changing Qt’s QLCDNumber Color
Posted in Code
August 23rd, 2007 —3:59 pm

This was annoyingly difficult to track down, so I’m posting a very straightforward version for posterity.

How to change the QLCDNumber foreground color

Clock::Clock(QWidget *parent) : QLCDNumber(parent)
{
    QPalette *p = new QPalette;
    p->setColor(QPalette::WindowText, QColor(158, 193, 76));
    setPalette(*p);
    setSegmentStyle(Filled);
}

Fixing the Internet, one page at a time…

No Comments »
Code Anyone Remember M.U.L.E.?
Posted in Code
July 31st, 2007 —8:06 pm

I played the heck out of this game called M.U.L.E. back in they heyday of the Commodore 64. There have been a few “tributes” to it, but I haven’t found any good substitute for the original version played on a C64 emulator (yuck!).

So in the DIY spirit, I’ve decided to make my own:
SMA, a M.U.L.E. clone

And come on, you can’t beat the name I picked out…

Hopefully this goes a bit faster than my other code projects (I never give up on them, just stop working on them for an unspecified amount of time).

A friend of mine is great at doing graphics, and he digs the game, too. I’ll do the back-end work for clients (in Qt for cross-platform playability), and write an Internet server, something nobody’s ever gotten around to.

This is in the very early prototype phase with only placeholder graphics and a very rudimentary interface (I just learned how to do this stuff in Qt!). I’ll publish more updates as they become available.

No Comments »
Code Dealing with NULLs in SQL
Posted in Code
June 29th, 2007 —3:59 pm

I ran across a nasty problem today that MS, a former cow-orker, failed to solve. And failed to notice. This isn’t a huge deal, but things aren’t getting reported right because of it.

Here are the basics:
Three fields of type money are being SELECTed from a table. Sometimes one of them is NULL. Two of the fields are being added together, and one is being subtracted from that sum.

The problem is, any time a NULL value is present, then entire mathematical operation evaluates to NULL as well.

Through some research, I discovered Microsoft’s T-SQL CASE keyword:

SELECT (Tax +
  CASE WHEN Freight IS NULL
  THEN '0.00' ELSE Freight
  END -
  Discount) AS 'Cost'
FROM myTable

Of course, the fields Tax, Freight, and Discount are all the in the table myTable.

This actually works quite nicely. The problem was tracking down the information. I’m posting it here mainly for my own use later, when I forget how to do something weird like this.

Normally I’d just set the NULL values to 0.00 and forge on ahead, but in this case I have no idea what I may be breaking somewhere else and needed a simple, non-intrusive peek into the database.

2008 April 25 Note: Today I needed this information. I’m a genius.

No Comments »
CodeThings that are Cool City Lookup
June 26th, 2007 —6:10 pm

Building up on yesterday’s AJAX code, I finished by getting an official list of US and Canadian postal codes, cities, provinces, and some extra information hooked up. Now I can auto-populate City, State, and Country based on a quick postal-code lookup. It works beautifully.

No Comments »