enum size in C++

Here's a nice trick I figured out today. A lot of times I'll set up an enum in C++ and then I want to index an array with the symbols defined in the enum. So, let's say I want to use an array with one entry for each of the symbols in the enum, and I can allocate the array at compile time because I can look at my enum and count how many values there are. But maybe later I'll need to add a few values to that enum and then I'll forget to count how many there are! I'll get compilation errors! Here's what I can do instead of counting:

Here's my enum:

enum BigCats {
   TIGER,
   LIGER,
   LION,
   COUGAR,
   NUM_CATS
};

Notice the NUM_CATS as the last element!

Maybe I want to have an array of ints that represents how awesome I think each cat is. Now I can do this:

int AwesomeCats[NUM_CATS]

powershell script to print function

here's a powershell script i worked up the other night. i do embedded programming at work with an ide that doesn't always search through a set of files correctly. so, i usually do it manually, and spend a lot of time in powershell searching through the code to find things. things like functions and junk. so, now instead of something like: grep functionName *.c, looking at the file name and line number and then going into the ide to open the file and see it, i'm just like: show-function functionName. WOW!

# PS script to print out a function given the name
# Nick Gamroth
# May 2008

if($args[0] -eq $NULL) {
	echo "ERROR: no function name given"
	return
}

# here's a junky regex to search for function definitions
$search = "[\w\s]" + $args[0] + "\("
# figure out what file the function is defined in
# hopefully, this will only be 1
$match = get-childitem . --include *.c -recurse | select-string -list -pattern $search 
#$file = $match | select-object Filename
$file = $match.Filename
$line = $match.LineNumber

# show the file starting at that line
$count = (get-content $file | measure-object).Count
$d = ($count - $line + 1)

# get all the lines starting at the one we found the method start at
$meth = get-content $file | select-object -last $d

$done = 0
$braces = 0
$i = 0

$meth | foreach-object {
	$l = $_
	echo $l
	if($_.Contains("`{")) {
		$braces++
	}
	if($l.Contains("`}")) {
		$braces--
	}
	if($braces -eq 0) {
		# make sure we get past the first line
		if($i -gt 1) {
			break
		}
	}

	
	$i++
}

Science Time!

I just remembered that I used to have some math/science related notes on this site before the redesign. These notes used to get a few Google searches, so I re-added them. I've even added some new stuff about discrete-time Fourier transforms!

Science: It Works, Bitches!

Coverflow ripoff

Remember coverflow? Remember how it was so much better as a standalone app than it is as a part of iTunes? Yeah, me too, and I totally miss it. I had a few spare minutes today, so I put together an interface prototype of a clone:

MAT Files From C

Here's a tip: you can easily generate binary .mat files right from C/C++! I've been trying to do this for a really long time, but it turns out I wasn't reading the MATLAB documentation, which tells you just how to do it!

#include "mat.h"  // matlabroot\extern\include

void main()
{
    double array[] = {1,2,3,4,5,6,7,8,9};
    mxArray *a = mxCreateDoubleMatrix(9, 1, mxREAL);
    memcpy(mxGetPr(a), array, 9 * sizeof(double)); 
    
    MATFile *matfile = matOpen("matfile.mat", "w");
    matPutVariable(matfile, "data", a);
}

You also need to link with libmat.lib, which is somewhere like :matlabroot\extern\lib\win32\microsoft\msvc60.

AwesomeForm

Wow, I wrote another CodeProject article!!

This one is about a Form class I made that is translucent. It's not bad, but it could be better. I'll work on it some more

undefined reference when linking library

wow, i thought that i was totally screwed for a minute over here. i'm writing this program that uses a bunch of external libraries and one of them is the bluesoleil bluetooth SDK from IVT. it comes with a .lib that i was having a rough time linking with when using Qt Creator. every time i'd try and build the project, i'd get undefined reference errors for every function defined in that library. here's how i figured out what was going on:

- used dumpbin /ALL to take a look at everything the lib was exporting
- verified that the functions i was calling existed
- noticed that function names in the lib were prepended with a '_'
- !!!
- noticed that the errors i were getting didn't have underscores
- figured out that this library was using C, but my code was all C++
- used extern "C" { ... } around my header files

this probably should have been obvious right away, but it's a hard problem to search for, and it gave me a rush when i figured it out.

Average of a file of numbers in powershell

I kind of like the idea of using powershell as a sort of MATLAB substitute for quick analysis of data. I had the opportunity to do that today since I ran some tests that resulted in a few files that were just lists of numbers. I was interested in the mean of all the numbers in each file, so I wrote this script:

# mean.ps1
# calculates the mean of a set of numbers stored in a text file.  each number
# should be on it's own line.
# Nick Gamroth

$data = get-content $args
write-host "Calculating mean of" $data.length "data points found in" $args

$sum = 0
for ($i = 0 ; $i -le $data.length ; $i++)
{
	$sum += [double]$data[$i]
}
$mean = $sum / $data.length

$mean

jabba the chatfest

I've done some work in the interface prototype for my jabber client. It's going pretty well, but there are a lof of performance bugs to work out.

Here's a screenshot:

Of course, those goofy looking images are just test images. In use, they'd be pictures of other users. The exciting part about the interface is that when you switch users, the chat window "flips" over, to reveal the conversation you're having with another user on the back. I wrote a CodeProject article about that control. I made an entry here about it, but I must have managed to delete it from the database.

It's New!

I redesigned this site! I got some new shoes that are grey and orange, and I like the color combination so I used it for this site. It's a little bit halloween-ish, but it's not bad.
Also, I learned how to use PHP and MySQL to create a database back-end. I'll probably post the source for it soon. It's really simple, so it'd probably be a good starting point for others who want to do something similar.