المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Best way to read ASCII input [modified]



C# Programming
02-24-2010, 04:21 AM
I am porting a Fortran numerical modeling code (console application) to C#. I am new to C#, so I mostly convert Fortran subroutines into C# class methods. My code reads ASCII input to obtain the modeling parameters. The basic method I use to read the input has not changed in 12 years, and I always felt that there should be a better way to do it. Now that I am moving to a new ********, would be a good time to learn that better way. In my reading I have seen references to parse trees and other concepts I am not familiar with. I would like to get a few suggestions or references to articles or books that might help me.

Below I show example of my input and code. The basic idea is to read a file line by line, breaking the line into tokens. those tokens are in string[] input. The read method in my InputData class looks for keywords, instantiating objects as appropriate. Each object has its own Read method. Each class with a Read method inherits from the Reader class.

This is a simplified input file. I will probably consider moving to XML input in the future, but right now I have to support this format.


FORMATION
UNITS METERS
TOP 1055END
Simplified code:

public class Reader
{
protected string readMode = "0";
public virtual void Read(string[] input) { }
}


public class InputData : Reader
{
public Formation formation;

public override void Read(string[] input)
{
if (input.Length == 0) return;

string firstword = input[0];
if (firstword.CompareTo("FORMATIOM") == 0)
{
formation = new Formation();
readMode = "FORMATION";
}
else if (readMode == "FORMATION")
{
formation.Read(input);
}
}
}

public class Formation : Reader
{

protected string depthUnits; // units used for depth measurements
protected double topBoundaryDepth;

public override void Read(string[] input)
{
string firstword = input[0];

if (firstword.CompareTo("UNITS") == 0)
{
depthUnits = input[1];
readMode = "0";
}
else if (firstword.CompareTo("TOP") == 0)
{
topBoundaryDepth = Convert.ToDouble(input[1]);
readMode = "0";
}
}
}modified on Monday, February 22, 2010 2:19 PM