Monday, November 23, 2009

Google funnies

I really think these kinds of things are injected by Google as easter eggs:

Sunday, November 22, 2009

DOMElement innerHMTL

Currently, there's no built-in method in the DOMElement that gets the inner/outerHTML of the element. There are a few solutions in the comments and on other blogs, but they loop through all the childNodes in order to get the innerHMTL. Getting the outerHTML is much easier (no looping) and just as useful: function outerHTML($e) { $doc = new DOMDocument(); $doc->appendChild($doc->importNode($e, true)); return $doc->saveHTML(); } Still, I'm not sure that is the most optimal way of doing it. It seems that DOMDocument::saveXML accepts an optional DOMElement parameter which, when specified, causes the function to return only that element's XML. You could rewrite our outerHTML function like this: function outerXML($e) { return $e->ownerDocument->saveXML($e); }

Wednesday, November 18, 2009

Use ssh-copy-id on a different port

ssh-copy-id has known problems with handling non-standard ports (e.g. connecting to a different port than 22). To overcome this issue, use a command like: $ ssh-copy-id "user@host -p 6842"

Wednesday, November 11, 2009

a + b in MIPS assembly

We're just starting to learn assembly at college, so here's my first program. It reads to numbers, sums them up and displays the result. Not really rocket science, but good for a first program without any previous practice: .data prompt1: .asciiz "a: " prompt2: .asciiz "b: " newline: .asciiz "\n" .text main: # print prompt 1 li $v0, 4 la $a0, prompt1 syscall # read in $t0 li $v0, 5 syscall move $t0, $v0 #print prompt 2 li $v0, 4 la $a0, prompt2 syscall # read in $t1 li $v0, 5 syscall move $t1, $v0 # $a0 = $t0 + $t1 add $a0, $t0, $t1 # print result li $v0, 1 syscall # newline li $v0, 4 la $a0, newline syscall # exit li $v0, 10 syscall References:

Wednesday, November 4, 2009

A brainfuck interpreter

In case you didn't know, there's an esoteric programming language named brainfuck. It is quite simple and I thought I should make an interpreter for it, in C. It is nowhere near the sub-200 bytes compilers others have made, but it's a good exercise. Well, here's my solution: /* A brainfuck intepreter written in C, complete with error checking so you don't hurt yourself while, uh, brainfucking. Nothing really special about the implementation and it is probably very poor performance-wise. Author: Felix Oghină License: (brain)fuck licenses! */ #include <stdio.h> #include <stdlib.h> #include <string.h> // by brainfuck standards (doesn't that sound funny?), the data pointer has // 30,000 bytes at its disposal, but I hate hard-coding such stuff. #define DATA_SIZE 30000 void usage() { puts( "Usage: brainfuck FILE\n" "If FILE is ommited or is '-', standard input is read" ); } int main(int argc, char **argv) { // used by the bf program unsigned char *dataptr = malloc(sizeof(char) * DATA_SIZE); // position of the data pointer unsigned int datapos = 0; // input file FILE *input; // level - deepness of brackets // i - uh, you need explanation for this one? unsigned int level, i; // we will read chars from the input into r unsigned char r; // determine input if (argc == 2) { if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { usage(); return EXIT_SUCCESS; } else { input = fopen(argv[1], "r"); if (input == NULL) { puts("Error opening input file"); return EXIT_FAILURE; } } } else { usage(); return EXIT_FAILURE; } // zero the data pointer for (i=0; i < DATA_SIZE; i++) { dataptr[i] = 0; } // start interpreting rewind(input); while (!feof(input)) { r = (unsigned char) fgetc(input); switch(r) { case '>': if (datapos < DATA_SIZE - 1) datapos++; else { puts("brainfuck error: pointer overflow"); return EXIT_FAILURE; } break; case '<': if (datapos > 0) datapos--; else { puts("brainfuck error: pointer underflow"); return EXIT_FAILURE; } break; case '+': dataptr[datapos]++; break; case '-': dataptr[datapos]--; break; case '.': putchar(dataptr[datapos]); break; case ',': dataptr[datapos] = getchar(); break; case '[': if (dataptr[datapos] == 0) { level = 1; while (level != 0) { r = (unsigned char) fgetc(input); if (r == '[') level ++; else if (r == ']') level --; } } break; case ']': if (dataptr[datapos] != 0) { level = 1; while (level != 0) { fseek(input, -2, SEEK_CUR); r = (unsigned char) fgetc(input); if (r == ']') level ++; else if (r == '[') level --; } } break; } } return EXIT_SUCCESS; }
Syntax Highlighting by Pygmentool
Download the source code

Well, guess it's time to fill up that swear jar now.