Either NULL terminate each string or double NULL terminate each string.. This technique is used in basic C serialization if binary isn't allowed.. You could use the NULL terminator as a delimiter. It isn't visible to the naked eye but can be viewed using a hex editor. This also makes it easy to read and allows the string to contain any characters.. Further optimisation can be done by storing the size of the array in the first chunk. This way, you only resize/setlength the read array once..
Demo:
Simba Code:
Procedure WriteINIArray(const Section, KeyName, FileName: String; const StrArr: TStringArray);
var
I: Integer;
Str: String;
begin
For I := 0 To High(StrArr) Do
Str := Str + StrArr[I] + #0;
WriteINI(Section, KeyName, ToStr(Length(StrArr)) + #0 + Str, FileName);
end;
Function ReadINIArray(const Section, KeyName, FileName: String): TStringArray;
var
Str: String;
I, J, K, Size: Integer;
Begin
J := 0;
I := 1;
Str := ReadINI(Section, KeyName, FileName);
While (Str[Inc(I)] <> #0) Do;
Size := StrToIntDef(Copy(Str, 0, I), -1);
If (Size > 0) Then
Begin
SetLength(Result, Size); //Optimisation..
For Inc(I) To Length(Str)
If (Str[I] <> #0) Then
Result[J] := Result[J] + Str[I]
Else
Inc(J);
End;
End;
var
Arr: TStringArray;
begin
Arr := ['one', 'two', 'three', 'four', 'five'];
WriteINIArray('Sector', 'Key', 'C:/Users/Brandon/Desktop/Test.INI', Arr);
writeln(ReadINIArray('Sector', 'Key', 'C:/Users/Brandon/Desktop/Test.INI'));
end.
Results:
Progress Report:
[one, two, three, four, five]
Successfully executed.