Archive

Archive for the ‘coding’ Category

using libcurl to download files

July 25th, 2010 thomas No comments

this example shows how to use curl for downloading a file in c++ with a class and a progress callback.

this is pseudocode, means you need some header for this to work, etc :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#define CURL_STATICLIB
#include <stdio.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>

class MyClass;

typedef struct job_info_t {
 int jobID;
 MyClass *parent;
} struct job_info_t;

int progress_callback(void *data, double download_total_size, double download_total_size_done, double ultotal, double uldone)
{
    job_info_t *jinfo = (job_info_t *)data;
    if(!jinfo) return 1;
   
    jinfo->parent->reportProgress(jinfo->jobID, download_total_size, download_total_size_done);
    return 0;
}

void MyClass::reportProgress(int jobID, double download_total_size, double download_total_size_done)
{
    printf("DLFile-%04d|progress: %0.3f\n", jobID, download_total_size_done / download_total_size);
}

int MyClass::downloadFile(int jobID, std::string localFile, string url)
{
    job_info jinfo;
    jinfo.parent = this;
    jinfo.jobID = jobID;
   
    CURL *curl = curl_easy_init();
    if(!curl)
    {
        printf("DLFile-%04d|error creating curl: %s\n", jobID, localFile.string().c_str());
        printf("DLFile-%04d|download URL: %s\n", jobID, url.c_str());
        return 1;
    }
    FILE *outFile = fopen(localFile.string().c_str(), "wb");
    if(!outFile)
    {
        perror("error opening file");
        printf("DLFile-%04d|error opening local file: %s.\n", jobID, localFile.string().c_str());
        printf("DLFile-%04d|download URL: %s\n", jobID, url.c_str());
        return 2;
    }

    char *curl_err_str[CURL_ERROR_SIZE];
    memset(curl_err_str, 0, CURL_ERROR_SIZE);

    CURLcode res;
    curl_easy_setopt(curl, CURLOPT_URL,              url.c_str());
   
    // logging stuff
//  curl_easy_setopt(curl, CURLOPT_STDERR,           InstallerLog::getSingleton()->getLogFilePtr());
    curl_easy_setopt(curl, CURLOPT_ERRORBUFFER,      curl_err_str[0]);

    // progess stuff
    curl_easy_setopt(curl, CURLOPT_NOPROGRESS,       0);
    curl_easy_setopt(curl, CURLOPT_PROGRESSDATA,     &jinfo);
    curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &progress_callback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA,        outFile);

    // http related settings
    curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,   1); // follow redirects
    curl_easy_setopt(curl, CURLOPT_AUTOREFERER,      1); // set the Referer: field in requests where it follows a Location: redirect.
    curl_easy_setopt(curl, CURLOPT_MAXREDIRS,        20);
    curl_easy_setopt(curl, CURLOPT_USERAGENT,        "YourUserAgent");
    curl_easy_setopt(curl, CURLOPT_FILETIME,         1);
   
   
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
    fclose(outFile);

    // print curl error if existing
    if(res != CURLE_OK)
    {
        printf("DLFile-%04d| CURL returned: %d\n", jobID, res);
        printf("DLFile-%04d| CURL error: %s\n", jobID, curl_err_str);
        return 3;
    }
    return 0;
}
Categories: coding, cpp Tags:

automatic crash logging on linux using gdb

July 15th, 2010 thomas No comments

The service we wrote for linux has still some segfaults and other problems, so we needed a way of automatically logging and restarting broken servers. When using the crashhandler mode of our init script, the whole service will be wrapped in gdb. Upon crash, it logs the callstack and the last 3 frames to a file and quits. The init script will then restart the service again.

you can view the init-script code here

Categories: coding Tags:

working a bit on litesql

June 29th, 2010 thomas No comments

created two tickets for litesql:
- MSVS compile problem: 27 (fixed already)
- CMake buildsystem improvements: 28

Categories: coding Tags:

working with litesql

June 27th, 2010 thomas No comments

reported a bug that was quickly fixed by gulliver: http://sourceforge.net/apps/trac/litesql/ticket/27

Categories: coding Tags:

finding out the linux distribution and version in CMake

June 23rd, 2010 thomas No comments

actually its quite hacky, but works well here:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
   # use the LSB stuff if possible :)
   EXECUTE_PROCESS(
      COMMAND cat /etc/lsb-release
      COMMAND grep DISTRIB_ID
      COMMAND awk -F= "{ print $2 }"
      COMMAND tr "\n" " "
      COMMAND sed "s/ //"
      OUTPUT_VARIABLE LSB_ID
      RESULT_VARIABLE LSB_ID_RESULT
   )
   EXECUTE_PROCESS(
      COMMAND cat /etc/lsb-release
      COMMAND grep DISTRIB_RELEASE
      COMMAND awk -F= "{ print $2 }"
      COMMAND tr "\n" " "
      COMMAND sed "s/ //"
      OUTPUT_VARIABLE LSB_VER
      RESULT_VARIABLE LSB_VER_RESULT
   )
   #message("LSB output: ${LSB_ID_RESULT}:${LSB_ID} ${LSB_VER_RESULT}:${LSB_VER}")
   if(NOT ${LSB_ID} STREQUAL "")
      # found some, use it :D
      set(INSTALLER_PLATFORM "${LSB_ID}-${LSB_VER}" CACHE PATH "Installer chosen platform")
   else(NOT ${LSB_ID} STREQUAL "")
      set(INSTALLER_PLATFORM "linux-generic" CACHE PATH "Installer chosen platform")
   endif(NOT ${LSB_ID} STREQUAL "")
Categories: coding Tags:

tuning the eclipse look (and feel)

June 10th, 2010 thomas No comments

well, to say it right off, the default eclipse GUI for me is:

  • bloated with features
  • far too big
  • unusable without tuning

following are some hints and tips how to tune eclipse to your likings:

a) tune fontsize in gtk
get the gtk-chtheme program. Under gentoo:

1
2
emerge -av x11-themes/gtk-engines x11-themes/gtk-chtheme
gtk-chtheme

select a theme you like and use the font button on the lower left side of the dialog. (for example i use Verdana regular, size 6)

b) tune eclipse fontsize:
open via the menu in eclipse:
Window -> Preferences : type “font” in the search bar on the top left side.

select “General -> Appearance -> Colors and Fonts”
type “font” in the new search bar on the right side.

Change the fonts as you like.

Categories: coding Tags:

embedding Ogre 1.7 into wxWidgets 2.9 cross platform

May 17th, 2010 thomas No comments

well, its harder than you might think, but i finally found this to be working quite well:

wxutils.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * File:   wxutils.h
 * Author: thomas
 *
 * Created on May 12, 2010, 9:23 AM
 */


#ifndef _WXUTILS_H
#define _WXUTILS_H

#include <wx/wx.h>
#include <string.h>

std::string getWindowHandle(wxWindow *window);

#endif  /* _WXUTILS_H */

wxutils.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "wxutils.h"

#include <iostream>
#include <sstream>

// this is in a separate file to reduce the clutter in the global namespace and their conflicts

#if OGRE_PLATFORM == OGRE_PLATFORM_LINUX
# ifdef __WXGTK__
#include <gdk/gdk.h>
#include <gtk/gtk.h> // just this should suffice as it should include gdk.h itself
/* Seems to be needed under Linux */
//#include <wx/gtk/win_gtk.h>
#include "wx/gtk/private/win_gtk.h"
#include <gdk/gdkx.h>
#include <GL/glx.h>

#if  wxCHECK_VERSION(2, 9, 0)
#define piz(wxwin) WX_PIZZA((wxwin)->m_wxwindow)
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
        GDK_WINDOW_XWINDOW(((GtkWidget*)piz(wxwin))->window) : \
        GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)

#else
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
        GDK_WINDOW_XWINDOW(GTK_PIZZA((wxwin)->m_wxwindow)->bin_window) : \
        GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)

#endif

#ifdef __WXX11__
#include "wx/x11/privx.h"
#define GetXWindow(wxwin)   ((Window)(wxwin)->GetHandle())
#endif

# endif //__WXGTK__
#endif //OGRE_PLATFORM_LINUX

std::string getWindowHandle(wxWindow *window)
{
#if WIN32
    std::stringstream sstr;
    size_t hWnd = (size_t)window->GetHandle();
    sstr << hWnd;
    std::string result = sstr.str();
    return result;
#else

    // TODO: Someone test this. you might to use "parentWindowHandle" if this
    // does not work.  Ogre 1.2 + Linux + GLX platform wants a string of the
    // format display:screen:window, which has variable types ulong:uint:ulong.
    GtkWidget* widget = window->GetHandle();
    //gtk_widget_set_double_buffered (widget, FALSE);
    gtk_widget_realize( widget );   // Mandatory. Otherwise, a segfault happens.
    //XSync(GDK_WINDOW_XDISPLAY(widget->window), False);
   
    Display* display = GDK_WINDOW_XDISPLAY( widget->window );

   
    //Window wid = GetXWindow(window);
    Window wid = GDK_WINDOW_XWINDOW( widget->window );

    std::stringstream sstr;
    /* Get the right display (DisplayString() returns ":display.screen") */
    std::string displayStr = DisplayString( display );
    displayStr = displayStr.substr( 1, ( displayStr.find( ".", 0 ) - 1 ) );

    int screenNum = DefaultScreen( display );

    /* Put all together */
    printf("using display: %s\n", displayStr.c_str());
    printf("using screen: %d\n", screenNum);
    printf("using window: 0x%x\n", wid);

    // old format:
    sstr << displayStr << ':' << screenNum << ':' << wid;
    std::string result = sstr.str();

    printf("getWindowHandle() = %s\n", result.c_str());
    return result;
#endif
}

how to use (panel_viewport is the widget that should show the 3d content):

1
2
3
4
5
6
...
mWindow = mRoot->initialise(false);
Ogre::NameValuePairList params;
params["externalWindowHandle"] = getWindowHandle(panel_viewport);
mWindow = Ogre::Root::getSingleton().createRenderWindow("OgreRenderWindow", 640, 480, false, &params);
...

works great so far under windows 7 and ubuntu 9.10 with Ogre 1.7 and wxWidgets 2.9

Categories: coding, cpp Tags:

WxWorkspaceView patched

May 17th, 2010 thomas No comments

so, i recently discovered this wxworkspaceview and the pixelization really bothered me, so i patched it away (issue link)

some before/after comparison (compare the borders):

Categories: coding Tags:

interactive graphs in wxWidgets

May 16th, 2010 thomas No comments
Categories: coding Tags:

CPU flags explained (linux)

April 16th, 2010 thomas No comments

i had to find some cpu flags and their descriptions, so i wrote a little utility script that explains what features your CPU has. It uses your kernel’s cpuinfo includes to gather the descriptions and /proc/cpuinfo to get the actual flags. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$ ./cpufeatures.py
                 fpu : Onboard FPU
                 vme : Virtual Mode Extensions
                  de : Debugging Extensions
                 pse : Page Size Extensions
                 tsc : Time Stamp Counter
                 msr : Model-Specific Registers
                 pae : Physical Address Extensions
                 mce : Machine Check Exception
                 cx8 : CMPXCHG8 instruction
                apic : Onboard APIC
                 sep : SYSENTER/SYSEXIT
                mtrr : Memory Type Range Registers
                 pge : Page Global Enable
                 mca : Machine Check Architecture
                cmov : CMOV instructions
                 pat : Page Attribute Table

cpufeatures.py :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/env python
import re

cpuflag_descriptions = {
   # filled during runtime, see readLinuxKernelCPUFeatures
}

def readLinuxKernelCPUFeatures():
   f = open('/usr/src/linux/arch/x86/include/asm/cpufeature.h', 'r')
   lines = f.readlines()
   f.close()
   for line in lines:
      m = re.search('#define X86_FEATURE_([A-Za-z0-9_]*).*\/\* (.*)\*\/', line)
      if m is None:
         continue
      cpuflag_descriptions[m.group(1).lower()] = m.group(2)

def readCPUInfo():
   f = open('/proc/cpuinfo')
   lines = f.readlines()
   f.close()
   return lines

def getCPUInfo():
   result = {}
   lines = readCPUInfo()
   proc = -1
   for line in lines:
      args = map(lambda arg: arg.strip(), line.split(':'))
      if len(args) != 2:
         continue
      if args[0] == 'processor':
         proc = int(args[1])
         result[proc] = {}
         continue
      if args[0] == 'flags':
         args[1] = args[1].split()
      result[proc][args[0]] = args[1]
   return result

def processCPUFlags(cpuinfo, cpu=0):
   flags = cpuinfo[0]['flags']
   for f in flags:
      if not f in cpuflag_descriptions:
         print "%20s : unkown" % (f)
      else:
         print "%20s : %s" % (f, cpuflag_descriptions[f])

def explainCPUFlags():
   cpuinfo = getCPUInfo()
   processCPUFlags(cpuinfo)

readLinuxKernelCPUFeatures()
#print cpuflag_descriptions
explainCPUFlags()
Categories: coding, hardware Tags: