Almighty Bus Error

Blog about computer science, code snippets and tips.

GitHub repository with examples here.
Currently a Computer Engineering student at FCT/UNL.
August 3, 2009 at 9:49pm

Comments (View)

Snippet.Py: Getting an image resolution

Using PIL (Python Image Library) it is possible to get the width and height of a image using the property size.

The following example will take an undefined number of arguments and print all the resolutions:

import Image
import sys

def main():
    argv = sys.argv[1:]
    if (len(argv) == 0):
        print "No file given!"
    else:
        for name in argv:
            img = Image.open(name)
            print name + ": %d %d" % img.size

if __name__ == "__main__":
    main()

Credit for the snippet goes to meqif. The code can also be found at git.

May 20, 2009 at 10:53am
Tags: Snippet  Cpp  typeid 

Comments (View)

Snippet.Cpp: Comparing Types of Classes

This snippet shows how to differentiate between two classes that have heritage from the same class.

Using the following classes: class A {} class B: public A { B(); } class C: public A { C(); }

With the following variables: A a; A *b = new B(); A *c = new C(); if(typeid(b) == typeid(a)) cout << "First is true" << endl; // A* != A if(typeid(*b) == typeid(B)) cout << "Second is true" << endl; // B = B if(typeid(*b) == typeid(*c)) cout << "Third is true" << endl; // B != C if(typeid(b) == typeid(c)) cout << "Fourth is true" << endl; // A* = A*

The pointer usage is important when using the typeid() in this situation.

As you might notice you can actually use the class name to compare like in the second example and not just objects.

Note: There is a name() method that can be used to print the class name. You might notice that there sometimes are pseudo-random numbers/letters before the actual name.

May 19, 2009 at 11:16am
Tags: Snippet  Java 

Comments (View)

Snippet.Java: Dump InputStream → OutputStream

This piece of code keeps reading 1024 bytes from the InputStream and writing to the OutStream until the read function returns an error value (-1) which means there is not anything else to read.

protected void dumpStream( InputStream in, OutputStream out )
throws IOException {
    byte[] arr = new byte[1024];
    int n;
    do {
        n = in.read( arr );
        out.write( arr, 0, n );
    } while (n != -1);
}
May 3, 2009 at 3:36pm
Tags: Snippet  C 

Comments (View)

Snippet.C: Getting the length of an array.

The following C macro gets the amount of memory the array uses and divides it by the size of the first position:

#define ARRAY_SIZE(x) (sizeof(x) / sizeof(x)[0])

Credits go to meqif.

April 17, 2009 at 8:26pm
Tags: Snippet  Python 

Comments (View)

Snippet.Py: Showing all the filenames in a directory

The following Python code prints the name of the chosen directory and all the files in it and then proceeds to print all the files in the other nested directories one nested directory at a time. This method is recursive for nested directories.

def print_dir(loc):
    print loc + "\n"
    for root, dirs, files in os.walk(loc):
        for f in files:
           print f + "\n"
        for d in dirs:
           print_dir(os.path.join(loc, d))
    return