6/05/2006

Easy Perl callbacks for SWIG

Took me a bit of time to figure this out. SWIG is an excellent tool for wrapping C++ libraries such that they become callable from scripting languages. The one thing I found lacking (and I think many others have too) is the inability to register perl code as a C/C++ callback.
The people who maintain SWIG like to do this with something they call a "director", which is basically a way to let you override virtual functions from the scripting language. I see two problems with that:
1 - There's no Perl support for it yet, and there doesn't seem to be any coming
2 - Doing callbacks by overriding classes is a pain in the butt. If I want (e.g.) a button press to be labeled "quit" and to quit the program when pressed, I should be able to pass in: sub{ exit(0); } to the button, not override a silly buttonListener class.
Anyway, stealing liberally from code I've found on the net, I managed to put together something very easy and usable. It also makes use of the excellent boost::function and boost::bind wrappers, but allows you to pass a perl anonymous subroutine or subroutine refrence as if it were either a void(void) C function, or a boost::function. That means there's no way to support a closure (which you don't need because Perl supports them on anonymous subs directly), and there's no way for the callback to pass an argument back (but you could just pass something in when you register the callback anyway).

Without further ado, here's a header to be swigged and included before any callback is defined, that should make using Perl subs as C++ callbacks completely transparent:
PerlFunction.h

11/11/2005

Using relative directories from shell

I'm a stickler for using exactly the same code in production and development environments. One nice solution to this problem, even though it's not officially 100% possible to do in Unix, is to use directories relative to your executable for all config/support/data files.

I say officially, because in theory a program can be started in such a way that it's mistaken about where it lives. But it's only in really rare cases, done on purpose, that this comes up in practice.

Anyway, in Perl there's nice little module called FindBin that lets you do this easily. But I've never known how to do it nicely in shell.

Well, this bit of ksh seems to work in every case I've tried:

Bin=$(d=$(dirname $0);print $d | grep '^/' || print $(pwd)/$d)

Pretty cool, eh? I'd always thought that if you run something from your $PATH variable, $0 would only have the name relative to whatever it matched in your path. But it turns out modern shells are much nicer about it than that, and always pass the full path from / in this case.

11/04/2005

rikaichan

I just got time to look at rikaichan,

which is a version of the rikaixul plugin for firefox, with many improvements by some fellow named Jon Zarate. I'm very impressed.

The guy didn't make a proper installation link, but one can install it (on windows) by saving the .xpi file to your desktop, opening the file with mozilla, then restarting.

One I get a chance to examine the code I'll probably borrow it back as the next version of rikaixul, but based on first impressions, there are many improvements:
1 - much faster load-time and more efficient afterwards
2 - it stays on for a tab, even when following links
3 - better single-kanji lookup with the shift-key
4 - effectively works as a dictionary as well

Problems:
1 - he doesn't mention the license, which had better be GPL. Since he did the work to make a nice about-box, etc. and made a homepage, it should be made very clear.

2/18/2005

The Burnmeister and Asskisser

I'm afraid my potty-mouthed attempts at computational-linguistic humor have started to interfere with Rikai's use at high-schools, etc.. Go figure!

Anyway, as a result, the permanent home for the links will have to stay here:
The Burnmaker (turns any old text into an extremely foul rant). If that doesn't sound amusing, or you're at school, don't go there.

The Asskisser (attempts, to the extent that this sort of thing is possible, to do the reverse).

---
25 Mar 2007: changed name & links to the burnmaker.com domain (was the "burnmeister")

12/29/2004

RikaiXUL update (use Rikai-functionality directly from Firefox)

(note: this article is out of date. Please see http://rikaixul.mozdev.org/installation.html for the latest version)


I've taken some of the new popup code, merged it with the code I used to have in RikaiXUL, and now you can have Japanese popups on any web-page without ever touching rikai.com
Install RikaiXUL v0.4

Released under the GPL.
To use:
- you MUST use the firefox browser.
- go to a Japanese web-page you want to read.
- right-click, and choose "rikaixul!" from the popup menu, or use tools/rikaixul! from the main menu.
- mouseover to see popups.
- use shift-rightclick (technically, any mouseup event not on button 1) to pop up information on a single kanji

thats it!
This is released under the GPL.
Please let me know if you use this in an educational setting (you may, you may!).

12/27/2004

New popup method released under the GPL

For the Japanese Rikai site I've developed a new way of doing both the popups and the mediation (i.e., proxying the web through rikai.com). It turns out this is all much simpler when done in Javascript in modern browsers, as I can let the client parse the html for me.

Here's the source:
  • Adding popups for Japanese
  • Rewriting URLs to point back to Rikai.com

    You're welcome to help get these work faster, integrate into Mozilla, or whatever..
    Some notes:
    - the popups load from a small script at http://www.rikai.com/perl/popup.pl. Because of browser security features, this will not work if the main page did not in the rikai.com domain. If you need popups to help develop, you'll need to repoint this at a local file or something.
    - the mediator points to a script at http://www.rikai.com/proxy. I have not as yet released this script as yet, but it does little other than spit back the html from the mediate_uri parameter, plus the small amount of javascript to add popups and repoint URLs.. Currently, that amounts to:

    <script language="JavaScript1.2" src="http://www.rikai.com/mediate/mediate.js">
    </script>
    <script language="JavaScript1.2" src="http://www.rikai.com/popups/popup-script3.js"></script>

    at the start of the body, and:

    <script language="Javascript1.2">addPopups();</script>
    <script language="Javascript1.2">proxyMediateAll();</script>

    at the end.
    It also plops in a base href tag so that relative path images and such load from the original server.
  • 12/17/2004

    Rikaixul -- The Rikai plugin to Mozilla

    (note: this article is out of date. Please see http://rikaixul.mozdev.org/installation.html for the latest version)


    Excited by XUL, I started to write a mozilla browser plugin to do what rikai does. The project is archived at the mozdev rikaixul, but I gave up on it after a while (and having a kid). It is functional, and in some ways better than the web-page (more below).

    Well, I'm happy to report that much of the code lives on in Moji, which is very close to being what I was trying to do. It includes all the dictionary and lookup code, even cleaned up a bit. But for some reason, Erwin (the maintainer) has never plugged in my mouseover code from rikaixul. All that needs to be done is to have a button which goes through the DOM of the main document and adds javascript callbacks to show that kanji and/or look up words from that location (just put the next 10 chars as an argument into the callback--since it's all local, the size of the doc isn't a big deal).

    All the dictionary/word lookup code is there in rikaixul for the taking. This actually works better than Rikai, because I just try to look up words from whereever you point the mouse. I.e., rikaixul made no attempt to segment the Japanese. I found this actually made it easier to use because I can always parse things better than any automated method, and so didn't as often get hung up on bad segmentation.

    Anyway, from time to time people ask how they can help. I really hope someone will pick this up and add mouseover functionality to moji (though I guess then mozilla users would never use this site, eh?).

    The colored diff "replacement" I've used for years

    If you use a color xterm (most modern ones are), try colordiff instead of sdiff. I'm kinda proud of the simplicity--it uses a gnu diff feature meant for C code and replaces the #ifdefs with some ansi sequences. The result? A quick color comparison of files.

    12/16/2004

    unistrok file

    Ah ha! I found the unistrok file, the original file full of stroke data for several thousand characters that I spent so much time typing in (when I should have been studying). Since JavaDict had the preprocessed versions without documentation, I was always getting asked about this.
    A little late I suppose :)

    DOS games I wrote in 93 or so

    NBall, a clone of the old Lucasarts game BallBlasters/Blazers or whatever, but with texture mapping to give it a bit more realism. Still works for me, though I can't find the source code to this. The opponent ship is a raytraced romulan I think. :)

    XStar, a top-view shooter. I can't get this running correctly on NVidia, it doesn't seem to like the VGA tricks I did back then. But it worked on another box I tried with dosbox. Includes source code.

    If you want soundblaster sound, or have trouble with these games otherwise, you might have better luck trying to run it from within dosbox, a program made just for this purpose (running old DOS games).

    I'd really like to find the old StarCastle game I made. Somewhere on the net I guess... Wish I'd kept all the really old classic computer stuff. Who knew there'd be emulators?

    Obviously ignore any cheesy "shareware" notices on these things.

    JavaDict, a Jap<->Eng dictionary package with HW recognition

    JavaDict was another little toy I made in grad-school to avoid actually studying Japanese (who had time for that? I was a CS guy!). I can't change that page at the U of A anymore, but I'm content to let it live there for now.

    It won me a laptop from IBM. Funny story--a year after the contest they sent me a laptop model from a year before the contest. It was clearly refurbished, not even in a proper box or with documentation. Then they reported it at it's new value that it sold at originally. So I ended up paying $2K bucks (in taxes) for a refurbished 2 year old laptop. Some prize! Is that how all these things work?

    Anyway, the beauty of the GPL is that elements have been ported to/incorporated into lots of other programs:

    • Robert Wells wrote a palmpilot version of the HW recognition
    • WaJei, which I haven't tried, uses the input method
    • KanjiPad is a x-windows proggie which uses the HW recognition
    • Here's a port I found... Is this for a handheld? That was my dream for this thing
    • Pocket Kanji, another PALM version of the HW recognition
    • Yudit, a text editor, uses the HW recognition for Japanese input
    • Some fellow named Marshal Ramsey made a version of the H/W recognition for the web, hosted on Master Jim Breen's site. Marshal added some very good kana recognition, wrote a paper, and got a PHD. But he didn't GPL it! Jeez. What do people think, should I tell Jim to take it down?
    • PADict, yet another PALMOS implementation of the H/W recognition.

    I have one request to all of these fine folks... If your application can interact with the 'net at all, please give people a way to contribute their actual stroke data to a public database. Real stroke information would make it possible to write real handwriting recognition algorithms (mine's a hack of sorts).

    Bogged.. A free boggle implementation for Linux/Unix/etc.

    Actually, it's possible to run on Windows too (perhaps with minor editing of the startup) if you have wish (part of tcl/tk) installed.

    I haven't touched this since college, but it still works, so now bogged lives here forever.

    Enjoy!

    Object oriented KSH

    I've decided to start off my programming tools page with a tip, something I've never seen anyone else implement or use, but which I find extremely useful.

    I'm a pretty heavy shell user in my day to day work, and use databases a lot (Sybase in my case). I never liked the GUI sql shells, never quite got used to starting emacs to use sqsh. Dammit, I like the shell. I figured there has to be an easier way.

    Sorry I can't post the code, but the trick is to write the function S:
    function S {
    ...
    eval 'function '"$1"' { (
    my_isql_like_thingy '"$2"' '"$3"' '"$4"' ...
    )}'
    }

    The idea is that I can run:
    S mytag SERVER DB USER PASSWORD

    and then whenever I want to run sql on that database, I need only run
    mytag '... sql ...'
    and I can use all the ordinary shell mechanics for editing the line, redirecting, loops, etc...

    Step 2: simple enough... In your startup .rc file, autodefine a bunch of these for the databases you use most often.

    Step 3: write a kick ass my_isql_like_thingy... Mine can present the data three different ways, bcp in/out raw data on stdin (streaming so you can bcp out with one sql and into something else at the same time), or list tables or procs or views, print the text of tables or procs or views. So, e.g., I can whip up simple scripties like:

    for p in $(p2 print views | fgrep Equity) ; do
    print "*** $p"
    p2 cols "$p" | fgrep -i tick
    done

    in a flash...
    Anyway, I find this useful. Where I work is a Sybase shop though, so even if I made a generic version who would care... Anything like this for mysql?