[Wsplgen-commits] r63 - trunk/src

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Mon Sep 25 07:44:31 CEST 2006


Author: mrchip
Date: 2006-09-25 07:44:29 +0200 (Mon, 25 Sep 2006)
New Revision: 63

Modified:
   trunk/src/file.cpp
   trunk/src/file.h
   trunk/src/shape.cpp
   trunk/src/shape.h
   trunk/src/tools.cpp
   trunk/src/tools.h
   trunk/src/tri.cpp
   trunk/src/wsplgen.cpp
   trunk/src/wsplgen.h
   trunk/src/xy.cpp
   trunk/src/xy.h
Log:
Speicherverwaltung wurde etwas ?\195?\188berarbeitet und SHAPE hat eine Funktion zum Erzeugen von Points bekommen.


Modified: trunk/src/file.cpp
===================================================================
--- trunk/src/file.cpp	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/file.cpp	2006-09-25 05:44:29 UTC (rev 63)
@@ -156,7 +156,7 @@
 	}
 	else
 	{
-		write_error(1222, "Ungültige Dateinamenserweiterung '%s' beim Dateinamen '%s' für den Parameter -DGM\nErlaubt sind nur ADF, 2DM, XYZ, TXT oder SHP\n", ToUpperCase(Ext).c_str(), FileName.c_str());
+		write_error(1222, "Ungültige Dateinamenserweiterung '%s' beim Dateinamen '%s' für den Parameter -DGM\nErlaubt sind nur ADF, GRD, 2DM, XYZ, TXT oder SHP\n", ToUpperCase(Ext).c_str(), FileName.c_str());
 	}
 
 	if (BegrenzungsPolygon)
@@ -326,7 +326,7 @@
 			if (Node1 && Node2 && Node3)
 			{
 				TElement *Element = new TElement(Node1, Node2, Node3);
-				ElementList->push_back(Element);
+				ElementList->Add(Element);
 			}
 			AnzElements++;
 		}
@@ -941,7 +941,7 @@
 				if (Node1 && Node2 && Node3)
 				{
 					TElement *Element = new TElement(Node1, Node2, Node3);
-					ElementList->push_back(Element);
+					ElementList->Add(Element);
 				}
 				continue;
 			}
@@ -961,7 +961,7 @@
 				if (Node1 && Node2 && Node3 && Node4)
 				{
 					TElement *Element = new TElement(Node1, Node2, Node3, Node4);
-					ElementList->push_back(Element);
+					ElementList->Add(Element);
 				}
 				continue;
 			}
@@ -1275,13 +1275,13 @@
 }
 
 //---------------------------------------------------------------------
-bool LoadLinien(std::string FileName, TProfilList* SperrenList, TProfilList* GrabenList, TProfilList* RohreList, TProfilList* GradientenList, int DebugLevel)
+bool LoadLinien(std::string FileName, TProfilList* SperrenList, TProfilList* GrabenList, TProfilList* Rohre1List, TProfilList* Rohre2List, int DebugLevel)
 {
 	write_fortschritt("->Laden der Linien Strukturen (Sperren, Gräben und Rohre) gestartet\n");
 
 	LoadPRJ(FileName, DebugLevel);
 
-	if (SperrenList == 0 || GrabenList == 0 || RohreList == 0)
+	if (SperrenList == 0 || GrabenList == 0 || Rohre1List == 0)
 	{
 		dump_error(__FILE__, __LINE__, "Sperrenliste oder Grabenliste oder Rohreliste ist undefiniert\n");
 	}
@@ -1420,17 +1420,17 @@
 		{
 			Typ = "GRABEN";
 		}
-		else if (Typ == "ROHR" || Typ == "Rohr" || Typ == "rohr")
+		else if (Typ == "ROHR" || Typ == "Rohr" || Typ == "rohr" || Typ == "ROHR1" || Typ == "rohr1" || Typ == "Rohr1")
 		{
 			Typ = "ROHR";
 		}
-		else if (Typ == "GRADIENT" || Typ == "Gradient" || Typ == "gradient")
+		else if (Typ == "GRADIENT" || Typ == "Gradient" || Typ == "gradient" || Typ == "ROHR2" || Typ == "rohr2" || Typ == "Rohr2")
 		{
 			Typ = "GRADIENT";
 		}
 		else
 		{
-			write_warning(3112, "Das Attribut 'TYP' in der Datei '%s' darf nur die Werte 'Damm', 'Graben', 'Gradient' oder 'Rohr' haben.\nDas Linienobjekt '%s' wird ignoriert.\n", DBFFileName.c_str(), Typ.c_str());
+			write_warning(3112, "Das Attribut 'TYP' in der Datei '%s' darf nur die Werte 'Damm', 'Graben', 'Rohr1' oder 'Rohr2' haben.\nDas Linienobjekt '%s' wird ignoriert.\n", DBFFileName.c_str(), Typ.c_str());
 			continue;
 		}
 
@@ -1450,8 +1450,8 @@
 
 		if (Typ == "DAMM")			SperrenList->insert(Linie);
 		else if (Typ == "GRABEN")	GrabenList->insert(Linie);
-		else if (Typ == "ROHR")		RohreList->insert(Linie);
-		else if (Typ == "GRADIENT")	GradientenList->insert(Linie);
+		else if (Typ == "ROHR")		Rohre1List->insert(Linie);
+		else if (Typ == "GRADIENT")	Rohre2List->insert(Linie);
 		else						dump_error(__FILE__, __LINE__, "Unbekannter Typ '%s' in der Datei '%s'\n", Typ.c_str(), DBFFileName.c_str());
 
 		for (int j=0; j < AnzVert; j++)
@@ -1470,10 +1470,10 @@
 
 		if (DebugLevel >= 9 || (DebugLevel >= 1 && Count % 10 == 0) || (Count > 0 && Count % 100 == 0))
 		{
-			write_fortschritt("%d von %d Liniensstrukturen geladen, %d Sperren, %d Gräben, %d Rohre\n", Count, RecordCount, SperrenList->size(), GrabenList->size(), RohreList->size());
+			write_fortschritt("%d von %d Liniensstrukturen geladen, %d Sperren, %d Gräben, %d Rohre Tpy 1, %d Rohre Tpy 2\n", Count, RecordCount, SperrenList->size(), GrabenList->size(), Rohre1List->size(), Rohre2List->size());
 		}
 	}
-	write_fortschritt("%d von %d Liniensstrukturen geladen, %d Sperren, %d Gräben, %d Rohre\n", Count, RecordCount, SperrenList->size(), GrabenList->size(), RohreList->size());
+	write_fortschritt("%d von %d Liniensstrukturen geladen, %d Sperren, %d Gräben, %d Rohre Typ 1, %d Rohre Typ 2\n", Count, RecordCount, SperrenList->size(), GrabenList->size(), Rohre1List->size(), Rohre2List->size());
 
 	DBFClose(hDBF);
 	SHPClose(hSHP);
@@ -2292,6 +2292,8 @@
 
 		TNode *Node = (*NodeList)[i];
 
+		if (Node->Z == NoZ)	continue;
+
 		fprintf(fh, "ND %6d %15.9e %15.9e %15.9e\n", Node->Nr, Node->X / 100.0, Node->Y / 100.0, Node->Z / 100.0);
 	}
 
@@ -2405,15 +2407,15 @@
 		write_error(4204, "Kann bei Datei '%s' das Attribut 'STATION' nicht erzeugen\n", FileName.c_str());
 	}
 
-	if (DBFAddField(DBFHandle, "WSP", FTDouble, 8, 2) == -1)
+	if (DBFAddField(DBFHandle, "WSP", FTDouble, 10, 2) == -1)
 	{
 		DBFClose(DBFHandle);
 		write_error(4204, "Kann bei Datei '%s' das Attribut 'WSP' nicht erzeugen\n", FileName.c_str());
 	}
 
 	SHPHandle SHPHandle = SHPCreate(FileName.c_str(), SHPT_ARC);
+	SHPSetMaxRecords(SHPHandle, ProfilList->size());
 
-
 	int AnzProfil = 0;
 	for (TProfilList::iterator i = ProfilList->begin(); i != ProfilList->end(); i++)
 	{
@@ -2472,7 +2474,7 @@
 //---------------------------------------------------------------------------
 bool SavePolygon(std::string FileName, TXYList *XyList, int DebugLevel)
 {
-	write_fortschritt("->Speichern des Polygons gestartet\n");
+	write_fortschritt("->Speichern des Begrenzungspolygons gestartet\n");
 
 	SavePRJ(FileName, DebugLevel);
 
@@ -2541,7 +2543,7 @@
 
 	write_fortschritt("%d von %d Stützstellen gespeichert\n", AnzPoint, XyList->size()+1);
 
-	write_fortschritt("<-Speichern des Polygons beendet\n");
+	write_fortschritt("<-Speichern des Begrenzungspolygons beendet\n");
 
 	return (true);
 }
@@ -2567,7 +2569,7 @@
 
 	TErgebnisPolygonList *ErgebnisPolygonList = PolygonList->Copy();
 
-	write_fortschritt("->Löschen der zu kleinen Ergebnispolygone gestartet\n");
+	write_fortschritt("->Löschen der zu kleinen Ringe gestartet\n");
 
 	int	Count = 0;
 	int DelCount = 0;
@@ -2592,12 +2594,12 @@
 
 		Count++;
 
-		if (Count % 10000 == 0) write_fortschritt("Ergebnispolygon %d von %d überpfüft\n", Count+1, ErgebnisPolygonList->size());
+		if (Count % 10000 == 0) write_fortschritt("Ring %d von %d überprüft\n", Count+1, ErgebnisPolygonList->size());
 	}
-	write_fortschritt("%d Ergebnispolygone gelöscht\n", DelCount);
-	write_fortschritt("minimale Polygonfläche: %.3f\n", MinArea);
+	write_fortschritt("%d Ringe gelöscht\n", DelCount);
+	write_fortschritt("kleinste gelöschte Ringfläche: %.3f\n", MinArea);
 
-	write_fortschritt("<-Löschen der zu kleinen Ergebnispolygone beendet\n");
+	write_fortschritt("<-Löschen der zu kleinen Ringe beendet\n");
 
 	SHPHandle SHPHandle = SHPCreate(FileName.c_str(), SHPT_POLYGON);
 
@@ -2743,7 +2745,7 @@
 }
 
 //---------------------------------------------------------------------------
-int OutElement(int AnzPolygone, TElement *Element, int TopoDiff, DBFHandle PolygonDBFHandle, SHPHandle PolygonSHPHandle, SHPObject *psShape, double *x, double *y, double *z)
+int OutElement(int AnzPolygone, TElement *Element, int TopoDiff, DBFHandle PolygonDBFHandle, SHPHandle PolygonSHPHandle)
 {
 	if (Element->Typ == TRI)
 	{
@@ -2783,6 +2785,9 @@
 
 		if (Depth < TopoDiff)	Depth = TopoDiff;
 
+		double  x[4];
+		double  y[4];
+		double  z[4];
 
 		double win = ThreeToWin (Node2->X, Node2->Y, Node1->X, Node1->Y, Node3->X, Node3->Y, 0);
 
@@ -2816,7 +2821,10 @@
 		z[2] = 0.0;
 		z[3] = 0.0;
 
+
+		SHPObject *psShape = SHPCreateSimpleObject(SHPT_POLYGON, 4, x, y, z);
 		SHPWriteObject(PolygonSHPHandle, -1, psShape);
+		SHPDestroyObject(psShape);
 
 		DBFWriteDoubleAttribute(PolygonDBFHandle, AnzPolygone, 0, Topo);
 		DBFWriteDoubleAttribute(PolygonDBFHandle, AnzPolygone, 1, Wsp);
@@ -2834,11 +2842,11 @@
 		TElement *TempElement = 0;
 
 		TempElement = new TElement(Node1, Node2, Node3);
-		AnzPolygone = OutElement(AnzPolygone, TempElement, TopoDiff, PolygonDBFHandle, PolygonSHPHandle, psShape, x, y, z);
+		AnzPolygone = OutElement(AnzPolygone, TempElement, TopoDiff, PolygonDBFHandle, PolygonSHPHandle);
 		delete TempElement;
 
 		TempElement = new TElement(Node1, Node3, Node4);
-		AnzPolygone = OutElement(AnzPolygone, TempElement, TopoDiff, PolygonDBFHandle, PolygonSHPHandle, psShape, x, y, z);
+		AnzPolygone = OutElement(AnzPolygone, TempElement, TopoDiff, PolygonDBFHandle, PolygonSHPHandle);
 		delete TempElement;
 	}
 
@@ -2858,32 +2866,27 @@
 		write_error(4201, "Kann Datei '%s' nicht zum Schreiben öffnen\n", FileName.c_str());
 	}
 
-	if (DBFAddField(DBFHandle, "Topo", FTDouble, 7, 2) == -1)
+	if (DBFAddField(DBFHandle, "Topo", FTDouble, 10, 2) == -1)
 	{
 		DBFClose(DBFHandle);
 		write_error(4204, "Kann bei Datei '%s' das Attribut 'Topo' nicht erzeugen\n", FileName.c_str());
 	}
 
-	if (DBFAddField(DBFHandle, "Wsp", FTDouble, 7, 2) == -1)
+	if (DBFAddField(DBFHandle, "Wsp", FTDouble, 10, 2) == -1)
 	{
 		DBFClose(DBFHandle);
 		write_error(4204, "Kann bei Datei '%s' das Attribut 'Wsp' nicht erzeugen\n", FileName.c_str());
 	}
 
-	if (DBFAddField(DBFHandle, "Depth", FTDouble, 7, 2) == -1)
+	if (DBFAddField(DBFHandle, "Depth", FTDouble, 10, 2) == -1)
 	{
 		DBFClose(DBFHandle);
 		write_error(4204, "Kann bei Datei '%s' das Attribut 'Depth' nicht erzeugen\n", FileName.c_str());
 	}
 
 	SHPHandle SHPHandle = SHPCreate(FileName.c_str(), SHPT_POLYGON);
+	SHPSetMaxRecords(SHPHandle, ElementList->size());
 
-	double  x[4];
-	double  y[4];
-	double  z[4];
-
-	SHPObject *psShape = SHPCreateSimpleObject(SHPT_POLYGON, 4, x, y, z);
-
 	int AnzPolygone = 0;
 	for (TElementList::iterator i=ElementList->begin(); i != ElementList->end(); i++)
 	{
@@ -2894,11 +2897,9 @@
 
 		TElement *Element = *i;
 
-		AnzPolygone = OutElement(AnzPolygone, Element, TopoDiff, DBFHandle, SHPHandle, psShape, x, y, z);
+		AnzPolygone = OutElement(AnzPolygone, Element, TopoDiff, DBFHandle, SHPHandle);
 	}
 
-	SHPDestroyObject(psShape);
-
 	SHPClose(SHPHandle);
 	DBFClose(DBFHandle);
 
@@ -2922,32 +2923,27 @@
 		write_error(4201, "Kann Datei '%s' nicht zum Schreiben öffnen\n", FileName.c_str());
 	}
 
-	if (DBFAddField(DBFHandle, "Topo", FTDouble, 9, 2) == -1)
+	if (DBFAddField(DBFHandle, "Topo", FTDouble, 10, 2) == -1)
 	{
 		DBFClose(DBFHandle);
 		write_error(4204, "Kann bei Datei '%s' das Attribut 'Topo' nicht erzeugen\n", FileName.c_str());
 	}
 
-	if (DBFAddField(DBFHandle, "Wsp", FTDouble, 9, 2) == -1)
+	if (DBFAddField(DBFHandle, "Wsp", FTDouble, 10, 2) == -1)
 	{
 		DBFClose(DBFHandle);
 		write_error(4204, "Kann bei Datei '%s' das Attribut 'Wsp' nicht erzeugen\n", FileName.c_str());
 	}
 
-	if (DBFAddField(DBFHandle, "Depth", FTDouble, 9, 2) == -1)
+	if (DBFAddField(DBFHandle, "Depth", FTDouble, 10, 2) == -1)
 	{
 		DBFClose(DBFHandle);
 		write_error(4204, "Kann bei Datei '%s' das Attribut 'Depth' nicht erzeugen\n", FileName.c_str());
 	}
 
 	SHPHandle SHPHandle = SHPCreate(FileName.c_str(), SHPT_POINT);
+	SHPSetMaxRecords(SHPHandle, NodeList->size());
 
-	double X = 0.0;
-	double Y = 0.0;
-	double Z = 0.0;
-
-	SHPObject *psShape = SHPCreateSimpleObject(SHPT_POINT, 1, &X, &Y, &Z);
-
 	int AnzNodes = NodeList->size();
 	int Count = 0;
 	for (TNodeList::iterator i=NodeList->begin(); i != NodeList->end(); i++)
@@ -2961,11 +2957,15 @@
 
 		if (Node->Z != NoZ)
 		{
-			X = Node->X / 100.0;
-			Y = Node->Y / 100.0;
-			Z = 0.0;
+			double X = Node->X / 100.0;
+			double Y = Node->Y / 100.0;
+			double Z = 0.0;
 
+//			SHPWritePoint(SHPHandle, X, Y);
+
+			SHPObject *psShape = SHPCreateSimpleObject(SHPT_POINT, 1, &X, &Y, &Z);
 			SHPWriteObject(SHPHandle, -1, psShape);
+			SHPDestroyObject(psShape);
 
 			DBFWriteDoubleAttribute(DBFHandle, Count, 0, Node->Z / 100.0);
 
@@ -2988,8 +2988,6 @@
 		}
 	}
 
-	SHPDestroyObject(psShape);
-
 	SHPClose(SHPHandle);
 	DBFClose(DBFHandle);
 
@@ -3033,20 +3031,6 @@
 
 	SHPHandle SHPHandle = SHPCreate(FileName.c_str(), SHPT_ARC);
 
-
-	double	x[2];
-	double 	y[2];
-	double 	z[2];
-
-	x[0] = 0.0;
-	x[1] = 0.0;
-	y[0] = 0.0;
-	y[1] = 0.0;
-	z[0] = 0.0;
-	z[1] = 0.0;
-
-	SHPObject *psShape = SHPCreateSimpleObject(SHPT_ARC, 2, x, y, z);
-
 	int Count = 0;
 	for (TEdgeVector::iterator i = EdgeList->EdgeVector.begin(); i != EdgeList->EdgeVector.end(); i++)
 	{
@@ -3063,12 +3047,20 @@
 		int Nr1 = Node1->Nr;
 		int Nr2 = Node2->Nr;
 
+		double	x[2];
+		double 	y[2];
+		double 	z[2];
+
 		x[0] = Node1->X / 100.0;
 		y[0] = Node1->Y / 100.0;
 		x[1] = Node2->X / 100.0;
 		y[1] = Node2->Y / 100.0;
+		z[0] = 0.0;
+		z[1] = 0.0;
 
+		SHPObject *psShape = SHPCreateSimpleObject(SHPT_ARC, 2, x, y, z);
 		SHPWriteObject(SHPHandle, -1, psShape);
+		SHPDestroyObject(psShape);
 
 		DBFWriteIntegerAttribute(DBFHandle, Count, 0, Nr1);
 		DBFWriteIntegerAttribute(DBFHandle, Count, 1, Nr2);
@@ -3077,7 +3069,6 @@
 
 		Count++;
 	}
-	SHPDestroyObject(psShape);
 
 	DBFClose(DBFHandle);
 	SHPClose(SHPHandle);

Modified: trunk/src/file.h
===================================================================
--- trunk/src/file.h	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/file.h	2006-09-25 05:44:29 UTC (rev 63)
@@ -31,7 +31,7 @@
 bool			LoadDGM2DM(std::string FileName, TNodeList *NodeList, TElementList *ElementList, TXYList *XyList, int DebugLevel);
 
 bool 			LoadProfile(std::string FileName, TProfilList* ProfilList, double VonKm, double BisKm, int DebugLevel);
-bool 			LoadLinien(std::string FileName, TProfilList* SperrenList, TProfilList* GrabenList, TProfilList* RohreList, TProfilList* GradientenList, int DebugLevel);
+bool 			LoadLinien(std::string FileName, TProfilList* SperrenList, TProfilList* GrabenList, TProfilList* Rohre1List, TProfilList* Rohre2List, int DebugLevel);
 
 bool 			LoadWsp(std::string FileName, TProfilList* ProfilList, std::string Tag, int DebugLevel);
 bool 			LoadWspTxt(std::string FileName, TProfilList* ProfilList, int DebugLevel);

Modified: trunk/src/shape.cpp
===================================================================
--- trunk/src/shape.cpp	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/shape.cpp	2006-09-25 05:44:29 UTC (rev 63)
@@ -22,6 +22,9 @@
 
 static int 	bBigEndian;
 
+static FILE*	fpSHX = 0;
+static FILE*	fpSHP = 0;
+
 /************************************************************************/
 /*                          SHPWriteHeader()                            */
 /*                                                                      */
@@ -50,7 +53,7 @@
 	ByteCopy(&i32, abyHeader+24, 4);
 	if(!bBigEndian) Swap4Bytes(abyHeader+24);
 
-	i32 = 1000;						/* version */
+	i32 = 1000;								/* version */
 	ByteCopy( &i32, abyHeader+28, 4 );
 	if( bBigEndian ) Swap4Bytes(abyHeader+28 );
 
@@ -93,33 +96,33 @@
 	/* -------------------------------------------------------------------- */
 	/*      Write .shp file header.                                         */
 	/* -------------------------------------------------------------------- */
-	fseek( psSHP->fpSHP, 0, 0 );
-	fwrite( abyHeader, 100, 1, psSHP->fpSHP );
+	fseek( fpSHP, 0, 0 );
+	fwrite( abyHeader, 100, 1, fpSHP );
 
 	/* -------------------------------------------------------------------- */
 	/*      Prepare, and write .shx file header.                            */
 	/* -------------------------------------------------------------------- */
-    i32 = (psSHP->nRecords * 2 * sizeof(int32) + 100)/2;   /* file size */
+    i32 = (psSHP->nRecords * 2 * sizeof(int32) + 100) / 2;   /* file size */
     ByteCopy( &i32, abyHeader+24, 4 );
     if( !bBigEndian ) Swap4Bytes(abyHeader+24 );
     
-    fseek( psSHP->fpSHX, 0, 0 );
-    fwrite( abyHeader, 100, 1, psSHP->fpSHX );
+    fseek( fpSHX, 0, 0 );
+    fwrite( abyHeader, 100, 1, fpSHX );
 
 	/* -------------------------------------------------------------------- */
 	/*      Write out the .shx contents.                                    */
 	/* -------------------------------------------------------------------- */
-	panSHX = (int32 *) malloc(sizeof(int32) * 2 * psSHP->nRecords);
+	panSHX = (int32 *) SfMalloc(sizeof(int32) * 2 * psSHP->nRecords);
 
 	for (int i = 0; i < psSHP->nRecords; i++)
     {
-		panSHX[i*2  ] = psSHP->panRecOffset[i]/2;
-		panSHX[i*2+1] = psSHP->panRecSize[i]/2;
-		if( !bBigEndian ) Swap4Bytes(panSHX+i*2);
-		if( !bBigEndian ) Swap4Bytes(panSHX+i*2+1);
+		panSHX[i*2  ] = psSHP->panRecOffset[i] / 2;
+		panSHX[i*2+1] = psSHP->panRecSize[i] / 2;
+		if( !bBigEndian ) Swap4Bytes(panSHX + i * 2);
+		if( !bBigEndian ) Swap4Bytes(panSHX + i * 2 + 1);
     }
 
-	fwrite(panSHX, sizeof(int32) * 2, psSHP->nRecords, psSHP->fpSHX);
+	fwrite(panSHX, sizeof(int32) * 2, psSHP->nRecords, fpSHX);
 
 	free (panSHX);
 }
@@ -157,7 +160,7 @@
 /* -------------------------------------------------------------------- */
 /*	Initialize the info structure.					*/
 /* -------------------------------------------------------------------- */
-	psSHP = (SHPHandle)calloc(sizeof(SHPInfo), 1);
+	psSHP = (SHPHandle)SfCalloc(sizeof(SHPInfo), 1);
 
 	psSHP->bUpdated = FALSE;
 
@@ -165,7 +168,7 @@
 	/*	Compute the base (layer) name.  If there is any extension	*/
 	/*	on the passed in filename we will strip it off.			*/
 	/* -------------------------------------------------------------------- */
-	pszBasename = (char *) malloc(strlen(pszLayer)+5);
+	pszBasename = (char *) SfMalloc(strlen(pszLayer)+5);
 	strcpy( pszBasename, pszLayer );
 	int i = strlen(pszBasename)-1;
 	while(i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/' && pszBasename[i] != '\\')
@@ -179,16 +182,16 @@
 	/*	Open the .shp and .shx files.  Note that files pulled from			*/
 	/*	a PC to Unix with upper case filenames won't work!					*/
 	/* -------------------------------------------------------------------- */
-	pszFullname = (char *) malloc(strlen(pszBasename) + 5);
+	pszFullname = (char *) SfMalloc(strlen(pszBasename) + 5);
 	sprintf( pszFullname, "%s.shp", pszBasename );
-	psSHP->fpSHP = fopen(pszFullname, pszAccess );
-	if( psSHP->fpSHP == NULL )
+	fpSHP = fopen(pszFullname, pszAccess );
+	if( fpSHP == NULL )
 	{
 		sprintf( pszFullname, "%s.SHP", pszBasename );
-		psSHP->fpSHP = fopen(pszFullname, pszAccess );
+		fpSHP = fopen(pszFullname, pszAccess );
 	}
 
-	if( psSHP->fpSHP == NULL )
+	if( fpSHP == NULL )
 	{
 		free(psSHP);
 		free(pszBasename);
@@ -197,16 +200,17 @@
 	}
 
 	sprintf( pszFullname, "%s.shx", pszBasename );
-	psSHP->fpSHX = fopen(pszFullname, pszAccess );
-	if( psSHP->fpSHX == NULL )
+	fpSHX = fopen(pszFullname, pszAccess );
+	if( fpSHX == NULL )
 	{
 		sprintf( pszFullname, "%s.SHX", pszBasename );
-		psSHP->fpSHX = fopen(pszFullname, pszAccess );
+		fpSHX = fopen(pszFullname, pszAccess );
 	}
 
-	if( psSHP->fpSHX == NULL )
+	if( fpSHX == NULL )
     {
-		fclose(psSHP->fpSHP);
+		fclose(fpSHP);
+		fpSHP = 0;
 		free(psSHP);
 		free(pszBasename);
 		free(pszFullname);
@@ -219,20 +223,23 @@
 	/* -------------------------------------------------------------------- */
 	/*  Read the file size from the SHP file.				*/
 	/* -------------------------------------------------------------------- */
-    pabyBuf = (uchar *) malloc(100);
-	fread(pabyBuf, 100, 1, psSHP->fpSHP);
+	pabyBuf = (uchar *) SfMalloc(100);
 
+	fread(pabyBuf, 100, 1, fpSHP);
+
 	psSHP->nFileSize = (pabyBuf[24] * 256 * 256 * 256 + pabyBuf[25] * 256 * 256 + pabyBuf[26] * 256 + pabyBuf[27]) * 2;
 
 	/* -------------------------------------------------------------------- */
 	/*  Read SHX file Header info                                           */
 	/* -------------------------------------------------------------------- */
-	fread(pabyBuf, 100, 1, psSHP->fpSHX);
+	fread(pabyBuf, 100, 1, fpSHX);
 
 	if( pabyBuf[0] != 0 || pabyBuf[1] != 0 || pabyBuf[2] != 0x27 || (pabyBuf[3] != 0x0a && pabyBuf[3] != 0x0d))
 	{
-		fclose(psSHP->fpSHP);
-		fclose(psSHP->fpSHX);
+		fclose(fpSHP);
+		fpSHP = 0;
+		fclose(fpSHX);
+		fpSHX = 0;
 		free(psSHP);
 		return(NULL);
 	}
@@ -240,13 +247,15 @@
     psSHP->nRecords = pabyBuf[27] + pabyBuf[26] * 256 + pabyBuf[25] * 256 * 256 + pabyBuf[24] * 256 * 256 * 256;
 	psSHP->nRecords = (psSHP->nRecords * 2 - 100) / 8;
 
-    psSHP->nShapeType = pabyBuf[32];
+	psSHP->nShapeType = pabyBuf[32];
 
     if( psSHP->nRecords < 0 || psSHP->nRecords > 256000000 )
     {
         /* this header appears to be corrupt.  Give up. */
-		fclose(psSHP->fpSHP);
-		fclose(psSHP->fpSHX);
+		fclose(fpSHP);
+		fpSHP = 0;
+		fclose(fpSHX);
+		fpSHX = 0;
 		free(psSHP);
 		return(NULL);
 	}
@@ -274,7 +283,7 @@
     memcpy( &dValue, pabyBuf+68, 8 );
     psSHP->adBoundsMin[2] = dValue;
     
-    if( bBigEndian ) Swap8Bytes(pabyBuf+76 );
+	if( bBigEndian ) Swap8Bytes(pabyBuf+76 );
     memcpy( &dValue, pabyBuf+76, 8 );
     psSHP->adBoundsMax[2] = dValue;
     
@@ -296,18 +305,18 @@
 
 	if (psSHP->nMaxRecords > 1)
 	{
-		psSHP->panRecOffset = (int *) malloc(sizeof(int) * psSHP->nMaxRecords);
-		psSHP->panRecSize =   (int *) malloc(sizeof(int) * psSHP->nMaxRecords);
-		pabyBuf = (uchar *) malloc(8 * psSHP->nMaxRecords);
+		psSHP->panRecOffset = (int *) SfMalloc(sizeof(int) * psSHP->nMaxRecords);
+		psSHP->panRecSize =   (int *) SfMalloc(sizeof(int) * psSHP->nMaxRecords);
+		pabyBuf = (uchar *) SfMalloc(8 * psSHP->nMaxRecords);
 	}
 	else
 	{
-		psSHP->panRecOffset = (int *) malloc(sizeof(int) * 1);
-		psSHP->panRecSize =   (int *) malloc(sizeof(int) * 1);
-		pabyBuf = (uchar *) malloc(8 * 1);
+		psSHP->panRecOffset = (int *) SfMalloc(sizeof(int) * 1);
+		psSHP->panRecSize =   (int *) SfMalloc(sizeof(int) * 1);
+		pabyBuf = (uchar *) SfMalloc(8 * 1);
 	}
 
-	fread( pabyBuf, 8, psSHP->nRecords, psSHP->fpSHX );
+	fread( pabyBuf, 8, psSHP->nRecords, fpSHX );
 
     for(int i = 0; i < psSHP->nRecords; i++ )
     {
@@ -338,7 +347,7 @@
 /* -------------------------------------------------------------------- */
 /*	Update the header if we have modified anything.			*/
 /* -------------------------------------------------------------------- */
-    if( psSHP->bUpdated )
+	if( psSHP->bUpdated )
     {
 		SHPWriteHeader( psSHP );
     }
@@ -349,8 +358,10 @@
 	free( psSHP->panRecOffset );
     free( psSHP->panRecSize );
 
-    fclose( psSHP->fpSHX );
-	fclose( psSHP->fpSHP );
+	fclose( fpSHX );
+	fpSHX = 0;
+	fclose( fpSHP );
+	fpSHP = 0;
 
     if( psSHP->pabyRec != NULL )
     {
@@ -410,7 +421,6 @@
 SHPHandle SHPAPI_CALL SHPCreate( const char * pszLayer, int nShapeType )
 {
 	char	*pszBasename, *pszFullname;
-	FILE	*fpSHP, *fpSHX;
 	uchar   abyHeader[100];
 	int32	i32;
 	double	dValue;
@@ -426,7 +436,7 @@
 /*	Compute the base (layer) name.  If there is any extension	*/
 /*	on the passed in filename we will strip it off.			*/
 /* -------------------------------------------------------------------- */
-	pszBasename = (char *) malloc(strlen(pszLayer)+5);
+	pszBasename = (char *) SfMalloc(strlen(pszLayer)+5);
 	strcpy( pszBasename, pszLayer );
 	int i = strlen(pszBasename)-1;
 	while(i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/' && pszBasename[i] != '\\')
@@ -439,8 +449,8 @@
 	/* -------------------------------------------------------------------- */
 	/*      Open the two files so we can write their headers.               */
 	/* -------------------------------------------------------------------- */
-    pszFullname = (char *) malloc(strlen(pszBasename) + 5);
-    sprintf( pszFullname, "%s.shp", pszBasename );
+	pszFullname = (char *) SfMalloc(strlen(pszBasename) + 5);
+	sprintf( pszFullname, "%s.shp", pszBasename );
     fpSHP = fopen(pszFullname, "wb" );
 	if(fpSHP == NULL) return(NULL);
 
@@ -494,8 +504,10 @@
 	/* -------------------------------------------------------------------- */
 	/*      Close the files, and then open them as regular existing files.  */
 	/* -------------------------------------------------------------------- */
-    fclose( fpSHP );
-    fclose( fpSHX );
+	fclose( fpSHP );
+	fpSHP = 0;
+	fclose( fpSHX );
+	fpSHX = 0;
 
     return(SHPOpen(pszLayer, "r+b"));
 }
@@ -569,7 +581,7 @@
     SHPObject	*psObject;
     int			i, bHasM, bHasZ;
 
-    psObject = (SHPObject *) calloc(1,sizeof(SHPObject));
+	psObject = (SHPObject *) SfCalloc(1, sizeof(SHPObject));
     psObject->nSHPType = nSHPType;
     psObject->nShapeId = nShapeId;
 
@@ -601,9 +613,8 @@
 		if (nParts > 1)	psObject->nParts = nParts;
 		else			psObject->nParts = 1;
 
-		psObject->panPartStart = (int *)malloc(sizeof(int) * psObject->nParts);
-		psObject->panPartType = (int *)malloc(sizeof(int) * psObject->nParts);
-
+		psObject->panPartStart = (int *)SfMalloc(sizeof(int) * psObject->nParts);
+		psObject->panPartType = (int *)SfMalloc(sizeof(int) * psObject->nParts);
 		psObject->panPartStart[0] = 0;
 		psObject->panPartType[0] = SHPP_RING;
 
@@ -621,14 +632,11 @@
 	/* -------------------------------------------------------------------- */
 	if (nVertices > 0)
 	{
-		psObject->padfX = (double *) calloc(sizeof(double), nVertices);
-		psObject->padfY = (double *) calloc(sizeof(double), nVertices);
-		psObject->padfZ = (double *) calloc(sizeof(double), nVertices);
-		psObject->padfM = (double *) calloc(sizeof(double), nVertices);
+		psObject->padfX = (double *) SfCalloc(sizeof(double), nVertices);
+		psObject->padfY = (double *) SfCalloc(sizeof(double), nVertices);
+		psObject->padfZ = (double *) SfCalloc(sizeof(double), nVertices);
+		psObject->padfM = (double *) SfCalloc(sizeof(double), nVertices);
 
-		assert(padfX != NULL);
-		assert(padfY != NULL);
-
 		for (int i = 0; i < nVertices; i++)
 		{
 			psObject->padfX[i] = padfX[i];
@@ -659,6 +667,114 @@
 }
                                   
 /************************************************************************/
+/*                           SHPSetMaxRecords()                         */
+/*                                                                      */
+/*      Sets nMaxRecords und reallocs enough Memory                     */
+/************************************************************************/
+void SHPAPI_CALL SHPSetMaxRecords(SHPHandle psSHP, int nNewMaxRecords)
+{
+	if (nNewMaxRecords > psSHP->nMaxRecords)
+	{
+		psSHP->nMaxRecords = nNewMaxRecords;
+		psSHP->panRecOffset = (int *)SfRealloc(psSHP->panRecOffset, sizeof(int) * psSHP->nMaxRecords);
+		psSHP->panRecSize = (int *)SfRealloc(psSHP->panRecSize, sizeof(int) * psSHP->nMaxRecords);
+	}
+}
+
+/************************************************************************/
+/*                           SHPWritePoint()                            */
+/*                                                                      */
+/*      Write out the vertices of a new structure.  Note that it is     */
+/*      only possible to write vertices at the end of the file.         */
+/************************************************************************/
+int SHPAPI_CALL SHPWritePoint(SHPHandle psSHP, double X, double Y)
+{
+	int		nRecordOffset, nRecordSize;
+	uchar	pabyRec[28];
+	int32	i32;
+
+	psSHP->bUpdated = TRUE;
+
+	/* -------------------------------------------------------------------- */
+	/*      Ensure that shape object matches the type of the file it is     */
+	/*      being written to.                                               */
+	/* -------------------------------------------------------------------- */
+	assert(SHPT_POINT == psSHP->nShapeType);
+
+	/* -------------------------------------------------------------------- */
+	/*      Add the new entity to the in memory index.                      */
+	/* -------------------------------------------------------------------- */
+	if (psSHP->nRecords + 1 > psSHP->nMaxRecords)
+	{
+		psSHP->nMaxRecords =(int)(psSHP->nMaxRecords * 1.3 + 100);
+		psSHP->panRecOffset = (int *)SfRealloc(psSHP->panRecOffset, sizeof(int) * psSHP->nMaxRecords);
+		psSHP->panRecSize = (int *)SfRealloc(psSHP->panRecSize, sizeof(int) * psSHP->nMaxRecords);
+	}
+
+	nRecordSize = 28;
+
+	int nShapeId = psSHP->nRecords++;
+
+	psSHP->panRecOffset[nShapeId] = nRecordOffset = psSHP->nFileSize;
+	psSHP->panRecSize[nShapeId] = nRecordSize - 8;
+	psSHP->nFileSize += nRecordSize;
+
+	/* -------------------------------------------------------------------- */
+	/*      Set the record number, record size, shape type, X, Y.           */
+	/* -------------------------------------------------------------------- */
+	i32 = nShapeId+1;						/* record # */
+	if(!bBigEndian) Swap4Bytes(&i32);
+	ByteCopy(&i32, pabyRec + 0, 4);
+
+	i32 = (nRecordSize-8)/2;				/* record size */
+	if(!bBigEndian) Swap4Bytes(&i32);
+	ByteCopy(&i32, pabyRec + 4, 4);
+
+	i32 = SHPT_POINT;						/* shape type */
+	if(bBigEndian) Swap4Bytes(&i32);
+	ByteCopy( &i32, pabyRec + 8, 4);
+
+	ByteCopy(&X, pabyRec + 12, 8);
+	if (bBigEndian) Swap8Bytes(pabyRec + 12);
+
+	ByteCopy(&Y, pabyRec + 20, 8);
+	if (bBigEndian) Swap8Bytes(pabyRec + 20);
+
+	/* -------------------------------------------------------------------- */
+	/*      Write out record.                                               */
+	/* -------------------------------------------------------------------- */
+	if( fseek(fpSHP, nRecordOffset, 0) != 0 || fwrite(pabyRec, nRecordSize, 1, fpSHP) < 1 )
+	{
+		dump_error(__FILE__, __LINE__, "Error in fseek() or fwrite().\n");
+		return (-1);
+	}
+
+	/* -------------------------------------------------------------------- */
+	/*	Expand file wide bounds based on this shape.			*/
+	/* -------------------------------------------------------------------- */
+	if (psSHP->adBoundsMin[0] == 0.0 && psSHP->adBoundsMax[0] == 0.0 && psSHP->adBoundsMin[1] == 0.0 && psSHP->adBoundsMax[1] == 0.0)
+	{
+		psSHP->adBoundsMin[0] = psSHP->adBoundsMax[0] = X;
+		psSHP->adBoundsMin[1] = psSHP->adBoundsMax[1] = Y;
+		psSHP->adBoundsMin[2] = psSHP->adBoundsMax[2] = 0.0;
+		psSHP->adBoundsMin[3] = psSHP->adBoundsMax[3] = 0.0;
+	}
+
+	if (psSHP->adBoundsMin[0] > X)		psSHP->adBoundsMin[0] = X;
+	if (psSHP->adBoundsMin[1] > Y)		psSHP->adBoundsMin[1] = Y;
+	if (psSHP->adBoundsMin[2] > 0.0)	psSHP->adBoundsMin[2] = 0.0;
+	if (psSHP->adBoundsMin[3] > 0.0)	psSHP->adBoundsMin[3] = 0.0;
+
+	if (psSHP->adBoundsMax[0] < X)		psSHP->adBoundsMax[0] = X;
+	if (psSHP->adBoundsMax[1] < Y)		psSHP->adBoundsMax[1] = Y;
+	if (psSHP->adBoundsMax[2] < 0.0)	psSHP->adBoundsMax[2] = 0.0;
+	if (psSHP->adBoundsMax[3] < 0.0)	psSHP->adBoundsMax[3] = 0.0;
+
+
+	return(nShapeId);
+}
+
+/************************************************************************/
 /*                           SHPWriteObject()                           */
 /*                                                                      */
 /*      Write out the vertices of a new structure.  Note that it is     */
@@ -670,13 +786,13 @@
 	uchar	*pabyRec;
 	int32	i32;
 
-    psSHP->bUpdated = TRUE;
+	psSHP->bUpdated = TRUE;
 
 	/* -------------------------------------------------------------------- */
 	/*      Ensure that shape object matches the type of the file it is     */
 	/*      being written to.                                               */
 	/* -------------------------------------------------------------------- */
-    assert(psObject->nSHPType == psSHP->nShapeType || psObject->nSHPType == SHPT_NULL);
+	assert(psObject->nSHPType == psSHP->nShapeType || psObject->nSHPType == SHPT_NULL);
 
 	/* -------------------------------------------------------------------- */
 	/*      Ensure that -1 is used for appends.  Either blow an             */
@@ -700,8 +816,8 @@
 	/* -------------------------------------------------------------------- */
 	/*      Initialize record.                                              */
 	/* -------------------------------------------------------------------- */
-	pabyRec = (uchar *)malloc(psObject->nVertices * 4 * sizeof(double) + psObject->nParts * 8 + 128);
-    
+	pabyRec = (uchar *)SfMalloc(psObject->nVertices * 4 * sizeof(double) + psObject->nParts * 8 + 128);
+
 	if(psObject->nSHPType == SHPT_POLYGON || psObject->nSHPType == SHPT_POLYGONZ || psObject->nSHPType == SHPT_POLYGONM || psObject->nSHPType == SHPT_ARC || psObject->nSHPType == SHPT_ARCZ || psObject->nSHPType == SHPT_ARCM || psObject->nSHPType == SHPT_MULTIPATCH)
 	{
 		/* -------------------------------------------------------------------- */
@@ -778,7 +894,7 @@
             }
         }
 
-        /*
+		/*
          * Write the M values, if any.
          */
         if( psObject->nSHPType == SHPT_POLYGONM
@@ -795,7 +911,7 @@
 
             ByteCopy( &(psObject->dfMMax), pabyRec + nRecordSize, 8 );
             if( bBigEndian ) Swap8Bytes(pabyRec + nRecordSize );
-            nRecordSize += 8;
+			nRecordSize += 8;
 
             for( i = 0; i < psObject->nVertices; i++ )
             {
@@ -846,7 +962,7 @@
                 ByteCopy( psObject->padfZ + i, pabyRec + nRecordSize, 8 );
                 if( bBigEndian ) Swap8Bytes(pabyRec + nRecordSize );
                 nRecordSize += 8;
-            }
+			}
         }
 
         if( psObject->nSHPType == SHPT_MULTIPOINTZ
@@ -863,7 +979,7 @@
             for( i = 0; i < psObject->nVertices; i++ )
             {
                 ByteCopy( psObject->padfM + i, pabyRec + nRecordSize, 8 );
-                if( bBigEndian ) Swap8Bytes(pabyRec + nRecordSize );
+				if( bBigEndian ) Swap8Bytes(pabyRec + nRecordSize );
                 nRecordSize += 8;
             }
         }
@@ -897,7 +1013,7 @@
         }
     }
     else if( psObject->nSHPType == SHPT_NULL )
-    {
+	{
 		/* -------------------------------------------------------------------- */
 		/*      Not much to do for null geometries.                             */
 		/* -------------------------------------------------------------------- */
@@ -931,7 +1047,7 @@
 	/* -------------------------------------------------------------------- */
 	/*      Set the shape type, record number, and record size.             */
 	/* -------------------------------------------------------------------- */
-    i32 = nShapeId+1;					/* record # */
+	i32 = nShapeId+1;					/* record # */
     if( !bBigEndian ) Swap4Bytes(&i32 );
     ByteCopy( &i32, pabyRec, 4 );
 
@@ -946,7 +1062,7 @@
 	/* -------------------------------------------------------------------- */
 	/*      Write out record.                                               */
 	/* -------------------------------------------------------------------- */
-	if( fseek(psSHP->fpSHP, nRecordOffset, 0) != 0 || fwrite(pabyRec, nRecordSize, 1, psSHP->fpSHP) < 1 )
+	if( fseek(fpSHP, nRecordOffset, 0) != 0 || fwrite(pabyRec, nRecordSize, 1, fpSHP) < 1 )
     {
 		free(pabyRec);
 		dump_error(__FILE__, __LINE__, "Error in fseek() or fwrite().\n");
@@ -966,8 +1082,8 @@
         psSHP->adBoundsMin[3] = psSHP->adBoundsMax[3] = psObject->padfM[0];
     }
 
-    for (int i = 0; i < psObject->nVertices; i++ )
-    {
+	for (int i = 0; i < psObject->nVertices; i++ )
+	{
 		if (psSHP->adBoundsMin[0] > psObject->padfX[i])	psSHP->adBoundsMin[0] = psObject->padfX[i];
 		if (psSHP->adBoundsMin[1] > psObject->padfY[i])	psSHP->adBoundsMin[1] = psObject->padfY[i];
 		if (psSHP->adBoundsMin[2] > psObject->padfZ[i])	psSHP->adBoundsMin[2] = psObject->padfZ[i];
@@ -979,7 +1095,7 @@
 		if (psSHP->adBoundsMax[3] < psObject->padfM[i])	psSHP->adBoundsMax[3] = psObject->padfM[i];
 	}
 
-    return(nShapeId);
+	return(nShapeId);
 }
 
 /************************************************************************/
@@ -989,9 +1105,7 @@
 /*	for one shape.							*/
 /************************************************************************/
 
-SHPObject SHPAPI_CALL1(*)
-SHPReadObject( SHPHandle psSHP, int hEntity )
-
+SHPObject SHPAPI_CALL1(*) SHPReadObject( SHPHandle psSHP, int hEntity )
 {
     SHPObject		*psShape;
 
@@ -1013,13 +1127,13 @@
 /* -------------------------------------------------------------------- */
 /*      Read the record.                                                */
 /* -------------------------------------------------------------------- */
-    fseek( psSHP->fpSHP, psSHP->panRecOffset[hEntity], 0 );
-    fread( psSHP->pabyRec, psSHP->panRecSize[hEntity]+8, 1, psSHP->fpSHP );
+    fseek( fpSHP, psSHP->panRecOffset[hEntity], 0 );
+	fread( psSHP->pabyRec, psSHP->panRecSize[hEntity]+8, 1, fpSHP );
 
 /* -------------------------------------------------------------------- */
 /*	Allocate and minimally initialize the object.			*/
 /* -------------------------------------------------------------------- */
-    psShape = (SHPObject *) calloc(1,sizeof(SHPObject));
+    psShape = (SHPObject *) SfCalloc(1,sizeof(SHPObject));
     psShape->nShapeId = hEntity;
 
     memcpy( &psShape->nSHPType, psSHP->pabyRec + 8, 4 );
@@ -1062,17 +1176,16 @@
 	if( bBigEndian ) Swap4Bytes(&nParts );
 
 	psShape->nVertices = nPoints;
-        psShape->padfX = (double *) calloc(nPoints,sizeof(double));
-        psShape->padfY = (double *) calloc(nPoints,sizeof(double));
-        psShape->padfZ = (double *) calloc(nPoints,sizeof(double));
-        psShape->padfM = (double *) calloc(nPoints,sizeof(double));
+	psShape->padfX = (double *) SfCalloc(nPoints,sizeof(double));
+	psShape->padfY = (double *) SfCalloc(nPoints,sizeof(double));
+	psShape->padfZ = (double *) SfCalloc(nPoints,sizeof(double));
+	psShape->padfM = (double *) SfCalloc(nPoints,sizeof(double));
 
 	psShape->nParts = nParts;
-        psShape->panPartStart = (int *) calloc(nParts,sizeof(int));
-        psShape->panPartType = (int *) calloc(nParts,sizeof(int));
+	psShape->panPartStart = (int *) SfCalloc(nParts,sizeof(int));
+	psShape->panPartType = (int *) SfCalloc(nParts,sizeof(int));
 
-        for( i = 0; i < nParts; i++ )
-            psShape->panPartType[i] = SHPP_RING;
+    for( i = 0; i < nParts; i++ ) psShape->panPartType[i] = SHPP_RING;
 
 /* -------------------------------------------------------------------- */
 /*      Copy out the part array from the record.                        */
@@ -1179,21 +1292,21 @@
 	if( bBigEndian ) Swap4Bytes(&nPoints );
 
 	psShape->nVertices = nPoints;
-        psShape->padfX = (double *) calloc(nPoints,sizeof(double));
-        psShape->padfY = (double *) calloc(nPoints,sizeof(double));
-        psShape->padfZ = (double *) calloc(nPoints,sizeof(double));
-        psShape->padfM = (double *) calloc(nPoints,sizeof(double));
+	psShape->padfX = (double *) SfCalloc(nPoints,sizeof(double));
+	psShape->padfY = (double *) SfCalloc(nPoints,sizeof(double));
+	psShape->padfZ = (double *) SfCalloc(nPoints,sizeof(double));
+	psShape->padfM = (double *) SfCalloc(nPoints,sizeof(double));
 
 	for( i = 0; i < nPoints; i++ )
 	{
-	    memcpy(psShape->padfX+i, psSHP->pabyRec + 48 + 16 * i, 8 );
-	    memcpy(psShape->padfY+i, psSHP->pabyRec + 48 + 16 * i + 8, 8 );
+		memcpy(psShape->padfX+i, psSHP->pabyRec + 48 + 16 * i, 8 );
+		memcpy(psShape->padfY+i, psSHP->pabyRec + 48 + 16 * i + 8, 8 );
 
-	    if( bBigEndian ) Swap8Bytes(psShape->padfX + i );
-	    if( bBigEndian ) Swap8Bytes(psShape->padfY + i );
+		if( bBigEndian ) Swap8Bytes(psShape->padfX + i );
+		if( bBigEndian ) Swap8Bytes(psShape->padfY + i );
 	}
 
-        nOffset = 48 + 16*nPoints;
+    nOffset = 48 + 16*nPoints;
         
 /* -------------------------------------------------------------------- */
 /*	Get the X/Y bounds.						*/
@@ -1262,10 +1375,10 @@
         int	nOffset;
         
 	psShape->nVertices = 1;
-        psShape->padfX = (double *) calloc(1,sizeof(double));
-        psShape->padfY = (double *) calloc(1,sizeof(double));
-        psShape->padfZ = (double *) calloc(1,sizeof(double));
-        psShape->padfM = (double *) calloc(1,sizeof(double));
+	psShape->padfX = (double *) SfCalloc(1,sizeof(double));
+	psShape->padfY = (double *) SfCalloc(1,sizeof(double));
+	psShape->padfZ = (double *) SfCalloc(1,sizeof(double));
+	psShape->padfM = (double *) SfCalloc(1,sizeof(double));
 
 	memcpy( psShape->padfX, psSHP->pabyRec + 12, 8 );
 	memcpy( psShape->padfY, psSHP->pabyRec + 20, 8 );
@@ -1273,7 +1386,7 @@
 	if( bBigEndian ) Swap8Bytes(psShape->padfX );
 	if( bBigEndian ) Swap8Bytes(psShape->padfY );
 
-        nOffset = 20 + 8;
+    nOffset = 20 + 8;
         
 /* -------------------------------------------------------------------- */
 /*      If we have a Z coordinate, collect that now.                    */
@@ -1716,8 +1829,8 @@
 /*	Compute the base (layer) name.  If there is any extension	*/
 /*	on the passed in filename we will strip it off.			*/
 /* -------------------------------------------------------------------- */
-    pszBasename = (char *) malloc(strlen(pszFilename)+5);
-    strcpy( pszBasename, pszFilename );
+	pszBasename = (char *) SfMalloc(strlen(pszFilename)+5);
+	strcpy( pszBasename, pszFilename );
     for( i = strlen(pszBasename)-1; 
 	 i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
 	       && pszBasename[i] != '\\';
@@ -1726,13 +1839,13 @@
     if( pszBasename[i] == '.' )
         pszBasename[i] = '\0';
 
-    pszFullname = (char *) malloc(strlen(pszBasename) + 5);
-    sprintf( pszFullname, "%s.dbf", pszBasename );
-        
-    psDBF = (DBFHandle) calloc( 1, sizeof(DBFInfo) );
-    psDBF->fp = fopen( pszFullname, pszAccess );
+	pszFullname = (char *) SfMalloc(strlen(pszBasename) + 5);
+	sprintf( pszFullname, "%s.dbf", pszBasename );
 
-    if( psDBF->fp == NULL )
+	psDBF = (DBFHandle) SfCalloc( 1, sizeof(DBFInfo) );
+	psDBF->fp = fopen( pszFullname, pszAccess );
+
+	if( psDBF->fp == NULL )
 	{
         sprintf( pszFullname, "%s.DBF", pszBasename );
         psDBF->fp = fopen(pszFullname, pszAccess );
@@ -1741,7 +1854,7 @@
     free( pszBasename );
     free( pszFullname );
     
-    if( psDBF->fp == NULL )
+	if( psDBF->fp == NULL )
     {
         free( psDBF );
         return( NULL );
@@ -1754,10 +1867,10 @@
 /* -------------------------------------------------------------------- */
 /*  Read Table Header info                                              */
 /* -------------------------------------------------------------------- */
-    pabyBuf = (unsigned char *) malloc(500);
-    if( fread( pabyBuf, 32, 1, psDBF->fp ) != 1 )
+	pabyBuf = (unsigned char *) SfMalloc(500);
+	if( fread( pabyBuf, 32, 1, psDBF->fp ) != 1 )
     {
-        fclose( psDBF->fp );
+		fclose( psDBF->fp );
         free( pabyBuf );
         free( psDBF );
         return NULL;
@@ -1771,7 +1884,7 @@
     
     psDBF->nFields = nFields = (nHeadLen - 32) / 32;
 
-    psDBF->pszCurrentRecord = (char *) malloc(nRecLen);
+	psDBF->pszCurrentRecord = (char *) SfMalloc(nRecLen);
 
 /* -------------------------------------------------------------------- */
 /*  Read in Field Definitions                                           */
@@ -1783,57 +1896,54 @@
     fseek( psDBF->fp, 32, 0 );
     if( fread( pabyBuf, nHeadLen-32, 1, psDBF->fp ) != 1 )
     {
-        fclose( psDBF->fp );
+		fclose( psDBF->fp );
         free( pabyBuf );
         free( psDBF );
         return NULL;
     }
 
-    psDBF->panFieldOffset = (int *) malloc(sizeof(int) * nFields);
-    psDBF->panFieldSize = (int *) malloc(sizeof(int) * nFields);
-    psDBF->panFieldDecimals = (int *) malloc(sizeof(int) * nFields);
-    psDBF->pachFieldType = (char *) malloc(sizeof(char) * nFields);
+	psDBF->panFieldOffset = (int *) SfMalloc(sizeof(int) * nFields);
+	psDBF->panFieldSize = (int *) SfMalloc(sizeof(int) * nFields);
+	psDBF->panFieldDecimals = (int *) SfMalloc(sizeof(int) * nFields);
+	psDBF->pachFieldType = (char *) SfMalloc(sizeof(char) * nFields);
 
-    for( iField = 0; iField < nFields; iField++ )
-    {
-	unsigned char		*pabyFInfo;
+	for( iField = 0; iField < nFields; iField++ )
+	{
+		unsigned char		*pabyFInfo;
 
-	pabyFInfo = pabyBuf+iField*32;
+		pabyFInfo = pabyBuf+iField*32;
 
-	if( pabyFInfo[11] == 'N' || pabyFInfo[11] == 'F' )
-	{
-	    psDBF->panFieldSize[iField] = pabyFInfo[16];
-	    psDBF->panFieldDecimals[iField] = pabyFInfo[17];
+		if( pabyFInfo[11] == 'N' || pabyFInfo[11] == 'F' )
+		{
+			psDBF->panFieldSize[iField] = pabyFInfo[16];
+			psDBF->panFieldDecimals[iField] = pabyFInfo[17];
+		}
+		else
+		{
+			psDBF->panFieldSize[iField] = pabyFInfo[16] + pabyFInfo[17]*256;
+			psDBF->panFieldDecimals[iField] = 0;
+		}
+
+		psDBF->pachFieldType[iField] = (char) pabyFInfo[11];
+
+		if( iField == 0 )	psDBF->panFieldOffset[iField] = 1;
+		else             	psDBF->panFieldOffset[iField] = psDBF->panFieldOffset[iField-1] + psDBF->panFieldSize[iField-1];
 	}
-	else
-	{
-	    psDBF->panFieldSize[iField] = pabyFInfo[16] + pabyFInfo[17]*256;
-	    psDBF->panFieldDecimals[iField] = 0;
-	}
 
-	psDBF->pachFieldType[iField] = (char) pabyFInfo[11];
-	if( iField == 0 )
-	    psDBF->panFieldOffset[iField] = 1;
-	else
-	    psDBF->panFieldOffset[iField] = 
-	      psDBF->panFieldOffset[iField-1] + psDBF->panFieldSize[iField-1];
-    }
-
-    return( psDBF );
+	return( psDBF );
 }
 
 /************************************************************************/
 /*                              DBFClose()                              */
 /************************************************************************/
 
-void SHPAPI_CALL
-DBFClose(DBFHandle psDBF)
+void SHPAPI_CALL DBFClose(DBFHandle psDBF)
 {
 /* -------------------------------------------------------------------- */
 /*      Write out header if not already written.                        */
 /* -------------------------------------------------------------------- */
 	if( psDBF->bNoHeader )
-        DBFWriteHeader( psDBF );
+		DBFWriteHeader( psDBF );
 
     DBFFlushRecord( psDBF );
 
@@ -1868,7 +1978,7 @@
 
     if( psDBF->panFieldOffset != NULL )
     {
-        free( psDBF->panFieldOffset );
+		free( psDBF->panFieldOffset );
         free( psDBF->panFieldSize );
         free( psDBF->panFieldDecimals );
         free( psDBF->pachFieldType );
@@ -1880,7 +1990,7 @@
     free( psDBF );
 
     if( pszStringField != NULL )
-    {
+	{
         free( pszStringField );
         pszStringField = NULL;
         nStringFieldLen = 0;
@@ -1893,37 +2003,30 @@
 /*      Create a new .dbf file.                                         */
 /************************************************************************/
 
-DBFHandle SHPAPI_CALL
-DBFCreate( const char * pszFilename )
-
+DBFHandle SHPAPI_CALL DBFCreate( const char * pszFilename )
 {
-    DBFHandle	psDBF;
-	FILE	*fp;
-    char	*pszFullname, *pszBasename;
-    int		i;
+	DBFHandle	psDBF;
+	char	*pszFullname, *pszBasename;
+	int		i;
 
 /* -------------------------------------------------------------------- */
 /*	Compute the base (layer) name.  If there is any extension	*/
 /*	on the passed in filename we will strip it off.			*/
 /* -------------------------------------------------------------------- */
-    pszBasename = (char *) malloc(strlen(pszFilename)+5);
-    strcpy( pszBasename, pszFilename );
-    for( i = strlen(pszBasename)-1; 
-	 i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/'
-	       && pszBasename[i] != '\\';
-	 i-- ) {}
+	pszBasename = (char *) SfMalloc(strlen(pszFilename)+5);
+	strcpy( pszBasename, pszFilename );
+	for( i = strlen(pszBasename)-1; i > 0 && pszBasename[i] != '.' && pszBasename[i] != '/' && pszBasename[i] != '\\'; i-- ) {}
 
-    if( pszBasename[i] == '.' )
-        pszBasename[i] = '\0';
+	if( pszBasename[i] == '.' ) pszBasename[i] = '\0';
 
-    pszFullname = (char *) malloc(strlen(pszBasename) + 5);
-    sprintf( pszFullname, "%s.dbf", pszBasename );
-    free( pszBasename );
+	pszFullname = (char *) SfMalloc(strlen(pszBasename) + 5);
+	sprintf( pszFullname, "%s.dbf", pszBasename );
+	free( pszBasename );
 
 /* -------------------------------------------------------------------- */
 /*      Create the file.                                                */
 /* -------------------------------------------------------------------- */
-	fp = fopen (pszFullname, "wb");
+	FILE* fp = fopen (pszFullname, "wb");
 	if (fp == NULL) return(NULL);
 
 	fputc(0, fp);
@@ -1937,9 +2040,9 @@
 /* -------------------------------------------------------------------- */
 /*	Create the info structure.					*/
 /* -------------------------------------------------------------------- */
-    psDBF = (DBFHandle) malloc(sizeof(DBFInfo));
+	psDBF = (DBFHandle) SfMalloc(sizeof(DBFInfo));
 
-    psDBF->fp = fp;
+	psDBF->fp = fp;
     psDBF->nRecords = 0;
     psDBF->nFields = 0;
     psDBF->nRecordLength = 1;
@@ -2019,7 +2122,7 @@
 	for( i = 0; i < 32; i++ )  pszFInfo[i] = '\0';
 
 	if( (int) strlen(pszFieldName) < 10 )          strncpy( pszFInfo, pszFieldName, strlen(pszFieldName));
-    else                                           strncpy( pszFInfo, pszFieldName, 10);
+	else                                           strncpy( pszFInfo, pszFieldName, 10);
 
     pszFInfo[11] = psDBF->pachFieldType[psDBF->nFields-1];
 
@@ -2207,7 +2310,7 @@
 
     pszValue = DBFReadStringAttribute( psDBF, iRecord, iField );
 
-    switch(psDBF->pachFieldType[iField])
+	switch(psDBF->pachFieldType[iField])
     {
       case 'N':
       case 'F':
@@ -2368,26 +2471,22 @@
           case 'N':
           case 'F':
 	    /* NULL numeric fields have value "****************" */
-            memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '*', 
-                    psDBF->panFieldSize[iField] );
-            break;
+			memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '*', psDBF->panFieldSize[iField] );
+			break;
 
-          case 'D':
+		  case 'D':
 		/* NULL date fields have value "00000000" */
-            memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '0', 
-					psDBF->panFieldSize[iField] );
+			memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '0', psDBF->panFieldSize[iField] );
             break;
 
           case 'L':
 	    /* NULL boolean fields have value "?" */ 
-            memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '?', 
-                    psDBF->panFieldSize[iField] );
+			memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '?', psDBF->panFieldSize[iField] );
             break;
 
           default:
             /* empty string fields are considered NULL */
-            memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '\0', 
-                    psDBF->panFieldSize[iField] );
+			memset( (char *) (pabyRec+psDBF->panFieldOffset[iField]), '\0', psDBF->panFieldSize[iField] );
             break;
         }
         return TRUE;
@@ -2407,7 +2506,7 @@
 
 				if( (int)sizeof(szSField)-2 < nWidth )	nWidth = sizeof(szSField)-2;
 
-				sprintf( szFormat, "%%%dd", nWidth );
+				sprintf(szFormat, "%%%dd", nWidth );
 				sprintf(szSField, szFormat, (int) *((double *) pValue) );
 				if( (int)strlen(szSField) > psDBF->panFieldSize[iField] )
 				{
@@ -2415,8 +2514,7 @@
 					nRetResult = FALSE;
 				}
 
-				strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]),
-				szSField, strlen(szSField) );
+				strncpy((char *) (pabyRec+psDBF->panFieldOffset[iField]), szSField, strlen(szSField) );
 			}
 			else
 			{
@@ -2465,11 +2563,9 @@
 /*      as is to the field position in the record.                      */
 /************************************************************************/
 
-int DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
-                              void * pValue )
-
+int DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField, void * pValue )
 {
-    int	       	nRecordOffset, i, j;
+	int	       	nRecordOffset, i, j;
     unsigned char	*pabyRec;
 
 /* -------------------------------------------------------------------- */
@@ -2516,8 +2612,10 @@
 /* -------------------------------------------------------------------- */
 /*      Assign all the record fields.                                   */
 /* -------------------------------------------------------------------- */
-    if( (int)strlen((char *) pValue) > psDBF->panFieldSize[iField] )
-        j = psDBF->panFieldSize[iField];
+	if( (int)strlen((char *) pValue) > psDBF->panFieldSize[iField] )
+	{
+		j = psDBF->panFieldSize[iField];
+	}
     else
     {
         memset( pabyRec+psDBF->panFieldOffset[iField], ' ',
@@ -2540,12 +2638,9 @@
 /*      Write a double attribute.                                       */
 /************************************************************************/
 
-int SHPAPI_CALL
-DBFWriteDoubleAttribute( DBFHandle psDBF, int iRecord, int iField,
-                         double dValue )
-
+int SHPAPI_CALL DBFWriteDoubleAttribute( DBFHandle psDBF, int iRecord, int iField, double dValue )
 {
-    return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );
+	return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );
 }
 
 /************************************************************************/
@@ -2554,12 +2649,9 @@
 /*      Write a integer attribute.                                      */
 /************************************************************************/
 
-int SHPAPI_CALL
-DBFWriteIntegerAttribute( DBFHandle psDBF, int iRecord, int iField,
-                          int nValue )
-
+int SHPAPI_CALL DBFWriteIntegerAttribute( DBFHandle psDBF, int iRecord, int iField, int nValue )
 {
-    double	dValue = nValue;
+	double	dValue = nValue;
 
     return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) &dValue ) );
 }
@@ -2581,9 +2673,7 @@
 /*      Write a string attribute.                                       */
 /************************************************************************/
 
-int SHPAPI_CALL
-DBFWriteNULLAttribute( DBFHandle psDBF, int iRecord, int iField )
-
+int SHPAPI_CALL DBFWriteNULLAttribute( DBFHandle psDBF, int iRecord, int iField )
 {
     return( DBFWriteAttribute( psDBF, iRecord, iField, NULL ) );
 }
@@ -2594,10 +2684,7 @@
 /*      Write a logical attribute.                                      */
 /************************************************************************/
 
-int SHPAPI_CALL
-DBFWriteLogicalAttribute( DBFHandle psDBF, int iRecord, int iField,
-		       const char lValue)
-
+int SHPAPI_CALL DBFWriteLogicalAttribute( DBFHandle psDBF, int iRecord, int iField, const char lValue)
 {
     return( DBFWriteAttribute( psDBF, iRecord, iField, (void *) (&lValue) ) );
 }
@@ -2608,9 +2695,7 @@
 /*	Write an attribute record to the file.				*/
 /************************************************************************/
 
-int SHPAPI_CALL
-DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple )
-
+int SHPAPI_CALL DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple )
 {
     int	       	nRecordOffset, i;
     unsigned char	*pabyRec;
@@ -2621,21 +2706,20 @@
     if( hEntity < 0 || hEntity > psDBF->nRecords )
         return( FALSE );
 
-    if( psDBF->bNoHeader )
-        DBFWriteHeader(psDBF);
+	if( psDBF->bNoHeader ) DBFWriteHeader(psDBF);
 
 /* -------------------------------------------------------------------- */
 /*      Is this a brand new record?                                     */
 /* -------------------------------------------------------------------- */
 	if( hEntity == psDBF->nRecords )
     {
-	DBFFlushRecord( psDBF );
+		DBFFlushRecord( psDBF );
 
-	psDBF->nRecords++;
-	for( i = 0; i < psDBF->nRecordLength; i++ )
-	    psDBF->pszCurrentRecord[i] = ' ';
+		psDBF->nRecords++;
+		for( i = 0; i < psDBF->nRecordLength; i++ )
+			psDBF->pszCurrentRecord[i] = ' ';
 
-	psDBF->nCurrentRecord = hEntity;
+		psDBF->nCurrentRecord = hEntity;
     }
 
 /* -------------------------------------------------------------------- */
@@ -2644,14 +2728,14 @@
 /* -------------------------------------------------------------------- */
     if( psDBF->nCurrentRecord != hEntity )
     {
-	DBFFlushRecord( psDBF );
+		DBFFlushRecord( psDBF );
 
-	nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;
+		nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;
 
-	fseek( psDBF->fp, nRecordOffset, 0 );
-	fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
+		fseek( psDBF->fp, nRecordOffset, 0 );
+		fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
 
-	psDBF->nCurrentRecord = hEntity;
+		psDBF->nCurrentRecord = hEntity;
     }
 
     pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
@@ -2670,9 +2754,7 @@
 /*      Read one of the attribute fields of a record.                   */
 /************************************************************************/
 
-const char SHPAPI_CALL1(*)
-DBFReadTuple(DBFHandle psDBF, int hEntity )
-
+const char SHPAPI_CALL1(*) DBFReadTuple(DBFHandle psDBF, int hEntity )
 {
     int	       	nRecordOffset;
     unsigned char	*pabyRec;
@@ -2688,21 +2770,22 @@
 
     if( psDBF->nCurrentRecord != hEntity )
     {
-	DBFFlushRecord( psDBF );
+		DBFFlushRecord( psDBF );
 
-	nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;
+		nRecordOffset = psDBF->nRecordLength * hEntity + psDBF->nHeaderLength;
 
-	fseek( psDBF->fp, nRecordOffset, 0 );
-	fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
+		fseek( psDBF->fp, nRecordOffset, 0 );
+		fread( psDBF->pszCurrentRecord, psDBF->nRecordLength, 1, psDBF->fp );
 
-	psDBF->nCurrentRecord = hEntity;
+		psDBF->nCurrentRecord = hEntity;
     }
 
     pabyRec = (unsigned char *) psDBF->pszCurrentRecord;
 
-    if ( nTupleLen < psDBF->nRecordLength) {
-      nTupleLen = psDBF->nRecordLength;
-      pReturnTuple = (char *) SfRealloc(pReturnTuple, psDBF->nRecordLength);
+	if ( nTupleLen < psDBF->nRecordLength)
+	{
+		nTupleLen = psDBF->nRecordLength;
+		pReturnTuple = (char *) SfRealloc(pReturnTuple, psDBF->nRecordLength);
     }
     
     memcpy ( pReturnTuple, pabyRec, psDBF->nRecordLength );
@@ -2716,39 +2799,41 @@
 /*      Read one of the attribute fields of a record.                   */
 /************************************************************************/
 
-DBFHandle SHPAPI_CALL
-DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename ) 
+DBFHandle SHPAPI_CALL DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename )
 {
-    DBFHandle	newDBF;
+	DBFHandle	newDBF;
 
-   newDBF = DBFCreate ( pszFilename );
-   if ( newDBF == NULL ) return ( NULL ); 
-   
-   newDBF->pszHeader = (char *) malloc ( 32 * psDBF->nFields );
-   memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );
+	newDBF = DBFCreate ( pszFilename );
+	if ( newDBF == NULL ) return ( NULL );
 
-   newDBF->nFields = psDBF->nFields;
-   newDBF->nRecordLength = psDBF->nRecordLength;
-   newDBF->nHeaderLength = 32 * (psDBF->nFields+1);
-    
-   newDBF->panFieldOffset = (int *) malloc ( sizeof(int) * psDBF->nFields ); 
-   memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
-   newDBF->panFieldSize = (int *) malloc ( sizeof(int) * psDBF->nFields );
-   memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
-   newDBF->panFieldDecimals = (int *) malloc ( sizeof(int) * psDBF->nFields );
-   memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
-   newDBF->pachFieldType = (char *) malloc ( sizeof(int) * psDBF->nFields );
-   memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(int) * psDBF->nFields );
+	newDBF->pszHeader = (char *) SfMalloc ( 32 * psDBF->nFields );
+	memcpy ( newDBF->pszHeader, psDBF->pszHeader, 32 * psDBF->nFields );
 
-   newDBF->bNoHeader = TRUE;
-   newDBF->bUpdated = TRUE;
-   
-   DBFWriteHeader ( newDBF );
-   DBFClose ( newDBF );
-   
-   newDBF = DBFOpen ( pszFilename, "rb+" );
+	newDBF->nFields = psDBF->nFields;
+	newDBF->nRecordLength = psDBF->nRecordLength;
+	newDBF->nHeaderLength = 32 * (psDBF->nFields+1);
 
-   return ( newDBF );
+	newDBF->panFieldOffset = (int *) SfMalloc ( sizeof(int) * psDBF->nFields );
+	memcpy ( newDBF->panFieldOffset, psDBF->panFieldOffset, sizeof(int) * psDBF->nFields );
+
+	newDBF->panFieldSize = (int *) SfMalloc ( sizeof(int) * psDBF->nFields );
+	memcpy ( newDBF->panFieldSize, psDBF->panFieldSize, sizeof(int) * psDBF->nFields );
+
+	newDBF->panFieldDecimals = (int *) SfMalloc ( sizeof(int) * psDBF->nFields );
+	memcpy ( newDBF->panFieldDecimals, psDBF->panFieldDecimals, sizeof(int) * psDBF->nFields );
+
+	newDBF->pachFieldType = (char *) SfMalloc ( sizeof(int) * psDBF->nFields );
+	memcpy ( newDBF->pachFieldType, psDBF->pachFieldType, sizeof(int) * psDBF->nFields );
+
+	newDBF->bNoHeader = TRUE;
+	newDBF->bUpdated = TRUE;
+
+	DBFWriteHeader ( newDBF );
+	DBFClose ( newDBF );
+
+	newDBF = DBFOpen ( pszFilename, "rb+" );
+
+	return ( newDBF );
 }
 
 /************************************************************************/
@@ -2762,14 +2847,10 @@
 /*                           'M' (Memo: 10 digits .DBT block ptr)       */
 /************************************************************************/
 
-char SHPAPI_CALL
-DBFGetNativeFieldType( DBFHandle psDBF, int iField )
-
+char SHPAPI_CALL DBFGetNativeFieldType( DBFHandle psDBF, int iField )
 {
-    if( iField >=0 && iField < psDBF->nFields )
-		return psDBF->pachFieldType[iField];
-
-    return  ' ';
+	if( iField >=0 && iField < psDBF->nFields )	return psDBF->pachFieldType[iField];
+	return  ' ';
 }
 
 /************************************************************************/

Modified: trunk/src/shape.h
===================================================================
--- trunk/src/shape.h	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/shape.h	2006-09-25 05:44:29 UTC (rev 63)
@@ -80,25 +80,22 @@
 /************************************************************************/
 typedef	struct
 {
-	FILE    *fpSHP;
-	FILE	*fpSHX;
+	int				nShapeType;				/* SHPT_* */
 
-	int		nShapeType;				/* SHPT_* */
+	int				nFileSize;				/* SHP file */
 
-    int		nFileSize;				/* SHP file */
+	int     		nRecords;
+	int				nMaxRecords;
+	int				*panRecOffset;
+	int				*panRecSize;
 
-    int         nRecords;
-    int		nMaxRecords;
-    int		*panRecOffset;
-    int		*panRecSize;
+	double			adBoundsMin[4];
+	double			adBoundsMax[4];
 
-    double	adBoundsMin[4];
-    double	adBoundsMax[4];
+	int				bUpdated;
 
-    int		bUpdated;
-
-    unsigned char *pabyRec;
-    int         nBufSize;
+	unsigned char 	*pabyRec;
+	int         	nBufSize;
 } SHPInfo;
 
 typedef SHPInfo * SHPHandle;
@@ -179,23 +176,21 @@
 void SHPAPI_CALL 			SHPGetInfo( SHPHandle hSHP, int * pnEntities, TShpType *pnShapeType, double * padfMinBound, double * padfMaxBound );
 
 SHPObject SHPAPI_CALL1(*)	SHPReadObject( SHPHandle hSHP, int iShape );
+void SHPAPI_CALL 			SHPSetMaxRecords(SHPHandle psSHP, int nNewMaxRecords);
 int SHPAPI_CALL             SHPWriteObject( SHPHandle hSHP, int iShape, SHPObject * psObject );
+int SHPAPI_CALL 			SHPWritePoint(SHPHandle psSHP, double X, double Y);
 
 void SHPAPI_CALL            SHPDestroyObject( SHPObject * psObject );
 void SHPAPI_CALL            SHPComputeExtents( SHPObject * psObject );
 SHPObject SHPAPI_CALL1(*)   SHPCreateObject( int nSHPType, int nShapeId, int nParts, int * panPartStart, int * panPartType, int nVertices, double * padfX, double * padfY, double * padfZ, double * padfM );
 SHPObject SHPAPI_CALL1(*)   SHPCreateSimpleObject( int nSHPType, int nVertices, double * padfX, double * padfY, double * padfZ );
 
-int SHPAPI_CALL
-      SHPRewindObject( SHPHandle hSHP, SHPObject * psObject );
+int SHPAPI_CALL            	SHPRewindObject( SHPHandle hSHP, SHPObject * psObject );
 
-void SHPAPI_CALL
-      SHPClose( SHPHandle hSHP );
+void SHPAPI_CALL            SHPClose( SHPHandle hSHP );
 
-const char SHPAPI_CALL1(*)
-      SHPTypeName( int nSHPType );
-const char SHPAPI_CALL1(*)
-      SHPPartTypeName( int nPartType );
+const char SHPAPI_CALL1(*)  SHPTypeName( int nSHPType );
+const char SHPAPI_CALL1(*)  SHPPartTypeName( int nPartType );
 
 /* -------------------------------------------------------------------- */
 /*      Shape quadtree indexing API.                                    */
@@ -231,34 +226,20 @@
     SHPTreeNode	*psRoot;
 } SHPTree;
 
-SHPTree SHPAPI_CALL1(*)
-      SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth,
-                     double *padfBoundsMin, double *padfBoundsMax );
-void    SHPAPI_CALL
-      SHPDestroyTree( SHPTree * hTree );
+SHPTree SHPAPI_CALL1(*) SHPCreateTree( SHPHandle hSHP, int nDimension, int nMaxDepth, double *padfBoundsMin, double *padfBoundsMax );
+void    SHPAPI_CALL     SHPDestroyTree( SHPTree * hTree );
 
-int	SHPAPI_CALL
-      SHPWriteTree( SHPTree *hTree, const char * pszFilename );
-SHPTree SHPAPI_CALL
-      SHPReadTree( const char * pszFilename );
+int	SHPAPI_CALL         SHPWriteTree( SHPTree *hTree, const char * pszFilename );
+SHPTree SHPAPI_CALL     SHPReadTree( const char * pszFilename );
 
-int	SHPAPI_CALL
-      SHPTreeAddObject( SHPTree * hTree, SHPObject * psObject );
-int	SHPAPI_CALL
-      SHPTreeAddShapeId( SHPTree * hTree, SHPObject * psObject );
-int	SHPAPI_CALL
-      SHPTreeRemoveShapeId( SHPTree * hTree, int nShapeId );
+int	SHPAPI_CALL         SHPTreeAddObject( SHPTree * hTree, SHPObject * psObject );
+int	SHPAPI_CALL         SHPTreeAddShapeId( SHPTree * hTree, SHPObject * psObject );
+int	SHPAPI_CALL         SHPTreeRemoveShapeId( SHPTree * hTree, int nShapeId );
 
-void 	SHPAPI_CALL
-      SHPTreeTrimExtraNodes( SHPTree * hTree );
+void 	SHPAPI_CALL     SHPTreeTrimExtraNodes( SHPTree * hTree );
 
-int    SHPAPI_CALL1(*)
-      SHPTreeFindLikelyShapes( SHPTree * hTree,
-                               double * padfBoundsMin,
-                               double * padfBoundsMax,
-                               int * );
-int     SHPAPI_CALL
-      SHPCheckBoundsOverlap( double *, double *, double *, double *, int );
+int    SHPAPI_CALL1(*)  SHPTreeFindLikelyShapes( SHPTree * hTree, double * padfBoundsMin, double * padfBoundsMax, int * );
+int     SHPAPI_CALL 	SHPCheckBoundsOverlap( double *, double *, double *, double *, int );
 
 /************************************************************************/
 /*                             DBF Support.                             */
@@ -299,68 +280,38 @@
 
 #define XBASE_FLDHDR_SZ       32
 
-DBFHandle SHPAPI_CALL
-      DBFOpen( const char * pszDBFFile, const char * pszAccess );
-DBFHandle SHPAPI_CALL
-      DBFCreate( const char * pszDBFFile );
+DBFHandle SHPAPI_CALL		DBFOpen( const char * pszDBFFile, const char * pszAccess );
+DBFHandle SHPAPI_CALL   	DBFCreate( const char * pszDBFFile );
 
-int	SHPAPI_CALL
-      DBFGetFieldCount( DBFHandle psDBF );
-int	SHPAPI_CALL
-      DBFGetRecordCount( DBFHandle psDBF );
-int	SHPAPI_CALL
-      DBFAddField( DBFHandle hDBF, const char * pszFieldName,
-                   DBFFieldType eType, int nWidth, int nDecimals );
+int	SHPAPI_CALL         	DBFGetFieldCount( DBFHandle psDBF );
+int	SHPAPI_CALL         	DBFGetRecordCount( DBFHandle psDBF );
+int	SHPAPI_CALL         	DBFAddField( DBFHandle hDBF, const char * pszFieldName, DBFFieldType eType, int nWidth, int nDecimals );
 
-DBFFieldType SHPAPI_CALL
-      DBFGetFieldInfo( DBFHandle psDBF, int iField, 
-                       char * pszFieldName, int * pnWidth, int * pnDecimals );
+DBFFieldType SHPAPI_CALL    DBFGetFieldInfo( DBFHandle psDBF, int iField, char * pszFieldName, int * pnWidth, int * pnDecimals );
 
-int SHPAPI_CALL
-      DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
+int SHPAPI_CALL      		DBFGetFieldIndex(DBFHandle psDBF, const char *pszFieldName);
 
-int 	SHPAPI_CALL
-      DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
-double 	SHPAPI_CALL
-      DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
-const char SHPAPI_CALL1(*)
-      DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
-const char SHPAPI_CALL1(*)
-      DBFReadLogicalAttribute( DBFHandle hDBF, int iShape, int iField );
-int     SHPAPI_CALL
-      DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
+int 	SHPAPI_CALL         DBFReadIntegerAttribute( DBFHandle hDBF, int iShape, int iField );
+double 	SHPAPI_CALL         DBFReadDoubleAttribute( DBFHandle hDBF, int iShape, int iField );
+const char SHPAPI_CALL1(*)  DBFReadStringAttribute( DBFHandle hDBF, int iShape, int iField );
+const char SHPAPI_CALL1(*)  DBFReadLogicalAttribute( DBFHandle hDBF, int iShape, int iField );
+int     SHPAPI_CALL         DBFIsAttributeNULL( DBFHandle hDBF, int iShape, int iField );
 
-int SHPAPI_CALL
-      DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField, 
-                                int nFieldValue );
-int SHPAPI_CALL
-      DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField,
-                               double dFieldValue );
-int SHPAPI_CALL
-      DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField,
-                               const char * pszFieldValue );
-int SHPAPI_CALL
-     DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
+int SHPAPI_CALL             DBFWriteIntegerAttribute( DBFHandle hDBF, int iShape, int iField, int nFieldValue );
+int SHPAPI_CALL	  			DBFWriteDoubleAttribute( DBFHandle hDBF, int iShape, int iField, double dFieldValue );
+int SHPAPI_CALL      		DBFWriteStringAttribute( DBFHandle hDBF, int iShape, int iField, const char * pszFieldValue );
+int SHPAPI_CALL	 			DBFWriteNULLAttribute( DBFHandle hDBF, int iShape, int iField );
 
-int SHPAPI_CALL
-     DBFWriteLogicalAttribute( DBFHandle hDBF, int iShape, int iField,
-			       const char lFieldValue);
-int SHPAPI_CALL
-     DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField,
-                               void * pValue );
-const char SHPAPI_CALL1(*)
-      DBFReadTuple(DBFHandle psDBF, int hEntity );
-int SHPAPI_CALL
-      DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple );
+int SHPAPI_CALL             DBFWriteLogicalAttribute( DBFHandle hDBF, int iShape, int iField, const char lFieldValue);
+int SHPAPI_CALL				DBFWriteAttributeDirectly(DBFHandle psDBF, int hEntity, int iField, void * pValue );
+const char SHPAPI_CALL1(*)  DBFReadTuple(DBFHandle psDBF, int hEntity );
+int SHPAPI_CALL             DBFWriteTuple(DBFHandle psDBF, int hEntity, void * pRawTuple );
 
-DBFHandle SHPAPI_CALL
-      DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename );
- 
-void	SHPAPI_CALL
-      DBFClose( DBFHandle hDBF );
-char    SHPAPI_CALL
-      DBFGetNativeFieldType( DBFHandle hDBF, int iField );
+DBFHandle SHPAPI_CALL       DBFCloneEmpty(DBFHandle psDBF, const char * pszFilename );
 
+void	SHPAPI_CALL         DBFClose( DBFHandle hDBF );
+char    SHPAPI_CALL        	DBFGetNativeFieldType( DBFHandle hDBF, int iField );
+
 void	TestShapeFileLib(void);
 
 #ifdef __cplusplus

Modified: trunk/src/tools.cpp
===================================================================
--- trunk/src/tools.cpp	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/tools.cpp	2006-09-25 05:44:29 UTC (rev 63)
@@ -15,6 +15,8 @@
 #endif
 
 //---------------------------------------------------------------------------
+#include <stdexcept>
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -67,7 +69,7 @@
 
 	ReturnCode = 2;
 
-	throw TFehler("Problem");
+	throw std::runtime_error("Problem");
 }
 
 //---------------------------------------------------------------------------
@@ -93,7 +95,7 @@
 
 	ReturnCode = 2;
 
-	throw TFehler("Fehler");
+	throw std::runtime_error("Fehler");
 }
 
 //---------------------------------------------------------------------------
@@ -359,12 +361,35 @@
 	return (Kombi);
 }
 //---------------------------------------------------------------------------
-void* SfRealloc(void *pMem, int nNewSize)
+void* SfRealloc(void *Mem, int NewSize)
 {
-	if (pMem == NULL)	return(malloc(nNewSize));
-	else                return(realloc(pMem, nNewSize));
+	void* MemoryPointer = 0;
+	if (Mem == NULL)	MemoryPointer = malloc(NewSize);
+	else                MemoryPointer = realloc(Mem, NewSize);
+
+	if (0 == MemoryPointer)	write_error(9201, "Es steht nicht genug Arbeitsspeicher zur Verfügung\n");
+
+	return (MemoryPointer);
 }
 //---------------------------------------------------------------------------
+void* SfMalloc(int Size)
+{
+	void* MemoryPointer = malloc(Size);
+
+	if (0 == MemoryPointer)	write_error(9201, "Es steht nicht genug Arbeitsspeicher zur Verfügung\n");
+
+	return (MemoryPointer);
+}
+//---------------------------------------------------------------------------
+void* SfCalloc(int Anz, int Size)
+{
+	void* MemoryPointer = calloc(Anz, Size);
+
+	if (0 == MemoryPointer)	write_error(9201, "Es steht nicht genug Arbeitsspeicher zur Verfügung\n");
+
+	return (MemoryPointer);
+}
+//---------------------------------------------------------------------------
 void    Swap2Bytes(void *Bytes)
 {
 	byte    Temp;
@@ -1072,6 +1097,7 @@
 	write_fortschritt ("->Erzeugung der Kanten gestartet\n");
 
 	NodeList->ClearEdgeIndex();
+
 	EdgeList->Clear();
 
 	TEdge* NewEdge = 0;
@@ -1383,6 +1409,11 @@
 				NodeNr++;
 				TNode* Node = new TNode(NodeNr, X, Y, Z, Wsp);
 				ProfilNodeList->Add(Node);
+
+				if (DebugLevel >= 9 || (DebugLevel >= 1 && NodeNr % 100 == 0) || NodeNr % 1000 == 0)
+				{
+					write_fortschritt("%d von %d Profilen, %d Knoten erzeugt\n", Count, ProfilList->size(), NodeNr);
+				}
 			}
 			else if (Sperre)
 			{
@@ -1449,6 +1480,11 @@
 				NodeNr++;
 				TNode* Node = new TNode(NodeNr, X, Y, Z, Wsp);
 				ProfilNodeList->Add(Node);
+
+				if (DebugLevel >= 9 || (DebugLevel >= 1 && NodeNr % 100 == 0) || NodeNr % 1000 == 0)
+				{
+					write_fortschritt("%d von %d Profilen, %d Knoten erzeugt\n", Count, ProfilList->size(), NodeNr);
+				}
 			}
 			else if (Sperre)
 			{
@@ -2443,18 +2479,20 @@
 
 		// Hier nur die Verwaltungsinformationen, aber nicht die richtigen Kanten löschen
 		// Es waren nur geliehene Kanten
+		write_fortschritt("->Freigeben des Speichers der 1. temporären Kanten gestartet\n");
 		TempEdgeList->Empty();
 		delete TempEdgeList;
+		write_fortschritt("<-Freigeben des Speichers der 1. temporären Kanten beendet\n");
 
 		// Hier nur die Verwaltungsinformationen, aber nicht die richtigen Kanten löschen
 		// Es waren nur geliehene Kanten
+		write_fortschritt("->Freigeben des Speichers der 2. temporären Kanten gestartet\n");
 		Temp2EdgeList->Empty();
 		delete Temp2EdgeList;
+		write_fortschritt("<-Freigeben des Speichers der 2. temporären Kanten beendet\n");
 
-
 		// Jetzt sind alle Knoten nass, die Nass sein sollen
 
-
 		//Zuerst die Übergangspunkt der Kanten löschen
 		for (TEdgeVector::iterator i = EdgeList->EdgeVector.begin(); i != EdgeList->EdgeVector.end(); i++)
 		{
@@ -2607,7 +2645,10 @@
 		Node->Wsp = SaveNode->Wsp;
 	}
 
+	write_fortschritt("->Freigeben des Speichers der kopierten Knoten gestartet\n");
 	delete SaveNodeList;
+	SaveNodeList = 0;
+	write_fortschritt("<-Freigeben des Speichers der kopierten Knoten beendet\n");
 
 	write_fortschritt("<-Ermittlung der Überschwemmungsgrenzen beendet\n");
 }

Modified: trunk/src/tools.h
===================================================================
--- trunk/src/tools.h	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/tools.h	2006-09-25 05:44:29 UTC (rev 63)
@@ -56,7 +56,9 @@
 void    				Swap2Bytes(void *Bytes);
 void    				Swap4Bytes(void *Bytes);
 void    				Swap8Bytes(void *Bytes);
-void* 					SfRealloc(void *pMem, int nNewSize);
+void* 					SfRealloc(void *Mem, int NewSize);
+void* 					SfMalloc(int Size);
+void* 					SfCalloc(int Anz, int Size);
 std::string				GetFileExt(std::string FileName);
 std::string				ExchangeFileExt(std::string FileName, std::string Ext);
 std::string				GetFilePath(std::string FileName);

Modified: trunk/src/tri.cpp
===================================================================
--- trunk/src/tri.cpp	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/tri.cpp	2006-09-25 05:44:29 UTC (rev 63)
@@ -286,7 +286,7 @@
 
 		TElement *Element = 0;
 		Element = new TElement(Node1, Node2, NewNode);
-		ElementList->push_back(Element);
+		ElementList->Add(Element);
 	}
 	else
 	{
@@ -394,7 +394,10 @@
 		}
 	}
 
+	write_fortschritt("->Freigeben des Speichers der Kanten gestartet\n");
 	delete EdgeList;
+	EdgeList = 0;
+	write_fortschritt("<-Freigeben des Speichers der Kanten beendet\n");
 
 	NodeList->ReScale();
 

Modified: trunk/src/wsplgen.cpp
===================================================================
--- trunk/src/wsplgen.cpp	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/wsplgen.cpp	2006-09-25 05:44:29 UTC (rev 63)
@@ -31,6 +31,9 @@
 #ifndef __BORLANDC__
 #include <unistd.h>
 #endif
+
+#include <stdexcept>
+
 #include "wsplgen.h"
 #include "parameter.h"
 #include "tools.h"
@@ -71,32 +74,29 @@
 	write_fortschritt ("email: info at s-l-f.de\n");
 
 	// Hier werden alle Profile drin verwaltet
-	TProfilList ProfilList;
+	TProfilList *ProfilList = new TProfilList();
 
 	// Hier werden die Knoten verwaltet, die aus den Profilen generiert werden
-	TNodeList ProfilNodeList;
+	TNodeList *ProfilNodeList = new TNodeList();
 
 	// Hier werden alle Dämme und Gräben und Rohre drin verwaltet
-	TProfilList SperrenList;
-	TProfilList GrabenList;
-	TProfilList RohreList;
-	TProfilList GradientenList;
+	TProfilList *SperrenList = new TProfilList();
+	TProfilList *GrabenList = new TProfilList();
+	TProfilList *Rohre1List = new TProfilList();
+	TProfilList *Rohre2List = new TProfilList();
 
 	// Hier wird das aktuelle Begrenzungspolygon drin verwaltet
 	TXYList BegrenzungsPolygon;
 
 	// Hier werden dir Knoten des DGM drin verwaltet
 	// (und an andere Klassen in Form von Pointern ausgeliehen)
-	TNodeList NodeList;
+	TNodeList *NodeList = new TNodeList();
 
 	// Hier werden die Gewaesserachsen drin verwaltet
 	TGewaesserAchseList GewaesserAchseList;
 
-	// Hier werden die Knoten verwaltet, die aus den Linien generiert werden
-	TNodeList LinienNodeList;
-
 	// Hier drin werden die Ergebnis Polygone drin verwaltet
-	TErgebnisPolygonList ErgebnisPolygonList;
+	TErgebnisPolygonList *ErgebnisPolygone = new TErgebnisPolygonList();
 
 	try
 	{
@@ -107,15 +107,13 @@
 		// Jetzt die Profile einlesen
 		// Dabei wird die Stationierung von km in cm umgerechnet
 		// [km] in float nach [cm] in int
-		LoadProfile(Parameter.FileNamePro, &ProfilList, Parameter.VonKm, Parameter.BisKm, Parameter.DebugLevel);
+		LoadProfile(Parameter.FileNamePro, ProfilList, Parameter.VonKm, Parameter.BisKm, Parameter.DebugLevel);
 
-		if (ProfilList.size() < 2)
+		if (ProfilList->size() < 2)
 		{
-			write_error(3204, "Es liegen nur %d Querprofilspuren vor\nEs werden aber mindestens 2 Querprofilspuren benötigt.", ProfilList.size());
+			write_error(3204, "Es liegen nur %d Querprofilspuren vor\nEs werden aber mindestens 2 Querprofilspuren benötigt.", ProfilList->size());
 		}
 
-		// if (Parameter.DebugLevel >= 7) SaveProfile("debug_output_01_Profile_ORIGINAL.shp", &ProfilList, Parameter.DebugLevel);
-
 		// Jetzt die Waserstände einlesen und
 		// anschliessend gleich auf die Profile interpolieren
 		// Dabei wird die Stationierung von km in cm umgerechnet
@@ -125,7 +123,7 @@
 		TProfilList *WspProfilList = new TProfilList;
 
 		LoadWsp(Parameter.FileNameWsp, WspProfilList, Parameter.WspTag, Parameter.DebugLevel);
-		InterpolateWsp(&ProfilList, WspProfilList, Parameter.DebugLevel);
+		InterpolateWsp(ProfilList, WspProfilList, Parameter.DebugLevel);
 
 		delete WspProfilList;
 		WspProfilList = 0; 
@@ -141,7 +139,7 @@
 		// Jetzt die Gräben und Dämme und Rohre und Gradienten einlesen, wenn es notwendig ist
 		if (Parameter.IsSetLin)
 		{
-			LoadLinien(Parameter.FileNameLin, &SperrenList, &GrabenList, &RohreList, &GradientenList, Parameter.DebugLevel);
+			LoadLinien(Parameter.FileNameLin, SperrenList, GrabenList, Rohre1List, Rohre2List, Parameter.DebugLevel);
 		}
 
 		// Jetzt ein Polygon einlesen oder aus dn Profilen bilden
@@ -151,7 +149,7 @@
 		}
 		else
 		{
-			BuildPolygon(&ProfilList, &BegrenzungsPolygon, Parameter.DebugLevel);
+			BuildPolygon(ProfilList, &BegrenzungsPolygon, Parameter.DebugLevel);
 		}
 
 		std::string PolygonFileName = ExchangeFileExt(Parameter.FileNameAusgabe, "_begrenzungspolygon.shp");
@@ -163,49 +161,48 @@
 		// Eigentlich braucht man die Elemente nicht
 		TElementList *ElementList = new TElementList;
 
-		LoadDGM(Parameter.FileNameDgm, &NodeList, ElementList, &BegrenzungsPolygon, Parameter.DebugLevel);
+		LoadDGM(Parameter.FileNameDgm, NodeList, ElementList, &BegrenzungsPolygon, Parameter.DebugLevel);
 
 		// Wenn die Elementliste noch leer ist, müssen die Knoten noch trianguliert werden.
 		if (ElementList->size() == 0)
 		{
-			if (false == Triangulate (&NodeList, ElementList, &BegrenzungsPolygon, Parameter.DebugLevel)) return (false);
+			if (false == Triangulate (NodeList, ElementList, &BegrenzungsPolygon, Parameter.DebugLevel)) return (false);
 
 			std::string TriNetzFileName = ExchangeFileExt(Parameter.FileNameAusgabe, "_tri.2dm");
-			if (Parameter.IsSetSaveTri) SaveNet(TriNetzFileName, &NodeList, ElementList, Parameter.DebugLevel);
+			if (Parameter.IsSetSaveTri) SaveNet(TriNetzFileName, NodeList, ElementList, Parameter.DebugLevel);
 			std::string TriElementFileName = ExchangeFileExt(Parameter.FileNameAusgabe, "_tri.shp");
 			if (Parameter.IsSetSaveTri) SaveElements(TriElementFileName, ElementList, 0, Parameter.DebugLevel);
 		}
 
-		NodeList.SortByNr();
+		NodeList->SortByNr();
 
-		if (SperrenList.size() > 0 || GrabenList.size() > 0)
+		if (SperrenList->size() > 0 || GrabenList->size() > 0)
 		{
 			// Jetzt die gesperrten Elemente entfernen
 			// und die Gräben einschneiden und die Dämme herausheben
-			ElementList->ChangeElements(&NodeList, &SperrenList, &GrabenList, Parameter.DebugLevel);
-			CheckForDuplicates(&NodeList, ElementList, Parameter.DebugLevel);
+			ElementList->ChangeElements(NodeList, SperrenList, GrabenList, Parameter.DebugLevel);
+			CheckForDuplicates(NodeList, ElementList, Parameter.DebugLevel);
 		}
 
+		write_fortschritt("->Freigeben des Speichers der Gräben gestartet\n");
+		delete GrabenList;
+		GrabenList = 0;
+		write_fortschritt("<-Freigeben des Speichers der Gräben beendet\n");
+
 		// Hier werden die Kanten drin verwaltet
 		// Die Kanten werden im Moment noch aus den Elementen generiert
 		// Dieser Umweg ist eigendlich nicht notwendig
 		// Der durchschnittliche Abstand der Knoten (bzw. die Kantenlänge) wird dabei mit ermittelt.
 		// Das DGM sollte einigermassen homogen sein
 		TEdgeList* EdgeList = new TEdgeList;
-		BuildEdgeList(&NodeList, ElementList, EdgeList, Parameter.DebugLevel);
+		BuildEdgeList(NodeList, ElementList, EdgeList, Parameter.DebugLevel);
 
-		// Die Elemete werden nun nicht mehr gebraucht
-		// delete ElementList;
-		// ElementList = 0;
-
 		// Den alten Knoten Kanten Index löschen
-		NodeList.ClearEdgeIndex();
+		NodeList->ClearEdgeIndex();
 
 		// Einen neuen Knoten Kanten Index aufbauen
 		EdgeList->BuildEdgeIndex();
 
-		// if (Parameter.DebugLevel >= 4) SaveEdges("debug_output_08_Edges.shp", EdgeList, Parameter.DebugLevel);
-
 		// Nachdem die durchschnittliche Kantenlänge bekannt ist,
 		// wird sie für die spätere Verwendung zwischengespeichert
 		double AvgDistance = EdgeList->AvgDistance;
@@ -216,41 +213,48 @@
 		// werden die Profile entsprechend fein interpoliert
 		// Dabei muss der Faktor zwischen Kantenlänge in Metern
 		// und Station in Centimetern beachtet werden
-		ProfilList.InterpoliereProfile(AvgDistance * 8.0);
-                                      
-		if (ProfilList.size() < 200) ProfilList.InterpoliereProfile(AvgDistance * 4.0);
-		if (ProfilList.size() < 200) ProfilList.InterpoliereProfile(AvgDistance * 2.0);
-		if (ProfilList.size() < 200) ProfilList.InterpoliereProfile(AvgDistance * 1.0);
+		ProfilList->InterpoliereProfile(AvgDistance * 8.0);
 
+		if (ProfilList->size() < 200) ProfilList->InterpoliereProfile(AvgDistance * 4.0);
+		if (ProfilList->size() < 200) ProfilList->InterpoliereProfile(AvgDistance * 2.0);
+		if (ProfilList->size() < 200) ProfilList->InterpoliereProfile(AvgDistance * 1.0);
+
 		// Jetzt die Stützstellen auffüllen
-		ProfilList.FillProfile(AvgDistance / 4.0, -1, Parameter.DebugLevel);
+		ProfilList->FillProfile(AvgDistance / 4.0, -1, Parameter.DebugLevel);
 
 		// Mal wieder Zwischenergebnisse produzieren
 		std::string ProfilFileName = ExchangeFileExt(Parameter.FileNameAusgabe, "_interpolierte_profile.shp");
-		SaveProfile(ProfilFileName, &ProfilList, Parameter.DebugLevel);
+		SaveProfile(ProfilFileName, ProfilList, Parameter.DebugLevel);
 
 		// Jetzt eine neue Knotenliste aus den Profilen generienen
-		BuildProfilNodeList(&ProfilList, &NodeList, &GewaesserAchseList, &SperrenList, &ProfilNodeList, AvgDistance * 2.0, Parameter.Sperre, Parameter.DebugLevel);
+		BuildProfilNodeList(ProfilList, NodeList, &GewaesserAchseList, SperrenList, ProfilNodeList, AvgDistance * 2.0, Parameter.Sperre, Parameter.DebugLevel);
 
 		std::string ProfilKnotenFileName = ExchangeFileExt(Parameter.FileNameAusgabe, "_profilknoten.shp");
-		SaveNodes(ProfilKnotenFileName, &ProfilNodeList, Parameter.DebugLevel);
+		SaveNodes(ProfilKnotenFileName, ProfilNodeList, Parameter.DebugLevel);
 
 		// Jetzt die Wasserstände der Profile übertragen
-		TransferWsp(&ProfilNodeList, &NodeList, &SperrenList, AvgDistance / 2.0, Parameter.DebugLevel);
+		TransferWsp(ProfilNodeList, NodeList, SperrenList, AvgDistance / 2.0, Parameter.DebugLevel);
 
-		// if (Parameter.DebugLevel >= 4) SaveNodes("debug_output_12_Nodes_mit_WSP.shp", &NodeList, Parameter.DebugLevel);
+		write_fortschritt("->Freigeben des Speichers der Dämme gestartet\n");
+		delete SperrenList;
+		SperrenList = 0;
+		write_fortschritt("<-Freigeben des Speichers der Dämme beendet\n");
 
-		ProfilNodeList.Clear();
+		write_fortschritt("->Freigeben des Speichers der Profilknoten gestartet\n");
+		delete ProfilNodeList;
+		ProfilNodeList = 0;
+		write_fortschritt("<-Freigeben des Speichers der Profilknoten beendet\n");
 
-		ProfilList.Clear();
+		write_fortschritt("->Freigeben des Speichers der Profile gestartet\n");
+		delete ProfilList;
+		ProfilList = 0;
+		write_fortschritt("<-Freigeben des Speichers der Profile beendet\n");
 
-		// if (Parameter.DebugLevel >= 4) SaveNodes("debug_output_13_Nodes_mit_WSP_mit_rohr.shp", &NodeList, Parameter.DebugLevel);
-
 		// Wurde der Wert für das Argument 'Bis' beim Parameter -DELTA nicht gesetzt
 		// so ist jetzt die Zeit das nachzuholen
 		if (false == Parameter.IsSetBis)
 		{
-			Parameter.Bis = NodeList.GetMaxDepth(Parameter.DebugLevel);
+			Parameter.Bis = NodeList->GetMaxDepth(Parameter.DebugLevel);
 			Parameter.IsSetBis = true;
 		}
 
@@ -283,53 +287,74 @@
 			}
 		}
 
-		TErgebnisPolygonList* TempErgebnisPolygone = new TErgebnisPolygonList;
-		NassTrockenBerechnung(&NodeList, EdgeList, TempErgebnisPolygone, &RohreList, &GradientenList, AvgDistance, Parameter.Von, Parameter.Bis, Parameter.Diff, Parameter.DebugLevel);
+		NassTrockenBerechnung(NodeList, EdgeList, ErgebnisPolygone, Rohre1List, Rohre2List, AvgDistance, Parameter.Von, Parameter.Bis, Parameter.Diff, Parameter.DebugLevel);
 
-		NodeList.SortByNr();
+		write_fortschritt("->Freigeben des Speichers der Rohre vom Typ 1 gestartet\n");
+		delete Rohre1List;
+		Rohre1List = 0;
+		write_fortschritt("<-Freigeben des Speichers der Rohre vom Typ 1 beendet\n");
 
-		std::string KnotenFileName = ExchangeFileExt(Parameter.FileNameAusgabe, "_knoten.shp");
-		SaveNodes(KnotenFileName, &NodeList, Parameter.DebugLevel);
+		write_fortschritt("->Freigeben des Speichers der Rohre vom Typ 2 gestartet\n");
+		delete Rohre2List;
+		Rohre2List = 0;
+		write_fortschritt("<-Freigeben des Speichers der Rohre vom Typ 2 beendet\n");
 
-		std::string ElementeFileName = ExchangeFileExt(Parameter.FileNameAusgabe, "_elemente.shp");
-		SaveElements(ElementeFileName, ElementList, Parameter.Von, Parameter.DebugLevel);
+		NodeList->ClearEdgeIndex();
 
-		ElementList->Clear();
-
-		ErgebnisPolygonList.Append(TempErgebnisPolygone);
-
-		if (Parameter.DebugLevel >= 8)
-		{
-			for (TErgebnisPolygonList::iterator i = ErgebnisPolygonList.begin(); i != ErgebnisPolygonList.end(); i++)
-			{
-				TErgebnisPolygon* ErgebnisPolygon = *i;
-				double AktDiff = ErgebnisPolygon->Diff;
-
-				if (Parameter.DebugLevel >= 9)	write_fortschritt("Wassertiefe %.2f\n", AktDiff / 100.0);
-			}
-		}
-
-		TempErgebnisPolygone->clear();
-		delete TempErgebnisPolygone;
-
 		// Löscht nur die Verwaltung und nicht die Knoten der Kanten,
 		// die Knoten waren nur geliehen
+		write_fortschritt("->Freigeben des Speichers der Kanten gestartet\n");
 		delete EdgeList;
+		EdgeList = 0;
+		write_fortschritt("<-Freigeben des Speichers der Kanten beendet\n");
 
+		std::string ElementeFileName = ExchangeFileExt(Parameter.FileNameAusgabe, "_elemente.shp");
+		SaveElements(ElementeFileName, ElementList, Parameter.Von, Parameter.DebugLevel);
+
+		write_fortschritt("->Freigeben des Speichers der Elemente gestartet\n");
 		delete ElementList;
 		ElementList = 0;
+		write_fortschritt("<-Freigeben des Speichers der Elemente beendet\n");
 
+		NodeList->SortByNr();
+
+		std::string KnotenFileName = ExchangeFileExt(Parameter.FileNameAusgabe, "_knoten.shp");
+		SaveNodes(KnotenFileName, NodeList, Parameter.DebugLevel);
+
 		// Speichern der Ergebnispolygone
-		SavePolygone(Parameter.FileNameAusgabe, &ErgebnisPolygonList, Parameter.DebugLevel);
+		SavePolygone(Parameter.FileNameAusgabe, ErgebnisPolygone, Parameter.DebugLevel);
 
 		// Speichern der Meta-Daten
 		SaveXml(Parameter.FileNameAusgabe, &Parameter);
+
+		write_fortschritt("->Freigeben des Speichers der ErgebnisPolygone gestartet\n");
+		delete ErgebnisPolygone;
+		ErgebnisPolygone = 0;
+		write_fortschritt("<-Freigeben des Speichers der ErgebnisPolygone beendet\n");
+
+		write_fortschritt("->Freigeben des Speichers der Knoten gestartet\n");
+		delete NodeList;
+		NodeList = 0;
+		write_fortschritt("<-Freigeben des Speichers der Knoten beendet\n");
 	}
-	catch (TFehler fehler)
+	catch (const std::runtime_error& e)
 	{
-		write_fortschritt ("Bekannter Fehler wurde korrekt abgefangen\n");
+		write_fortschritt ("Fehler wurde korrekt abgefangen\n");
 		write_fortschritt ("Programmabbruch\n");
 	}
+	catch (const std::length_error& e)
+	{
+		write_fortschritt ("Es steht leider nicht genug Speicher zur Verfügung\n", e.what());
+		write_fortschritt ("Fehler wurde korrekt abgefangen\n");
+		write_fortschritt ("Programmabbruch\n");
+	}
+	catch (const std::exception& e)
+	{
+		write_fortschritt ("%s\n", e.what());
+		write_fortschritt ("Fehler wurde korrekt abgefangen\n");
+		write_fortschritt ("Bitte informieren sie den Programmierer\n");
+		write_fortschritt ("Programmabbruch\n");
+	}
 	catch (...)
 	{
 		write_fortschritt ("Unbekannter Fehler wurde korrekt abgefangen\n");

Modified: trunk/src/wsplgen.h
===================================================================
--- trunk/src/wsplgen.h	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/wsplgen.h	2006-09-25 05:44:29 UTC (rev 63)
@@ -10,11 +10,16 @@
 // Read the file COPYING coming with WSPLGEN for details.
 //
 
-const char Version[] = "0.9.17 r1";
+const char Version[] = "0.9.17 r2";
 
+// const char Version[] = "0.9.17 r1";
+
 // Ein Fehler bei der Erzeugung von Dämmen und Gräben wurde behoben (entartete Elemente).
 // Die triangulierten Netze werden jetzt auch als SHP-Datei gespeichert.
 // Die Speicherverwaltung beim Speichern von Elementen, Knoten und Kanten wurde verbessert.
+// Diverse Datenstrukturen werden jetzt nicht mehr als lokale Variable abgelegt, sondern
+// dynamisch erzeugt und entsprechend wieder zerstört. So ist es möglich die Speicherverwaltung
+// besser im Griff zu haben und nicht alles am Ende des Programmes zu erledigen.
 
 // const char Version[] = "0.9.16 r2";
 

Modified: trunk/src/xy.cpp
===================================================================
--- trunk/src/xy.cpp	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/xy.cpp	2006-09-25 05:44:29 UTC (rev 63)
@@ -120,10 +120,13 @@
 		if (AktEdgeIndex->Edge == Edge)
 		{
 			Found = true;
-			delete AktEdgeIndex;
 
 			if (AktEdgeIndex == EdgeIndex)	EdgeIndex = NextEdgeIndex;
 			else 							PrevEdgeIndex->NextEdgeIndex = NextEdgeIndex;
+
+			delete AktEdgeIndex;
+			AktEdgeIndex = 0;
+
 			break;
 		}
 
@@ -243,6 +246,7 @@
 	{
 		TXY *Xy = (*this)[i];
 		delete Xy;
+		Xy = 0;
 	}
 	clear();
 
@@ -408,8 +412,10 @@
 //---------------------------------------------------------------------
 void TNodeList::Clear(void)
 {
+	write_fortschritt("->Löschen des Suchbaumes der Knoten gestartet\n");
 	if (QuadTree)	delete QuadTree;
 	QuadTree = 0;
+	write_fortschritt("<-Löschen des Suchbaumes der Knoten beendet\n");
 
 	if (SearchNodeList)
 	{
@@ -418,11 +424,22 @@
 		SearchNodeList = 0;
 	}
 
-	for (unsigned int i=0; i<size(); i++)
+	if (size() > 0)
 	{
-		TNode *Node = (*this)[i];
-		delete Node;
+		int Count = 0;
+		for (TNodeList::iterator i = begin(); i != end(); i++)
+		{
+			TNode *Node = *i;
+			delete Node;
+			Node = 0;
+
+			Count++;
+
+			if (Count % 100000 == 0)	write_fortschritt("%d Objekte freigegeben\n", Count);
+		}
+		write_fortschritt("%d Objekte freigegeben\n", Count);
 	}
+
 	clear();
 
 	MaxNr = -1;
@@ -460,6 +477,12 @@
 //---------------------------------------------------------------------
 void TNodeList::Add(TNode *Node)
 {
+	if (capacity() < size() + 1)
+	{
+		reserve((size_t)(capacity() * 1.1 + 100000));
+		write_fortschritt("Kapazität der Knotenliste wurde auf %d erhöht\n", capacity());
+	}
+
 	push_back(Node);
 
 	if (size() == 1)
@@ -506,8 +529,10 @@
 {
 	write_fortschritt("->Sortieren der Knoten gestartet\n");
 
+	write_fortschritt("->Löschen und Neuaufbauen des Suchbaumes der Knoten gestartet\n");
 	if (QuadTree)   delete QuadTree;
 	QuadTree = new TQuadTree<TNodeList, TNode>();
+	write_fortschritt("<-Löschen und Neuaufbauen des Suchbaumes der Knoten beendet\n");
 
 	for (iterator i = begin(); i != end(); i++)
 	{
@@ -522,7 +547,11 @@
 //---------------------------------------------------------------------
 void TNodeList::SortByNr(void)
 {
+	write_fortschritt("->Sortieren der Knoten gestartet\n");
+
 	std::sort(this->begin(), this->end(), TNodeSortByNr());
+
+	write_fortschritt("<-Sortieren der Knoten beendet\n");
 }
 
 //---------------------------------------------------------------------
@@ -845,15 +874,23 @@
 //---------------------------------------------------------------------
 void TNodeList::ClearEdgeIndex(void)
 {
-	write_fortschritt("->Löschen des Knoten/Kanten-Index gestartet\n");
+	if (size() > 0)
+	{
+		write_fortschritt("->Löschen des Knoten/Kanten-Index gestartet\n");
 
-	for (iterator i = begin(); i != end(); i++)
-	{
-		TNode *Node = *i;
-		Node->ClearEdgeIndex();
+		int Count = 0;
+		for (iterator i = begin(); i != end(); i++)
+		{
+			TNode *Node = *i;
+			Node->ClearEdgeIndex();
+
+			Count++;
+
+			if (Count % 100000 == 0)	write_fortschritt("%d Objekte freigegeben\n", Count);
+		}
+
+		write_fortschritt("<-Löschen des Knoten/Kanten-Index beendet\n");
 	}
-
-	write_fortschritt("<-Löschen des Knoten/Kanten-Index beendet\n");
 }
 
 //---------------------------------------------------------------------
@@ -873,7 +910,7 @@
 
 		if (Depth < 0)
 		{
-			dump_error(__FILE__, __LINE__, "Der Wasserstand ist negative\n");
+			dump_error(__FILE__, __LINE__, "Der Wasserstand ist negativ\n");
 		}
 
 		if (Count == 0 || Depth > MaxDepth)	MaxDepth = Depth;
@@ -911,7 +948,7 @@
 
 		if (Wsp < 0.0)
 		{
-			dump_error(__FILE__, __LINE__, "Der Wasserstand ist negative\n");
+			dump_error(__FILE__, __LINE__, "Der Wasserstand ist negativ\n");
 		}
 
 		if (Count == 0 || Wsp > MaxWsp)	MaxWsp = Wsp;
@@ -1024,14 +1061,36 @@
 //---------------------------------------------------------------------
 void TElementList::Clear(void)
 {
-	for (TElementList::iterator i=begin(); i != end(); i++)
+	if (size() > 0)
 	{
-		TElement *Element = *i;
-		delete Element;
+		int Count = 0;
+		for (iterator i=begin(); i != end(); i++)
+		{
+			TElement *Element = *i;
+			delete Element;
+			Element = 0;
+
+			Count++;
+
+			if (Count % 100000 == 0)	write_fortschritt("%d Objekte freigegeben\n", Count);
+		}
+		write_fortschritt("%d Objekte freigegeben\n", Count);
 	}
 	clear();
 }
 
+//---------------------------------------------------------------------
+void TElementList::Add(TElement* Element)
+{
+	if (capacity() < size() + 1)
+	{
+		reserve((size_t)(capacity() * 1.1 + 200000));
+		write_fortschritt("Kapazität der Elementliste wurde auf %d erhöht\n", capacity());
+	}
+
+	push_back(Element);
+}
+
 //---------------------------------------------------------------------------
 void TElementList::HelpChangeElements(TNodeList *NodeList, TProfilList *LinienList, int ZWert, int DebugLevel)
 {
@@ -1043,9 +1102,6 @@
 	{
 		TElement* Element = (*this)[i];
 
-		// Hier werden die zum Löschen markierten Elemente ignoriert.
-		if (Element->Typ == NO_ELEMENT)	continue;
-
 		Count++;
 
 		if (DebugLevel >= 9 || (DebugLevel >= 1 && Count % 5000 == 0) || (Count % 50000 == 0))
@@ -1053,6 +1109,10 @@
 			write_fortschritt("%d von %d Elementen, %d Elemente angepasst\n", Count, size(), ChangeCount);
 		}
 
+		// Hier werden die zum Löschen markierten Elemente ignoriert.
+		if (Element->Typ == NO_ELEMENT)	continue;
+
+
 		// Hier werden die neuen  Elemente wieder auf einen korrekten Typ gesetzt und dann ignoriert.
 		if (Element->Typ == NEWTRI)
 		{
@@ -1382,23 +1442,91 @@
 			ChangeCount++;
 		}
 	}
-	write_fortschritt("%d von %d Elemente, %d Elemente geändert\n", Count, size(), ChangeCount);
+	write_fortschritt("%d von %d Elemente, %d Elemente angepasst\n", Count, size(), ChangeCount);
 }
+
 //---------------------------------------------------------------------------
+void TElementList::CleanChangedElements(int DebugLevel)
+{
+	write_fortschritt("->Suche nach entarteten Elementen gestartet\n");
+
+	int Count = 0;
+	for (unsigned int i = 0; i < size(); i++)
+	{
+		TElement* Element = (*this)[i];
+
+		Count++;
+
+		if (DebugLevel >= 9 || (DebugLevel >= 1 && Count % 5000 == 0) || (Count % 50000 == 0))
+		{
+			write_fortschritt("%d von %d Elementen überprüft\n", Count, size());
+		}
+
+		// Hier werden die zum Löschen markierten Elemente ignoriert.
+		if (Element->Typ == NO_ELEMENT)	continue;
+
+		// Hier werden die neuen  Elemente wieder auf einen korrekten Typ gesetzt und dann ignoriert.
+		if (Element->Typ == NEWTRI)
+		{
+			Element->Typ = TRI;
+		}
+
+		if (Element->Typ == QUAD)
+		{
+			dump_error(__FILE__, __LINE__, "Viereckselemente werden im Moment noch nicht unterstützt");
+		}
+
+		TNode* Node1 = Element->Node1;
+		TNode* Node2 = Element->Node2;
+		TNode* Node3 = Element->Node3;
+
+		if (Node1->X == Node2->X && Node1->Y == Node2->Y)
+		{
+			Element->Typ = NO_ELEMENT;
+			continue;
+		}
+
+		if (Node2->X == Node3->X && Node2->Y == Node3->Y)
+		{
+			Element->Typ = NO_ELEMENT;
+			continue;
+		}
+
+		if (Node3->X == Node1->X && Node3->Y == Node1->Y)
+		{
+			Element->Typ = NO_ELEMENT;
+			continue;
+		}
+
+	}
+	write_fortschritt("%d von %d Elementen überprüft\n", Count, size());
+
+	write_fortschritt("<-Suche nach entarteten Elementen beendet\n");
+}
+
+//---------------------------------------------------------------------------
 void TElementList::ChangeElements(TNodeList *NodeList, TProfilList *SperrenList, TProfilList *GrabenList, int DebugLevel)
 {
 	write_fortschritt("->Anpassung der Elemente an Dämme und Gräben gestartet\n");
 
 	NodeList->SortByXY();
 
-	write_fortschritt("->Anpassung der Elemente an Dämme gestartet\n");
-	HelpChangeElements(NodeList, SperrenList, DammZ, DebugLevel);
-	write_fortschritt("<-Anpassung der Elemente an Dämme beendet\n");
+	if (SperrenList && SperrenList->size() > 0)
+	{
+		write_fortschritt("->Anpassung der Elemente an Dämme gestartet\n");
+		HelpChangeElements(NodeList, SperrenList, DammZ, DebugLevel);
+		write_fortschritt("<-Anpassung der Elemente an Dämme beendet\n");
+	}
 
-	write_fortschritt("->Anpassung der Elemente an Gräben gestartet\n");
-	HelpChangeElements(NodeList, GrabenList, GrabenZ, DebugLevel);
-	write_fortschritt("<-Anpassung der Elemente an Gräben beendet\n");
+	if (GrabenList && GrabenList->size() > 0)
+	{
+		write_fortschritt("->Anpassung der Elemente an Gräben gestartet\n");
+		HelpChangeElements(NodeList, GrabenList, GrabenZ, DebugLevel);
+		write_fortschritt("<-Anpassung der Elemente an Gräben beendet\n");
+	}
 
+	CleanChangedElements(DebugLevel);
+
 	write_fortschritt("<-Anpassung der Elemente an Dämme und Gräben beendet\n");
 }
 
@@ -1468,6 +1596,7 @@
 	{
 		TErgebnisPolygon *ErgebnisPolygon = *i;
 		delete ErgebnisPolygon;
+		ErgebnisPolygon = 0;
 	}
 	clear();
 }
@@ -1623,17 +1752,32 @@
 	delete SearchEdge;
 	delete SearchNode1;
 	delete SearchNode2;
+
+	SearchEdge = 0;
+	SearchNode1 = 0;
+	SearchNode2 = 0;
 }
 
 //---------------------------------------------------------------------
 void TEdgeList::Clear(void)
 {
-	for (TEdgeVector::const_iterator i = EdgeVector.begin(); i != EdgeVector.end(); i++)
+	if (EdgeVector.size() > 0)
 	{
-		TEdge *Edge = *i;
-		delete Edge;
-		Anz--;
+		int Count = 0;
+		for (TEdgeVector::const_iterator i = EdgeVector.begin(); i != EdgeVector.end(); i++)
+		{
+			TEdge *Edge = *i;
+			delete Edge;
+			Edge = 0;
+			Anz--;
+
+			Count++;
+
+			if (Count % 100000 == 0)	write_fortschritt("%d Objekte freigegeben\n", Count);
+		}
+		write_fortschritt("%d Objekte freigegeben\n", Count);
 	}
+
 	if (Anz != 0)
 	{
 		dump_error(__FILE__, __LINE__, "Anz der Kanten ist nicht 0\n");
@@ -1665,6 +1809,12 @@
 //---------------------------------------------------------------------
 void TEdgeList::Add(TEdge *edge)
 {
+	if (EdgeVector.capacity() < EdgeVector.size() + 1)
+	{
+		EdgeVector.reserve((size_t)(EdgeVector.capacity() * 1.1 + 200000));
+		write_fortschritt("Kapazität der Kantenliste wurde auf %d erhöht\n", EdgeVector.capacity());
+	}
+
 	EdgeVector.push_back(edge);
 	AvgDistance = AvgDistance * Anz;
 	Anz++;
@@ -1686,12 +1836,12 @@
 }
 
 //---------------------------------------------------------------------
-TEdge* TEdgeList::FindWithIndex(TNode *Node1, TNode *Node2)
-{
-	if (Anz == 0)	return(0);
-
-	TEdgeIndex *AktEdgeIndex = Node1->EdgeIndex;
+TEdge* TEdgeList::FindWithIndex(TNode *Node1, TNode *Node2)
+{
+	if (Anz == 0)	return(0);
 
+	TEdgeIndex *AktEdgeIndex = Node1->EdgeIndex;
+
 	while (AktEdgeIndex)
 	{
 		TEdgeIndex *NextEdgeIndex = AktEdgeIndex->NextEdgeIndex;
@@ -1705,10 +1855,10 @@
 
 		AktEdgeIndex = NextEdgeIndex;
 	}
-
-	return (0);
-}
 
+	return (0);
+}
+
 //---------------------------------------------------------------------
 void TEdgeList::BuildEdgeIndex(void)
 {
@@ -1785,6 +1935,7 @@
 		{
 			TPoint *Point = *i;
 			delete Point;
+			Point = 0;
 		}
 		delete PointList;
 	}
@@ -1820,6 +1971,7 @@
 	if (PointList->size() == Anz)
 	{
 		delete NewPoint;
+		NewPoint = 0;
 	}
 	else
 	{
@@ -2159,11 +2311,21 @@
 //---------------------------------------------------------------------
 void TProfilList::Clear(void)
 {
-	for (TProfilList::iterator i=begin(); i != end(); i++)
+	if (size() > 0)
 	{
-		TProfil *Profil = *i;
+		int Count = 0;
+		for (TProfilList::iterator i=begin(); i != end(); i++)
+		{
+			TProfil *Profil = *i;
 
-		delete Profil;
+			delete Profil;
+			Profil = 0;
+
+			Count++;
+
+			if (Count % 10000 == 0)	write_fortschritt("%d Objekte freigegeben\n", Count);
+		}
+		write_fortschritt("%d Objekte freigegeben\n", Count);
 	}
 
 	clear();
@@ -2246,6 +2408,9 @@
 
 		delete FirstProfilNeu;
 		delete NextProfilNeu;
+
+		FirstProfilNeu = 0;
+		NextProfilNeu = 0;
 	}
 
 	for (TProfilList::iterator i = NewProfilList->begin(); i != NewProfilList->end(); i++)
@@ -2255,6 +2420,7 @@
 
 	NewProfilList->clear();
 	delete NewProfilList;
+	NewProfilList = 0;
 
 	write_fortschritt("<-Interpolation der Profile beendet\n");
 }
@@ -2397,9 +2563,11 @@
 			TPoint *Point = *i;
 
 			delete Point;
+			Point = 0;
 		}
 
 		delete PointList;
+		PointList = 0;
 	}
 	MinX = 999999999;
 	MaxX = -999999999;
@@ -2578,6 +2746,7 @@
 		TGewaesserAchse *GewaesserAchse = *i;
 
 		delete GewaesserAchse;
+		GewaesserAchse = 0;
 	}
 
 	clear();

Modified: trunk/src/xy.h
===================================================================
--- trunk/src/xy.h	2006-09-17 15:16:49 UTC (rev 62)
+++ trunk/src/xy.h	2006-09-25 05:44:29 UTC (rev 63)
@@ -259,7 +259,9 @@
 	public:
 						~TElementList(void);
 		void			Clear(void);
+		void			Add(TElement* Element);
 		void			ChangeElements(TNodeList* NodeList, TProfilList *SperrenList, TProfilList *BruchList, int DebugLevel);
+		void 			CleanChangedElements(int DebugLevel);
 };
 
 



More information about the Wsplgen-commits mailing list