Thursday, September 30, 2010

WinXP multiple taskbars with little pain

I dont have admin rights on my work machine but needed a multi screen toolbar app now winXP. "multimon" seemed to install without an issue and worked well. It doesnt seem to need to mess with the OS to run.

http://www.mediachance.com/free/multimon.htm

Update:
A great alt key alternative is Vista Switcher(also works on XP) This app gives you a preview of the widow and allows you to filter based on monitor with ctrl+alt+tab

http://www.ntwind.com/software/vistaswitcher.html

Wednesday, September 29, 2010

C Bit packing and structs

Bit packing of a struct is achieved with the ":" after the member of the struct this little item is great for working with binary images of data however it can lead to surprises. Always keep in mind the compiled result will mask and shift every thing you place in the struct.

#include <stdint.h>
#include <iostream>

using namespace std;

typedef struct {
    uint8_t a :6;
    uint8_t b :1;
    uint8_t c :1;
} Test1;

typedef struct {
    uint8_t a;
    uint8_t b;
    uint8_t c;
} Test2;

typedef union {
    struct {
 uint8_t a :6;
 uint8_t b :1;
 uint8_t c :1;
    } bits;
    uint8_t full;
} Test3;

int main()
{
    Test3 test3;
    
    cout << sizeof(Test1) << endl;
    cout << sizeof(Test2) << endl;
    cout << sizeof(Test3) << endl;
    
    test3.bits.a = 0;
    test3.bits.b = 0;
    test3.bits.c = 0;
    
    cout << "Test3 inited:" << endl 
  << (int)test3.bits.a << endl
  << (int)test3.bits.b << endl
  << (int)test3.full   << endl;
    
    test3.bits.a = 0xff;
    
    cout << "Test3.bits.a loaded with 255:" << endl 
  << (int)test3.bits.a << endl
  << (int)test3.bits.b << endl
  << (int)test3.full   << endl;
    
    test3.bits.a = 0x0;
    test3.bits.b = 0xff;
    
    cout << "Test3.bits.b loaded with 255:" << endl 
  << (int)test3.bits.a << endl
  << (int)test3.bits.b << endl
  << (int)test3.full   << endl;
}

The output looks like this (depending on machine endianness)
1
3
1
Test3 inited:
0
0
0
Test3.bits.a loaded with 255:
63
0
252
Test3.bits.b loaded with 255:
0
1
2

A perl implementation of find

Recently working on a system which has a busted find command need a fix.. it is about 300% slower than the real thing so use it when needed only

#!/usr/bin/env perl

use strict;
use warnings;
use Cwd;

my $start=$ARGV[0];

sub find
{
    my ($dirname) = @_;
    my $dh;
    opendir($dh, $dirname) or die "Couldn't open dir '$dirname': $!";
    my @files = readdir($dh);
    closedir($dh);

    #print files...
    foreach my $file (@files) 
    {
 if(
    ($file) &&
    ($file !~ /^\.$/) &&
    ($file !~ /^\.\.$/)
    )
 {
     my $path = "$dirname/$file";
     print "$path\n";
     if(-d "$path")
     { 
  find("$path"); 
     }
 }
    }
}

$start = "." unless $start;

find($start);

Friday, September 24, 2010

Friends and Inhertance

In C++ there is a common gotcha with friends and inheritance.

The rules are summarized as
  1. Friend access is one way.
  2. Friends of a friend have no access.
  3. Children of a friend have no access.
  4. Children of a parent with a friend will allow the friend to access them like the parent.

    IE friend access of a derived class is limited to interface present on the original base class only. Virtual functions will operate as normal if they are accessible.

Example code;

#include <iostream>

using namespace std;

class FriendOfFriendA;
class FriendA;


class A{    
public:
    A() : stuff(8) {}

    int get(){return stuff;}
    
protected:
    virtual int vget(){return stuff;}
    int stuff;

    friend class FriendA;
};

class KidA : public A {
public:
    KidA() : A(), kidStuff(9) { stuff = 16; }

protected:
    int kidGet(){return kidStuff;}
    virtual int vget(){return kidStuff;}
    int kidStuff;
};

class FriendA{
public:
    int getA(A& a){return a.stuff;}
    int getKidA(KidA& a){return a.stuff;}
    //int getKidAstuff(KidA a){return a.kidStuff;}  //BAD only a's stuff is a friend
    //int getKidAstuff(KidA a){return a.kidGet();}  //BAD only a's stuff is a friend

    int getVirtualA(A* a){return a->vget();}

    friend class FriendOfFriendA;

    static int pubStaticGetA(A& a) { return a.stuff; }
private:
    static int privStaticGetA(A& a) { return a.stuff; }
};

class FriendOfFriendA{
public:
    //int getADDirect(A& a){return a.stuff;}   //BAD no freind of a friend access
    int getAIndirect(A& a){return FriendA::pubStaticGetA(a);}
    int getADoubleIndirect(A& a){return FriendA::privStaticGetA(a);}
};

class FriendAKid : public FriendA{
public:
    //int getADDirect(A& a){return a.stuff;} //BAD no child of a friend access
    int getADIndirect(A& a){return getA(a);}
};


int main()
{
    A    a;
    KidA kidA;

    FriendA         friendA;
    FriendAKid      friendAKid;
    FriendOfFriendA friendOfFriendA;

    cout << "friendA.getA(a) : " << friendA.getA(a) << endl;
    cout << "friendA::pubStaticGetA(a) : " << FriendA::pubStaticGetA(a) << endl;

    cout << "friendA.getVirtualA(a) : " << friendA.getVirtualA(&a) << endl;
    cout << "friendA.getVirtualA(kidA) : " << friendA.getVirtualA(&kidA) << endl;
   
    cout << "friendA.getKidA(kidA) : " << friendA.getKidA(kidA) << endl;
    
    //cout << "friendOfFriendA.getADDirect(a) : " << friendOfFriendA.getADDirect(a) << endl;
    cout << "friendOfFriendA.getAIndirect(a) : " << friendOfFriendA.getAIndirect(a) << endl;
    cout << "friendOfFriendA.getADoubleIndirect(a) : " << friendOfFriendA.getADoubleIndirect(a) << endl;
    
    cout << "friendAKid.getADIndirect(a) : " << friendAKid.getADIndirect(a) << endl;
}

This produces this result:
friendA.getA(a) : 8
friendA::pubStaticGetA(a) : 8
friendA.getVirtualA(a) : 8
friendA.getVirtualA(kidA) : 9
friendA.getKidA(kidA) : 16
friendOfFriendA.getAIndirect(a) : 8
friendOfFriendA.getADoubleIndirect(a) : 8
friendAKid.getADIndirect(a) : 8

Tuesday, September 21, 2010

Adjusting emacs tabs in namespaces only

Dealing with a large project with lots of c++ namespaces and scoping; Here is how to kill the indents from the namespace regions in emacs

;; Basic indentation
(setq c-basic-offset 4)

(defun my-c-setup ()
  (c-set-offset 'innamespace 0))
(add-hook 'c++-mode-hook 'my-c-setup)

To find out your current syntax state hit \C-c\C-s (crtl-c ctrl-s)


Refer:
http://www.emacswiki.org/emacs/IndentingC
http://google-styleguide.googlecode.com/svn/trunk/google-c-style.el

Thursday, September 16, 2010

sh simple counted command line loops

For repeated predictable execution of times

i=64; while [ $i -ne 0 ]; do  echo $i; i=`expr $i - 1`; done

Portable script shebang

Let say you need a script to execute on multiple systems but the admins and OS's all have there own idea on where things belong. Here is a quick way to get the script to locate the interpreter 99% of the time

#!/usr/bin/env perl

Tuesday, September 14, 2010

Quick and dirty hex converter

void convertHex()
{
    string exchangeHex = "0123";
    int exchange;

    //exchangeHex
    std::stringstream ss;
    ss << std::hex << exchangeHex;
    ss >> exchange;
 
    cout << exchange << endl;
}

Tuesday, September 7, 2010

Googles got a bouncy logo

UPDATE: Rob Hawkes has recreated it in html5 here;

http://rawkes.com/experiments/google-bouncing-balls-canvas/

And Mark Brenig-Jones has mod'ed it slightly
http://dotty-dots.appspot.com/?h=40526f624861776b6573

http://rawkes.com/blog/2010/09/07/recreating-googles-bouncing-balls-logo-in-html5-canvas



ORIGINAL POST:
http://www.google.fr/

probably just for today..

a fatal relocation error

A fatal relocation error:

This error means that you have included a dynamic library in your build but at run time the library that it uses is mismatched. This can happened if you rebuilt the lib after the exe, or failed to move the new lib into place etc.

Monday, September 6, 2010

xterm colors and settings

Edit ~/.Xresources and add the following

xterm*foreground: white
xterm*background: black

Then execute this command
xrdb ~/.Xresources
Then open a new terminal to test.

Here is a more complex version of it
http://www.xfce.org/various/Xresources.txt

Wednesday, September 1, 2010

graphviz map of the linkage between .a files

This hacked up piece of perl script will generate a dot compatible piece of text that will display the mapping of the libs in a project.

#!/usr/bin/perl 

my $path = "path/to/libs"

# get the nm results for the system
@ret = `find $path | grep "\.a$" | xargs nm -A`;
#@ret = `cat data.tmp`;

# 0 = lib[object]
# 6 = DEF/UNDEF 
# ./path_to_lib.a[object_in_lib.o]: [152]   |             0|           0|FUNC |GLOB |0    |UNDEF  | func_name_in_obj

my $count = 20;

my %defs     = ();
my %connects = ();

# break appart the result and map what are uages of functions and what are definations

print " // mapping functions...\n";
foreach $line (@ret)
{
    chomp($line);
    @cols = split(/\|/, $line);

    $file  = $cols[0];
    $undef = $cols[6];
    $func  = $cols[7];

    # $file =~ s/:.*//;  # for in lib object inter-dependency...
    $file =~ s/\[.*//;    # for lib file inter-dependency...
    if( $undef =~ /UNDEF/ )
    {
 $connects{ $func }{ $file } = 1;
    }
    else
    {
 $defs{$func} = $file;
    }
}

# now merge the usage with the definition as the file linkage
my %map = ();

print " // merging func usage to definition...\n";
foreach $func (sort keys %connects)
{
    $files = $connects{$func};
    $def_file = $defs{$func};

    foreach $file (sort keys %$files)
    {
 $map{$def_file}{$file} = 1;
    }
}

my %node = ();
my $node_count = 0;

print " // generating node index...\n";
foreach $src_file (sort keys %map)
{
    unless( exists( $node{$src_file} ) )
    {
 $node{$src_file} = $node_count;
 $node_count = $node_count + 1;
    }

    $files = $map{$src_func};
    foreach $tar (sort keys %$files)
    {
 unless( exists( $node{$tar} ) )
 {
     $node{$tar} = $node_count;
     $node_count = $node_count + 1;
 }
    }
}

print " // generating dot file...\n";

print "digraph linkmapping {\n";
foreach $key (sort keys %node)
{
    $idx = $node{$key};
    print "    n$idx [label=\"$key\"];\n";
}

foreach $src_file (sort keys %map)
{
    $files = $map{$src_func};
    $src_node = $node{$src_file};

    foreach $tar (sort keys %$files)
    {
 $tar_node = $node{$tar};
 print "    n$tar_node -> n$src_node;\n";
    }
}
print "}";

Execute with the following (assuming you called the saved file "nm_deps.pl"
nm_deps.pl > map.dot
dot map.dot -Tpng -omap.png

count the occurace of words in a file

cat filename | tr '[:punct:]' ' ' | tr ' ' '\012' | sort | uniq -c | sort -n