mod XML lua

Discuss, ask for help, share ideas, give suggestions, read tutorials, and tell us about bugs you have found with MicroMacro in here.

Do not post RoM-Bot stuff here. There is a subforum for that.
Forum rules
This is a sub-forum for things specific to MicroMacro.

This is not the place to ask questions about the RoM bot, which uses MicroMacro. There is a difference.
Post Reply
Message
Author
zer0
Posts: 213
Joined: Sat Feb 16, 2008 11:55 pm

mod XML lua

#1 Post by zer0 » Mon Nov 10, 2008 6:07 am

Hey Elverion,

I've been using the mod XML Parser as u know, I ran into a parser error, which had me stumped for a bit, so I decided to add error handling for the parse() call in the open function, here it is:

Code: Select all

function open(filename)
	callbacks = {
		StartElement = startElement,
		EndElement = endElement,
		CharacterData = characterData,
		_nonstrict = true,
		stack = {{}}}

	local p = lxp.new(callbacks);
	local file = io.open(filename);

	if( file == nil ) then
		local err = string.format("Cannot open file \'%s\' for reading.", filename);
		error(err, 0);
	end

        local parse = {}
	for l in file:lines() do
        parse.result, parse.msg, parse.line, parse.col, parse.pos = p:parse(l)
        if (parse.result) then
        else
            error("XML Parse Error." .. 
                    "\nfile: " .. filename ..
                    "\nline: " .. parse.line ..
                    "\ncolumn: " .. parse.col ..
                    "\nabsolute postion: " .. parse.pos ..
                    "\nmessage: " .. parse.msg)            
        end
		p:parse("\n");
	end
	p:parse();
	p:close();
	file:close();

	return callbacks.stack[1][1];
end
If you could add this to the XML file that would be cool. ;)

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: mod XML lua

#2 Post by Administrator » Mon Nov 10, 2008 2:27 pm

I've saved this for now, but won't be able to test it. Kind of surprised that I never thought of adding that in while I was writing the function. Well, thanks for the addition. I might change it a bit so that the error message follows the same format as other error messages, but it will definitely be added.

zer0
Posts: 213
Joined: Sat Feb 16, 2008 11:55 pm

Re: mod XML lua

#3 Post by zer0 » Tue Nov 11, 2008 12:07 am

It was only because I had modified the XML and broken it, that's why I added it.

It gave me a chance to look at the code a bit, so I'm more than happy to help. Over the holiday season, I might start taking a look at the Micromacro source, and familiarising myself with the code base.

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: mod XML lua

#4 Post by Administrator » Tue Nov 11, 2008 2:04 am

I'm actually trying to finalize the changes for 0.99 right now. I've already begun re-writing the whole thing for 1.0. So far, things are going well. Unless I come across some problems, the code for 1.0 should be much cleaner and easier to maintain. As things are now, it's a mess.

zer0
Posts: 213
Joined: Sat Feb 16, 2008 11:55 pm

Re: mod XML lua

#5 Post by zer0 » Tue Nov 11, 2008 6:48 am

Yeah I understand, ZS Shaiya is up to about 3500 lines of code now, and there is quite a bit of cleaning up, error handling and optimisation to do (it's all a learning experience for me).

Hey I found that I need to parse XML data and convert a value or an attribute from a string to a number or boolean data type.
There is both an implicit version, and an explicit one.
You may find it useful, maybe a way to incorporate it into the XML mod, I dunno, anyway here they are.

Code: Select all

-- explicit string conversion to another basic data type.
-- currently only number and boolean types supported.
-- @param data The data you want to convert.
-- @param data_type The basic data type you want to change it too.
-- @return The converted data
function stringConvert(data, data_type)
    converts = {}
    converts["boolean"] = function()
        if (data == "true") then
            return true
        elseif (data == "false") then
            return false
        else
            return nil
        end
    end
    converts["number"] = function()
        print("data: " .. data)
        local l_res = string.find(data, "[^%.?0-9+]")
        if (l_res == nil) then
            print("number")
            return tonumber(data)
        else
            print("not number")
            return nil
        end    
    end
    print("data: " .. data)
    print("data_type: " .. data_type)
    return converts[data_type]()
end

-- implicit string conversion to another basic data type.
-- currently only number and boolean types supported.
-- @param data The data you want to convert.
-- @return The converted data.
function string_convert(data)
    print("data: " .. data)
    if (data == "true") then
        return true
    elseif (data == "false") then
        return false
    elseif(string.find(data, "[^%.?0-9+]") == nil) then
        return tonumber(data)
    else
        return data
    end
end

User avatar
Administrator
Site Admin
Posts: 5307
Joined: Sat Jan 05, 2008 4:21 pm

Re: mod XML lua

#6 Post by Administrator » Tue Nov 11, 2008 6:24 pm

Yeah, I guess that could be useful. I'll go ahead and add a condensed version of that to the XML module.

I've attached the update.
Attachments
xml.lua
(4.89 KiB) Downloaded 120 times

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests