Friday, October 14, 2011

Dennis Ritchie is dead

Known and remembered only by true programmers, the creator of the C programming language, Dennis Ritchie, has passed away, at the age of 70.

As a great fan of the C language myself, I'm deeply saddened by the news. C language shaped pretty much everything we have today in terms of programming, and almost everything we have today was written in C, and C is still the most widely used language. And C was released in 1973 – 38 years ago. So think about the importance of this man.

Rest in peace, Dennis. Your legacy will be eternal.

Saturday, September 24, 2011

Smells Like Teen Spirit for orchestra

I started having formal music training when I was a child, and although I didn’t have any classes for years, I still like to study music when I have time, specially orchestration and related disciplines. Writing music for orchestra is not an easy task and it requires not only a lot of study, but also a lot of practice.

Today is the 20th anniversary of Nirvana’s Nevermind album. At the time it was released I was just a kid, and it made me to play a lot of guitar. So after 20 years I decided to make a tribute to Smells Like Teen Spirit and write a full orchestra arrangement to this song, mixing the original voice track over it – this voice track can be easily found on YouTube.

It took me about a month to write, render and mix the whole thing, and so far I’m pretty satisfied with the results. It gave that 4-chord music a whole new dimension. And here it is: Smells Like Teen Spirit for orchestra.

Wednesday, September 21, 2011

A sprintf with automatic memory allocation

Personally, I’m a sprintf lover. Not only in C, but in any program language that implements it. I use sprintf all the time, it’s extremely simple, clean and quick.

The sprintf version found on the C standard library requires a previously allocated memory space, where the formatted string will be placed after the operation. However, there are many times where you don’t know how long the result string can be, or you simply don’t want to waste space with a “worst case scenario” allocation. I used to face this a lot. There’s a GNU extension called asprintf, which you can use on Linux, but it doesn’t return the pointer, it returns the number of bytes – although it’s fine, it’s not exactly what I wanted.

So I decided to write my own version of a sprintf function which automatically finds out how much space is needed, allocates the memory and then returns the pointer, with the formatted string. Then I just have to free the pointer after use.

In order to accomplish this task, I used some least known functionalities from the C standard library, so it may be interesting to you to study how I did the job, so the technique can be applied when you need. Also, I found out that some of them behave differently on Linux and Win32. The idea is simple, though: find out how much memory is needed; allocate the memory; call sprintf; return the pointer. After all, I ended up with a valuable and handly function to work with.

This is the Linux version:
char* allocfmt(const char *fmt, ...)
	va_list ap;
	va_start(ap, fmt);
	int len = vsnprintf(0, 0, fmt, ap);

	va_start(ap, fmt);
	char *retbuf = malloc(sizeof(char) * (len + 1));
	vsprintf(retbuf, fmt, ap);
	return retbuf;
And this is the Unicode-ready Win32 version:
#include <tchar.h>
#include <windows.h>
LPTSTR allocfmt(LPCTSTR fmt, ...)
	va_list ap;
	va_start(ap, fmt);
	int len = _vsctprintf(fmt, ap);
	TCHAR *retbuf = malloc(sizeof(TCHAR) * (len + 1));
	_vsntprintf(retbuf, len, fmt, ap);

	*(retbuf + len) = 0;
	return retbuf;

Wednesday, September 14, 2011

Fast, in-place C string trim

I want to share with the world this code for a fast trim function in C which is part of my personal library, and that I use quite often. It performs both left and right trim as an in-place operation, changing the original string. The return value is the same pointer that was passed as argument.

This is the version I use on Linux:
char* trim(char *s)
	char *pRun = s;
	while(*pRun == ' ') ++pRun;
	if(pRun != s)
	memmove(s, pRun, (strlen(pRun) + 1) * sizeof(char));
	pRun = s + strlen(s) - 1;
	while(*pRun == ' ') --pRun;
	*(++pRun) = 0;
	return s;
And this is the version I use on Win32, which is Unicode-ready:
	TCHAR *pRun = s;
	while(*pRun == TEXT(' ')) ++pRun;
	if(pRun != s)
	memmove(s, pRun, (lstrlen(pRun) + 1) * sizeof(TCHAR));
	pRun = s + lstrlen(s) - 1;
	while(*pRun == TEXT(' ')) --pRun;
	*(++pRun) = 0;
	return s;

Thursday, August 18, 2011

How to install Chromium on Windows

Note: This post was updated on February 20, 2014.

Google Chrome is a modern browser, the most advanced one we have today. But unfortunately Google, on its way to world domination, uses it to silently collect personal data from you. Yes it does, even with those anti-privacy options unset – and it further links the information to your Google account, what is even worst. But there is a way to enjoy this amazing browser, away from Google’s eyes: directly using the open source project which feeds Chrome: Chromium.

Since I couldn’t find a decent tutorial anywhere, I decided to write my own, this step-by-step guide on how to download and install Chromium on Windows.

Chromium is usually used by the developers, but it’s also available to anyone who wants. This excellent post explains in detail how the versioning works. The Chromium builds for Windows are published on Chromium repository. These builds are made automatically by the buildbot. Beware: this is the cutting-edge repository, so any version may have bugs (if you find a bug, just download another version, by the way).

You’ll notice that some build numbers are missing: it happens because the buildbot only publishes the builds which pass 100% through all the automatic tests. The most recent build number which was published is written down as latest good known revision. I prefer to download the latest build of previous version, the one who became the Chrome beta stage, which is likely to be fairly stable.

The current (February 20, 2014) Chromium version under development is 35, therefore I recommend to get the last build of Chromium 34 (build 252031) which can be downloaded here.

History of last builds:
23 (2012-09-20) 157677, 24 (2012-10-30) 164895, 25 (2012-12-19) 173798, 26 (2013-02-13) 182231,
27 (2013-03-27) 190946, 28 (2013-05-08) 198631, 29 (2013-06-26) 208550, 30 (2013-08-14) 217340,
31 (2013-09-25) 225096, 32 (2013-11-05) 233008, 33 (2013-12-17) 241258, 34 (2014-02-20) 252031.

Although named “chrome”, this is in fact Chromium. Once downloaded, unzip the chrome-win32 folder into your “Program Files (x86)” folder (if in Windows XP, into your “Program Files”). Then doubleclick the chrome.exe file, and it will automatically do three things:

  • create a shortcut to Chrome on your desktop, which you’ll probably want to drag into your start menu;
  • create a small Registry entry at “HKEY_CURRENT_USER\Software\Chromium”; and
  • create your profile folder on the following directory:
    • on Windows 7: “C:\Users\<user>\AppData\Local\Chromium”
    • on Windows XP: “C:\Documents and Settings\<user>\Local Settings\Application Data\Chromium”

At this point, Chromium is ready to run.

To update Chromium: Chromium doesn’t have automatic updates, you must do so manually. To update the Chromium version to the latest (or rollback), just download and unzip the “chrome-win32” folder over your current one, overwriting all files. Your profile folder isn’t touched.

To uninstall Chromium: these Chromium packages don’t create uninstall entries on the Windows Control Panel. To uninstall Chromium, you must essentially undo all the steps done on the installation: delete the “chrome-win32” folder, the shortcut, your profile folder (if you want to remove all your personal stuff) and the Registry entry.

And that’s it. Enjoy this great browser.

Follow up (Jun/2017): I stopped tracking these a while ago, but I released the app I developed to check the Chromium releases: Chromium Peeker is now open source.

Wednesday, July 20, 2011

Dialog based Win32 programs

This is the kind of program I write most: C/C++, pure Win32 API, dialog box based. Over the years I developed a couple of handful routines that I believe to be pretty solid nowadays, since I use them without any modification for some years already, and they work flawlessly.

I decided to share this knowledge with everyone mainly because today I see a lot of .NET programs – or even worse, WPF – that most of the time they could have been written in good Win32, avoiding the huge amounts of bloating you have to carry with those “modern” libraries. I know C/C++ is not for everyone, pointers demand care, but I strongly believe that any respectable programmer should have a clear understanding of the C language. Otherwise, he’s just a script kiddie.

Modern languages with garbage collectors overprotect the programmers, much like those overprotective parents do with their children – and we all know these children become problematic, limited adults.

So stop being lazy and go learn C. And after you learn C, you can read my article on Win32 programming, that I published on CodeProject today.

Thursday, July 14, 2011

uTorrent is dead

Last month, my uTorrent client – actually written as “µTorrent” – updated itself to version 3. Well, I always was a huge uTorrent fan, mainly because it’s simple and lightweight, as its slogan says.

But after that update, I had a surprise.

The new uTorrent interface showed itself full of gadgets, lots of new buttons and a bunch of useless new features – I totally hated all that stuff. What about that slogan? It took me some time to figure out how to hide all that stuff from my eyes, so that I would see only the torrent list. After a couple weeks, I began considering rollback to the previous version.

The last stable version of the 2.x branch is uTorrent 2.2.1 build 25302, which albeit has a bug where the tray icon never goes away, corrects some memory leak bugs from 2.2, among others.

Since the new versions probably will remain bloated, I’ll stick with 2.2.1 until I find another client worth changing – at this moment, I doubt I’ll find one. Also, after completely remove 3 and install 2.2.1, I instantly noticed a performance increase: version 2.2.1, being much more simple, is faster as well.

Quoting Leonardo, simplicity is the ultimate sophistication.

Sunday, July 10, 2011

The browser version race

The browser wars is a good thing, since you have the browsers competing for the market share by offering improvements and new features. The truly winner of the browser wars is the browser user.

However, there’s another war in course, which I read about a couple weeks ago, and they named it the version race, or the version wars. It seems that providing new features and improvements on the browsers is not enough anymore: one must provide a freshness of a “new browser”, and this is being achieved by increasing the major version number each new release. As a programmer myself, I found it really annoying.

Version numbers have their significance, and it’s being lost now. Let me explain one of the most common interpretations: for example, let’s take the “1.7.4” version number. Here, the “4” is the release number, and it should be increased each time the developer publishes some correction, bug fixing or some minor feature which doesn’t break compatibility with the current version.

The “7” is the minor version number, which is increased each time you have a significant new feature, maybe with some minor compatibility break (usually corrected by the program itself), but despite the improvement, you’re still on the same major version.

And the “1” is the major version number, which is increased every time you have a big change on the program, often with a significant part of the source code being entirely rewritten. It’s almost like a new program under the same name.

I remember when Google Chrome browser was launched in 2008, and everybody was amazed with its fast evolution, with a galloping major version number increasing. Then at some point, with so many major version numbers (and minimal improvements), people simply didn’t care about it anymore. In the future, when Chrome really have something new to show… it will be just another version number, because they wasted the whole arsenal of numbers already.

The worst: Mozilla entered the version race with Firefox 5 in June of 2011, which should have been versioned just as “4.1”.

An alternative to this is join the Microsoft boat, which likes to use years as version numbers, like “Office 2010” – although internally they follow the regular version number convention. This year-versioning is good, because it gives you a clear sense of time, you quickly associate the program with “how old” it is. Maybe a month/year versioning for the browsers, since there are several versions within a year. But what about “Chrome 7”? What do you associate it to nowadays?

Version race leaves a blurry track behind the software evolution. Stop the version race.

Saturday, July 9, 2011

Replay Gain

I’d say this Replay Gain thing is the coolest thing that appeared on the personal computer music listening world since the birth of Winamp in 1997.

How it works: first you use a Replay Gain scanner to add a couple new tags to each song of your music library. These new tags stamp a relative perceived loudness level corrector to each song, so that when your player plays them, it will raise or lower the volume accordingly, and all the songs will have the same volume (or almost), and you don’t need to raise/lower the volume yourself anymore. This is specially useful if you have a long playlist in shuffle mode, with songs at all sorts of different volumes.

Several music players of today implement Replay Gain, but some of them don’t perform the scan. If you want a suggestion, try foobar2000, it can do all the job.

Corollary: if you have a large and heterogeneous music library, Replay Gain will make you smile.

Now on the technical side: I found what it seems to be the C implementation of the Replay Gain scanner at the (rather old-school) project site. It really drove me curious, I guess I’ll download and try to compile it later.

About this blog

Hello, dear reader.

There are times when you just want to say something, or maybe share something which seemed interesting at that moment, or maybe just voice your opinion about a particular event.

Well, that’s why I’ve just created this blog. I have no idea about what I’m gonna write down here, there’s nothing specific, just writing away. And as a first post, I just wanted to register the idea of this lack of ideas, and at the same time, the high amount of them.

So be it.