Monday, October 8, 2007

svn_load_dirs.pl woes

I use subversion to manage all software projects. It is a nice utility, much better than the commercial SourceSafe. And it is free. Have not encounter any problems in the past three years.

Today I am trying to upgrade boost from 1.33 to 1.34. There are a lot of changes between the two versions, so it is not possible to manually change them. Furthermore, although WinMerge is nice to show all the differences, it does not understand subversion command. If a file exists in 1.34 but not in 1.33, the correct command is "svn copy" rather than copy.

My installation does not have this magic file svn_load_dirs.pl. I grabbed it from collab: http://svn.collab.net/repos/svn/tags/1.4.2/contrib/client-side/svn_load_dirs.pl.in. I did a copy and paste, and run the command, it gives me an error:

'SVN_BINDIR@' is not recognized as an internal or external command,

Not good. Look further I found the file extension ends with .in. So some work needs to be done to convert it to .pl. Open the file, I found this line:

# Specify the location of the svn command.
my $svn = '@SVN_BINDIR@/svn';

Cool. Changed the line to

my $svn = 'C:\Program Files\Subversion\bin\svn.exe';

and it worked properly.

Thursday, October 4, 2007

Default C/C++ char is signed

Programmer often forgot that the default char type is signed, instead of unsigned. In VC, /J gives the char unsigned. We ran into a problem this week:

if ( character > 126 character < 32) {
sprintf(sTemp, "%02X", character);
os << '#' <<>

In VC 2003, sTemp gives "FFFFFFFFFac" for character 0xac. To correct the issue, convert it to unsigned integer type, such as
sprintf(sTemp, "%02X", (unsigned char)character );

Note that this statement

if ( character > 126 character <>

implies that the character is unsigned type. This variable should be declared as unsigned char at the beginning.

Wednesday, August 29, 2007

Quirks in STL string class

Today I found a pretty annoyance when using STL string class. The find function returns the index if it find the string, and string::npos if it does not. For example,

std::string str("joeliscrazy");
index = str.find("joes");

The find returns a type of string::size_type, which is size_t in most implementations. Now since size_type is unsigned, you no longer can write the following statement

if ( index < 0 ) { }

Unless you define index as a signed type, such as int.

Other string libraries (java, .Net and MFC) use signed type as index, thus avoid this problem. Since most modern platforms provide 32-bit integer, who on the earth require a unsigned int as string index?

Tuesday, April 10, 2007

C++ != OO

When debating programming philosophies, many associate specific C++ features, such as data encapsulation with OO. Because of these bla bla, they argued, OO philosophy is superior to functional programming.
Well, there are certain features from C++ are not OO. For example, template and generic programming are not OO. What I think about OO is that this thinking assumes that everything should be repsented with classses and objects.
In the early days in the OO theory, advocates borrowed heavily from data analysis in the databasae theory. The thinking went like that you should treat everything as objects, with their private data as properties, and actions performed as methods. This sounds very well, however, there are at least two problems: a class can virtually have unlimited number of members and methods. Human beings have limits on learning things. When a class grows so big it is difficult to expand and maintain. Another problem is more fundamental. Our programming is to solve real problems, not to model the world. We only need a small part of data and concern on how the data is processed into another digital form. We realy do not care about how many objects we need to have in the analysis.

Monday, February 26, 2007

Valid characters for ID and NAME attributes

The W3C defines ID and NAME requirement as -

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

So colons is a valid character used inside an ID.

Why do I mention this? We want to find a way to specify unique ID names, even we do not know what the outside HTML will look like. Say we are building a component containing some HTMLs. This component is going to be embeded into other pages. We may consider add a prefix for example

wasterpack::id_error

is a valid ID.

Sunday, February 25, 2007

Tricking Microsoft Script Editor to edit PHP files

Microsoft Script Editor is a great tool to edit HTML pages and debug client javascripts. However, it only recognize file with several extensions, and show unformatted pages to .php and .tpl files. It seems that Microsoft does not want you to use this tool to edit PHP and template files. Many people want this feature and have to change the file extension back and forth. Needlessly to say, this is a tedious work and prone to errors.
Today I found that you can hardlink a file under Windows XP, just like the command "ln" on UNIX. A hard linked file looks like a regular file, but points to another file. If you change the contents of one file, the another changes as well. Let's say that we need to edit file index.php, we can create a hardlink file called index.html. Now every changes made to index.html go into index.php. In this way we can edit file contents in Frontpage or Script Editor, but test it with index.php.

The command to create a hardlink file is
fsutil hardlink create index.html index.php

Note that the new file name is before the old file name.

Thursday, February 15, 2007

Ternary Operator - multiple branches

I use ternary operator ?: in C++ a lot. Unfortunately I never went further to use it on multiple branches. Today I found that it is legal to write

char* str = size < 10 ? "small" :
size < 20 ? "medium" :
size < 30? "large" :
"extra large";

It is equivalent as:

if ( size < 10 )
str = "small";
else if (size <20)
str = "medium";
else if (size < 30)
str = "large";
else
str = "extra large";

Nicer, uuuhhh