Hi there!
Some days ago I wrote a script that inluded reading and parsing a text file.
While the entire script is too specific to my rack, I want to share the functionality that is responsible for loading/parsing the file contents, so that you can adapt it to your own needs.
In my case, the file contained several lists of presets for plugins within my rack.
It was used to map widget values to GP preset names, which would then be loaded.
The file formatting I chose looks a bit like an ini file. It consists of bracketed “titles”.
The script will import data and then find and return a section from starting point of a matching title.
[FX1]
16th stutter
Phaser FM
Robotic Rasp
Robotic Vibrato
-
[FX2]
Classic Chorus
Flange
Liquid Chorus
-
-
[FX3]
Slapback
Send Echo Amb
Send Reverse Ghosts
Send Tape Echo
Send Tape Echo 2
And here’s the script to tinker with!
Var
// Caches the last selected preset list for better performance
CACHED_PRESET_LIST : String Array
CACHED_PRESET_LIST_ID : String
// C:\Users\<user>\Documents\Gig Performer\LocalRackPresets.txt
PRESETS_FILE : String = GigPerformerDocumentsFolder() + "MyPresets.txt"
PRESETS_DATA : String Array
// really important not to have spaces / tabs in here!
LineBreak : String = <<<
>>>
Function LoadPresetsFile() Returns String Array
var data : String
var csvData : String Array
Print("Load presets file "+PRESETS_FILE)
data = LoadStringFromTextFile(PRESETS_FILE)
// GPScript has no string split function.
// to work around, replace line breaks by comma,
// then parse as CSV (not real CSV data, just "strings delimited by comma").
If IndexOfSubstring(data, ",", false) >= 0 Then
Print("Error while reading file: File must not contain comma!")
End
csvData = ParseCSVString(ReplaceString(data, LineBreak, ",", true))
Print("Loaded preset file contains "+IntToString(Size(csvData))+" lines")
result = csvData
End
Function ArrayFindIndexOfValueStartingWith(arr: String Array, searchTerm: String, offset: Integer) Returns Integer
var found_index : Integer = -1
var searchTermLength : Integer = Length(searchTerm)
var i : Integer
For i = offset; i < Size(arr) And found_index < 0; i = i + 1 do
If CopySubstring(arr[i],0,searchTermLength) == searchTerm Then
found_index = i
End
End
result = found_index
End
Function GetPresetList(presetListName:String) Returns String Array
var presetList: String Array = []
var presetSectionStart, presetSectionEnd : Integer
var i: integer
If CACHED_PRESET_LIST_ID == presetListName Then
presetList = CACHED_PRESET_LIST
Else
presetSectionStart= ArrayFindIndexOfValueStartingWith(PRESETS_DATA, "["+presetListName+"]", 0)
If presetSectionStart < 0 Then
Print("GetPresetList: Section ["+presetListName+"] not found")
End
presetSectionEnd = ArrayFindIndexOfValueStartingWith(PRESETS_DATA, "[", presetSectionStart+1)
If presetSectionEnd < 0 Then
presetSectionEnd = Size(PRESETS_DATA) // if not found then assume the requestted preset is the last one within the file
End
For i = presetSectionStart+1; i < presetSectionEnd; i = i + 1 do
If PRESETS_DATA[i] != "" And CopySubstring(PRESETS_DATA[i],0,2) != "//" Then
presetList <-- TrimString(PRESETS_DATA[i])
End
End
If Size(presetList) > 0 Then
Print("GetPresetList: Section ["+presetListName+"] preset count: " + IntToString(Size(presetList)))
End
CACHED_PRESET_LIST = presetList
CACHED_PRESET_LIST_ID = presetListName
End
result = presetList
End
Initialization
PRESETS_DATA = LoadPresetsFile()
End
// and finally map the loaded data to the value of a widget.
On WidgetValueChanged (w : Widget, arg_index : Integer, newValue : Double) from MY_WIDGET_ID
var knownPresets : String Array = GetPresetList("FX2")
var widgetValueToArrayIndex : Integer = ScaleRange(GetWidgetValue(w), 0, Size(knownPresets)-1)
var selectedPreset : String = knownPresets[widgetValueToArrayIndex ]
// do whatever you wanna do with it :)
End