Read a ini file

Good to know.

Your class gives back a nothing so it seems it cant read this inis … dunno whats the problem here

Give a man a fish and you feed him for a day;
Teach a man to fish and you feed him for a lifetime

Seems to me Dave’s class, especially with all its warnings, is not the right way to proceed.

Sasha, instead of using what is basically a black box, You will probably benefit from building your own. I would use Dictionary, and Split().

  • Read all the Ini file with TextInputStream and place it into a variable. The example of the LR will do nicely, simply replace the TextArea by a variable, lets call it myText.
  • Split that with EndOfLIne
dim myLines() as string = split(myLine, EndoLine)
  • Read each slot in myLines. If it contains brackets, it is a headline ; store it for later use or discard it.
  • If the line contains “=” which you can verify with Instr(), add the content to the dictionary, which I will call myDictionary.

dim pair() as string = split(myLIne(i), "=") myDictionary.Value(pair(0)) = pair(1)

When the dictionary contains all the keys, you can do

dim maxplayers as integer = val(Dictionary.value("MaxPlayers"))

As you can see, it is not awfully complicated.

or just use WFS IniFile which uses windows apis for reading & writing ini files instead of reinventing the wheel

@Michel Bujardet
hm im not in all this reading operations normally, to read and write this things later back in a ini format… but it seems in Xojo you have to do all the things by your self.

But which example means LR ?

to use the old kernel32.dll is not a good idea i think, especially if you think about MultiPlattform, instead i could use VisualStudio then :wink: But thanks anyways :smiley: (the url was not shown to me…only if i use the quotte function… weird)

@Sascha Mierke . So u want an INI, which was born from the WIN platform, want it xplat, and want xojo to include it as part of the standard toolset. That is quite specific and unlikely to happen. But why don’t you file a feature request?

[quote=276251:@Sascha Mierke]@Michel Bujardet
hm im not in all this reading operations normally, to read and write this things later back in a ini format… but it seems in Xojo you have to do all the things by your self.

But which example means LR ?[/quote]

Xojo is a developer tool and a programming language. Pardon me, but if you intend to develop a program, you better learn the ropes, otherwise you are facing a world of incomprehension and pain.

Reading and parsing a file is a very basic operation you may want to learn.

Click on the links in my post. On the TextInputStream page, I refer to the first example. I made an effort to explain as clearly as possible what needs to be done. now it’s on you.

Michel… everything you described is EXACTLY what my class does… including all the logic to properly cast/read/write various datatypes (Integer, Float, BOolean, Color etc).

But as I kept trying to say… It is SIMILAR to the WIN INI format, and it does work quite well… but if the file was created as a TRUE INI format, there may be things that my class won’t work with (dots in key or sectionnames, subsections, multiple values per key)

But then that is not why I designed it in the first place… It was created to store standard application configuration data in an easy to use format, using syntax similar to what WIN developers were used to.

Dim ini_file As classINI_FILE
ini_file=New classINI_FILE(Config_File)
ini_file.LOAD
//
optionVariable = ini_file.GET(section, key, default)
...
Dim ini_file As classINI_FILE
ini_file=New classINI_FILE(Config_File)
//
ini_file.PUT(section,key,default)
....
//
ini_file.SAVE

GET and PUT each have overloads for BOOLEAN, COLOR, DOUBLE, FOLDERITEM, INTEGER, SINGLE and STRING datatypes

[quote=276259:@Dave S]Michel… everything you described is EXACTLY what my class does… including all the logic to properly cast/read/write various datatypes (Integer, Float, BOolean, Color etc).
[/quote]

I am not surprised we have similar approaches. There are not that many way to use key pairs. Except what I describe simply stores the text as is after parsing each key. No casting of any kind.

From what the OP posted and the example with MaxPlayers, a string only approach probably prevents a lot of pain with dots and stuff anyway.

I could have created a class in minutes, but I believe in empowering people. Seems to me the OP expects things to be written for him. Not sure it is the right way to approach Xojo …

[quote=276251:@Sascha Mierke]
to use the old kernel32.dll is not a good idea i think, especially if you think about MultiPlattform, instead i could use VisualStudio then :wink: But thanks anyways :smiley: (the url was not shown to me…only if i use the quotte function… weird)[/quote]

#if TargeWin32
// do win32 code
#endif

of course on the page for reading ini files MS literally says “you should read and write to the registry”

[quote=276256:@Michel Bujardet]Xojo is a developer tool and a programming language. Pardon me, but if you intend to develop a program, you better learn the ropes, otherwise you are facing a world of incomprehension and pain.

Reading and parsing a file is a very basic operation you may want to learn.

Click on the links in my post. On the TextInputStream page, I refer to the first example. I made an effort to explain as clearly as possible what needs to be done. now it’s on you.[/quote]

Reading ini files is not a thing that Xojo have to support (there a many other things that xojo lacks off)… but the lack of libraries for many things is a bit annoying :wink:

[quote=276267:@Norman Palardy]#if TargeWin32
// do win32 code
#endif

of course on the page for reading ini files MS literally says “you should read and write to the registry”[/quote]

i know that :wink: but that doesnt helps that maybe the linux GUI have to read the ini files from the deticated server too :). And the registry is one of the badest save places, i know ms likes the idea but any good .net developers not.

so i have to write my own iniclass somehow ^^

RegEx

INI files on Linux ???
Really ?
Given a choice I’d use XML or JSON but if you have to read other applications config files I suppose they could be ini’s
Seems odd (at least unlikely but … )

yes … the server application uses ini files… like every unreal4 app :wink:

and i think the problem of reading this files seems to be the unix format… i coded a little test here:

[code] if sFileName <> Nil and sFileName.Exists then

Dim regexcomment As New RegEx
Dim regexsection As  New Regex
Dim regexkey As New Regex
Dim m as RegExMatch
Dim oReader as TextInputStream

regexcomment.SearchPattern = "^([\\s]*#.*)"
regexsection.SearchPattern = "^[\\s]*\\[[\\s]*([^\\[\\s].*[^\\s\\]])[\\s]*\\][\\s]*$"
regexkey.SearchPattern = "^\\s*([^=]*[^\\s=])\\s*=(.*)"

oReader = TextInputStream.Open(sFileName)
oReader.Encoding = Encodings.UTF8

While not oReader.EOF
  
  Dim line as String = oReader.ReadLine()
  
  If line.Len > 0 then
    m = nil
    if regexcomment.Search(line) <> nil  then
      m = regexcomment.Search(line)
      System.DebugLog("Skipping Comment: " + m.SubExpressionString(0))
    elseif regexsection.Search(line) <> nil  then
      m = regexsection.Search(line)
      System.DebugLog("Adding Section: " + m.SubExpressionString(0))
    elseif regexkey.Search(line) <> nil  then
      m = regexkey.Search(line)
      System.DebugLog("Adding Key:" + m.SubExpressionString(0))
    end if
  end if
wend

else
Msgbox “Ini-File not found”
end if

[/code]

The Section of the GameUserSettings cant be read, but a UTF8 ini seems to be no problem here…

But now its very late and i got to bed :slight_smile: zzZZ

Until now I had no idea it was an unreal ini you were trying to read since you never mentioned that until just now

Inis are so simple it shouldn’t even require a regex

Oh i said that the server application uses the inis :wink:

But i got the reason of the problem now… i cant read the file correctly cause the encoding: UCS-2 LE … it seems xojo dont support this, cause under encodings there is nothing like this :frowning:

is i think this is the problem too of @Dave S ini class. Normal Ini files on windows are UTF-8

UCS2 LE is another name for UTF16 LE.

Above is true for the 65’536 points in the Basic Multilingual Plane, at least. UTF16 includes ways to address the astral planes, which UCS2 pretends do not exist. But any valid UCS2 is also valid UTF16. (The LE stands for little-endian.)

[quote=276303:@Norman Palardy]Until now I had no idea it was an unreal ini you were trying to read since you never mentioned that until just now

Inis are so simple it shouldn’t even require a regex[/quote]
I’m growing to love RegEx, that’s why it’s was my go-to.

Come on, Norman, bring your stalker skills to my level. I knew it was an Ark server from a screenshot in another thread.

[quote=276336:@Sascha Mierke]Oh i said that the server application uses the inis :wink:

But i got the reason of the problem now… i cant read the file correctly cause the encoding: UCS-2 LE … it seems xojo dont support this, cause under encodings there is nothing like this :frowning:

is i think this is the problem too of @Dave S ini class. Normal Ini files on windows are UTF-8[/quote]

you have the source code… add the proper encodings to suit your needs