Powered by
Movable Type 3.38 mod_perl/2

 Programmierung Archives

2006, October 12 (Thu)

Linux, Programmierung Deutsche Umlaute auf Tastatur mit US-Layout

Wer viel programmiert hat wahrscheinlich bemerkt, dass das englische Tastaturlayout dafür besser geeignet ist, als das deutsche, da dort viele Sonderzeichen leichter zu erreichen ist. Allerdings stehen dort keine Umlaute zur Verfügung. Oft kann man diese zwar nur ae, oe, ue ersetzen, in formellen Schreiben ist dies jedoch eher unerwünscht.
Um dieses Problem zu umgehen, habe ich mir eine Keymap für xmodmap angelegt.
Diese ermöglicht es mir, die zweite Windows-Taste (Menü-Taste) als eine Art zweites Shift zu benutzen. D.h. mit Win2+a mache ich ein ä, mit Shift+Win2+a ein ä. Selbiges natürlich auch bei ÖöÜüß.

Um dies zu erreichen legt man eine ~/.Xmodmap mit folgendem Inhalt an:
keycode 117 = Mode_switch
keycode  38 = a A adiaeresis Adiaeresis
keycode  30 = u U udiaeresis Udiaeresis
keycode  32 = o O odiaeresis Odiaeresis
keycode  39 = s S ssharp
Um das Layout sofort zu aktivieren, einfach xmodmap ~/.Xmodmap ausführen.

Der Keycode 117 steht dabei für die Windows-Menü-Taste. Die Keycodes von anderen Tasten kann man über das Programm xev herausfinden. Die Namen von weiteren Sonderzeichen aus anderen Sprachen kann man sich aus den jeweiligen Layouts in /usr/share/xmodmap heraussuchen.


2007, January 31 (Wed)

Netzwelt, Programmierung, Web Dump Irssi Scripts

Don’t get me wrong, please. I haven’t written these scripts — I was
merely using them.

Because I’ve got them lying ‘round in my so called “Web Dump”
for a long time now, I thought I’d write a slightly more comprehensive
introduction to what made my life easier just a little while ago.

I hope this can be of interest to some of you, and maybe I have a lucky
hand with keywords so that even more people can benefit from these nice
scripts when a search engine spider picks them up.

I have lsted these works in no particular order:


2007, June 28 (Thu)

Linux, Programmierung Direct I/O filesystem access in Linux with O_DIRECT

I just tried to read files from the filesystem using direct i/o to avoid the operating systems caching effects.
The manpage of open lists the flag O_DIRECT for this. Although I included fcntl.h, my compiler complained that it couldn't find O_DIRECT:

contrib/myfile.c:44: error: ‘O_DIRECT’ undeclared (first use in this function)

The solution was easy:
You just have to

#define _GNU_SOURCE

before including fnctl.h.

This is actually mentioned in the manpage, but only in a footnote that is easy to overlook.


2007, October 24 (Wed)

Linux, Programmierung thttpd and hgweb.cgi

hgweb.cgi, which is a Pythonscript to provide webaccess to a hg repository, does not work out of the box with thttpd.

The error is:

KeyError: 'REQUEST_URI'
args = ('REQUEST_URI',)

The quick-and-dirty fix is kinda simple; write a small wrapper which sets REQUEST_URI and runs hgweb.cgi. The content of REQUEST_URI seems to do not matter at all.

#!/bin/sh
export REQUEST_URI="/blafuck/"
exec ./hgweb.cgi


2009, March 09 (Mon)

Programmierung Jabber components

I could not really find an easy example how to get started with Jabber components (using xmpp4r), so this I will cover the absolute basics here.

You will need a Jabber server to connect your component to. I suggest to install ejabberd locally. Also needed is xmpp4r, which can be installed through rubygems (e.g. using sudo gem install xmpp4r). Feel free to use your favourite package manager to install it, if packaged.

The following snippet is used to configure ejabberd to listen for your component. After altering the configuration, do not forget to restart ejabberd. DNS seems to be important for elang-stuff, so make sure the host you use (e.g. localhost) can be resolved properly.

 {5348, ejabberd_service, [
                            {access, all}, 
                            {shaper_rule, fast},
                            {ip, {127, 0, 0, 1}},
                            {host, "mycomponent.localhost",
                             [{password, "secret"}]
                            }
                           ]},

Take a look at the sample configfile and use a predefined, commented service configuration, so you won’t have to fiddle with erlang-stuff.

Now you can create this small file, name it mycomponent.rb and use the following content:

require 'xmpp4r'

class MyComponent < Jabber::Component
	def initialize config
		super config['jid']
		
		# connect to the main server
		connect config['server'], config['port']
		
		# authenticate
		auth config['password']
		
		add_message_callback do |message|
			Thread.new do
				handle_message message
			end
		end
		
		# stop this thread
		Thread.stop
	end
	
	# handles incoming messages
	def handle_message message
		puts "received message from #{message.from}: #{message.body}"
	end
end

# configuration
config = {
	'jid' => 'mycomponent.localhost',
	'server' => 'localhost',
	'port' => 5348,
	'password' => 'secret'
}

# instantiate, connect and auth
mycomponent = MyComponent.new config

Using this kind of Hash as configuration makes it very easy to use YAML to store your configuration in a file. If ruby can’t find xmpp4r, you might have to require 'rubygems' before, if you installed xmpp4r via rubygems.

Now you can run your component and send messages to mycomponent.localhost. The handle_message will print any messages received.

Programmierung Ruby Logger Singleton

Sometimes you might want a Singleton Logger in Ruby. Here is what works:


require 'logger'
require 'singleton'

class Logger
    include Singleton
    @@old_initialize = Logger.instance_method :initialize

    def initialize
        @@old_initialize.bind(self).call(STDERR)
    end  
end


2009, March 19 (Thu)

Programmierung Jabber component mapping Microblogging to Multi-User-Chat

I have put some glue between xmpp4r and twitter, et voila, a Jabber component mapping
microblogging to a Jabber Multi-User-Chat.

Working right now:


  • friends joining as participants

  • friends timeline

  • posting updates

  • sending and receiving direct/private messages

Missing:


  • follow/unfollow (I'd like to implement this as invite/kick)

  • blocking (ban)

  • avatars (can be easily done)

  • a lot more

You can try this out by joining the MUC-room {identi.ca|twitter}@omb.jabber.teamidiot.de
and using your username as nickname and providing your password.
As this is not safe (I might steal your password) I suggest to setup your own component,
try it out and provide feedback.

The repository (mercurial) is at: http://gonzo.teamidiot.de/repos/ombmuc/.


2009, October 04 (Sun)

Programmierung calling C++ bind-bound functions with variable parameters

/*

Consider this following small example program.

*/
#include <functional>
#include <iostream>

using namespace std::placeholders;

/*

We write a subroutine that outputs the multiple of its arguments, a and b.

*/
void multiple(int a, int b, const std::string & who) {
  std::cout << who << ":\t" << a << "×" << b << "=" << a*b << std::endl;
}

int main() {
  /*

We then call it from main.

 */
  multiple(2,3,"direct");

  /*

Next, we want to use bind to bind some arguments and call it. Unsurprisingly, the output is the same.

 */
  std::bind(multiple,2,3,"bind")();

  /*

We can also store this bound function and call it:

 */
  auto bound(std::bind(multiple,2,3,"bound"));
  bound();

  /*

Things get wicked, when we want to predefine only one parameter, though :(

 */
  //std::bind(multiple,2,_1,"bindp")(3); // you wish!
Continue reading “calling C++ bind-bound functions with variable parameters ” »


2010, May 26 (Wed)

Programmierung cpan for local user configuration

To install perl modules without root permission, this seems to work for me within cpan:

o conf mbuildpl_arg "\
--lib=~/myperl/lib \
--installman1dir=~/myperl/man/man1 \
--installman3dir=~/myperl/man/man3 \
--installscript=~/myperl/bin \
--installbin=~/myperl/bin \
--install_base=~/myperl"

o conf makepl_arg "\
LIB=~/myperl/lib \
INSTALLMAN1DIR=~/myperl/man/man1 \
INSTALLMAN3DIR=~/myperl/man/man3 \
INSTALLSCRIPT=~/myperl/bin \
INSTALLBIN=~/myperl/bin \
INSTALL_BASE=~/myperl"

o conf commit

Then add something like

export PERL5LIB=~/myperl/lib:~/myperl/lib/perl5/site_perl/5.10.0${PERL5LIB:+:}$PERL5LIB

to your .zshenv or environment profile. I had to add the site_perl dir because Sub::Uplevel installed there, yes, I’m too lazy to investigate. So beware of strange makefiles.

Now you can restart cpan with the PERL5LIB environment set and should be able to install modules.

I had to switch INSTALL_BASE for PREFIX in the past, ymmv.


2010, June 18 (Fri)

Programmierung int log2 in gcc / c++

I wonder why this result isn’t on google’s #1 for int log2 gcc.

To get the integer logarithm to the base of 2, try

      sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(   n );
     sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(  n );
sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll( n );