configuration file

General FreeBASIC programming questions.
blahboybang
Posts: 385
Joined: Oct 16, 2005 0:15
Location: USA
Contact:

configuration file

Postby blahboybang » May 14, 2006 0:18

Could someone give me some tips on how to make a program that reads configurations from a text file? I'm not sure where to start with that.
uziq
Posts: 41
Joined: Mar 27, 2006 17:56
Location: USA
Contact:

Postby uziq » May 14, 2006 1:14

If you're targetting Windows, then you can use GetPrivateProfileString along with ini files.

This Dialog Example shows how.
blahboybang
Posts: 385
Joined: Oct 16, 2005 0:15
Location: USA
Contact:

Postby blahboybang » May 14, 2006 1:19

no, I need to make my own protocol, one that will work on win or lin.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » May 14, 2006 2:50

try xml and libxml? or if you want a good time, write your own xml funcs ;p
blahboybang
Posts: 385
Joined: Oct 16, 2005 0:15
Location: USA
Contact:

Postby blahboybang » May 14, 2006 2:52

I just want a way to read configurations from an ordinary text file with no extra libs. Maybe I'll ask Zire. She's done it.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » May 14, 2006 2:56

ok then
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » May 14, 2006 3:34

I would recommend using XML. There also exist libraries with a nice interface for reading .INI files as well.

Without external libs, a quick and dirty method would be to simply comma-seperate a list of variable names and their values. Your library might use a UDT containing both of those strings, and have clients access those vars through simple Get/Set functions.

The client would call something like RegisterConfigVar("screenresx","640"), and the library would make a new UDT for "screenresx" and place it in a list or array. Clients would then call GetConfigVarValue("screenresx") to access the var's value. Alternatively, RegisterConfigVar might return an integer handle to the new var for clients to use for efficiency.

Either way, you're encapsulating the idea of a configuration variable, it's creation and it's access.
blahboybang
Posts: 385
Joined: Oct 16, 2005 0:15
Location: USA
Contact:

Postby blahboybang » May 14, 2006 3:51

I don't want a bulky library, though. Api's frustrate me. :*(
blahboybang
Posts: 385
Joined: Oct 16, 2005 0:15
Location: USA
Contact:

Postby blahboybang » May 14, 2006 4:01

Look, it's not impossible. Do you think the xml libs just poofed out of thin air? Someone had to write them, and they did it without any bulky libraries.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » May 14, 2006 4:12

whoa. that tripped me out right there
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » May 14, 2006 5:49

blahboybang wrote:I don't want a bulky library, though.

You don't need one, especially if you're developing your own config file format. For simple config variables the format - and therefore the parser - can be just as simple. Depending on how you want to use these variables, you may need a more sophisticated manager, but the actual parsing of the config file need not be complex at all.
JohnB
Posts: 236
Joined: Jul 22, 2005 3:53
Location: Minnesota Arizona

Postby JohnB » May 14, 2006 7:59

You might checkout Imortis Inglorian Text Parser Tutorials in QB Express,
QBasic / QuickBasic / FreeBasic Magazine. I used a modified version to parse .OBJ graphic files. You could define and control your own format for the ini file. If I remember correctly the base parser code was less than 100 lines.

JohnB
Dirk_Fist
Posts: 82
Joined: Mar 07, 2006 11:28

Postby Dirk_Fist » May 14, 2006 19:22

Code: Select all

'read file into string
open "my.ini" for input as 1: file$=input$(LOF(1),1): close 1
 
'parse string
's = start of segment
'e = end of segment
s=0: e=instr(file$,chr$(13))

number1 = val(mid$(file$,s,e-s)):  s=e+1: e=instr(s,file$,chr$(13))

number2 = val(mid$(file$,s,e-s)):  s=e+1: e=instr(s,file$,chr$(13))

number3 = val(mid$(file$,s,e-s))


you can use any character as a seperator
string value's don't need val()
if you NEED a more flexible setup
you can use KEY VALUE pairs

Code: Select all

'read file into string
open "my.ini" for input as 1: file$=input$(LOF(1),1): close 1

'find KEY
s=instr(file$,key$)

if s=0 then
  'key not found set default value or flag error
  errout("Required value "+key$+" not specified in config")
end if
'find end of line
e=instr(s,file$+chr$(13),chr$(13)): if e=0 then e=len(file$)
s+=len( key$)

Value = mid$(file$,s,s-e)  ' use appropriate conversion here
Last edited by Dirk_Fist on May 16, 2006 13:00, edited 1 time in total.
blahboybang
Posts: 385
Joined: Oct 16, 2005 0:15
Location: USA
Contact:

Postby blahboybang » May 14, 2006 23:12

JohnB wrote:You might checkout Imortis Inglorian Text Parser Tutorials in QB Express


Awesome, that's prolly what I'm looking for.
JohnB
Posts: 236
Joined: Jul 22, 2005 3:53
Location: Minnesota Arizona

Postby JohnB » May 15, 2006 6:07

My parser code is here. Look at Function Parse(ToParse as String). I had to slightly modify Imortis's code.

http://www.freebasic.net/forum/viewtopic.php?t=2265&highlight=

JohnB

Return to “General”

Who is online

Users browsing this forum: No registered users and 7 guests