Configuration File

J

Jay

I want to store various text settings in a configuration file, that I will read into C# and make use
of. Here's a simplified example of the file:

#comment
loop(var=1:5){
deviceundertest freq:5.6e6 power:3.7
analyser freq:5.6e6 span:1e3 resbw:100 atten:20
}

As you can see, the configuration file will define:
- comments, which start with the # character
- looping - the loop line effectively means for(int var=1; var<=5; var++){ }
- equpment settings (eg analyser is the name of some equipments, and what follows are parameters
that are sent to the equipment)

There is no requirement to write this file from C# - it will be written and edited in (eg) Notepad.
Is there any support within C# and .net for this type of file, or should I simply read it as a text
file and parse it with my own code? I thought I'd ask the question before I start to write the
parsing code.

Thank everyone...
 
D

DeveloperX

No direct support, all the configuration stuff in dotnet relies on XML.
You could rewrite it as xml simply enough and use the inbuilt support.
You would still have to write all the logic to process the loops and
would still be subject to typo's, so your idea of just reading the file
and processing it from your own format seems sound.

If you seperate the logic for reading the file from the logic of
processing the data within you will find it easier to replace the
format later if necessary. Do so via an interface you could support a
number of formats.

As for the processing, you've got regex's but in this case I'd just
process it with string methods.
 
K

Kevin Spencer

As long as you are using a proprietary configuration file format, you'll
have to write a proprietary parser for it.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

In case of Minimalism, break Philip Glass.
 
J

Jay

Thanks for your replies Kevin and DeveloperX.

I don't have a reason in principle for not using XML, but I didn't think I'd be able to code the
loop in XML. Some of the loops will be nested too.

loop(var=1:5){
deviceundertest freq:5.6e6 power:3.7
loop(%f=2e3, 3e3, 4e3, 5e3){
analyser freq:%f span:1e3 resbw:100 atten:20
}
}

The file format is to be developed by me, so I can completely change it if necessary.

I know very little about XML apart from seeing a few simple examples ( eg, a car, model,
manufacturer example).

If someone could show me an example of coding the loop, that would be very helpful.

Jay


As long as you are using a proprietary configuration file format, you'll
have to write a proprietary parser for it.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

In case of Minimalism, break Philip Glass.
 
D

DeveloperX

I knocked up something that should work, but I can't get it to you
until Monday (and there's a bit that doesn't work I need to tweak (I
don't code much xml tbh)). If you drop me a note to n n t p d e v at o
p e r a m a i l dot c o m (don't know why I did that it's a spam
account anyway...) I'll email it over on Monday. It's not big or
clever, just takes an xml file that conforms to your requirements (I
believe) and turns it into a set of tests to be executed. It's no more
than a hundred lines of code. Only thing I need to know is what's
supposed to happen in your nested loop, should the inner loop be run 5
times with the same values, or does the outer loop affect the inner
loops variables, or option c?
 
J

Jay

Thanks very much DeveloperX for putting this together for me - I will email my email address
directly to you shortly.

My example wasn't that great, so let me improve it to illustrate how the loops work. Also, I going
to assume that all loop variables are doubles, not ints as before. As you can see, my ideas for the
configuration file are still being developed! Sorry for the lack of clarity in my last posting.

- comments start with #
- loop variables start with % to make it easier for the parser to spot them

loop(%p=1:15:2){ #means start:stop:step
deviceundertest freq:5.6e6 power:%p #use outer loop variable (%p)
loop(%f=2e3, 3e3, 4.3e3, 9.1e4){
analyser freq:%f span:1e3 reflev=%p resbw:100 atten:20 #use both outer and inner loop
variables (%p and %f)
}
}


The loops are equivalent to this C# code (I'm a C-programmer new to C# so it may have syntax
errors...

delta=1e-6;
double[] fArray = {2.0e3, 3.0e3, 4.3e3, 9.1e4}
for(double p=1.0; p<=15.0+delta; p+=2.0){ //delta prevents rounding problems
//make use of outer loop variable (p) in the settings for "deviceundertest"
...
foreach(double f in fArray){
//make use of outer and inner loop variables (p and f) in the settings for "analyser"
...
}
}


Thanks again for all your help...

Jay


I knocked up something that should work, but I can't get it to you
until Monday (and there's a bit that doesn't work I need to tweak (I
don't code much xml tbh)). If you drop me a note to n n t p d e v at o
p e r a m a i l dot c o m (don't know why I did that it's a spam
account anyway...) I'll email it over on Monday. It's not big or
clever, just takes an xml file that conforms to your requirements (I
believe) and turns it into a set of tests to be executed. It's no more
than a hundred lines of code. Only thing I need to know is what's
supposed to happen in your nested loop, should the inner loop be run 5
times with the same values, or does the outer loop affect the inner
loops variables, or option c?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top