• Advertisement
Sign in to follow this  
  • entries
    177
  • comments
    531
  • views
    163906

Rise of the Parser

Sign in to follow this  

95 views

I got bored working on the Z80 stuff, so I whipped up this Anim8or file parser. It's a lot simpler then I thought it would be, but I don't know how to through all the elements. Each element can also contain other elements, so I end up having to go through a lot of lists via a crapload of loops and it just looks so wrong. Can anyone enlighten me as to a better method to go through the data? See the Main function for how I'm currently outputting it.

The file:
A simple cube.

header {
version { "0.9" }
build { "2005.08.29" }
}
environment {
grid { 1 10 50 50 }
framerate { 24 }
}
object { "object01"
mesh {
name { "mesh01" }
material { " -- default --" }
smoothangle { 45 }
/* 8 points, 6 faces, 8 uvCoords */
materiallist {
materialname { " -- default --" }
}
points {
(-32 -32 -32) (-32 -32 32) (-32 32 -32) (-32 32 32) (32 -32 -32)
(32 -32 32) (32 32 -32) (32 32 32)
}
texcoords {
(0 0) (0 0) (0 1) (0 1) (1 0) (1 0) (1 1) (1 1)
}
faces {
4 4 0 -1 ( (0 0) (4 4) (6 6) (2 2) )
4 4 0 -1 ( (1 1) (3 3) (7 7) (5 5) )
4 4 0 -1 ( (0 0) (2 2) (3 3) (1 1) )
4 4 0 -1 ( (4 4) (5 5) (7 7) (6 6) )
4 4 0 -1 ( (2 2) (6 6) (7 7) (3 3) )
4 4 0 -1 ( (0 0) (1 1) (5 5) (4 4) )
}
}
}





The codes:
Anyway to eliminate the Base class, but not stick ReadString in both File and Element?

using System;
using Collections = System.Collections.Generic;
using IO = System.IO;

namespace Anim8or
{

public class Base
{

public Base()
{
return;
}

protected static string ReadString( IO.StreamReader StreamReader )
{
bool Quotation = false;
string Token = "";
char Character = ' ';
while( ( Token == "" ) && ( StreamReader.EndOfStream == false ) )
{
while( true )
{
Character = (char)StreamReader.Read();
if( Character == '\"' )
{
Quotation = !Quotation;
}
else if( ( Character == '\n' ) || ( Character == '\r' ) )
{
break;
}
else if( ( Character == ' ' ) && ( Quotation == false ) )
{
break;
}
else
{
Token += Character;
}
}
}
return Token;
}

}

public class Element : Base
{

public string Name = "";

public Collections.List AttributeList = new Collections.List();

public Collections.List ElementList = new Collections.List();

public Element Parent = null;

public Element( string NewName, Element NewParent, IO.StreamReader StreamReader ) : base()
{
Name = NewName;
Parent = NewParent;
string Item = "";
Collections.List StringList = new Collections.List();
while( StreamReader.EndOfStream == false )
{
Item = ReadString( StreamReader );
if( Item == "{" )
{
for( int i = 0; i < StringList.Count - 1; i++ )
{
AttributeList.Add( StringList );
}
ElementList.Add( new Element( StringList[StringList.Count-1], this, StreamReader ) );
StringList.Clear();
}
else if( Item == "}" )
{
for( int i = 0; i < StringList.Count; i++ )
{
AttributeList.Add( StringList );
}
StringList.Clear();
return;
}
else
{
StringList.Add( Item );
}
}
return;
}

}

public class File : Base
{

public Element RootElement = null;

private IO.StreamReader StreamReader = null;

public File( string FileName ) : base()
{
StreamReader = new IO.StreamReader( FileName );
RootElement = new Element( "root", null, StreamReader );
StreamReader.Close();
return;
}

}


class MainClass
{

public static void Main( string[] Arguments )
{
Console.Title = "Anim8or Parser";
File File = new File( "Cube.an8" );
for( int i = 0; i < File.RootElement.ElementList.Count; i++ )
{
Console.WriteLine( File.RootElement.ElementList.Name );
if( File.RootElement.ElementList.AttributeList.Count > 0 )
{
Console.Write( "\t" );
for( int n = 0; n < File.RootElement.ElementList.AttributeList.Count; n++ )
{
Console.Write( "{0} ", File.RootElement.ElementList.AttributeList[n] );
}
Console.Write( "\n" );
}
for( int j = 0; j < File.RootElement.ElementList.ElementList.Count; j++ )
{
Console.WriteLine( "\t{0}", File.RootElement.ElementList.ElementList[j].Name );
if( File.RootElement.ElementList.ElementList[j].AttributeList.Count > 0 )
{
Console.Write( "\t\t" );
for( int n = 0; n < File.RootElement.ElementList.ElementList[j].AttributeList.Count; n++ )
{
Console.Write( "{0} ", File.RootElement.ElementList.ElementList[j].AttributeList[n] );
}
Console.Write( "\n" );
}
for( int k = 0; k < File.RootElement.ElementList.ElementList[j].ElementList.Count; k++ )
{
Console.WriteLine( "\t\t{0}", File.RootElement.ElementList.ElementList[j].ElementList[k].Name );
if( File.RootElement.ElementList.ElementList[j].ElementList[k].AttributeList.Count > 0 )
{
Console.Write( "\t\t\t" );
for( int n = 0; n < File.RootElement.ElementList.ElementList[j].ElementList[k].AttributeList.Count; n++ )
{
Console.Write( "{0} ", File.RootElement.ElementList.ElementList[j].ElementList[k].AttributeList[n] );
}
Console.Write( "\n" );
}
}
}

}
Console.ReadLine();
}

}

}






The result:
Tabs are used to represent what element the other elements/data belong to.


I'm hoping to turn it into a .an8 viewer with .an8 -> .x conversion.

Edit: It would appear that the C style comment in the file totally fucked the materiallist element. I shall have to fix this.

Edit 2: Nevermind, seems I needed another layer of looping to show what materiallist contained.
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement