New topic in General: 

Module for reading INI/INF files

Jailout2000 - Wed Apr 02, 2008 12:03 am

<http://forums.realsoftware.com/viewtopic.php?t=21589>
                                                                                
                                                                                
Ok so I was bored and wanted something to do, I got the idea of making an INI 
reader. Well since INI and INF are the same format, it obviously can also do 
INF.

Well, here's the function that reads INI/INF files, I fixed 3 problems in it 
(see here), but it should work on any platform and any format of INI files.

If you don't want to copy and paste, I made a download.
Code:Function GetConfigEntry(File As FolderItem, EntryName As String, 
HeaderName As String, Encoding As TextEncoding = Nil, NoValue As String = "") 
As String
  // "Some Value" = GetConfigEntry(GetFolderItem("Setup.ini"), "Some Option", 
"Setup")
  // INI files are tricky, so baer with me if something goes wrong.
  //
  Dim F As FolderItem = File
  If F = Nil Then Return NoValue
  If F.Exists = False Then Return NoValue
  If F.IsReadable = False Then Return NoValue
  Dim Input As TextInputStream = F.OpenAsTextFile
  If Input = Nil Then Return NoValue
  Dim Enc As TextEncoding = Encoding
  If Enc = Nil Then Enc = Encodings.UTF8 //Default INI file encoding
  Dim Data As String = Input.ReadAll(Enc) //It's ok if Enc is nil...
  Input.Close
  //
  //  Done loading data, now parse it (ugh! I hope this works)...
  //  Parses the most common INI version (which I forget which is).
  //
  Data = ReplaceLineEndings(Data, EndOfLine)
  Dim i As Integer = 1, Line, CurrentHeader, Name, Value, ValueWithComment As 
String
  While i <= CountFields(Data, EndOfLine)+1
    Line = NthField(Data, EndOfLine, i)
    If Trim(Line) <> "" Then
      If Left(Line, Len(";")) <> ";" Then
        If Left(Line, Len("[")) = "[" Then //Header detected, set current 
header and pass the header...
          CurrentHeader = Mid(Line, 2, InStr(Line, "]")-2) //Get the 
header name.
          Line = Mid(Line, Len("["+CurrentHeader+"]")+1) //Forward line to 
just past the header.
          If Trim(Line) = "" Then
            i = i + 1
            Continue While
          End If
        End If
        Name = NthField(Line, "=", 1) //Name of the setting.
        ValueWithComment = Mid(Line, Len(Name+"=")+1) //Value with comment.
        Value = Mid(Line, Len(Name+"=")+1, 
Len(Line)-Len(Name+"=")-InStr(ValueWithComment, ";")) //Value without comment.
        If Right(Name, Len(" ")) = " " Then //Hmmm... someone put a space :/
          Name = Left(Name, Len(Name)-Len(" "))
          Value = Mid(Value, Len(" ")+1)
        End If
        If CurrentHeader = HeaderName And Name = EntryName Then Return Value
      End If
    End If
    i = i + 1
  Wend
  Return NoValue
End Function
Your welcome everyone.

-Jailout2000                                                                    
                
_________________
My Official Site | My Official Forum (If you goto this link you will get a 300 
Multiple Choices error, which is supposed to happen)
                                        
                                                                                
                                                                                
                                                                                
                                                                                
                                        



-- 
Over 900 classes with 18000 functions in one REALbasic plug-in. 
The Monkeybread Software Realbasic Plugin v8.1. 

&lt;http://www.monkeybreadsoftware.de/realbasic/plugins.shtml&gt;

[email protected]

Reply via email to