Posts for 2011 September

DIY gnome applets

Post by Nico Brailovsky @ 2011-09-29 | Permalink | 1 comments | Leave a comment

We all know Gnome, and similar GUIs, are there only as a fancy console multiplexer, but even so it's useful to have widgets in your menus or dockbars to display useful data, like the release date of DNF (*). Gnome has a limited amount of applets from which you can choose, and most of them are crap or limited in their customization. You can always create your own widgets, but that's a pain in the ass for lazy people like me. Fortunately we lazy people can now use something an order of magnitude more useful than widgets in Gnome : we can use console commands!

Using something called Compa you can add a meta-widget, that will display the output of any CLI program. This means, of course, that you have all the power of the console to use in your custom made widgets. Need to check your laptop's battery? No need to search for a widget anymore, just cat /proc/acpi/battery/BAT0/state. Need to check the weather? Just wget your favorite forecast page and parse it with grep, sed an awk. OK, maybe that's a little bit too much.

Once more this proves that anything can be done in console mode - and whatever you can't isn't worth doing anyway.

(*) Wow, this article has been written a LONG time ago!


Cool C++0X features XI: decltype and disappearing constness

Post by Nico Brailovsky @ 2011-09-27 | Permalink | Leave a comment

After a long, long hiatus, the C++0x series are back. You may want to check where we left by reading the last posts of this series.

In the last few entries we saw how to use decltype for type inference. Object types is a problem that seems easy but gets complicated very quickly, for example when you start dealing with constness. Constness is difficult in many ways but this time I want to review how constness works with type inference. This topic is not C++0x specific as it's present for template type deduction too, but decltype adds a new level of complexity to it.

Let's start with an example. Would this compile?

struct Foo {
    int bar;
};
void f(const Foo foo)
{
    foo.bar = 42;
}

Clearly not, having a const Foo means you can't touch foo.bar. How about this?

struct Foo {
    int bar;
};
void f(const Foo foo)
{
    int& x = foo.bar;
}

That won't compile either, you can't initialize an int reference from a const int, yet we can do this:

void f(const Foo foo)
{
    const int& x = foo.bar;
}

If we know that works it must mean that s.result's type is const int. Right? Depends.

Just as the name implies decltype yields the declared type of a variable, and what's the declared type for Foo.bar?

struct Foo {
    int bar;
};
void f(const Foo foo)
{
    // This won't compile
    int& x = foo.bar;
    // This will
    decltype(foo.bar) x = 42;
}

That's an interesting difference, but it makes sense once you are used to it. To make things more interesting, what happens if you start adding parenthesis (almost) randomly? Try to deduce the type of x:

void f(const Foo foo)
{
    decltype((foo.bar)) x
}

If decltype(x) is the type of x then decltype((foo.bar)) is the type of (foo.bar). Between foo.bar and (foo.bar) there's a very important difference; the first refers to a variable whilst the last refers to an expression. Even though foo.bar was declared as int, the expression (foo.bar) will yield a const int&, since that's the type (though implicit and not declared, since the expression is not declared).

This is how we would complete the example then:

void f(const Foo foo)
{
    // These two statements are equivalent
    decltype((foo.bar)) x = 42;
    const int& y = 42;
    // It's very easy to confirm that the typeof x is now const int&
    // This won't compile:
    x = 24;
}

As I said, disappearing constness is not a C++0x specific problem as it may occur on template type deduction, but that's besides the point of this post. Next time we'll continue working with type deduction, but with the new auto feature this time.


Running commands on Windows from Linux, through ssh

Post by Nico Brailovsky @ 2011-09-22 | Permalink | 1 comments | Leave a comment

Running Windows is something I don't usually like (running of Windows is a different story) but having to run something on Windows command line interface is something I wouldn't wish even to my worst enemies. I was stuck in that situation, don't remember why, but I needed to run a command in a Windows machine, automatically, and I only had ssh (is there a better way of automating scripted tasks in Windows, remotely and without a GUI?). Well, this is what I came up with:

ssh host cmd /c dir

Running that in a bash shell will show the directory listing of C: in machine "host". Ugly as hell, but it's a good way of kickstarting a batch script.


Throwing destructors

Post by Nico Brailovsky @ 2011-09-20 | Permalink | Leave a comment

We already know what happens when you throw from a constructor. Ending up with a half built object is not good, but suppose we do manage to build one correctly. What happens if we throw in a destructor instead? The results are usually much worse, with a very real possibility of having your program terminated. Read on for a brief explanation on the perils of throwing constructors.

So, according to RAII pattern, resource deallocation should occur during the destructor, yet resource freeing is not exempt of possible errors. How would you notify of an error condition?

Yet the worst part is not resource leaking through half destroyed objects, the worst part is having your program call std::abort.

Think of it this way: when an exception is active, the stack is unwind, i.e. the call stack is traversed backwards until a function which can handle the exception is found. And you just can't unwind the stack while unwinding the stack (you'd need a stack of stacks) so the reasonable thing to do is call std::abort.

So, what can you do about it? Go to your favorite jobs posting site and start searching for a PHP position, you'll sleep better at nights.


Zero padding for Bash scripts

Post by Nico Brailovsky @ 2011-09-15 | Permalink | 1 comments | Leave a comment

Lately I found myself trying to generate a video from a series of images generated by a program. Doesn't sound difficult, until you start running into a stupid issue: your 1000th frame will come before your 2nd frame!

Luckily there's a very easy fix for this problem, just add zero padding in a bash script. How?

for i in seq 1 10; do echo $i; done

That will print all the numbers between 1 and 10. This one will do the same, with zero padding:

for i in `seq 1 10`; do printf "%02dn" $i; done

200th post!

Post by Nico Brailovsky @ 2011-09-14 | Permalink | Leave a comment

Yes, this is the post number 200 on this blog. Considering we are a month away of starting the fourth year, I guess that gives me quite a lousy periodicity, but I am still surprised I post somewhat regularly here after such a long time (hey, in programmer years that's like a whole life!).

Since the beginning this blog has mutated from being  my public notepad to being a place where I research new topics, or write about things that are generally interesting to me. I lost many readers for posting crazy metaprogramming stuff and for constantly babbling about Vim and Linux, but hey, I'm proud of if. Let's see what the next 200 posts bring here.


Automagic document conversion for your makefiles

Post by Nico Brailovsky @ 2011-09-13 | Permalink | 1 comments | Leave a comment

So, now you have a common makefile, ready to be used for a TDD project and for code coverage report automagic generation. Not only that, but it even speaks to endlessly annoy your team. What else can we add to this makefile? Well, automatic documentation generation, clearly.

You want to batch convert .doc to .pdf using the command line on a server without a GUI? Or you need automated .ppt to .swf conversion through cron, a sysvinit service, or a remote web server? Online conversion services such as Zamzar.com and Media-convert.com not working for you? Whichever formats you need to batch convert, PyODConverter is a simple Python script for just this purpose.

-- http://www.oooninja.com/2008/02/batch-command-line-file-conversion-with.html


Activating tildes and accents for a USA keyboard layout in Ubuntu

Post by Nico Brailovsky @ 2011-09-08 | Permalink | 2 comments | Leave a comment

Wow. This time the title of the post may actually be longer than its contents. How do you enable accents and tildes in Ubuntu? You need it to type cool characters like á, Ó or ñ (hey, my name has one of these!).

If you are on Windows I think you have to install a new map, and then guess where the key would be. Or use an alt+something magic spell. In Ubuntu, it works by default you just need to add a compose key, Go to System > Preferences > Keyboard > Options > compose key position, select right alt (or whatever you fancy), there you go, now it works. Try it by typing alt + ' + a.


A talking makefile

Post by Nico Brailovsky @ 2011-09-06 | Permalink | 1 comments | Leave a comment

So, after learning how to use makefiles, then how to use makefiles for TDD and for code coverage report, now you need to annoy your whole team with a talking makefile. What could be better to notify everyone on your team when a test fails than a synthesized voice commanding you to fix your program?

test: $(TEST_SRCS)
    @for TEST in $(TEST_BINS); do
        make "$$TEST";
        echo "Execute $(TEST)";
        if ! ./$$TEST; then
            echo "Oh noes! I detected a failed test from $$TEST. Go and fix your program!" | festival --tts ;
    done

Try it. You'll love it.

Bonus chatter: when Valgrind detects over $MUCHOS errors it'll print "Too many errors detected. Go and fix your program", then it won't print so much detail in the next backtraces.


Edit pdf files in Ubuntu

Post by Nico Brailovsky @ 2011-09-01 | Permalink | Leave a comment

Well, for some reason my LaTeX py-pygments stopped compiling. Thanks for breaking backwards compatibility, you pig-ments.

I had two options, either spend hours trying to fix this by altering the preamble, or just edit the pdf file. Yeah, I know, editing the pdf sounds ugly as hell, but hey at 2 am in the morning I'll take anything. And pdfedit was there to save the day (night). Just apt-get install pdfedit, it's in the repo.