Index: branches/terrain/src/lib/util/Makefile.am
===================================================================
--- branches/terrain/src/lib/util/Makefile.am	(revision 9140)
+++ branches/terrain/src/lib/util/Makefile.am	(revision 9324)
@@ -24,4 +24,5 @@
 		filesys/file.cc \
 		filesys/directory.cc \
+		filesys/binary_file.cc \
 		\
 		preferences.cc \
@@ -41,7 +42,9 @@
 		executor/executor_lua.h \
 		executor/functor_list.h \
+		byte_order.h \
 		\
 		filesys/file.h \
 		filesys/directory.h \
+		filesys/binary_file.h \
 		\
 		preferences.h \
Index: branches/terrain/src/lib/util/byte_order.h
===================================================================
--- branches/terrain/src/lib/util/byte_order.h	(revision 9324)
+++ branches/terrain/src/lib/util/byte_order.h	(revision 9324)
@@ -0,0 +1,33 @@
+#ifndef _BYTE_ORDER_H
+#define _BYTE_ORDER_H
+
+enum ByteOrder { LittleEndian = 1, BigEndian = 2 };
+
+#ifdef HAVE_SDL_SDL_H
+#include <SDL/SDL_byteorder.h>
+#else
+#include <SDL_byteorder.h>
+#endif
+
+
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+#define NATIVE_BYTEORDER LittleEndian
+#else
+#define NATIVE_BYTEORDER BigEndian
+#endif	
+
+
+inline void swap16( short *_s )
+{
+	char *ci = (char*)_s, tmp;
+	tmp = ci[0]; ci[0] = ci[1]; ci[1] = tmp;
+}
+
+inline void swap32( int *_i )
+{
+	char *ci = (char*)_i, tmp;
+	tmp = ci[0]; ci[0] = ci[3]; ci[3] = tmp;
+	tmp = ci[2]; ci[2] = ci[1]; ci[1] = tmp;
+}
+
+#endif
Index: branches/terrain/src/lib/util/filesys/binary_file.cc
===================================================================
--- branches/terrain/src/lib/util/filesys/binary_file.cc	(revision 9324)
+++ branches/terrain/src/lib/util/filesys/binary_file.cc	(revision 9324)
@@ -0,0 +1,205 @@
+#include "binary_file.h"
+#include <ctype.h>
+#include <stdlib.h>
+
+int bytesize( const char _c )
+{
+	switch( _c) {
+		case 'f':
+		case 'i':
+			return 4;
+		case 'b':
+			return 1;
+		case 's':
+			return 2;
+		default:
+			return 0;
+	}
+}
+
+pDataSemantics BinaryFile::compileSemantics( const char *_semantics )
+{
+	const char *curr = _semantics;
+	char *tmp = new char[strlen(_semantics)];
+	//Find out how many subchunks exist.
+	pDataSemantics chunk = new DataSemantics();
+	bool terminated = false;
+	int open = 0;
+	while ( *curr ) {
+		switch( *curr ) {
+			case 'i':
+			case 'f':
+			case 'b':
+			case 's':
+				chunk->numChunkNodes++;
+				break;
+			case '(':
+				terminated = false;
+				chunk->numChunkNodes++;
+				while ( *curr && terminated == false ) {
+					if ( *curr == ')' )
+						terminated = true;
+					curr++;
+				}
+				if ( !terminated ) {
+					printf( "Syntax error: ')' is missing" );
+					delete chunk;
+					return NULL;
+				}	
+				break;
+			default:
+				break;
+		}
+		curr++;
+	}
+	//Compile the chunks...
+	chunk->chunks = new pDataSemantics[chunk->numChunkNodes];
+	int currChunk = 0;
+	curr = _semantics;
+	while ( *curr ) {
+		switch( *curr ) {
+			case 'i':
+			case 'f':
+			case 'b':
+			case 's':
+				chunk->chunks[currChunk] = new DataSemantics();
+				chunk->chunks[currChunk]->isTerminal = true;	
+				chunk->chunks[currChunk]->type = bytesize( *curr );
+				chunk->chunks[currChunk]->totalSize = bytesize( *curr );						
+				currChunk++;
+				break;
+			case '(':
+				curr++;
+				open = 0;
+				memset( tmp, 0, strlen( _semantics ) );
+				char * cp = tmp;
+				while ( *curr ) {
+					if ( *curr == '(' )
+						open++;
+					if ( *curr == ')' ) {
+						if ( open )
+							open--;
+						else
+							break;
+					}
+					*cp = *curr;
+					cp++;
+					curr++;
+				}
+				*cp = '\0';
+				chunk->chunks[currChunk] = compileSemantics( tmp );
+				currChunk++;					
+				break;
+			case ')':
+				printf( "Syntax error: ')' shouldn't appear here\n" );
+				delete[] tmp; delete chunk;
+				return NULL;
+				break;
+			default:
+				int multiplier = 1, quantifier = 0;
+				const char *start = curr;
+				const char *end = curr;
+				while ( isdigit( *end ) ) {
+					end++;
+				}
+				for ( const char *d = end-1; d >=start; --d ) {
+					quantifier+=multiplier*(*d-'0');
+					multiplier*=10;
+				}
+				if ( start == end ) {
+					printf( "Syntax error: '%c' is not known.\n", *curr );
+					delete[] tmp; delete chunk;
+					return NULL;
+				}				
+				if ( currChunk ) {
+					chunk->chunks[currChunk-1]->quantifier = quantifier;
+					chunk->chunks[currChunk-1]->totalSize*= quantifier;					
+				}	
+				else {
+					printf( "Syntax error: Quantifier without a preceding type\n" );
+					delete[] tmp; delete chunk;
+					return NULL;
+				}
+				curr = end-1;
+				break;
+		}
+		curr++;
+	}
+	for ( int i = 0; i < chunk->numChunkNodes; ++i ) {
+		chunk->totalSize+= chunk->chunks[i]->totalSize;
+	}
+	delete[] tmp;
+	return chunk;
+}
+
+void convert_data( pDataSemantics _comp, void *_data )
+{
+	char *ptr = (char*)_data;
+	short *sptr;
+	int *iptr;
+	if ( _comp->isTerminal ) {
+		switch( _comp->type ) {
+			case TT_INT:
+				iptr = (int*)ptr;			
+				for ( int i = 0; i < _comp->quantifier; ++i ) {
+					swap32( iptr );
+					iptr++;
+				}
+				break;
+			case TT_SHORT:
+				sptr = (short*)ptr;
+				for ( int i = 0; i < _comp->quantifier; ++i ) {
+					swap16( sptr );
+					sptr++;
+				}			
+				break;
+			case TT_BYTE:
+				break;
+		}
+	}
+	else {
+		for ( int j = 0; j < _comp->quantifier; ++j ) {
+			for ( int i = 0; i < _comp->numChunkNodes; ++i ) {
+				convert_data( _comp->chunks[i], ptr );
+				ptr+= _comp->chunks[i]->totalSize;
+			}			
+		}
+
+	}	
+}
+void BinaryFile::read( const char *_semantics, int _chunks, 
+	void* _buf, size_t& _bytesRead )
+{
+	pDataSemantics comp = compileSemantics( _semantics );		
+	fread( _buf, comp->totalSize, _chunks, this->handle() );
+	if ( byteorder != NATIVE_BYTEORDER ) {
+		for ( int i = 0; i < _chunks; ++i ) {
+			convert_data( comp, (char*)_buf+i*comp->totalSize );
+		}	
+	}	
+	_bytesRead = comp->totalSize*_chunks;
+	delete comp;	
+}
+void BinaryFile::read( const char *_semantics, 
+	void* _buf, size_t& _bytesRead )
+{
+	pDataSemantics comp = compileSemantics( _semantics );		
+	fread( _buf, comp->totalSize, 1, this->handle() );
+	
+	if ( byteorder != NATIVE_BYTEORDER ) {
+		convert_data( comp, _buf );
+	}	
+	delete comp;		
+}
+
+void BinaryFile::write( const char *_semantics, 
+	void* _buf, size_t& _bytesWritten )
+{
+	pDataSemantics comp = compileSemantics( _semantics );
+	if ( byteorder != NATIVE_BYTEORDER ) {
+		convert_data( comp, _buf );
+	}	
+	_bytesWritten = comp->totalSize;
+	fwrite( _buf, comp->totalSize, 1, this->handle() );
+	delete comp;
+}
Index: branches/terrain/src/lib/util/filesys/binary_file.h
===================================================================
--- branches/terrain/src/lib/util/filesys/binary_file.h	(revision 9324)
+++ branches/terrain/src/lib/util/filesys/binary_file.h	(revision 9324)
@@ -0,0 +1,109 @@
+#ifndef _BINARY_FILE_H
+#define _BINARY_FILE_H
+
+#include <stdio.h>
+#include <string>
+#include "byte_order.h"
+#include "file.h"
+
+enum TerminalType 	{ TT_INT = 4, TT_BYTE = 1, TT_SHORT = 2 };
+
+/**
+ * Holds the compiled semantic-string.
+ */
+struct DataSemantics {
+
+	DataSemantics()
+	{
+		totalSize = 0; chunks = NULL;
+		quantifier = 1;
+		isTerminal = false;
+		numChunkNodes = 0;
+	}
+
+	~DataSemantics() 
+	{
+		delete[] chunks;
+	}
+	
+	int 			totalSize;
+	int 			quantifier;
+	int 			numChunkNodes;
+	bool 			isTerminal;
+	union {
+		int				type;
+		DataSemantics	**chunks; 
+	};	
+};
+
+typedef DataSemantics *pDataSemantics;
+
+class BinaryFile : public File {
+	public:		
+		BinaryFile()
+			: 	File( "" ),
+				byteorder( NATIVE_BYTEORDER )
+		{ /* nothing to be done */ }
+		
+		BinaryFile( const std::string& _file )
+			: 	File( _file ), 
+				byteorder( NATIVE_BYTEORDER )
+		{ /* nothing to be done */ }
+
+		BinaryFile( const BinaryFile& _file )
+			: 	File( _file.name() ), 
+				byteorder( _file.getByteorder() )
+		{ /* nothing to be done */ }
+		
+		/**
+		 * Reads a chunk of data based on the _semantics string. The string contains
+		 * a description of how the bytes are laid out in the file. If you want
+		 * to read out a struct which looks like this
+		 * struct {
+		 *		int 	magic;
+		 *		short	size;
+		 *		float	x, y, z;
+		 * };
+		 * then the semantics string is "isf3". If you want to read 5 of these
+		 * chunks, just put the whole string into paranthesis. "(isf3)5". Nifty, isn't
+		 * it?
+		 */
+		void read( const char *_semantics, void* _buf, size_t& _bytesRead );
+		
+		/**
+		 * The same as the read above, but with the ability to read multiple
+		 * chunks once. Very useful if you don't know how many chunks you're 
+		 * going to read at compile time.
+		 */
+		void read( const char *_semantics, int _chunks, 
+			void* _buf, size_t& _bytesRead );
+		/**
+		 * Writes a chunk of data based on the _semantics string. The string contains
+		 * a description of how the bytes should be laid out in the file. Have a look
+		 * at the description to read().
+		 */
+		void write( const char *_semantics, void* _buf, size_t& _bytesWritten );
+		inline void seekg( int _pos )
+		{
+			fseek( handle(), _pos, SEEK_SET );
+		}
+		/**
+		 * @brief Sets the byteorder of the file. The order affects all subsequent
+		 * calls to write() and read(). It it is not wise to change the order
+		 * in the middle of reading a file. You're totally on your own.
+		 */
+		inline void setByteorder( ByteOrder _order ) 
+		{ byteorder = _order; }
+		
+		/**
+		 * Returns the file's byte-order.
+		 */
+		inline ByteOrder getByteorder() const
+		{ return byteorder; }
+		
+	private:
+		pDataSemantics compileSemantics( const char *_semantics );	
+		ByteOrder 		byteorder;
+};
+
+#endif
Index: branches/terrain/src/lib/util/filesys/file.cc
===================================================================
--- branches/terrain/src/lib/util/filesys/file.cc	(revision 9140)
+++ branches/terrain/src/lib/util/filesys/file.cc	(revision 9324)
@@ -155,14 +155,21 @@
 }
 
-bool File::open(OpenMode mode)
-{
-#warning implement
-  return false;
+bool File::open( OpenMode mode )
+{
+   static const char* openModeStrings[] = {
+			"r", "w", "r+", "a+" };	
+	_mode = mode;
+	_handle = fopen( name().c_str(), openModeStrings[mode] );
+	return ( _handle != NULL );
 }
 
 bool File::close()
 {
-#warning implement
-  return false;
+	if ( _handle != NULL ) {
+		int success = fclose( _handle );
+		_handle = NULL;		
+		return ( success == 0 );
+	}
+	return false;
 }
 
Index: branches/terrain/src/lib/util/filesys/file.h
===================================================================
--- branches/terrain/src/lib/util/filesys/file.h	(revision 9140)
+++ branches/terrain/src/lib/util/filesys/file.h	(revision 9324)
@@ -16,12 +16,10 @@
   public:
   //! How the File should be opened.
-  typedef enum
-  {
-    ReadOnly,      //!< ReadOnly mode
-    WriteOnly,     //!< WriteOnly mode
-    ReadWrite,     //!< Read and Write mode together
-    Append,        //!< Append at the end.
+  typedef enum {
+    ReadOnly 	= 0,     //!< ReadOnly mode
+    WriteOnly 	= 1,     //!< WriteOnly mode
+    ReadWrite	= 2,     //!< Read and Write mode together
+    Append 		= 3      //!< Append at the end.
   } OpenMode;
-
 public:
   File();
@@ -41,6 +39,8 @@
   virtual bool open(OpenMode mode);
   virtual bool close();
-  int handle() const { return this->_handle; };
-
+  FILE* handle() const 
+  { return this->_handle; };
+  inline OpenMode mode() const
+  { return _mode; }
   /** @returns the FileName of this File */
   const std::string& name() const { return this->_name; };
@@ -73,8 +73,8 @@
 
   private:
-    int                 _handle;          //!< The FileHandle (if set).
+    FILE*               _handle;          //!< The FileHandle (if set).
     std::string         _name;            //!< The Name of the File.
     stat*               _status;          //!< The Stat of the File.
-
+	OpenMode			_mode;
     static std::string  _cwd;             //!< The currend Working directory.
 
