Sunday, March 4, 2012

My Firefox addons

Just to mention, these are the addons I’m using today on my Firefox 10.0.2:
  • Adblock Plus 2.0.3 — Blocks all advertising content according to filters. I use to install this addon wherever computer I am at, and apart from a few subscriptions, I also have a couple rules of my own.
  • Download Statusbar 0.9.10 — Replaces the traditional download window with small progress bars at the bottom of Firefox.
  • Element Hiding Helper for Adblock Plus 1.2.1 — Extra functionality to Adblock Plus, easing the process of select specific elements within a page for adding blocking rules.
  • F6 0.2 — When working with the tabs on top, when you hit F6 key you put the focus on the tabs, instead of the address bar. This addon makes sure that the address bar will be focused when you hit F6.
  • Flagfox 4.1.12 — Displays a flag from the country where the site is from, not by its country code top level domain, but rather by its server location, determined by its IP address.
  • Forecastfox 2.0.21 — Adds a weather monitor on the toolbar, displaying current weather and temperature. I was told the data is picked up from airports. It has shown to be fairly accurate.
  • UI Fixer 1.4.4 — Rearranges the horrible design of new Firefox interface, for example, restoring the text on the titlebar and placing the orange button at the toolbar.
  • Undo Close Tab Replacement 6 — Shows a list of the most recently closed pages, so any of them can be reopened again.

Saturday, February 18, 2012

Hide members on inheritance in C++

I was working on my personal Win32 classes, when I came across something I was willing to do. Suppose I have the following class:
class Base {
protected:
	int someValue;
	void doSomething();
public:
	void thePublicMethod();
	void anotherPublicMethod();
};
Since someValue and doSomething() are protected, they are visible and can be used by any subsequent derived class. Then, let’s have a derived class:
class Derived : public Base {
	void newStuff() {
		someValue = 42;
		doSometing();
	}
};
The public methods are still public. Now see that I used someValue and doSomething() inside newStuff() method, which is private. And now I don’t want subsequent derived classes to have access to someValue and doSometing(). I want to hide them from subsequent derived classes.

At first, I thought it wouldn’t be possible without redefining and overriding, which would add bloat here. But then I came across a trick I didn’t know, which allows you to hide both methods and variables without redefining or overriding them:
class Derived : public Base {
	Base::someValue;
	Base::doSomething; // now they are protected: hidden!

	void newStuff() {
		doSometing();
		someValue = 42;
	}
};
This trick changes the access modifier of methods and member variables without touching them, with zero overhead or additional bloat. Yes, C++ is a damn complex language.

Friday, February 17, 2012

Optimization settings for Visual C++ 2008

These are the optimization settings I use on Visual C++ 2008. These settings are set to the release builds, on the project settings window, and they complement the default release settings.
  • C/C++
    • General
      • Debug Information Format: Disabled
    • Code Generation
      • Enable String Pooling: Yes
      • Enable C++ Exceptions: No
      • Runtime Library: Multi-threaded (/MT)
      • Enable Enhanced Instruction Set: Streaming SIMD Extensions 2 (/arch:SSE2)
  • Linker
    • Debugging
      • Generate Debug Info: No

On debug builds I don’t change the settings much: I just disable the C++ exceptions, and enable the SSE2 and the string pooling.

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_end(ap);

	va_start(ap, fmt);
	char *retbuf = malloc(sizeof(char) * (len + 1));
	vsprintf(retbuf, fmt, ap);
	va_end(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);
	va_end(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:
LPTSTR trim(LPTSTR s)
{
	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;
}