cerating config files?

Just starting out? Need help? Post your questions and find answers here.
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

cerating config files?

Post by wmorton »

Hi all

I have a problem which I presume is simple, but I can't figure it out.

I have a program which reads strings from a text file, in order to determine a person's name, and then it uses specific parameters depending on their name. Code example:

Code: Select all

If name$ = "ANDY" : voice$ = "1" : pitch = 10: speed = 12 : EndIf
If name$ = "BOB" : voice$ = "7" : pitch = 8: speed = 11 : EndIf
If name$ = "CHRIS" : voice$ = "4" : pitch = 7: speed = 8 : EndIf
If name$ = "DAVID" : voice$ = "8" : pitch = 9: speed = 10 : EndIf
However - in future - so I don't have to continually alter my program and re-compile it every time, I would like it to read the above information from a text file that I can change whenever I want. Something like this:

ANDY,1,10,12
BOB,7,8,11
CHRIS,4,7,8
DAVID,8,9,10

However - the names would change from time to time, and the list would get shorter and longer (sometimes there might be 12 voices, other times there might be 16 voices, etc.)

How can I get a config file working with this? Any advice is greatly appreciated!

Thanks

Will
sec
Enthusiast
Enthusiast
Posts: 789
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

c:\puppy.txt wrote: ANDY,1,10,12
BOB,7,8,11
CHRIS,4,7,8
DAVID,8,9,10

Code: Select all

OpenFile(0, "c:\puppy.txt")
While  Eof(0) = #FALSE
  el$ = ReadString() 
  name$ = StringField(el$, 1, ",")
  voice$ = StringField(el$, 2, ",")
  pitch.l  = Val(StringField(el$, 3, ","))
  speed.l = Val(StringField(el$, 4, ","))
  ;process for each ppl at here as creating a LinkList with Structure for  using later.
  Debug name$
  Debug voice$
  Debug pitch
  Debug speed      
Wend
CloseFile(0)
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

Post by wmorton »

Thanks Sec, that's a great help - but I think the LinkedList stuff is where I am having most problems, I don't really understand the example for it that comes with PB. Does anyone know of a good tutorial for this?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Hi wmorton,

Have you tried preference files? PureBasic has a pretty good approach that seems to suit what you are after.

There is a small code example in the help file under the preference entry.

Edit: IIRC there are also some preference examples in the code archive.
@}--`--,-- A rose by any other name ..
sec
Enthusiast
Enthusiast
Posts: 789
Joined: Sat Aug 09, 2003 3:13 am
Location: 90-61-92 // EU or ASIA
Contact:

Post by sec »

preference file needs to know name of group for accessing that is not good choice as in case imo
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Could be.

Depends on usage. If this is one prog accessed by different people from time to time, or one chunk of resources accessed via a network then they would identify themselves (logging in) and that would define the grouping (their user acc).

The admin can add users/groups and pref lists.
The users can mod pref lists.

But it depends on the context.

EDIT:

So I guess the question is how the program or the resource is used.
One comp/prog, many people at different times?
Networked.
Each has their own prog on own comp.
@}--`--,-- A rose by any other name ..
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

Post by wmorton »

The program would be used by one person on one PC at a time.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Assuming one pc, many users at different times. One way:

Code: Select all

If CreatePreferences("Preferences.txt")
  PreferenceGroup("jsmith")
  WritePreferenceString("name", "jim")
  WritePreferenceLong("voice",2)
  WritePreferenceLong("pitch",10)
  WritePreferenceLong("speed",12)
  PreferenceGroup("jbrown")
  WritePreferenceString("name", "john")
  WritePreferenceLong("voice",3)
  WritePreferenceLong("pitch",11)
  WritePreferenceLong("speed",13)
  ClosePreferences()
EndIf
Above creates file. File looks like this:

Code: Select all

[jsmith]
name = jim
voice = 2
pitch = 10
speed = 12
[jbrown]
name = john
voice = 3
pitch = 11
speed = 13
It can be edited with a text editor for quick and dirty maintenance.

Users arrive and sit down and load prog, prog asks for user name, then collects preferences.

Code: Select all

; assume name entered into useraccount$
If OpenPreferences("Preferences.txt")
  If PreferenceGroup(useraccount$)
    Debug ReadPreferenceString("name", "NONE")
    Debug ReadPreferenceLong ("voice", 0)
    Debug ReadPreferenceLong ("pitch", 0)
    Debug ReadPreferenceLong ("speed", 0)
  Else
    Debug "nudge wmorton and ask to be put on the list!"
  EndIf
  ClosePreferences()
EndIf
But any text file can do it. For a straight text file just readstring/readlong/readlong/readlong until you hit the user or the end of file.
@}--`--,-- A rose by any other name ..
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

Post by wmorton »

Thanks guys, I really appreciate your help.

I am a little confused though, I'll do my best to explain exactly what mhy program does.

It opens a text file and extracts the following information from it:
Name.s
data.s

It then launches another program (not one of mine) for processing like this:

prog.exe <output filename> data.s name.s -v str(voice.l) -p str(pitch.l) -s str(speed.l)

Once prog.exe has finished, my program opens the next text file in the folder and does the same.

My original (and current) solution was to hard-code the list of name/voice/pitch/speed associations, and do
if name.s = "Dare2" : voice.l = 3 : endif
or whatever.

However, this would mean that if this list of name & voice associations got longer, shorter, or changed in any way (for instance if it was later decided that Sam should be voice 13 not 14) then the program would need to be re-compiled.

If I was just having to deal with name$ it would be easy, but voice, pitch, and speed are not available from the hundreds of text files I am processing, hence I need to have a config/prefs file that my program can look at to find the correct name/voice/pitch/speed association

I hope this explains my program better!
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Heya,

Then sec's approach would do it. You create the text file:
sec wrote:
c:\puppy.txt wrote: ANDY,1,10,12
BOB,7,8,11
CHRIS,4,7,8
DAVID,8,9,10
You can change this at will, with text editor. Then
sec (slightly modified) wrote:

Code: Select all

OpenFile(0, "c:\puppy.txt")
While  Eof(0) = #FALSE
  el$ = ReadString() 
  name$ = StringField(el$, 1, ",")
  voice.l = Val(StringField(el$, 2, ","))
  pitch.l  = Val(StringField(el$, 3, ","))
  speed.l = Val(StringField(el$, 4, ","))

; ------ Do your existing app launch stuff here?
; ------ Would happen once per user in the text file.
Wend
CloseFile(0)
Alternatively, a batch file?


EDIT:

So your prog passes parameters to another app which, presumably, outputs a file that is configured/customised to an individual?

BTW - You are a musician?
@}--`--,-- A rose by any other name ..
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

Post by wmorton »

Argh, sory guys, please bear with me! I'm having a real hard time understanding this!

How would the example you just typed work?

say I have a folder full of text files, 001.txt 002.txt 003.txt etc.

001.txt contains one line:
BILL,blah blah blah

002.txt contains one line:
SAM,bler bler bler

003.txt contains one line:
CHRIS,flar flar flar

(hypothetical examples only! but the text files are set out like that)

so, my prog opens 001.txt (first file in folder) and gets name$ ("BILL") and data_string$ ("blah blah blah")

it then sends the information to external prog for processing:

prog.exe NEW_001.txt data_string$+" "+name$+" -v "+str(voice.l)+" -p "+str(pitch.l)+" -s "+str(speed.l)

What I don't understand is - how do I get voice.l and pitch.l and speed.l depending on the current name$ ?

Thanks again for your help, and apologies for my continued ignorance

:)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Heya,

I am just a spambot and this conversation is extending my Heuristic capabilities to the full. I may need to go back to my creator for an upgrade.

Anyhow:

So there are several files, not one?
Depending on the user you send the contents of one of those files to a third party app?
ASIDE and Q: -- How do you (or your prog) get the current name/identify the current person?
When there is a match you send the details to the 3rd party app?
ASIDE and Q: -- Why don't you just type in 3rd party app command line each time?

If you already know the names (assuming 001.TXT to 016.TXT being the limits?) do a string of reads. (If not, use ExamineDirectory)

Code: Select all

For n = 1 to 16
  name$=""
  if OpenFile(0, "c:\"+Right("000"+Str(n),3)+".txt")
    el$ = ReadString()
    name$ = StringField(el$, 1, ",")
    voice.l = Val(StringField(el$, 2, ","))
    pitch.l  = Val(StringField(el$, 3, ","))
    speed.l = Val(StringField(el$, 4, ","))
    CloseFile(0)
    if name$=TheNameINeed$
      break
    endif
  endif
Next
If name$<>""
  ; ------ Do your existing app launch stuff here
EndIf
Beware: Above code just typed in, probably full of typos and errors.


And so:

Either you are a musician ..

or ..

A druglord! (where name is the name of a subdealer, voice is the code for the emergency lawyer and pitch is the code for the geographic territory assigned the subdealer, and speed is the quality of the substance acceptable in that particular pitch)

j/k

:)
@}--`--,-- A rose by any other name ..
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

Post by wmorton »

I am a musician :)

There are - unfortunately - a lot more than 16 text files.... I am testing my program currently with a folder of 48 files (but when I have the program working properly it will be working with tens of thousands of files at a time!)

I think I may be causing confusion by using names... the names are nothing to do with other users - there will only ever be one person using the prog at a time...

imagine this: each text file contains two pieces of information: string_a (previously known as name$!) and string_b

So far, my program looks through whatever folder you tell it to, opens each text file, gets string_a and string_b, then sends this information to an external program with string_a, string_b, voice, pitch, and speed.

However, voice, pitch and speed are all dependent on the value of string_a.

What I am trying to do is to have my program look at a prefs file each time I launch the program (prefs.txt for the sake of example)

prefs.txt would contain:
string_a, string_a's voice, string_a's pitch, string_a's speed
string_b, string_b's voice, string_b's pitch, string_b's speed
string_c, string_c's voice, string_c's pitch, string_c's speed

It would then know, that when it opened a particular file during the main program loop (until NextDirectoryEntry returns 0 no more file in directory), which value to send to the external program depending on the value of string_a

I hope this makes more sense! I hope I make more sense!

Thanks for bearing with me :)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Okay, the other shoe has dropped. :)

(I think)

So, loop steps through files getting "IDname" and another value.

For each IDname you want to look up a series of values and pass them on to the 3rd party app along with the name and the other value.

The best way will depend on the number of unique names. Are there tens of thousands of potential IDnames? (Thinking DataBase). Or just a few within a known bound? (Thinking pre-populated array).
@}--`--,-- A rose by any other name ..
wmorton
User
User
Posts: 81
Joined: Fri Jul 25, 2003 5:39 pm

Post by wmorton »

Yes, correct :)

For the projects this program will be used on, there will likely be a maximum of around 100 unique IDnames. Should be less though. This will change (more or less) with each project though
Post Reply