Computer Science Canada

File Parser that uses <tags>

Author:  ericfourfour [ Mon Sep 04, 2006 12:50 am ]
Post subject:  File Parser that uses <tags>

This program is a file parser. It is very usefull for practically every type of program. I would recommend you learning pointers and variable types before using this. What it allows you to do is load a file and then parse tags from it.

For example:

If you wanted to parse the tag <cash>, you would simply call the function parse_tag with "cash" as the first parameter and it would return the value in between <cash> and </cash> as a variable type called str_array which is just a record that contains an array of strings and the upper bound.

So now your function call looks like this:

code:

parse_tag ("cash",


What if you have more than one tag in the file named "cash"? Well, the second parameter in the parse_tag function is the tag number. If you want the second "cash" then you would use 2 as the tag number. If you wanted the first, third or even tength tag you would use 1, 3 or 10. For this example we will use the first.

You function call now looks like this:

code:

parse_tag ("cash", 1,


The third parameter can remain nil for now because that required some more explaining.

So you are left with:

code:

parse_tag ("cash", 1, nil)


In a simple program you must first initialize the parser and load the file.

To do this you would call the procedures:

code:

initialize ()
load_file (file_name : string)


There is one final thing to remember. The parser is a class so you need to have a pointer to it to use it.

A simple program that parses this file,

code:

testfile.txt

<name> ericfourofur </name>
<age> 16 </age>


and displays the name and age would look like this:

code:

import FileParser
var parser : ^FileParser := nil
new FileParser, parser

parser -> initialize ()

if not parser -> load_file ("testfile.txt") then
    %There was an error loading the file
end if

var name := parser -> parse_tag ("name", 1, nil)
if name = nil then
    %There was an error parsing the name
end if

var age := parser -> parse_tag ("age", 1, nil)
if age = nil then
    %There was an error parsing the name
end if

for i : 0 .. name -> bound
    put name -> str (i)
end for

for i : 0 .. age -> bound
    put age -> str (i)
end for


The code is over commented so it should be easy to understand. There is also a program named File Parser Test that shows all of the features of the parser and it is over commented aswell.

Edit 1: spelling mistake
Edit 2: added documentation
Edit 3: fixed a mistake in FileParser (9 characters of one line were causing me a lot of grief Mad)

Author:  NikG [ Mon Sep 04, 2006 2:50 am ]
Post subject: 

Nice idea!

It looks like it could be easily implemented in other people's programs so that they could use this to load settings or information like that for their own progs (I'm guessing that's the point of xml?).

Author:  bruized [ Thu Nov 02, 2006 6:49 pm ]
Post subject: 

It may be a little late (it's never too late to learn Very Happy ) but I was just wondering what a "parser" was.

Author:  [Gandalf] [ Fri Nov 03, 2006 4:11 am ]
Post subject: 

Learn to use Wikipedia. Smile

This is actually quite a useful program, good job ericfourfour.

Author:  ericfourfour [ Fri Nov 03, 2006 8:20 am ]
Post subject: 

Quote:
This is actually quite a useful program
That's my goal for most of the things I submit.


: