From 21d0956d68ef825fc27cd5eb1a032b3c421eb89f Mon Sep 17 00:00:00 2001
From: Shon Ferguson <shonferg@users.sourceforge.net>
Date: Sun, 23 Oct 2005 21:16:02 +0000
Subject: [PATCH] Made some changes to help Niflib work better with GCC.

---
 NIF_Blocks.cpp |  5 +++--
 NIF_Blocks.h   |  5 ++---
 NIF_IO.cpp     | 37 ++++++++++++++++++++++++++++++++++++-
 NIF_IO.h       | 35 ++++-------------------------------
 nif_attrs.h    |  2 +-
 nif_math.cpp   |  2 +-
 niflib.cpp     |  6 ++++--
 pyniflib.i     |  1 +
 8 files changed, 52 insertions(+), 41 deletions(-)

diff --git a/NIF_Blocks.cpp b/NIF_Blocks.cpp
index 10cd86d0..3ebaee3c 100644
--- a/NIF_Blocks.cpp
+++ b/NIF_Blocks.cpp
@@ -210,7 +210,8 @@ list<blk_ref> ABlock::GetLinks() {
 	vector<attr_ref>::iterator it;
 	for ( it = _attr_vect.begin(); it != _attr_vect.end(); ++it ) {
 		if ( (*it)->HasLinks() == true ) {
-			links.merge( list<blk_ref>(*it) );
+			list<blk_ref> link_list = (*it)->asLinkList();
+			links.merge( link_list );
 		}
 	}
 
@@ -2418,4 +2419,4 @@ void UnknownMixIn::Write( ofstream& out ) {
 //}
 //cout << dec << setfill(' ');
 
-//delete [] data;
\ No newline at end of file
+//delete [] data;
diff --git a/NIF_Blocks.h b/NIF_Blocks.h
index a2e8b082..107766df 100644
--- a/NIF_Blocks.h
+++ b/NIF_Blocks.h
@@ -1332,7 +1332,7 @@ public:
 	}
 	void asString( ostream & out ) {
 		out << ABlock::asString();
-		out << UnknownMixIn::asString;
+		out << UnknownMixIn::asString();
 	}
 	string GetBlockType() { return UnknownMixIn::GetBlockType(); }
 };
@@ -1488,5 +1488,4 @@ public:
 //	string GetBlockType() { return "NiParticleSystemController"; }
 //};
 
-
-#endif // TAH_NIF_LIB_NIF_BLOCKS_H
\ No newline at end of file
+#endif // TAH_NIF_LIB_NIF_BLOCKS_H
diff --git a/NIF_IO.cpp b/NIF_IO.cpp
index 815dc483..974048d3 100644
--- a/NIF_IO.cpp
+++ b/NIF_IO.cpp
@@ -300,4 +300,39 @@ ostream & operator<<(ostream & lh, nifIndex & rh) {
 		lh << "None";
 	}
 	return lh;
-}
\ No newline at end of file
+}
+
+ostream & operator<<(ostream & lh, Str & rh) {
+	//Fix string
+	char * s = new char[rh._n + 1];
+	strncpy(s, rh._c, rh._n);
+	s[rh._n] = 0;
+	lh << s;
+	delete [] s;
+	return lh;
+}
+
+ostream & operator<<(ostream & lh, Hex & rh) {
+	return lh << dec << rh._n << " (0x" << hex << uppercase << rh._n << ")" << dec;
+}
+
+ostream & operator<<(ostream & lh, Index & rh) {
+	if (int(rh._n) != -1)
+		return lh << "Block " << rh._n;
+	else
+		return lh << "None";
+}
+
+ostream & operator<<(ostream & lh, Bin & rh) {
+	uint x = rh._n;
+	for (uint i = 0; i < rh._w; i++) {
+		if((x & 0x80) !=0) {
+			lh << "1";
+		}
+		else {
+			lh << "0";
+		}
+		x <<= 1;
+	}
+	return lh;
+}
diff --git a/NIF_IO.h b/NIF_IO.h
index 906319d7..1860fefd 100644
--- a/NIF_IO.h
+++ b/NIF_IO.h
@@ -136,15 +136,7 @@ int BlockSearch( ifstream& in );
 class Str {
 public:
 	Str(const char * c, int n) { _c = c; _n = n; }
-	friend ostream & operator<<(ostream & lh, Str & rh) {
-		//Fix string
-		char * s = new char[rh._n + 1];
-		strncpy(s, rh._c, rh._n);
-		s[rh._n] = 0;
-		lh << s;
-		delete [] s;
-		return lh;
-	}
+	friend ostream & operator<<(ostream & lh, Str & rh);
 private:
 	const char * _c;
 	uint _n;
@@ -155,9 +147,7 @@ public:
 	Hex(uint & n) { _n = n; }
 	Hex(ushort & n) { _n = uint(n); }
 	Hex(byte & n) { _n = uint(n); }
-	friend ostream & operator<<(ostream & lh, Hex & rh) {
-		return lh << dec << rh._n << " (0x" << hex << uppercase << rh._n << ")" << dec;
-	}
+	friend ostream & operator<<(ostream & lh, Hex & rh);
 private:
 	uint _n;
 };
@@ -167,12 +157,7 @@ public:
 	Index(uint & n) { _n = n; }
 	Index(ushort & n) { _n = uint(n); }
 	Index(byte & n) { _n = uint(n); }
-	friend ostream & operator<<(ostream & lh, Index & rh) {
-		if (int(rh._n) != -1)
-			return lh << "Block " << rh._n;
-		else
-			return lh << "None";
-	}
+	friend ostream & operator<<(ostream & lh, Index & rh);
 private:
 	uint _n;
 };
@@ -182,19 +167,7 @@ public:
 	Bin(uint &  n) { _n = n; _w = 32; }
 	Bin(ushort & n) {_n = uint(n); _w = 16; }
 	Bin(byte & n) { _n = uint(n); _w = 8; }
-	friend ostream & operator<<(ostream & lh, Bin & rh) {
-		uint x = rh._n;
-		for (uint i = 0; i < rh._w; i++) {
-			if((x & 0x80) !=0) {
-				lh << "1";
-			}
-			else {
-				lh << "0";
-			}
-			x <<= 1;
-		}
-		return lh;
-	}
+	friend ostream & operator<<(ostream & lh, Bin & rh);
 private:
 	uint _n;
 	uint _w;
diff --git a/nif_attrs.h b/nif_attrs.h
index b5a8a63d..17b12d3e 100644
--- a/nif_attrs.h
+++ b/nif_attrs.h
@@ -1313,4 +1313,4 @@ private:
 	int original_root;
 };
 
-#endif
\ No newline at end of file
+#endif
diff --git a/nif_math.cpp b/nif_math.cpp
index a46241c4..41a5948a 100644
--- a/nif_math.cpp
+++ b/nif_math.cpp
@@ -289,4 +289,4 @@ void QuatToEuler( Quaternion & quat, ostream & out ) {
 		<< "         Attitude:  " << a << endl
 		<< "         Bank:  " << b << endl;
 	
-}
\ No newline at end of file
+}
diff --git a/niflib.cpp b/niflib.cpp
index 32683751..63a67d7b 100644
--- a/niflib.cpp
+++ b/niflib.cpp
@@ -173,7 +173,8 @@ blk_ref CreateBlock( string block_type ) {
 //Reads the given file by file name and returns a reference to the root block
 blk_ref ReadNifTree( string file_name ) {
 	//Read block list
-	return FindRoot( ReadNifList( file_name ) );
+	vector<blk_ref> blocks = ReadNifList( file_name );
+	return FindRoot( blocks );
 }
 
 blk_ref FindRoot( vector<blk_ref> & blocks ) {
@@ -248,7 +249,8 @@ vector<blk_ref> ReadNifList( string file_name ) {
 		in.read( blockName, blockNameLength );
 		blockName[blockNameLength] = 0;
 		if ( (blockName[0] != 'N' || blockName[1] != 'i') && (blockName[0] != 'R' || blockName[1] != 'o') && (blockName[0] != 'A' || blockName[1] != 'v')) {
-			cout << "ERROR!  Bad block position.  Invalid Name:  " << Str(blockName, blockNameLength) << "\a" << endl;
+			Str block_name(blockName, blockNameLength);
+			cout << "ERROR!  Bad block position.  Invalid Name:  " << block_name << "\a" << endl;
 			cout << "====[ " << file_name << " | Block " << i - 1 << " | " << blocks[i - 1]->GetBlockType() << " ]====" << endl;
 			cout << blocks[i - 1]->asString();
 			throw runtime_error("Read failue - Bad block position");
diff --git a/pyniflib.i b/pyniflib.i
index 9ff41d84..d172999b 100644
--- a/pyniflib.i
+++ b/pyniflib.i
@@ -61,6 +61,7 @@ struct Key {
 };
 
 %template(vector_float) std::vector<float>;
+%template(vector_blk_ref) std::vector<attr_ref>;
 %template(vector_blk_ref) std::vector<blk_ref>;
 %template(vector_Vector3) std::vector<Vector3>;
 %template(vector_Color) std::vector<Color>;
-- 
GitLab