It would be possible to read/write through a dictionary. I’ve written a sample, but it’s a really basic design that will show its weakness if the format strays from what you’ve described above.
I have a more robust INI in/out class, but that would be changing the format which you did not mention being an acceptable solution.
#tag Module
Protected Module modDict
#tag Method, Flags = &h0
Function FromString(sInput as String) As Dictionary
// Convert EOLs to known
sInput = sInput.ReplaceLineEndings(EndOfLine.UNIX)
// Split on EOLs
var arsLines() as String = sInput.Split(EndOfLine.UNIX)
// Parse the lines
var dict as new Dictionary
for each sLine as String in arsLines
var arsKeyValPair() as String = sLine.Split(":")
// Not enough data
if arsKeyValPair.LastIndex < 1 then
System.DebugLog("Not enough data: " + sLine)
continue for sLine
end
dict.Value(arsKeyValPair(0)) = arsKeyValPair(1)
next sLine
return dict
End Function
#tag EndMethod
#tag Method, Flags = &h0
Function ToString(extends dict as Dictionary) As String
var arsLines() as String
for each vKey as Variant in dict.Keys
arsLines.Add(vKey.StringValue + ":" + dict.Value(vKey).StringValue)
next vKey
// Intentionally using the EOL function because I know TimS is on Windows
return String.FromArray(arsLines, EndOfLine)
End Function
#tag EndMethod
End Module
#tag EndModule
if you need to change the file content, it would read all, make a method that find and update Restart key, save the file. with the dictionary class you just assign the key a new value.
If your text file is really that straightforward and the format is well known, you could just replace the string like this:
Var sampleData As String
Var txtFile As FolderItem
Var ins As TextInputStream
Var outs As TextOutputStream
Var original, change As String ' these values would be passed to this method
original = "Profiler:False"
change = "Profiler:True"
txtFile = FolderItem.ShowOpenFileDialog("text/plain")
If txtFile <> Nil And txtFile.Exists Then
ins = TextInputStream.Open(txtFile)
sampleData = ins.ReadAll
ins.Close
sampleData = sampleData.ReplaceAll(original,change)
outs = TextOutputStream.Create(txtFile) ' Recreates the text file
outs.WriteLine(sampleData)
outs.Close
End If