Stoppt die Vorratsdatenspeicherung - www.vorratsdatenspeicherung.de

Archive for the ‘Programmierung’ Category

Brainfuck

Tuesday, March 16th, 2010

Hmmm… Kleine Fingerübung in Perl (pre-alpha :-) )

Ah ja: Brainfuck: http://lmgtfy.com/?q=Brainfuck

#!/usr/bin/env perl
use Term::ReadKey;
ReadMode 'raw';
my $debug = 0;

my $code;
if ($ARGV[0]){
  { local $/ = undef; local *FILE; open FILE, "<".$ARGV[0]; $code = <FILE>; close FILE }
}

$code ||=
">+++++++++[<+++++++++++++>-]<-.-----.+++++++
.>+++[>++++++++++<-]
>++.<<----.+++.
--------- . -------.>++++[<+++++>-]<+.>++++++++++.";

$code =~ s/[^\.,+-\[\]<>]//msg;
$| = 1;
my @val;
@val[ 0 .. 29999 ] = (0) x 30000;
my $vpt = 0;
my @ls;
my @le;
my $lpt = 0;
my $cc  = 0;
my @c   = split( '', $code );
my $nc  = @c;
while ( $cc < $nc ) {
    $_ = $c[ $cc++ ];
    print "vpt:$vpt \$val[\$vpt]"
      . $val[$vpt]
      . " loop_counter:$loop_counter loop(counter)_start:"
      . $ls[$loop_counter]
      . " loop(counter)_end:"
      . $le[$loop_counter]
      . " cc:$cc command:$_\n"
      if $debug;
    /</ and do { $vpt        and $vpt--; next };
    />/ and do { $vpt < @val and $vpt++; next };
    /\+/ and do { $val[$vpt] = $val[$vpt] + 1; next; };
    /\-/ and do { $val[$vpt] = $val[$vpt] - 1; next; };
    /\./ and do { print chr( $val[$vpt] ); next; };
    /\,/ and do { $val[$vpt] = ord( ReadKey 0 ); next; };
    /\[/ and do {
        if   ( $val[$vpt] ) { $ls[ $lpt++ ] = $cc - 1 }
        else                { $cc = defined $le[$lpt] ? $le[$lpt] : (index($code,']',$cc)+1) }
        next;
    };
    /\]/ and do {
        $le[ --$lpt ] = $cc;
        $cc = $ls[$lpt];
        next;
    };
}
print "\n";

Establish a 2nd database connection in Rails

Thursday, June 18th, 2009

OK, you can find some howto’s everywhere on the web but only on this site I found the complete solution:

class SecondConnection < ActiveRecord::Base

SecondConnection.establish_connection(
:adapter  => "informix",
:host     => "localhost",
:username => "hansnase",
:password => "very_secret",
:database => "ccdb@localhost"
)

self.abstract_class = true
end

“self.abstract_class = true” makes the thing actually work. Otherwise Rails seems to cache the database connection and does not switch back if you access any other classes but SecondConnection.

Yeah!

Later: Not so Yeah…

I had to establish another call to

  ActiveRecord::Base.establish_connection
       logger.info { "Establishing Base Connection" }
       log_connections

I assume it’s either an Informix problem or related to the old rails version I’m driving with.
Nevertheless: It works now.

Rails and incoming mails on a Mac Leopard workstation

Wednesday, June 17th, 2009

Obviously I’m in a need of such an installation, otherwise I wouldn’t have googled about this (btw.: Do you know lmgtfy.com).

Here are some snippets from:

  1. ChrizDee
  2. Craig Ambrose

And? What did I do:

I first enabled Postfix using 1). After this I have changed /etc/aliases:


echo 'rails_mailer: "|/usr/local/bin/mail_handler.rb"' >> /etc/aliases
chmod 755 /usr/local/bin/mail_handler.rb"' >> /etc/aliases
sudo newaliases
sudo postfix reload
#!/usr/bin/ruby
require 'net/http'
require 'uri'Net::HTTP.post_form URI.parse('http://localhost:3000/emails'), { "email" => STDIN.read }

The script above will be triggered by every mail that will be sent to rails_mailer@localhost (resp. your hostname). It will put a post-request to localhost:3000/email including the email in the parameter “email”.

Hpricot: XML to Hash

Friday, March 20th, 2009

Well: I nearly lost all my hairs about this. Strange that I couldn’t find something like this in the docs.Situation:

I have some XML code from that I need parts being put into a simple hash (see below).

The following small ruby snippet does exactly this.

Only thing that’s not working is the “inner_xml” part. I personally don’t need it but your mileage may vary and you may want to fix the issue (and give me a note)

require 'rubygems'
require 'hpricot'
require 'pp'

text = <<eof
<payment>
		<vat>
			<pay_vat>0</pay_vat>
			<vat_id/>
		</vat>
		<creditcard>
			<type>master</type>
			<holder>Franz Fluchsfinger</holder>
			<company/>
			<no>52660xxxxxxxxxxx79</no>
			<kpn/>
			<valid_until>09/10</valid_until>
			<data>Just some data</data>
			<xml_for_demo><inner_xml>inner text</inner_xml></xml_for_demo>
			<signature>a5f4e2shortened4b1e523ca873837c8f1c9ea66ee1924d</signature>
		</creditcard>
</payment>
eof

xml =Hpricot.XML(text)
a= xml/"creditcard/*"
h={}
a.each {|b|
	h[b.name] = b.inner_text if b.is_a? Hpricot::Elem
}
pp h	

result:

[code]]czozMDE6XCINCnJ1YnkgdGVzdC5yYg0Ke1wiY29tcGFueVwiPSZndDtcIlwiLA0KXCJ4bWxfZm9yX2RlbW9cIj0mZ3Q7XCJpbm5lciB0ZXh0XCIsDXtbJiomXX0KXCJub1wiPSZndDtcIjUyNjYweHh4eHh4eHh4eHg3OVwiLA0KXCJob2xkZXJcIj0mZ3Q7XCJGcmFueiBGbHVjaHNmaW5nZXJcIiwNClwic2lnbmF0e1smKiZdfXVyZVwiPSZndDtcImE1ZjRlMnNob3J0ZW5lZDRiMWU1MjNjYTg3MzgzN2M4ZjFjOWVhNjZlZTE5MjRkXCIsDQpcInR5cGVcIj0mZ3Q7XCJtYXN7WyYqJl19dGVyXCIsDQpcInZhbGlkX3VudGlsXCI9Jmd0O1wiMDkvMTBcIiwNClwia3BuXCI9Jmd0O1wiXCIsDQpcImRhdGFcIj0mZ3Q7XCJKdXN0IHNvbWUgZGF0YXtbJiomXX1cIn0NClwiO3tbJiomXX0=[[/code]

Btw.: I found the solution partly on http://railsforum.com/viewtopic.php?id=22055

Rails Snippets

Wednesday, February 25th, 2009

Some small snippets of Rails/Ruby codes that I’d like to not forget…

I found an interesting site on the Web: http://matthall.wordpress.com/2006/12/06/how-to-execute-a-rails-controller-action-via-cron/ 

Tweaking it a bit:
[code]]czoyMTQ6XCJyZXF1aXJlIFwnLi4vY29uZmlnL2Vudmlyb25tZW50XCcNCnJlcXVpcmUgXCdwcFwnDQppZCA9IEFSR1ZbMF0NCmlmIGlkLm5pe1smKiZdfWw/DQpyYWlzZSAmbHQ7PGVvZj4NClVTQUdFOiAkMCBSRVNFTExFUl9JRA0KZW9mDQplbmQNCmFwcCA9IEFjdGlvbkNvbnRyb2xsZXJ7WyYqJl19OjpJbnRlZ3JhdGlvbjo6U2Vzc2lvbi5uZXcNCmFwcC5nZXQgXCIva3VuZGVuL3N0YXR1c19tYWlsLyN7aWR9XCI8L2VvZj5cIjt7WyYqJl19[[/code]

This works fine from a Rails subdirectory and calls the same action like http://RAILSSITE/kunden/status_mail/ID

Git Installation

Monday, March 10th, 2008

GIT? Was’n das?

Git ist ein Versionskontrollsystem, ähnlich wie das bekannte CVS bzw. dessen Nachfolger “Subversion”.
Der große Vorteil von “git” ist die hervorragende Unterstützung von “Branches”, etwas was ich mit CVS/Subversion immer nur ziemlich rudimentär hinbekommen habe.

Hier nun die Installation auf einem älteren SuSE-System:

1) Sourcecode von http://git.or.cz
cd /usr/local/src
aktuell: wget http://kernel.org/pub/software/scm/git/git-1.5.4.4.tar.bz2

2) auspacken
tar xjvpf git-1.5*
cd git-1.5*
./configure
make
make install

It’s that simple :-)

Allerdings: Wenn Ihr mit einem nicht lokalen Repository arbeitet _muss_ die gleiche Prozedur auch auf dem Rechner mit dem Repository stattfinden (also wget, tar, configure, make, make install)
Sonst gibt’s immer ein freundliches:

git clone ssh://user@HOSTNAME/fileserver/git/REPOSITORY
Initialized empty Git repository in /home/jrelease/REPOSITORY/.git/
bash: git-upload-pack: command not found
fatal: The remote end hung up unexpectedly
fetch-pack from ’ssh://user@HOSTNAME/fileserver/git/REPOSITORY’ failed.