...go to masscommunicating.blogspot.com

Highlight Code in Webpages

This page uses code from http://softwaremaniacs.org/soft/highlight/en/ to highlight syntax. Here is their LICENSE file.

Here is a more comprehensive discussion: http://www.hanselman.com/blog/HowToPostCodeToYourBlogAndOtherReligiousArguments.aspx.

Import many EnsEMBL Databases

You can download EnsEMBL's MySQL databases in one go from ftp://ftp.ensembl.org/pub/current_mysql. Here is a Perl script to import them all into a MySQL database based on http://www.ensembl.org/info/docs/webcode/install/ensembl-data.html. WARNING: THIS SCRIPT DROPS THE DATABASES IT CREATES BEFORE INSTALLING. Read the script before running it. If it does something unintended, it's your own fault.

1. Running the script to test what it will import:

cd {path_where_you_downloaded_the_databases_to}
perl import_dbs.pl -u {mysql user name} -p {mysql password} [-d {name regex filter}]

2. Running the import:

cd {path_where_you_downloaded_the_databases_to}
perl import_dbs.pl -u {mysql user name} -p {mysql password} [-d {name regex filter}] -x

Here is the script.

#!/usr/bin/perl
# import_dbs.pl
use strict;

use Getopt::Std;

use File::Glob ':glob';
use File::Spec qw(catfile);
use File::Basename;
use File::Temp qw(tempfile);

my %opts;

getopts('u:p:d:x', \%opts);

my $mysql_user = $opts{u} || 'root';
my $mysql_pass = $opts{p} ||'';

my $d = $opts{d};
my $execute = $opts{x};

my @files = <*>;

foreach (@files) {
    if (-d) {
        my $dir = $_;
        my $qdir = basename ($dir);

        if (defined ($d)) {
            next if !($qdir =~ /$d/);
        }

        my $f = File::Spec->catfile($dir, $qdir . ".sql.gz");

        if (-f $f) {
            print "Adding DB $qdir (via $f)\n";

            if ($execute) {
                my ($fh, $filename) = tempfile();

                print $fh <<END;
DROP DATABASE IF EXISTS `$qdir`;
CREATE DATABASE `$qdir`;
END
                close $fh;

                print `mysql --user=$mysql_user --password=$mysql_pass < $filename`;
                print `gunzip -c $f | mysql --user=$mysql_user --password=$mysql_pass $qdir`;

                my $txtfiles = my $f = File::Spec->catfile($dir, "*.txt.gz");
                my @tf = bsd_glob ($txtfiles);

                foreach my $txt (@tf) {
                    print "Adding data from $txt\n";
                    `gunzip $txt`;
                    $txt=~ s/\.gz$//;
                    print `mysqlimport --user=$mysql_user --password=$mysql_pass --fields_escaped_by=\\\\ -L $qdir $txt`;
                    `gzip $txt`;
                }
            }
        }
    }
}

Regex find/replace in GEdit

That one was a bit annoying. There seems to be a plugin which doesn't work anymore. You can use gedit-commander, but I couldn't find any documentation for it. Some browsing of the code gives this:

It has some other features, including some support for doxygen tags. Also it's in the gedit-developer-plugins package on Ubuntu oneiric.

Print (source) files in a directory as a LaTeX longtable

#!/bin/perl -w

use strict;

sub listdir {
	my $dirname = shift;
	my @files = <$dirname/*>;
	$dirname =~ s/^\.\///gi ;
	my $output = "";
	foreach (@files) {
		listdir ($_) if (-d);
		unless (-d) {
			s/.*\/(.*)$/$1/gi ;
			s/\_/\\\_/gi ;
			if(m/(c|cpp|h|hpp|asm)$/i) { # change this to pick different files
			# you can also tune the p{7cm} size to change
			# the width of the second column
				$output .= "\\texttt{".$_."} & \\multicolumn{1}{p{7cm}|}{ } \\\\\n";
			}
		}
	}
	unless ($output eq "") { # this prints the directory name
		print "\\hline\\multicolumn{2}{|l|}{\\texttt{".$dirname."}} \\\\\\hline\n";
		print $output;
	}
}

print <<EOF;
\\begin{center}
\\begin{longtable}{|l\@{\\vrule width 2pt}l\@{\\vrule width 1pt}|}

\\caption{...list of files and descriptions...}\\label{tbl:label_name_here}\\\\

\\hline
\\textbf{Filename} & \\multicolumn{1}{l|}{\\textbf{Contents}} \\\\
\\hline
\\endfirsthead

\\hline
\\endlastfoot

\\hline
\\multicolumn{2}{l}{\\textit{Continued on the next page...}}\\\\
\\endfoot

\\multicolumn{2}{r}{\\textit{...continued from last page.}}\\\\
\\hline
\\textbf{Filename} & \\multicolumn{1}{l|}{\\textbf{Contents}} \\\\
\\hline
\\endhead
EOF

print listdir(".");

print <<EOF;
\\hline
\\end{longtable}
\\end{center}
EOF

Remove Mesh Polygons from Mathematica's PDF Output

Use this awesome package by Will Robertson: http://library.wolfram.com/infocenter/MathSource/7029/ .

Convert IPE 6 PDF Files to IPE 7 PDF on Windows

I usually only store the PDF files for my IPE figures. Here is a simple Windows batch script to convert the PDF files in-place (ipe6upgrade wants XML, so it does not work on PDF directly) and create a backup.

set IPE=C:\Program Files\ipe7
copy /Y %1 %1.bak
"%IPE%\bin\ipeextract.exe" %1 %1.xml
"%IPE%\bin\ipe6upgrade.exe" %1.xml %1.ipe
"%IPE%\bin\ipetoipe.exe" -pdf %1.ipe %1
del %1.xml %1.ipe %1.ipe7

SCons Builder to use AMD/ATI's Brook+ Compiler

Download the builder source here . You can use it in SCons scripts to get Objects/C++ code from .br files like this:

root = Environment(
    tools = ['default', 'brook'],
)

root.Append(
	CPPPATH = '$BROOK_CPPPATH',
	LIBPATH = '$BROOK_LIBPATH',
	LIBS = ['brook'],
)

files = [
	root.BrCC('kernel.br'),
	"main.cpp"
]
root.Program('some_program', files);

The path to the compiler is read either from the system environment variable BROOKROOT, or can be specified by declaring an option 'BROOKROOT' in SCons.

NASM/YASM macro for declaring C functions that link regardless of underscores

Some compilers want an underscore, others don't. Here is how I fixed it, better looking suggestions welcome...

%macro public_c_symbol 1
GLOBAL %1,_%1
%1:
_%1:
%endmacro

public_c_symbol my_external_proc:
	; ...
	RET

Assembly.lang file for Gedit + NASM/YASM

I extracted this from a gnome bugzilla entry. I extended the file a little to include some 64-bit registers and instructions. Install e.g. by copying to /usr/share/gtksourceview-2.0/language-specs/ (there might also be a way to use your gedit config in your home directory). Assembly.lang

Four in a row, 3D

This is some very basic program written in Haskell which plays four in a row in 2 or three dimensions. It includes a perl script to run it on a webserver. Download the source here.

XPDF Helper Script

Here is a very simple script that runs Xpdf only once and reloads as long as it is still running, which is useful to run it e.g. from inside Kile or other latex editors. If anyone knows how to do a similar thing with KPDF, please let me know.

#!/bin/bash

ISRUNNING=`ps aux | grep "xpdf -remote sessionname" | grep -v grep | wc -l`

if [ $ISRUNNING -eq "1" ]
then
	echo "xpdf is running"
	xpdf -remote sessionname -reload
	xpdf -remote sessionname -raise
else
	echo "xpdf is not running"
	xpdf -remote sessionname $* &
fi

Update: The same for KPDF:

#!/bin/bash

PID=`ps ax | grep "kpdf $1" | grep -v grep | awk '{print $1}'`

if [ $PID ]
then
	echo "kpdf is running"
	dcop `dcopfind -a kpdf-$PID` KPDF::Shell raise
else
	echo "xpdf is not running"
	kpdf $* &
fi

To make this work in KDE, you'll need to disable the focus stealing prevention for xpdf or kpdf, this can be done by adding a rule in Control Centre -> Desktop -> Window-specific settings.