GP32 Gp32 Xml Library


pea

developer
Joined
Oct 3, 2004
Messages
1,089
Age
45
Location
New Zealand
Website
www.projectitis.com
Hi all,

I have developed a very simple to use XML parsing library for the GP32 over the last few days. It uses Mr.Mirkos SDK. It is very handy for doing things like loading game data (scores, options, game levels etc).

If anybody is interested, I will release a copy of it for beta testing on my site. I would like to iron out the bugs (if any :) ) before I release it fully.

Supports basic structuring:
Code:
<classroom>
	<blackboard width=21cm height="38 cm">
  This is what is written on the blackboard, and becomes a
  text node which is a child of blackboard. It supports
  any type 'of string' which may contains "<tags>" which
  are ignored because they are in the string.
	</blackboard>
	<desks row='1'>
  <pupil>John</pupil>
  <pupil>Mary</pupil>
  <pupil>Sally</pupil>
	</desks>
	<desks row='2'>
  <pupil>Thomas</pupil>
  <pupil>Henry</pupil>
  <pupil>Jane</pupil>
	</desks>
</classroom>

It currently has functions to:
load and parse from textfile
parse from memory
add attributes to nodes on the fly
add new nodes as siblings or children on the fly

What it doesn't have YET are:
functions to save xml
functions to remove nodes
functions to reorder nodes

What it doesn't have that it will probably never have are:
Support for declaration tags <?something>
Support for DTD tags <!something>
Support for substitution/variables (whatever they are called)
Comments

Simple example:
Code:
#include "gp32.h"
#include "gp_xml.h"

int main() {
	tGP_XMLElement *xml;
	int res, line, col;
	
	// Set CPU speed
	gp_setCpuspeed(66);
	
	// Parse XML file
	xml = gp_xmlLoad("dev0:\\GPMM\\test.xml", &line, &col, &res);
	// res returns non-zero on fail
	// if fail, line and col contain the character in the xml that caused the error

	if (xml){
  // Free XML file
  gp_xmlFree( xml, 1 );
	}
	
	// Reset GP
	gp_Reset();
}

More examples:
Code:
	// Stepping through all sibling nodes
	while(xml){
  sprintf( buffer, "Next sibling node is:%s", xml->name );
  xml = xml->next;
	}
	
	// Stepping through all child nodes of a node
	if (xml->children){
  xml = xml->children;
  while(xml){
  	sprintf( buffer, "Next child node is:%s", xml->name );
  	xml = xml->children;
  }
	}
	
	// Stepping through all attributes of a node
	attribute = xml->attributes;
	while(attribute){
  sprintf( buffer, "Next attribute: %s = %s", attribute->name, attribute->value );
  attribute = attribute->next;
	}
 
Nice work! That would be very useful Pea.

I use a vanilla GPSDK.
How closely is it tied to Mirkos?

Is it just file open, close and read?
 
Excuse my english please :)

I did something like this a year ago :D And it's used by Vorton, Efezero and other Chui's programs for GP32. You can use if you like it. It use LGPL.

ML_Lib by Mortimor

What a pity! you don't put your attention on spanish programmers, we have made a lot of things that you don't know.
 
hehe its true mortimor! I wanted to do it anyway, so no loss. I bet lots of stuff goes by unnoticed from other devvers too.

gp32rich - yes, those are the only functions you will need to replace. As I recall smc_fopen, smc_fread, smc_fwrite and smc_fclose. I may be using a function instead of smc_fread to read the whole file in one go, but should be no problem to convert to official GP32 SDK.

Will upload tomorrow.

EDIT: Beta release here http://www.pea.co.nz/gp32/downloads.php
 
Pea,

Thanks for this little lib. I may look at using a GPSDK version in place of my own much hackier version ;).

Any problems with it being distributed with LGPL or GPL apps? If you do I will clean up my own.

Mortimor,

I'll go a take a peek at your lib, LGPL so that's nice :D.

An XML lib should make config file managment a lot neater :). Needs read and write for all nodes though ;).
 
mortimor posted on Mar 24 2005 at 10:23 AM said:
What a pity! you don't put your attention on spanish programmers, we have made a lot of things that you don't know.
Yes! Sadly it's true that most don't look outside the english based sites. :unsure:

But that's our loss. ;) :rolleyes:

Thanks for bringing your lib to our attention.
And thanks Pea for sharing yours too.
 
Last edited by a moderator:
mortimor posted on Mar 24 2005 at 11:23 AM said:
What a pity! you don't put your attention on spanish programmers, we have made a lot of things that you don't know.

That's not the case for all of us ;). The GP32 is the reason I drag my very bad Spanish out every so often.
 
Last edited by a moderator:
There are some very portable XML libs around now (ie: you needn't remake everything for GP32 just because you can ;)

ie: The canonicle XML parse lib thats used in most places is expat:

http://expat.sourceforge.net/

Its fairly portable; I built it for GP32 a couple years ago without a problem.

jeff

(By all means, keep devving your own of course, but using this sort of thing gets you Unicode etcetc with no work :)
 
Yes, I had a look for some around before I started - though what made me make my own anyway was because I had developed one a few years back for Delphi (Object Pascal), so I already had the basic parsing done.

I also wanted a very simple version, with nothing that wasn't needed (except to make it easier to use). Since I am developing it mainly for myself, it doesn't need unicode support, or those extra XML tags and styleSheets etc :)
 
yeah, I know the feeling; I whipped up a 30 line parser for perl a couple years back.. just needed some simple layout thsat is "more or less xml"... Just thought I'd point it out :)

jeff
 
gp32rich posted on Mar 24 2005 at 04:52 PM said:
mortimor posted on Mar 24 2005 at 10:23 AM said:
What a pity! you don't put your attention on spanish programmers, we have made a lot of things that you don't know.
Yes! Sadly it's true that most don't look outside the english based sites. :unsure:

But that's our loss. ;) :rolleyes:

Thanks for bringing your lib to our attention.
And thanks Pea for sharing yours too.

I don't go to Spanish sites because I can't read it, I am sure that is the same for many. Someone who speaks both though could keep us posted here though :)
 
Last edited by a moderator:
DJWillis posted on Mar 24 2005 at 04:01 PM said:
[...]

Mortimor at

I'll go a take a peek at your lib at LGPL so that's nice :D.

An XML lib should make config file managment a lot neater :). Needs read and write for all nodes though ;).

For my config files I use my RIFF lib that is a personal implementation of the RIFF format file. With this lib I can store score tables, options and whatever I want without problems. I can even upgrade game options or some table scores automatically, even if new options have appeared.
The lib is very small, very easy to use and GPSDK compatible.
But if you need a file format to store a lot of predefined values (not generated by the program but by some other program or person), XML is a more versatile solution.

Oankali.
 
Last edited by a moderator:
Sorry, forgot to add that the XML lib now has node deletion and saving support. Most functions work based on a certain node. For example, the save function requires a node as its input, if this is the root node, the whole XML tree is saved - if this is a node further down the tree, only that branch is saved with the input node as the root.
 
Back
Top