2009-04-30

Mingw on Linux

I'm using MinGW for years, but never tried to use it on Linux machine as a crosscompiler. Today I've had first experience. First thing I've noticed is how actually easy to install such environment - there is a totally automated build script available from MinGW site.
Pros:
  • One environment
  • Speed. Native environment vs MSys is much, much faster.
Cons:
  • Building non-Linux things on Linux means transferring them from one machine to another.

2009-04-29

Fastest way to create database in PostgreSQL

This will create database and it's associated role from Linux shell:
su postgres
createuser -SDRPE user
createdb -E UTF8 -O user database

Fixing OpenSUSE Home/End keys

Comment lines 135-136 in /etc/inputrc :
#"e[1~":       history-search-backward
#"e[4~":       set-mark

2009-04-27

Icon and version information resource file

This should be an update for one of the recent posts.

resource.rc:
ID ICON "path/to/my.ico"
...the ID can be anything. It doesn't matter unless it is referred in the code.

Run windres as follows:
windres resource.rc -O coff -o resource.o
Include resource.o along with other object files while linking, e.g.,
g++ -o app obj1.o obj2.o resource.o

And, at no extra charge, it is possible to include version information to the application, adding the following boilerplate to resource.rc file:
1 VERSIONINFO
FILEVERSION     1,0,0,0
PRODUCTVERSION  1,0,0,0
BEGIN
    BLOCK "StringFileInfo"
    BEGIN
        BLOCK "040904E4"
        BEGIN
            VALUE "CompanyName", "Company name"
            VALUE "FileDescription", "Application name"
            VALUE "FileVersion", "1.0"
            VALUE "InternalName", "my_app"
            VALUE "LegalCopyright", "Copyright info"
            VALUE "OriginalFilename", "my_app.exe"
            VALUE "ProductName", "My App"
            VALUE "ProductVersion", "1.0"
        END
    END
    BLOCK "VarFileInfo"
    BEGIN
        VALUE "Translation", 0x409, 1252
    END
END

Links:
- Original post
- Additional info

Credit goes to Evan McLean.

2009-04-16

C Lua snippets

Get currently executed chunk name:
lua_Debug lua_info;
lua_getstack(L, 0, &lua_info);
lua_getinfo(L, "n", &lua_info);
string name = lua_info.name;
Opposite to lua_register:
lua_pushnil(L);
lua_setglobal(L, name.c_str());
stack_dump in C++ flavour ;)
static void stack_dump (lua_State *L) {
    int top = lua_gettop(L);
    for(int i = 1; i <= top; i++) {
        int t = lua_type(L, i);
        switch(t) {
            case LUA_TSTRING:
                cout << "'" << lua_tostring(L, i) << "'";
                break;
            case LUA_TBOOLEAN:
                cout << (lua_toboolean(L, i) ? "true" : "false");
                break;
            case LUA_TNUMBER:
                cout << lua_tonumber(L, i);
                break;
            default:
                cout << lua_typename(L, t);
                break;
        }
        cout << "  ";
    }
    cout << endl;
}

2009-04-12

Gettext compilation on MSys

Trying to build gettext library on MSys I've got some errors related to pthread. Included readme states you need to have Cygwin for that. For those, who prefer MSys over Cygwin here goes a quick fix:
./configure
Add -lpthread to LIBS section in gettext-runtime/src/Makefile and gettext-tools/src/Makefile.
Remove tests target from gettext-runtime/Makefile and gettext-tools/Makefile.
make; make install
Be happy.

2009-04-07

Libiconv compilation sed on msys error

Trying to build libiconv library under msys environment, I've got sed related errors:
sed: -e expression #2, char 32: Extra characters after command
Fortunately, I've found a solution in the internet:
diff -ur tmp/libiconv-1.12/windows/windres-options src/libiconv-1.12/windows/windres-options
--- tmp/libiconv-1.12/windows/windres-options 2007-05-27 19:42:10 +0100
+++ src/libiconv-1.12/windows/windres-options 2008-05-28 13:41:03 +0100
@@ -14,20 +14,29 @@
fi
version="$1" # something like 2.0 or 2.17 or 2.17.3 or 2.17.3-pre3

-sed_extract_major='/^[0-9]/{s/^([0-9]*).*/1/p;q}
+sed_extract_major='
+/^[0-9]/{
+  s/^([0-9]*).*/1/p
+  q
+}
a
0
-q
'
-sed_extract_minor='/^[0-9][0-9]*[.][0-9]/{s/^[0-9]*[.]([0-9]*).*/1/p;q}
+sed_extract_minor='
+/^[0-9][0-9]*[.][0-9]/{
+  s/^[0-9]*[.]([0-9]*).*/1/p
+  q
+}
a
0
-q
'
-sed_extract_subminor='/^[0-9][0-9]*[.][0-9][0-9]*[.][0-9]/{s/^[0-9]*[.][0-9]*[.]([0-9]*).*/1/p;q}
+sed_extract_subminor='
+/^[0-9][0-9]*[.][0-9][0-9]*[.][0-9]/{
+  s/^[0-9]*[.][0-9]*[.]([0-9]*).*/1/p
+  q
+}
a
0
-q
'

{
Credit goes to Keith Marshall.

Tango patcher

For those who are not scared to apply mods on Windows, check out this hot Tango patcher. It is based on one of my favorite open source childs, Tango Desktop Project, and will change almost all the Windows system icons to Tango ones.. And yes, there is Vista version too.

2009-04-06

Toshiba U300-154 upgrade

Toshiba U300-154
Something happened yesterday. I've promised to my customer to replace the broken HDD for it's laptop. Unfortunately I was unable to find any 2.5" Sata HDD, except expensive shops that are working on sundays. So, I've got to put out HDD out of my lovely Toshiba U300-154. Looking at my semi-dead laptop I decided that this is a good time for some upgrades.
And.. voila! My laptop now has 4GB of RAM instead of 2, and awesome Seagate's 500GB HDD with G-Force sensor instead of 200GB with no mods...

2009-04-01

Set up AVR timer interrupt

/* initialize timer interrupt */
void init_isr_timers() {
    /* OC1A/OC1B disconnected */
    TCCR1A = 0;
    /* CTC mode, top in OCR1A / Timer clock = CLK/1024 */
    TCCR1B = (1<<WGM12)|(1<<CS12)|(1<<CS10);
    /* set OCR1A top value for 20Hz (10 periods in one second) */
    /*   0x0168 for CPU @ 7.3728MHz */
    OCR1A = 0x0168;
    /* enable Timer/Counter 1 interrupt */
    TIMSK = (1<<OCIE1A);
}

/* timer/counter 1 interrupt handler */
ISR(TIMER1_COMPA_vect) {
    /* Ohhh, lovely! :) */
}

int main() {
    /* initialize timer interrupt */
    init_isr_timers();
    /* enable interrupts */
    sei();

    /* main loop */
    for(;;) {}
}

Store an array in a session

// begin the session
session_start();

// create an array
$my_array = array('blonde', 'girls', 'are', 'stupid');

// put the array in a session variable
$_SESSION['truth'] = $my_array;

Add application icon on Eclipse with MinGW

source\resource.rc:
RESOURCE_ICON_APP ICON DISCARDABLE "../files/app.ico"
Project > Properties > C/C++ Build > Settings > Tool Settings > MinGW C++ Linker > Miscellaneous > Other objects:
Release\source\resource.o
Project > Properties > C/C++ Build > Settings > Build Steps > Pre-build steps > Command:
windres -i ..sourceresource.rc -o sourceresource.o

Create MinGW library from dll

For this, you need pexports tool from mingw-utils.
pexports library.dll > library.def
dlltool -D library.dll -d library.def -l library.dll.a
Note, that if dll was built by different environment than gcc, this probably will not work because of the naming conventions. If this the case, read here.