• entries
    743
  • comments
    1924
  • views
    578722

Map.NET

Sign in to follow this  
Aardvajk

79 views

I've decided to yet again try to rewrite my level editor in C# and Windows Forms on the basis that I have to get my head round it some time and my C++ level editor is getting a bit hard to maintain.

So, behold - Map.NET...



Early days yet, but it is going a lot better than the last attempt.

I can't quite work out whether I love or hate C#. It is definately one or the other.

This application needs to be able to parse text files that can contain quoted strings. I've managed to bodge together a file parser class that can handle this, but since it appears that StreamReader does not have the equivalent to a std::istream::putback function, I'm having to Peek() every character before I Read() it, which seems a bit strange:


using System;
using System.IO;
using System.Text;

class FileParser
{
private StreamReader Sr=null;

public FileParser(string Path)
{
try
{
Sr=new StreamReader(Path);
}
catch(Exception)
{
Sr=null;
}
}

public bool Failed { get { return Sr==null; } }

public bool Next(out String Token)
{
StringBuilder S=new StringBuilder();

Int32 C=Sr.Peek(); while(C!=-1 && Char.IsWhiteSpace(Convert.ToChar(C))){ Sr.Read(); C=Sr.Peek(); }

if(C==-1){ Token=""; return false; }

if(C=='\"')
{
Sr.Read(); C=Sr.Peek();
while(C!=-1 && C!='\n' && C!='\"')
{
S.Append(Convert.ToChar(C)); Sr.Read(); C=Sr.Peek();
}

Sr.Read(); Token=S.ToString(); return true;
}

while(C!=-1 && C!='\"' && C!=':' && !Char.IsWhiteSpace(Convert.ToChar(C))){ S.Append(Convert.ToChar(C)); Sr.Read(); C=Sr.Peek(); }
Token=S.ToString(); return true;
}

public void Dispose()
{
if(Sr!=null) Sr.Close();
}
}



I welcome comments on the above, but be gentle since C# is all a bit new to me.
Sign in to follow this  


3 Comments


Recommended Comments

If you want to use it, I created this StringStream class a while ago to deal with text files. You should try to figure it out before using it though, as you'll want to have it deal with whatever tokens you're using. Also it's rather slow and inefficient, I'm sure there's areas where it could be improved.


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

namespace FireBird
{

public class StringStream
{

public string CurrentString
{
get
{
if( ( Position >= StringList.Count ) || ( Position < 0 ) )
{
return "";
}
return StringList[Position];
}
}

public int Length
{
get
{
return StringList.Count;
}
}

private int Position = 0;

private Collections.List<string> StringList = new Collections.List<string>();

public StringStream( IO.StreamReader StreamReader )
{
StreamReader.BaseStream.Position = 0;
while( true )
{
string Line = StreamReader.ReadLine();
if( Line != null )
{
string[] StringArray = Line.Split( ' ', '\t' );
for( int i = 0; i < StringArray.Length; i++ )
{
if( ( StringArray[i].Length > 0 ) && ( StringArray[i] != " " ) )
{
StringList.Add( StringArray[i].Trim( ' ', '\t' ) );
}
StringList.Add( "\n" );
}
}
else
{
break;
}
}
return;
}

public string LookBackward()
{
if( Position - 1 < 0 )
{
return "";
}
return StringList[Position-1];
}

public string LookBackward( int Amount )
{
if( Position - Amount < 0 )
{
return "";
}
return StringList[Position-Amount];
}

public string LookForward()
{
if( Position + 1 >= StringList.Count )
{
return "";
}
return StringList[Position+1];
}

public string LookForward( int Amount )
{
if( Position + Amount >= StringList.Count )
{
return "";
}
return StringList[Position+Amount];
}

public string MoveBackward()
{
Position -= 1;
return CurrentString;
}

public string MoveBackward( int Amount )
{
Position -= Amount;
return CurrentString;
}

public string MoveForward()
{
Position += 1;
return CurrentString;
}

public string MoveForward( int Amount )
{
Position += Amount;
return CurrentString;
}

public string MoveTo( string Value )
{
while( ( CurrentString != "" ) && ( CurrentString != Value ) )
{
MoveForward();
}
return CurrentString;
}

public float ReadFloat()
{
float Value = 0.0f;
float.TryParse( MoveForward(), out Value );
return Value;
}

public void ReadFloat( ref float Value )
{
float.TryParse( MoveForward(), out Value );
return;
}

public int ReadInt()
{
int Value = 0;
int.TryParse( MoveForward(), out Value );
return Value;
}

public void ReadInt( ref int Value )
{
int.TryParse( MoveForward(), out Value );
return;
}

public void ReadLine()
{
while( ( CurrentString != "" ) && ( CurrentString != "\n" ) )
{
MoveForward();
}
return;
}

public string ReadQuotation()
{
if( MoveForward() != "\"" )
{
return CurrentString;
}

Share this comment


Link to comment
Scet - thanks. I'll have a proper look at that when I get a chance.

Superpig - sounds like a pretty good idea. Cheers.

Share this comment


Link to comment

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