Index: branches/terrain/acinclude.m4
===================================================================
--- branches/terrain/acinclude.m4	(revision 9323)
+++ branches/terrain/acinclude.m4	(revision 9324)
@@ -105,4 +105,5 @@
       `ls -dr /usr/local/qt*/include   2>/dev/null`
       `ls -dr /opt/qt*/include         2>/dev/null`
+      `ls -dr /opt/local/include       2>/dev/null`
       `ls -dr /Developer/qt*/include   2>/dev/null`
       `ls -dr /c/Qt/*/include          2>/dev/null`
@@ -115,4 +116,5 @@
       `ls -dr /usr/local/qt*/lib       2>/dev/null`
       `ls -dr /opt/qt*/lib             2>/dev/null`
+      `ls -dr /opt/local/lib  		   2>/dev/null`
       `ls -dr /Developer/qt*/lib       2>/dev/null`
       `ls -dr /c/Qt/*/lib              2>/dev/null`
@@ -125,4 +127,5 @@
       `ls -dr /usr/local/qt*/bin       2>/dev/null`
       `ls -dr /opt/qt*/bin             2>/dev/null`
+      `ls -dr /opt/local/bin		      2>/dev/null`
       `ls -dr /Developer/qt*/bin       2>/dev/null`
       `ls -dr /c/Qt/*/bin              2>/dev/null`
@@ -143,4 +146,18 @@
     fi
 
+## Search in all IncludeDirectories for QT-includes
+    for _qt_include_directory in ${QT_COMMON_INCLUDE_DIRECTORIES}
+    do			
+     	if test -e ${_qt_include_directory}/QtCore/QCoreApplication ; then
+       	QT_INCLUDE_DIRECTORY="${_qt_include_directory}"
+       	break
+     	fi
+    done
+    if test -n ${QT_INCLUDE_DIRECTORY} ; then
+      echo "Qt-Include Directory is: ${QT_INCLUDE_DIRECTORY}"
+    else
+      AC_MSG_ERROR([Qt Headers not found])
+    fi
+
 ## Search in all LibraryDirectories for QT-includes
     for _lib_directory in ${QT_COMMON_LIB_DIRECTORIES}
@@ -172,73 +189,82 @@
     fi
 
-## Setting up the Environment Variables.
-    QT_LIBS=""
-    QT_CXXFLAGS="-I${QT_INCLUDE_DIRECTORY}"
-    LDFLAGS="${LDFLAGS} -L${QT_LIB_DIRECTORY} -Wl,-rpath -Wl,${QT_LIB_DIRECTORY}"
-
-    CACHED_CPPFLAG=${CPPFLAGS}
-    CACHED_LDFLAGS=${LDFLAGS}
-
-    LDFLAGS="${LDFLAGS} ${QT_LDFLAGS}"
-    for _lib in $2
-    do
-      AC_CHECK_LIB([$_lib], [main], [_lib_check="yes"], [_lib_check="no"])
-      if test "x$_lib_check" = "xyes" ; then
-        _found_lib=$_lib
-        break
-      fi
-    done
-
-    if [test "x$_lib_check" = "xyes"] ; then
-         QT_LIBS="${QT_LIBS} -l$_found_lib"
-         AX_CHECK_REQUIRED_LIB([QtCore QtCore4], [main],,,[http://www.trolltech.com] )
-         AX_CHECK_REQUIRED_LIB([QtGui QtGui4], [main],,,[http://www.trolltech.com] )
-         AX_CHECK_REQUIRED_LIB([QtOpenGL QtOpenGL4], [main],,,[http://www.trolltech.com] )
-
-         $3
-    else
-         echo "------------------"
-         echo "LIBRARY $2 not found.in $1"
-         echo "please install the $1-LIBRARY-package which can be found at $5"
-         echo "------------------"
-         $4
-         exit -1
-    fi
-
-    echo "QT_LIBS          = ${QT_LIBS}"
-    echo "QT_CXXFLAGS      = ${QT_CXXFLAGS}"
-    echo "QT_LD_FLAGS      = ${QT_LDFLAGS}"
-    echo "QT_BIN_DIRECTORY = ${QT_BIN_DIRECTORY}"
-
-    AC_SUBST([QT_LIBS])
-    AC_SUBST([QT_CXXFLAGS])
-    AC_SUBST([QT_LDFLAGS])
-
-    #----------#
-    # QT - MOC #
-    #----------#
-    AC_PATH_PROGS([QT_MOC], [moc-qt4 moc], [no], ["${QT_BIN_DIRECTORY}:${PATH}"])
-      if test x${QT_MOC} = xno ; then
-         AC_MSG_ERROR([QT MOC not found])
-      fi
-    AC_SUBST([QT_MOC])
-
-    AC_PATH_PROG([QT_UIC], [uic], [no], ["${QT_BIN_DIRECTORY}:${PATH}"])
- #     if test x${QT_UIC} = xno ; then
- #       AC_MSG_WARN([QT UIC not found])
- #     fi
-    AC_SUBST([QT_UIC])
-
-    AC_PATH_PROG([QT_RCC], [rcc], [no], ["${QT_BIN_DIRECTORY}:${PATH}"])
-#      if test x${QT_RCC} = xno ; then
-#        AC_MSG_WARN([QT RCC not found])
-#      fi
-    AC_SUBST([QT_RCC])
-
-
-
-    # Restore Values of CPPFLAGS and LDFLAGS
-    CPPFLAGS=${CACHED_CPPFLAG}
-    LDFLAGS=${CACHED_LDFLAGS}
+		## Setting up the Environment Variables.
+		    QT_LIBS=""
+		    QT_CXXFLAGS="-I${QT_INCLUDE_DIRECTORY}"
+			 if test x$osX = xyes ; then
+				LDFLAGS="${LDFLAGS} -L${QT_LIB_DIRECTORY}"
+			 else
+		    	LDFLAGS="${LDFLAGS} -L${QT_LIB_DIRECTORY} -Wl,-rpath -Wl,${QT_LIB_DIRECTORY}"
+			 fi
+		    CACHED_CPPFLAG=${CPPFLAGS}
+		    CACHED_LDFLAGS=${LDFLAGS}
+
+		    LDFLAGS="${LDFLAGS} ${QT_LDFLAGS}"
+		    for _lib in $2
+		    do
+		      AC_CHECK_LIB([$_lib], [main], [_lib_check="yes"], [_lib_check="no"])
+		      if test "x$_lib_check" = "xyes" ; then
+		        _found_lib=$_lib
+		        break
+		      fi
+		    done
+
+		    if [test "x$_lib_check" = "xyes" || test "x$osX" = "xyes"] ; then
+					if [test x$osX = xyes] ; then
+		# kraus: Need to add a check here. For the moment this just works fine...
+						QT_LIBS="${QT_LIBS} -framework QtCore -framework QtGui -framework QtOpenGL"
+						QT_LD_FLAGS="${QT_LD_FLAGS} -L${QT_LIB_DIRECTORY}"
+					else
+		         	QT_LIBS="${QT_LIBS} -l$_found_lib"
+		         	AX_CHECK_REQUIRED_LIB([QtCore QtCore4], [main],,,[http://www.trolltech.com] )
+		         	AX_CHECK_REQUIRED_LIB([QtGui QtGui4], [main],,,[http://www.trolltech.com] )
+		         	AX_CHECK_REQUIRED_LIB([QtOpenGL QtOpenGL4], [main],,,[http://www.trolltech.com] )
+
+		         	$3
+					fi
+		    else
+		         echo "------------------"
+		         echo "LIBRARY $2 not found.in $1"
+		         echo "please install the $1-LIBRARY-package which can be found at $5"
+		         echo "------------------"
+		         $4
+		         exit -1
+		    fi
+			 echo "configuration for qt: "
+		    echo "QT_LIBS          = ${QT_LIBS}"
+		    echo "QT_CXXFLAGS      = ${QT_CXXFLAGS}"
+		    echo "QT_LD_FLAGS      = ${QT_LDFLAGS}"
+		    echo "QT_BIN_DIRECTORY = ${QT_BIN_DIRECTORY}"
+
+		    AC_SUBST([QT_LIBS])
+		    AC_SUBST([QT_CXXFLAGS])
+		    AC_SUBST([QT_LDFLAGS])
+
+		    #----------#
+		    # QT - MOC #
+		    #----------#
+		    AC_PATH_PROGS([QT_MOC], [moc-qt4 moc], [no], ["${QT_BIN_DIRECTORY}:${PATH}"])
+		      if test x${QT_MOC} = xno ; then
+		         AC_MSG_ERROR([QT MOC not found])
+		      fi
+		    AC_SUBST([QT_MOC])
+
+		    AC_PATH_PROG([QT_UIC], [uic], [no], ["${QT_BIN_DIRECTORY}:${PATH}"])
+		 #     if test x${QT_UIC} = xno ; then
+		 #       AC_MSG_WARN([QT UIC not found])
+		 #     fi
+		    AC_SUBST([QT_UIC])
+
+		    AC_PATH_PROG([QT_RCC], [rcc], [no], ["${QT_BIN_DIRECTORY}:${PATH}"])
+		#      if test x${QT_RCC} = xno ; then
+		#        AC_MSG_WARN([QT RCC not found])
+		#      fi
+		    AC_SUBST([QT_RCC])
+
+
+
+		    # Restore Values of CPPFLAGS and LDFLAGS
+		    CPPFLAGS=${CACHED_CPPFLAG}
+		    LDFLAGS=${CACHED_LDFLAGS}
 ])
 
Index: branches/terrain/src/lib/graphics/importer/bsp_file.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/bsp_file.cc	(revision 9323)
+++ branches/terrain/src/lib/graphics/importer/bsp_file.cc	(revision 9324)
@@ -21,9 +21,9 @@
 #include "bsp_file.h"
 #include "bsp_tree_node.h"
-#include <fstream>
 #include "util/loading/resource_manager.h"
-
+#include <string>
 #include <sys/stat.h>
 #include <string.h>
+#include "filesys/binary_file.h"
 #include "debug.h"
 #include "material.h"
@@ -42,4 +42,15 @@
 using namespace std;
 
+static const char* HEADER_SEMANTICS  		= "b4i35";
+static const char* NODE_SEMANTICS  			= "i9";
+static const char* LEAF_SEMANTICS  			= "i12";
+static const char* MODEL_SEMANTICS			= "i10";
+static const char* BRUSH_SEMANTICS  		= "i3";
+static const char* BRUSHSIDE_SEMANTICS  	= "i2";
+static const char* FACE_SEMANTICS  			= "i26";
+static const char* VERTEX_SEMANTICS  		= "i10b4";
+static const char* LIGHTMAP_SEMANTICS 		= "b49152";
+static const char* TEXTURE_SEMANTICS 		= "b64i2";
+static const char* PLANE_SEMANTICS 			= "f4";
 
 // Constructor
@@ -81,5 +92,166 @@
 
 }
-
+void BspFile::readEntities()
+{
+	/* We do nothing here */
+}
+void BspFile::readTextures()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Textures];
+	this->numTextures = lump.length/sizeof(BspTexture);
+	this->textures = new char[lump.length];
+	file.seekg( lump.offset );	
+	file.read( TEXTURE_SEMANTICS, this->numTextures, 
+		this->textures, slurpedBytes );
+}
+void BspFile::readPlanes()
+{	
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Planes];
+	this->numPlanes = lump.length/sizeof(plane);
+	this->planes = new plane[this->numPlanes];
+	file.seekg( lump.offset );
+	file.read( PLANE_SEMANTICS, this->numPlanes, 
+		this->planes, slurpedBytes );
+}
+
+void BspFile::readNodes()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Nodes];
+	this->numNodes = lump.length/sizeof(node);
+	this->nodes = new node[this->numNodes];
+	file.seekg( lump.offset );
+	file.read( NODE_SEMANTICS, this->numNodes, 
+		this->nodes, slurpedBytes );
+}
+
+void BspFile::readLeafs()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Leafs];
+	this->numLeafs = lump.length/sizeof(leaf);
+	this->leaves = new leaf[this->numLeafs];
+	file.seekg( lump.offset );
+	file.read( LEAF_SEMANTICS, this->numLeafs, 
+		this->leaves, slurpedBytes );	
+}
+
+void BspFile::readLeafFaces()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Leaffaces];
+	this->numLeafFaces = lump.length/sizeof(int);
+	this->leafFaces = (char*)( new int[this->numLeafFaces] );
+	file.seekg( lump.offset );
+	file.read( "i", this->numLeafFaces, 
+		this->leafFaces, slurpedBytes );
+}
+
+void BspFile::readLeafBrushes()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Leafbrushes];
+	this->numLeafBrushes = lump.length/sizeof(int);
+	this->leafBrushes = (char*) ( new int[this->numLeafBrushes] );
+	file.seekg( lump.offset );
+	file.read( "i", this->numLeafBrushes, 
+		this->leafBrushes, slurpedBytes );	
+}
+
+void BspFile::readModels()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Models];
+	this->numBspModels = lump.length/sizeof(model);
+	this->bspModels = new model[this->numBspModels];
+	file.seekg( lump.offset );
+	file.read( MODEL_SEMANTICS, this->numBspModels, 
+		this->bspModels, slurpedBytes );
+}
+
+void BspFile::readBrushes()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Brushes];
+	int numBrushes = lump.length/sizeof(brush);
+	this->brushes = new brush[numBrushes];
+	file.seekg( lump.offset );
+	file.read( BRUSH_SEMANTICS, numBrushes, 
+		this->brushes, slurpedBytes );
+}
+void BspFile::readVertices()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Vertices];
+	this->numVertex = lump.length/sizeof(BspVertex);
+	this->vertice = (char*)( new BspVertex[this->numVertex] );
+	file.seekg( lump.offset );
+	file.read( VERTEX_SEMANTICS, this->numVertex, 
+		this->vertice, slurpedBytes );
+}
+
+void BspFile::readMeshVerts()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Meshverts];
+	int num = lump.length / sizeof( meshvert );
+	this->meshverts = new meshvert[num];
+	file.seekg( lump.offset );
+	file.read( "i", num, this->meshverts, slurpedBytes );
+}
+
+void BspFile::readEffects()
+{
+	/* not loaded atm */
+}
+
+void BspFile::readFaces()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Faces];
+	this->numFaces = lump.length/sizeof(face);
+	this->faces = new face[this->numFaces];
+	file.seekg( lump.offset );
+	file.read( FACE_SEMANTICS, this->numFaces, 
+		this->faces, slurpedBytes );
+}
+
+void BspFile::readLightmaps()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Lightmaps];
+	this->numLightMaps = lump.length/sizeof(lightmap);
+	this->lightMaps = new lightmap[this->numLightMaps];
+	file.seekg( lump.offset );
+	file.read( LIGHTMAP_SEMANTICS, this->numLightMaps, 
+		this->lightMaps, slurpedBytes );
+}
+void BspFile::readLightvols()
+{
+	/* not loaded atm */	
+}
+void BspFile::readBrushSides()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Brushsides];
+	this->numBrushSides = lump.length/sizeof(brushside);
+	printf( "numBrushSides: %d\n", numBrushSides );
+	this->brushSides = new brushside[this->numBrushSides];
+	file.seekg( lump.offset );
+	file.read( BRUSHSIDE_SEMANTICS, this->numBrushSides, 
+		this->brushSides, slurpedBytes );
+}
+void BspFile::readVisdata()
+{
+	size_t slurpedBytes;
+	bsp_lump& lump = header.lumps[Visdata];
+	this->visData = new char[lump.length];
+	file.seekg( lump.offset );
+	file.read( "i2", 1, this->visData, slurpedBytes );
+	file.read( "b", lump.length-8, 
+		this->visData+8, slurpedBytes );	
+}
 /**
  *  Loads a quake3 level (*.bsp)
@@ -88,269 +260,111 @@
 int BspFile::read(const char* name)
 {
-  this->scale = 1.0;
-  int offset;
-  int size;
-  struct stat results;
-
-
-  if (stat( name , &results) == 0) {
-    PRINTF(0)("BSP FILE: Datei %s gefunden. \n", name);
-    ifstream bspFile (name, ios::in | ios::binary);
-    bspFile.read(this->header, 260);
-    PRINTF(0)("BSP FILE: BSPVersion: %i. \n", ((int *)(header) )[1]);
-    if(SDL_SwapLE32(((int *)(header) )[1]) == 46)    PRINTF(0)("BSP FILE: This is the good one! :-)  \n");
-    else  PRINTF(0)("BSP FILE: Wrong BSPVersion.\n");   //!< now, we should do some error handling
-
-    // Get the Nodes
-    offset = SDL_SwapLE32(((int *)(header) )[8]);
-    size    = SDL_SwapLE32(((int *)(header))[9]);
-    PRINTF(4)("BSP FILE: NodeSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumNodes: %i. \n", size / sizeof(node));
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(node));
-    PRINTF(4)("BSP FILE: NodeOffset: %i. \n", offset);
-    this->numNodes = size/sizeof(node);
-    this->nodes = new node [this->numNodes];
-    bspFile.seekg(offset);
-    bspFile.read((char*)this->nodes, size);
-
-    // and their Planes
-    offset = SDL_SwapLE32(((int *)(header) )[6]);
-    size    = SDL_SwapLE32(((int *)(header))[7]);
-    PRINTF(4)("BSP FILE: PlanesSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumPlanes: %i. \n", size / sizeof(plane));
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", sizeof(plane));
-    PRINTF(4)("BSP FILE: PlanesOffset: %i. \n", offset);
-    this->numPlanes = size/sizeof(plane);
-    this->planes = new plane [this->numPlanes];
-    bspFile.seekg(offset);
-    bspFile.read((char*)this->planes, size);
-
-    // Get the Leafs
-    offset = SDL_SwapLE32(((int *)(header) )[10]);
-    size    = SDL_SwapLE32(((int *)(header))[11]);
-    PRINTF(4)("BSP FILE: LeaveSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumLeaves: %i. \n", size / sizeof(leaf));
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(leaf));
-    PRINTF(4)("BSP FILE: LeaveOffset: %i. \n", offset);
-    this->numLeafs = size/sizeof(leaf);
-    this->leaves = new leaf [this->numLeafs];
-    bspFile.seekg(offset);
-    bspFile.read((char*)this->leaves, size);
-
-    // Get the Models
-    offset = SDL_SwapLE32(((int *)(header))[16]);
-    size    = SDL_SwapLE32(((int *)(header))[17]);
-    PRINTF(4)("BSP FILE: ModelsSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumModels: %i. \n", size / sizeof(model));
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(model));
-    PRINTF(4)("BSP FILE: ModelsOffset: %i. \n", offset);
-    this->numBspModels = size/sizeof(model);
-    this->bspModels = new model [this->numBspModels];
-    bspFile.seekg(offset);
-    bspFile.read((char*)this->bspModels, size);
-
-    // Get the leafFaces
-    offset = SDL_SwapLE32(((int *)(header))[12]);
-    size    = SDL_SwapLE32(((int *)(header))[13]);
-    PRINTF(4)("BSP FILE: leafFacesSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumleafFaces: %i. \n", size / 4);
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
-    PRINTF(4)("BSP FILE: leafFacesOffset: %i. \n", offset);
-    this->numLeafFaces = size/4;
-    this->leafFaces = new char [size];
-    bspFile.seekg(offset);
-    bspFile.read(this->leafFaces, size);
-
-    // Get the leafBrushes
-    offset = SDL_SwapLE32(((int *)(header))[14]);
-    size    = SDL_SwapLE32(((int *)(header))[15]);
-    PRINTF(4)("BSP FILE: leafBrushesSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumleafBrushes: %i. \n", size / 4);
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
-    PRINTF(4)("BSP FILE: leafBrushesOffset: %i. \n", offset);
-    this->numLeafBrushes = size/4;
-    this->leafBrushes = new char [size];
-    bspFile.seekg(offset);
-    bspFile.read(this->leafBrushes, size);
-
-    // Get the brushes
-    offset = SDL_SwapLE32(((int *)(header))[18]);
-    size    = SDL_SwapLE32(((int *)(header))[19]);
-    PRINTF(4)("BSP FILE: BrushesSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumBrushes: %i. \n", size / sizeof(brush));
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(brush));
-    PRINTF(4)("BSP FILE: BrushesOffset: %i. \n", offset);
-    this->brushes = new brush [size/sizeof(brush)];
-    bspFile.seekg(offset);
-    bspFile.read((char*)this->brushes, size);
-
-    // Get the brushSides
-    offset =   SDL_SwapLE32(((int *)(header))[20]);
-    size    = SDL_SwapLE32(((int *)(header))[21]);
-    PRINTF(4)("BSP FILE: BrushSidesSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumBrushSides: %i. \n", size / 8);
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 8);
-    PRINTF(4)("BSP FILE: BrushSidesOffset: %i. \n", offset);
-    this->numBrushSides = size/sizeof(brushside);
-    this->brushSides = new brushside [this->numBrushSides];
-    bspFile.seekg(offset);
-    bspFile.read((char*)this->brushSides, size);
-
-    // Get the Vertice
-    offset = SDL_SwapLE32(((int *)(header))[22]);
-    size    = SDL_SwapLE32(((int *)(header))[23]);
-    PRINTF(4)("BSP FILE: VerticeSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumVertice: %i. \n", size / 44);
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 44);
-    PRINTF(4)("BSP FILE: VerticeOffset: %i. \n", offset);
-    this->numVertex = size/44;
-    this->vertice = new char [size];
-    bspFile.seekg(offset);
-    bspFile.read(this->vertice, size);
-
-    // Get the MeshVerts
-    offset = SDL_SwapLE32(((int *)(header))[24]);
-    size    = SDL_SwapLE32(((int *)(header))[25]);
-    PRINTF(4)("BSP FILE: MeshVertsSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumMeshVerts: %i. \n", size / sizeof(meshvert));
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(meshvert));
-    PRINTF(4)("BSP FILE: MeshVertsOffset: %i. \n", offset);
-    this->meshverts = new meshvert [size / sizeof(meshvert)];
-    bspFile.seekg(offset);
-    bspFile.read((char*)this->meshverts, size);
-
-    // Get the Faces
-    offset = SDL_SwapLE32(((int *)(header))[28]);
-    size    = SDL_SwapLE32(((int *)(header))[29]);
-    PRINTF(4)("BSP FILE: FacesSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumFaces: %i. \n", size / sizeof(face));
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(face));
-    PRINTF(4)("BSP FILE: FacesOffset: %i. \n", offset);
-    this->numFaces = size/sizeof(face);
-    this->faces = new face [this->numFaces];
-    bspFile.seekg(offset);
-    bspFile.read((char*)this->faces, size);
-
-    //Get the lightmaps
-    offset = SDL_SwapLE32(((int *)(header))[30]);
-    size    = SDL_SwapLE32(((int *)(header))[31]);
-    this->numLightMaps = size/ sizeof(lightmap);
-    this->lightMaps = new lightmap [this->numLightMaps];
-    bspFile.seekg(offset);
-    bspFile.read((char*)this->lightMaps, size);
-
-
-    // Get the Visdata
-    offset = SDL_SwapLE32(((int *)(header))[34]);
-    size    = SDL_SwapLE32(((int *)(header))[35]);
-
-    this->visData = new char [size];
-    bspFile.seekg(offset);
-    bspFile.read(this->visData, size);
-
-    PRINTF(4)("BSP FILE: VisDataSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumVisData: %i. \n", size /1 - 8);
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 1);
-    PRINTF(4)("BSP FILE: VisDataOffset: %i. \n", offset);
-
-    // Get the Textures
-    offset = SDL_SwapLE32(((int *)(header))[4]);
-    size    = SDL_SwapLE32(((int *)(header))[5]);
-
-    this->textures= new char [size];
-    bspFile.seekg(offset);
-    bspFile.read(this->textures, size);
-
-    PRINTF(4)("BSP FILE: TextureSize: %i Bytes. \n", size);
-    PRINTF(4)("BSP FILE: NumTextures: %i. \n", size /72);
-    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 72);
-    PRINTF(4)("BSP FILE: TextureOffset: %i. \n", offset);
-    this->numTextures = size/72;
-
-    bspFile.close();
-
-    for(int i = 0 ; i < this->numTextures; i++)
-      PRINTF(4)("BSP FILE: Texture 0: %s. \n", &this->textures[8+ 72*i]);
-    this->load_textures();
-
-    // Load the lightMaps
-    this->glLightMapTextures = new GLuint[this->numLightMaps];
-    for(int i = 0; i < this->numLightMaps; i++)
-      this->glLightMapTextures[i] = this->loadLightMapToGL(this->lightMaps[i]);
-
-    //Create white texture for if no lightmap specified
-    glGenTextures(1, &this->whiteLightMap);
-    glBindTexture(GL_TEXTURE_2D, this->whiteLightMap);
-        //Create texture
-    this->whiteTexture[0]=255;
-    this->whiteTexture[1]=255;
-    this->whiteTexture[2]=255;
-
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
-    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
-
-    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, 2);
-
-
-
-    /* control the mipmap levels */
-    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
-    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
-
-    /* build the Texture  OpenGL V >= 1.1 */
-    glTexImage2D(GL_TEXTURE_2D,
-                 0,
-                 GL_RGBA8,
-                 1,
-                 1,
-                 0,
-                 GL_RGB,
-                 GL_UNSIGNED_BYTE,
-                 (const GLvoid *)&(this->whiteTexture));
-
-   gluBuild2DMipmaps(   GL_TEXTURE_2D, GL_RGBA8, 1, 1,
-                      GL_RGB, GL_UNSIGNED_BYTE,(const GLvoid *) &(this->whiteTexture));
-
-
-
-    // Get the number of patches
-    this->numPatches = 0;
-    this->patchOffset   = 0;
-
-    for( int i = 0; i < this->numFaces; i++) {
-      face& cFace = ((face *)(this->faces))[i];
-      if (SDL_SwapLE32(cFace.type) == 2)
-        this->numPatches += (SDL_SwapLE32(cFace.size[0]) -1 ) / 2  * (SDL_SwapLE32(cFace.size[1]) -1) / 2;
-    }
-
-    // Allocate Memory
-    this->patchVertice = new char[8*8*44*(this->numPatches+10)];
-    this->patchIndexes = new char[7*8*2*4*(this->numPatches+10)];
- // this->patchRowIndexes = new int*[7*4*this->numPatches]; Not needed?
-    this->patchTrianglesPerRow = new char[7*4*this->numPatches];
-    this->VertexArrayModels  = new VertexArrayModel*[this->numPatches];
-
-    PRINTF(4)("BSP FILE:NumberOfPatches: %i . \n", numPatches);
-
-    this->swapAllBspCoordinates();
-
-    // Do tesselation for all Faces of type 2
-    for( int i = 0; i < this->numFaces; i++) {
-      if (SDL_SwapLE32((this->faces)[i].type) == 2)
-        this->tesselate(i);
-    }
-
-    PRINTF(4)("BSP FILE:PatchOffset: %i . \n", this->patchOffset);
-
-    return  1;
-  } else {
-    PRINTF(4)("BSP FILE: Datei nicht gefunden. \n");
-    return -1;
-  }
-
-
-
+	this->scale = 1.0;
+
+	size_t slurpedBytes;
+	string theBSPFile( name );
+	file.setFileName( theBSPFile );
+	if (file.exists() && file.open( File::ReadOnly ) ) {
+    	PRINTF(0)("BSP FILE: Datei %s gefunden. \n", name);
+    	
+		//BSP-Files have little endian order.
+		file.setByteorder( LittleEndian );
+		file.read( HEADER_SEMANTICS, (void*)&header, slurpedBytes );
+		if ( header.version != 46 ) {
+			PRINTF(0)("BSP FILE: Wrong BSPVersion. We only handle 0x2e-files!\n");   
+			//!< now, we should do some error handling		
+			return ( -1 );
+		}
+		readEntities();
+		readTextures();
+		readPlanes();
+		readNodes();
+		readLeafs(); readLeafFaces(); readLeafBrushes(); 
+		readModels(); 
+		readVertices(); readMeshVerts();
+		readBrushSides(); readBrushes();
+		readEffects();
+		readFaces();
+		readLightmaps(); readLightvols();
+		readVisdata(	
+    	file.close();
+
+	    for(int i = 0 ; i < this->numTextures; i++)
+	    	PRINTF(4)("BSP FILE: Texture 0: %s. \n", &this->textures[8+ 72*i]);
+	    this->load_textures();
+
+	    // Load the lightMaps
+	    this->glLightMapTextures = new GLuint[this->numLightMaps];
+	    for(int i = 0; i < this->numLightMaps; i++)
+	    	this->glLightMapTextures[i] = this->loadLightMapToGL(this->lightMaps[i]);
+
+	    //Create white texture for if no lightmap specified
+	    glGenTextures(1, &this->whiteLightMap);
+	    glBindTexture(GL_TEXTURE_2D, this->whiteLightMap);
+	        //Create texture
+	    this->whiteTexture[0]=255;
+	    this->whiteTexture[1]=255;
+	    this->whiteTexture[2]=255;
+
+	    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+	    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
+	    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+	    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+
+	    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_PRIORITY, 2);
+	    /* control the mipmap levels */
+	    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MIN_LOD, 5);
+	    glTexParameterf(GL_TEXTURE_ENV, GL_TEXTURE_MAX_LOD, 0);
+
+	    /* build the Texture  OpenGL V >= 1.1 */
+	    glTexImage2D(GL_TEXTURE_2D,
+	                 0,
+	                 GL_RGBA8,
+	                 1,
+	                 1,
+	                 0,
+	                 GL_RGB,
+	                 GL_UNSIGNED_BYTE,
+	                 (const GLvoid *)&(this->whiteTexture));
+
+	   gluBuild2DMipmaps(   GL_TEXTURE_2D, GL_RGBA8, 1, 1,
+	                      GL_RGB, GL_UNSIGNED_BYTE,(const GLvoid *) &(this->whiteTexture));
+
+
+
+	    // Get the number of patches
+	    this->numPatches = 0;
+	    this->patchOffset   = 0;
+
+	    for( int i = 0; i < this->numFaces; i++) {
+	      face& cFace = ((face *)(this->faces))[i];
+	      if ( cFace.type  == 2)
+	        this->numPatches += ( cFace.size[0] -1 ) / 2  * ( cFace.size[1] -1 ) / 2;
+	    }
+
+	    // Allocate Memory
+	    this->patchVertice = new char[8*8*44*(this->numPatches+10)];
+	    this->patchIndexes = new char[7*8*2*4*(this->numPatches+10)];
+	 // this->patchRowIndexes = new int*[7*4*this->numPatches]; Not needed?
+	    this->patchTrianglesPerRow = new char[7*4*this->numPatches];
+	    this->VertexArrayModels  = new VertexArrayModel*[this->numPatches];
+
+	    PRINTF(4)("BSP FILE:NumberOfPatches: %i . \n", numPatches);
+
+	    this->swapAllBspCoordinates();
+
+	    // Do tesselation for all Faces of type 2
+	    for( int i = 0; i < this->numFaces; i++) {
+	      if (  (this->faces)[i].type  == 2)
+	        this->tesselate(i);
+	    }
+
+	    PRINTF(4)("BSP FILE:PatchOffset: %i . \n", this->patchOffset);
+
+	    return  1;
+  	} else {
+    	PRINTF(0)("BSP FILE: Datei nicht gefunden. \n");
+    	return -1;
+  	}
 }
 
@@ -430,5 +444,5 @@
   char fileName [500];
   char ext [500];
-  struct stat results;
+  //struct stat results;
 
 
@@ -660,7 +674,7 @@
   int      errorCode = 0;           //!< the error code for the texture loading functions
   unsigned int   lightMap;          //!< the OpenGL texture handle
-  int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
+ /* int      mipmapLevel = 0;         //!< the maximum mipmap level for this texture
   int      mipmapWidth = 0;         //!< the width of the mipmap
-  int      mipmapHight = 0;         //!< the height of the mipmap3
+  int      mipmapHight = 0;         //!< the height of the mipmap3*/
   float sc, scale, temp;
   for(int i = 0; i < 128*128*3 ; i++)
Index: branches/terrain/src/lib/graphics/importer/bsp_file.h
===================================================================
--- branches/terrain/src/lib/graphics/importer/bsp_file.h	(revision 9323)
+++ branches/terrain/src/lib/graphics/importer/bsp_file.h	(revision 9324)
@@ -19,4 +19,5 @@
 
 #include <vector>
+#include "filesys/binary_file.h"
 class SDL_Surface;
 class BspTreeNode;
@@ -27,5 +28,38 @@
 
 
- struct plane
+struct bsp_lump {
+	int			offset;
+	int			length;
+};
+
+enum bsp_lumps {
+	Entities	 	=      0,
+	Textures	 	=      1,
+	Planes 			=      2,
+	Nodes		  	=      3,
+	Leafs		  	=      4,
+	Leaffaces	 	=      5,
+	Leafbrushes		=      6,
+	Models	 		=      7,
+	Brushes	 		=      8,
+	Brushsides		=      9,
+	Vertices	 	=     10,
+	Meshverts	 	=     11,
+	Effects		 	=     12,
+	Faces	 		=     13,
+	Lightmaps	 	=     14,
+	Lightvols	 	=     15,
+	Visdata			=     16
+};
+
+struct bsp_header {
+	// The magic number always "IBSP"
+	char		magic[4];
+	// The version, 0x2e for quake 3 models 
+	int			version;
+	bsp_lump	lumps[17];
+};
+
+struct plane
 {
   float x;     //!< 1st component of the plane's normal
@@ -34,4 +68,5 @@
   float d;     //!< distance of the plane to the origin
 };
+
 
 typedef struct
@@ -82,4 +117,5 @@
 }
 brushside;
+
 
 struct face
@@ -101,4 +137,5 @@
 } ;
 
+
 typedef struct
 {
@@ -130,5 +167,4 @@
 }
 AMat;
-
 typedef struct
 {
@@ -144,4 +180,5 @@
 }
 BspTexture;
+
 
 class BspFile
@@ -162,57 +199,75 @@
 
 private:
-  BspTreeNode* root;
-  char header [ 280 ];       //!< Buffer for header of BSP-File
-  node* nodes;               //!< Buffer to store BSP-Tree-Nodes
-  leaf* leaves;              //!< Buffer to store BSP-Tree-Leaves
-  plane* planes;             //!< Buffer to store planes separateing the space
-  model* bspModels;          //!< Buffer to store BSP-Model-List
-  char* leafFaces;           //!< Buffer to store leafFaces
-  face* faces;               //!<
-  char* leafBrushes;         //!< Buffer to store brush indice
-  brush* brushes;            //!< Buffer to store  brushes
-  brushside* brushSides;     //!<
-  char* vertice;             //!<
-  meshvert* meshverts;       //!< Buffer to store meshverice
-  char* visData;             //!< Buffer to store visibility data
-  char* textures;            //!< Holds all the texture filename strings
-  char* patchVertice;        //!<
-  char* patchIndexes;
-  char* patchTrianglesPerRow;
-  lightmap* lightMaps;       //!< Buffer to store lightmap-images
-
-
-  int** patchRowIndexes;
-  VertexArrayModel** VertexArrayModels; 
-  int patchOffset;
-
-  int numNodes;
-  int numLeafs;
-  int numVertex;
-  int numPlanes;
-  int numBspModels;
-  int numLeafFaces;
-  int numFaces;
-  int numLeafBrushes;
-  int numTextures;
-  int numPatches;
-  int numBrushSides;
-  int numLightMaps;
-  
-  float  scale;
-
-  BspTreeNode* build_tree_rec( int i );
-  unsigned int loadLightMapToGL(lightmap&);
-  AMat* Materials;
-  unsigned int* glLightMapTextures;
-  unsigned int whiteLightMap;
-  unsigned char whiteTexture[3];
-  void swapAllBspCoordinates();
-  void swapCoords(int * array);
-  void swapCoords(float * array);
-  SDL_Surface* testSurf;
-
- 
-  ::std::vector<MoviePlayer* > MovieMaterials; //!< Movieplayer Materials
-};
-
+	void readEntities();
+	void readTextures();
+	void readPlanes();
+	void readNodes();
+	void readLeafs();
+	void readLeafFaces();
+	void readLeafBrushes();
+	void readBrushSides();
+	void readModels();
+	void readBrushes();
+	void readVertices();
+	void readMeshVerts();
+	void readEffects();
+	void readFaces();
+	void readLightmaps();
+	void readLightvols();
+	void readVisdata();
+  	BinaryFile file;
+	BspTreeNode* root;
+	bsp_header header;       	 //!< Buffer for header of BSP-File
+	node* nodes;               //!< Buffer to store BSP-Tree-Nodes
+	leaf* leaves;              //!< Buffer to store BSP-Tree-Leaves
+	plane* planes;             //!< Buffer to store planes separateing the space
+	model* bspModels;          //!< Buffer to store BSP-Model-List
+	char* leafFaces;           //!< Buffer to store leafFaces
+	face* faces;               //!<
+	char* leafBrushes;         //!< Buffer to store brush indice
+	brush* brushes;            //!< Buffer to store  brushes
+	brushside* brushSides;     //!<
+	char* vertice;             //!<
+	meshvert* meshverts;       //!< Buffer to store meshverice
+	char* visData;             //!< Buffer to store visibility data
+	char* textures;            //!< Holds all the texture filename strings
+	char* patchVertice;        //!<
+	char* patchIndexes;
+	char* patchTrianglesPerRow;
+	lightmap* lightMaps;       //!< Buffer to store lightmap-images
+
+
+	int** patchRowIndexes;
+	VertexArrayModel** VertexArrayModels; 
+	int patchOffset;
+
+	int numNodes;
+	int numLeafs;
+	int numVertex;
+	int numPlanes;
+	int numBspModels;
+	int numLeafFaces;
+	int numFaces;
+	int numLeafBrushes;
+	int numTextures;
+	int numPatches;
+	int numBrushSides;
+	int numLightMaps;
+
+	float  scale;
+
+	BspTreeNode* build_tree_rec( int i );
+	unsigned int loadLightMapToGL(lightmap&);
+	AMat* Materials;
+	unsigned int* glLightMapTextures;
+	unsigned int whiteLightMap;
+	unsigned char whiteTexture[3];
+	void swapAllBspCoordinates();
+	void swapCoords(int * array);
+	void swapCoords(float * array);
+	SDL_Surface* testSurf;
+
+
+	::std::vector<MoviePlayer* > MovieMaterials; //!< Movieplayer Materials
+};
+
Index: branches/terrain/src/lib/graphics/importer/bsp_manager.cc
===================================================================
--- branches/terrain/src/lib/graphics/importer/bsp_manager.cc	(revision 9323)
+++ branches/terrain/src/lib/graphics/importer/bsp_manager.cc	(revision 9324)
@@ -196,5 +196,6 @@
   this->outputFraction = 1.0f;
 
-  if ( viscluster < 0  || ((int *)(this->bspFile->header))[35] == 0 )  //!< if (sizeof(Visdata) == 0)
+  if ( ( viscluster < 0  ) ||
+ 		( bspFile->header.lumps[Visdata].length == 0 ) )  //!< if (sizeof(Visdata) == 0)
   {
 
Index: branches/terrain/src/lib/util/Makefile.am
===================================================================
--- branches/terrain/src/lib/util/Makefile.am	(revision 9323)
+++ 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 9323)
+++ 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 9323)
+++ 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.
 
