Loading an entire file into a string?

Discuss about anything related to the Irrlicht Engine, or read announcements about any significant features or usage changes.
Post Reply
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Loading an entire file into a string?

Post by 3DModelerMan »

I want to load the contents of an entire text file into an irr::core::stringc. I'm integrating LUA right now, and I want to be able to load scripts from inside of .zip files that have been added to the Irrlicht file system. So I need a way to load LUA scripts from Irrlicht.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
christianclavet
Posts: 1638
Joined: Mon Apr 30, 2007 3:24 am
Location: Montreal, CANADA
Contact:

Post by christianclavet »

You could use this (taken from IRR RPG)

Code: Select all

stringc newScript = "";
				stringc filename = "../media/scripts/";
				filename += scriptname;
				std::string line;
				ifstream fileScript (filename.c_str());
				if (fileScript.is_open())
				{
					while (! fileScript.eof() )
					{
						getline (fileScript,line);
						newScript += line.c_str();
						newScript += '\n';
					}
					fileScript.close();
				}
I'm using this to preload the script in the editor. Then newScript can be used with LUA.
3DModelerMan
Posts: 1691
Joined: Sun May 18, 2008 9:42 pm

Post by 3DModelerMan »

Yeah, but that uses std::ifstream, I need to use the Irrlicht file system so I can load from zip files.
That would be illogical captain...

My first full game:
http://www.kongregate.com/games/3DModel ... tor#tipjar
slavik262
Posts: 753
Joined: Sun Nov 22, 2009 9:25 pm
Location: Wisconsin, USA

Post by slavik262 »

Use malloc or calloc to create a char buffer of size (file length + 1) with a null terminator, read the file into the buffer, create your sringc from the buffer, and free the buffer.
pippy3
Posts: 155
Joined: Tue Dec 15, 2009 7:32 am

Post by pippy3 »

Irrlicht already has a XML phaser. Simply create a script class extended from IAttributeExchangingObject, then write in the deserializeAttributes/ serializeAttributes functions. You can then add an out->addString ("Script", scriptChar);. or similar.

You can then ask the file stream to serialize / deserialize the Lua script.

It's important to not recode everything when you run into a problem.
Post Reply