I have a function called GetServerName. I need to pass the file name (for example, "test.txt"), as well as the desired section line (for example, "server")
File test.txtcontains something like this
data1 | abcd
data2 | efgh
server| 'serverName1'
data3 | ijkl
I need to extract the server name, so in my function I will pass something like GetServerName('test.txt', 'server'), and it should return serverName1.
My problem is that it test.txtused to be an ANSI-encoded file. Now it can be an ANSI-encoded file or a Unicode-encoded file. Below the function worked correctly for the ANSI-encoded file, but it gave a problem if the file is encoded in UNICODE. I suspect something with a function LoadStringsFromFile. Because when I debug, I could see that it returns Unicode characters instead of human-readable characters. How to solve my problem simply? (or how to find the encoding type of my file and how to convert the UNICODE string to ANSI for comparison, then I can do it myself)
function GetServerName(const FileName, Section: string): string;
//Get Smartlink server name
var
DirLine: Integer;
LineCount: Integer;
SectionLine: Integer;
Lines: TArrayOfString;
//Lines: String;
AHA: TArrayOfString;
begin
Result := '';
if LoadStringsFromFile(FileName, Lines) then
begin
LineCount := GetArrayLength(Lines);
for SectionLine := 0 to LineCount - 1 do
begin
AHA := StrSplit(Trim(Lines[SectionLine]), '|')
if AHA[0] = Section then
begin
Result := AHA[1];
Exit;
end
end
end;
end;
Edit
On Windows, when I save as text files. I get 4 options as I attached to the image. I found it, Windows mentions unicode as UTF-16LE encoding (bit confusing)
