I'm trying to create a GUI in C# (Windows Form Application, Visual Studio) that reads and writes certain variables from/to a text file. I have no problem with the GUI itself, but I need a function that can read from (and eventually write to) the text file, recognize the variables and put them in internal variables or directly into the GUI controls. The text file looks something like this (it's much longer though):
/* Control */
/YD/YDC/MCSTEP 20001 /YD/YDC/NCSTEP 0
/YD/YDC/DCSIZC 0.075
/* Properties */
/YD/YDPE/D1PEFR 3
0.7 0.75 .5
/YD/YDPM/I2PMSET 21 3 2
1 3 0
1 0 1
/YD/YDPN/D3PNFAC 231 2 3 4
0 0
1e-010 0
9.2966e-008 0
1.8583e-007 0
0 0
1e-010 0
9.2966e-008 0.71221
1.8583e-007 1.4688
0 0
1e-010 0
9.2966e-008 0
1.8583e-007 0
I have a code in C that reads the text file, but it's horribly long. I have the impression that this can be done elegantly with a relatively short code in C#. I have tried using a dictionary as suggested here, but the format of my text file is too complex for it... unless some guru out there knows some tricks that can help me.
Your suggestions are welcome. Sample code would be greatly appreciated.
The following defines the format of the text file:
- All variables begin with
/YD/
. - Text between
/*
and*/
are comments (no need to read). - Separators can be spaces or tab characters and you can have one or many of them between variables and values. You can also have empty lines.
- Sometimes there is more than one variable per line.
- Sometimes a variable and its value are in different lines.
- Some variables are single values, others are 1-D, 2-D or 3-D.
- Variables with the form xxxxxx (not necessarily fixed number of characters) are followed by its single value.
- Variables with the form x1xxxxx are 1-D. These are followed by an
integer number which tells you the size of the variable, followed by
the values of the variable (e.g. the variable
/YD/YDPE/D1PEFR 3 0.7 0.75 .5
is a 1-D variable, with three values: 0.7, 0.75 and 0.5). - Variables with the form x2xxxxx are 2-D. These are followed first by a number that tells you how to read the variable, then the size of the variable (2 numbers), followed by the actual values of the variable. E.g. the variable
/YD/YDPM/I2PMSET 21 3 2 1 3 0 1 0 1
is a 2-D variable, where the number 21 means that first you read changing the "x" coord then the "y" coord (a 12 would mean you read first changing "y" then "x"), following numbers3
and2
mean the size is [3,2], and the following 6 numbers are the values of the variable in order(x1y1, x2y1, x3y1, x1y2, x2y2, x3y2)
. - Finally, variables of the form x3xxxx are 3-D. These are followed by a number that tells you how to read it, then the size (3 numbers) and then the values. E.g. variable
/YD/YDPN/D3PNFAC 231 2 3 4 ...
is 3-D, first you read "x", then "z", then "y", and the size is [2,3,4]. The values are then read in order(x1y1z1, x2y1z1, x1y1z2, x2y1z2, x1y1z3, x2y1z3, x1y1z4, x2y1z4, x1y2z1, x2y2z1, ...)
.