Learn from mistakes

Ever made a mistake because you thought you knew how to use the APIs but they didn't work quite the way you assumed? Or that there was an API that would've made your job much easier had you known it existed? Or the food you had for lunch wasn't good and made you all confused while coding?

Well here's your chance to share those experiences so that the next person trying to do things wouldn't have to repeat the same mistakes, all over again.

String length, in bytes or characters?

The intent is clearly to remove trailing slash and the savvy developer is also aware of UTF-8 character encoding and related issues. Too bad bytes and characters got mixed up in the process; str is an array of bytes but g_utf8_strlen() returns the length in characters. Thus the array index is incorrect, though it will work correctly if the string contains only ASCII.

The previous should've been written using strlen() which returns the length in bytes:

Assuming the path separator is /

One possible use of the code in the previous example is to extract the directory component of a file name. This will work fine on Unix-like systems. However, large sections of the GNOME infrastructure can be used on other operating systems with different path separators (e.g. ':' or '\'). To make your life easier glib provides a series of operating-system independent functions for handling file names. In the previous example the appropriate function would be g_path_get_dirname. The full list is: