Author Topic: String Issue (IXSplit, XSplit)  (Read 2282 times)

0 Members and 1 Guest are viewing this topic.

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
String Issue (IXSplit, XSplit)
« on: February 16, 2009, 04:25:43 pm »
I figured out that strings read from a file are terminated by two unknown chars.
You can access these chars but I don't understand why they are there.

Example
Code: [Select]
procedure ActivateServer();
var
  b: string;
  i: integer;
begin
  b:= ReadFile('MyFile.txt');
  for i:= 1 to length(b) do
  begin
    WriteLn(b[i] + '**');
  end;
end;

MyFile
Code: [Select]
u



Well, if you transfer this on IXSplit or XSplit you will quickly notice an
issue...

Code: [Select]
function IXSplit(SOURCE: string; Delimiter: string): array of string;
var
  i, x, d: integer;
  s, b: string;
begin
  d:= length(Delimiter);
  i:= 1;
  SetArrayLength(Result, 0);
  while (i <= length(SOURCE)) do
  begin
    s:= Copy(SOURCE, i, d);
    if (s = Delimiter) then
    begin
      SetArrayLength(Result, x + 1);
      Result[x]:= b;
      Inc(i, d);
      Inc(x, 1);
      b:= '';
    end else
    begin       
      b:= b + Copy(s, 1, 1);
      WriteLn(b);
      Inc(i, 1);
    end;
  end;
  if (b <> '') then
  begin
    SetArrayLength(Result, x + 1);
    Result[x]:= b;
  end;
end;

procedure ActivateServer();
var
  b: string;
  a: array of string;
begin
  b:= ReadFile('MyFile.txt');
  a:= IXSplit(b, ' ');
end;

MyFile now contains
Code: [Select]
gimmemore ofcourse jacuzzi goodbye

Result


The highlighted strings and the blank one are the problem, because normally there should
be only one goodbye and no blank string at the end. I guess, this causes many XSplit problems
which are mentioned on this forum.
Soldat Global Account System: #soldat.sgas @ quakenet

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: String Issue (IXSplit, XSplit)
« Reply #1 on: February 16, 2009, 04:48:56 pm »
It's ending with two chars, 13 and 10 (carriage return + newline). Looks like that's where your issues are. Does it work with Explode?

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: String Issue (IXSplit, XSplit)
« Reply #2 on: February 16, 2009, 05:04:24 pm »
Yeah, i believe that the core gayly adds extra characters at the end.. i did a small experiment a lil while ago, if i remember correctly:
actual -> readfileresult
"" -> ""
"a" -> "a\r\n"
"a\r\n" -> "a\r\n"
"a\r\nb" -> "a\r\nb\r\n"
so it is required to end in a \r\n unless its blank.. i think..  if my memory doesnt fail me.

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: String Issue (IXSplit, XSplit)
« Reply #3 on: February 16, 2009, 05:55:37 pm »
You are using WriteLn.... Write LINE... What do you expect? Every programmer knows WriteLn appends the newline characters to any string you pass to it.

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: String Issue (IXSplit, XSplit)
« Reply #4 on: February 16, 2009, 06:20:23 pm »
Well, it's not really about the newline character and of course I know what it stands for.
It's because of the additional chars the ReadFile() procedure or whatever adds to the content.
Exactly what DorkeyDear mentioned. I don't understand why those chars are appended?

It's ending with two chars, 13 and 10 (carriage return + newline). Looks like that's where your issues are. Does it work with Explode?

Explode is not concerned by this problem.
Soldat Global Account System: #soldat.sgas @ quakenet

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: String Issue (IXSplit, XSplit)
« Reply #5 on: February 16, 2009, 06:26:27 pm »
What is the exact code you are using to get the output in the picture? I'm a little confused with the bits you posted above it.
nvm that, I see.

If those two chars are your issue then you can just do...
Code: [Select]
file := ReadFile('filewhatever');
file := copy(file, 0, length(file)-2);
Syntax is probably off 'cause I haven't done any scripting in a few weeks, but you get the point.
« Last Edit: February 16, 2009, 06:29:46 pm by iDante »

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: String Issue (IXSplit, XSplit)
« Reply #6 on: February 17, 2009, 06:13:04 am »
Code: [Select]
function ReadFromFile(File_Name: string): string;
begin
  Result:= ReadFile(File_Name);
  Result:= Copy(Result, 0, length(Result) - 2);
end;

Exactly :)

I'm still wondering why the core adds these chars. Any reasons?
Soldat Global Account System: #soldat.sgas @ quakenet