Sunday, June 17, 2012

Linus Torvalds is my hero

I’m a big fan of Linus Torvalds, not only for what he did with Linux, but also for his figure, particularly his lack of tenderness. His thoughts about C++ are legendary already, and that impressed me to the point that today I’m writing mostly ANSI C code.

Now, during a talk at Aalto University in Finland, he voiced about the lack of support Nvidia has given Linux, and his zero-diplomacy balls have striked loudly again. For my personal delight, I must admit, since I had a problem with a Nvidia video card driver on my Ubuntu just a couple months ago, to which I simply shrugged after days of pain.

Listen to the man:


And Nvidia, fuck you!

Saturday, June 2, 2012

Nu Sans programming font

I’ve just discovered out the Nu Sans monospace font on a Reddit discussion regarding the Mensch font – which is also a good font. However, Nu Sans caught my attention when I tested it on Visual Studio 2008 because it has that Lucida Console feel: a large font within small line height. And this is good for programming, because you can fit more lines on the screen. The TTF of the link above is a demo without accents, but it’s pretty usable.

So I just found a new font for my programming font collection.

Sunday, May 13, 2012

FLAC and LAME frontend

I use the FLAC and LAME command line tools, and I’ve tried many FLAC and LAME frontends. And I disliked all them. One day I decided to write my own, with a parallel multithreaded handling of the files, so all the files are converted at once, taking advantage of multicore CPUs.

Today I’ve just published it as open source on GitHub. It’s pure C and Win32. I hope it can be useful to someone – not only the program itself, but also my C programming techniques. It can be found at rodrigocfd/flac-lame-gui.

Saturday, May 12, 2012

My first program on GitHub

That’s a simple C Win32 program to shrink down padding bytes from ID3v2 tags on MP3 files. Maybe the most important thing, I think it showcases my style of C programming, and I’m glad to share it with the world. I hope it can be useful to someone someday.

It can be browsed at rodrigocfd/id3-padding-remover.

Thursday, April 19, 2012

I hate the new Gmail look

I’ve managed to keep the classic Gmail look until today, when all of a sudden Gmail morphed irreversibly into the horrendous new interface, gray and white, without colors or contrast. The option to revert to the old good look cannot be found anymore, so that I’m stuck into this ominous thing! And the built-in themes don’t help!

I’m about to install Stylish addon and slap some CSS over this ugly interface, which I hate with passion. Let me say it again: I hate the new Gmail interface, which seems to have been inspired by the nothing.

Google is becoming more evil day after day.

Wednesday, March 21, 2012

Finding memory leaks in Win32

I’ve just discovered an interesting function to aid the Win32 programmer in finding eventual memory leaks in debugging mode, and with a very straightforward use: _CrtDumpMemoryLeaks.

The function compares the current memory state with the one at the program start, and if any unnalocated memory block is found, it returns TRUE and prints some debug information on the output window. So, to use it effectively, without false positives, one must call it when all objects are out of scope. Here’s an example:
#include <crtdbg.h>
#include <string>
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
{
	std::wstring name = L"abcdef";
	_CrtDumpMemoryLeaks(); // will accuse a leak!
	return 0;
}
In the code above, there will be a false positive, because the string object didn’t go out of scope yet, therefore its destructor wasn’t called so far, so there’s still memory allocated. But this would rather lead to our expectations:
#include <crtdbg.h>
#include <string>
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPWSTR, int)
{
	{
		std::wstring name = L"abcdef";
	}
	_CrtDumpMemoryLeaks(); // no leaks!
	return 0;
}
Now, since the string was declared within the scope of the nested block, its destructor will be called when the nested block ends, and no memory will remain allocated after it. This way, _CrtDumpMemoryLeaks will correctly report no leaks; indeed, nothing will be printed to the output window.

And the function is removed when _DEBUG is not defined, so you don’t need to worry about it on your release builds.

I particularly found it useful to use the _CrtDumpMemoryLeaks call inside an _ASSERT macro, so that when a memory leak is found, a huge popup will honk right in my face, in addition to the debug messages. So I actually proceed like this:
_ASSERT(!_CrtDumpMemoryLeaks());

Saturday, March 17, 2012

Portable Git on Windows

I use Git on Linux a lot, but these days I was willing to use it on Windows too. I remember some time ago I found some ugly installers, so I thought there should be a portable package. For my surprise, I found one: msysgit. It’s a self-contained package build upon MinGW with everything one would need.

And it’s incredibly easy to use: just unzip the package anywhere, then doubleclick “git-bash.bat”. Like magic, you have a Linux-like terminal with a ready-to-use Git. And all the stuff exhales quality: it seems to be very well built and feels good on the tip of the fingers.

Now if you need to use Git on Windows, there’s no other way to go but msysgit.

Tuesday, March 6, 2012

Load Explorer file extension icon in Win32

When programming with C and Win32, sometimes you need to load an icon, as displayed on Windows Explorer, relative to a file extension. For example, you need to load the Explorer icon associated to the “txt” file extension.

Here is a quick function I use to do it:
HICON ExplorerIcon(const wchar_t *fileExtension)
{
	wchar_t extens[10];
	SHFILEINFO shfi = { 0 };

	lstrcpy(extens, L"*.");
	lstrcat(extens, fileExtension);
	SHGetFileInfo(extens, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(shfi),
		SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES);
	SHGetFileInfo(extens, FILE_ATTRIBUTE_NORMAL, &shfi, sizeof(shfi),
		SHGFI_ICON | SHGFI_SMALLICON | SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES);

	return shfi.hIcon; // use DestroyIcon() on this handle
}

// Usage example:
HICON hIconTxt = ExplorerIcon(L"txt");
Remember to release the HICON handle by calling DestroyIcon after using it, or you’ll have a resource leak.

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.