Page 1 of 1

[Feature Request] lib/mods/xml.lua - Write to XML files.

Posted: Thu Mar 26, 2009 5:53 am
by zer0
Elv I have found that your XML library module is invaluable. Nearly all my configuration data now exists in XML files.

There is one thing that I would like it to do though. To easily be able to write to the files as well with set (mutator) functions.

Reason I would like this is I think I have figured out how the findPatternInProcess function works from reading your posts on 1.0, and I would like to have the memory offsets once located be saved in a XML config file.
I know that you can save them in a lua file easily like you have done with the ROM bot.

But I do see a need for being able to write to the XML files as well.

Or is it relatively simple to write XML files with the LuaExpat library and no wrapper functions need to be created?

Re: [Feature Request] lib/mods/xml.lua - Write to XML files.

Posted: Thu Mar 26, 2009 2:55 pm
by Administrator
It's something I've thought about before, but there are a few problems with it. First and foremost is with the parser. In some instances, it strips certain characters or groups of characters. In some cases, it would seem to be a feature, in others a bug. It's hard to say.

Code: Select all

<someElement>
    this is the value.
</someElement>
In this example, the someElement's value would have it's preceding and trailing newline characters stripped. This would seem to be a feature, and just a minor annoyance when you want to keep them.

Code: Select all

<someElement>
    --[[ This is a comment ]]

    index = 0;
</someElement>
In this example, everything gets screwed up. This has serious problems with placing Lua code inside XML tags. For whatever reason, it sometimes decides to strip the ']]' from lines, and other times it leaves them. It's a guess and check kind of game.

Plus, there's also a problem with casting now. Values supplied as hex are converted to decimal on parse, which means they will be saved as decimal.


That being said, here's a work-in-progress:

Code: Select all

-- Write a node to a file
function Node:write(filename)
	local outfile, err = io.open(filename, "w");
	if( outfile == nil ) then
		error(err, 2);
	end

	local level = 0;

	local function write_subnodes(node)
		--node:debug();

		if( level > 0 ) then
			outfile:write("\n");
		end

		outfile:write(string.rep("\t", level) .. "<" .. node._NAME);
		local i = 1;
		while(true) do
			local name = node._ATTRIBUTES[i];
			if( name == nil ) then
				break;
			end;

			local value = node:getAttribute(name);
			i = i + 1;

			outfile:write(" " .. name .. "=\"" .. value .. "\"");
		end

		local elements = node:getElements();

		if( node._VALUE or #elements > 0 ) then
			outfile:write(">");
		else
			outfile:write(" />");
		end


		if( node._VALUE ) then
			outfile:write(node._VALUE);
		end

		level = level + 1;
		for i,v in pairs(elements) do
			write_subnodes(v);
		end
		level = level - 1;

		if( node._VALUE or #elements > 0 ) then
			if( #elements > 1 or (node._VALUE and string.find(node._VALUE, "\n")) ) then
				outfile:write("\n" .. string.rep("\t", level) .. "</" .. node._NAME .. ">");
			else
				outfile:write("</" .. node._NAME .. ">");
			end

			if( level > 0 ) then
				outfile:write("\n");
			end
		end
	end

	write_subnodes(self);

end
Just call write on your root node, and it should work.