Jump to content
dopeness

Pasring and other things string related

Recommended Posts

Hello again, I am trying to figure out how to parse through data. I wanted to just store it into a string, but do to the lack of basic string functions i couldn't. So now I tried just saving it to a file and then reading the file line by line, but scar doesn't even have a function to read line by line, instead it just stores everything into a string which brings me back to square one. So, how would i go about sifting through a bunch of data in a string and pull out of it what i need. Thanks a lot guys.

Link to comment
Share on other sites

Hello, just made couple functions that you might find useful.. CountFileLines() and GetFileLine().

I included an example for you. :) Hopefully they help you out.

 

-Jani

 

[scar]function CountFileLines(path: string): Integer;

var

f: Integer;

s: string;

begin

if (path <> '') then

if FileExists(path) then

begin

f := OpenFile(path, False);

ReadFileString(f, s, FileSize(f));

CloseFile(f);

Result := (CountStr(#13#10, s) + 1);

end;

end;

 

function GetFileLine(path: string; line: Integer): string;

var

f: Integer;

s: string;

TSA: TStringArray;

begin

if ((path <> '') and (line > 0)) then

if FileExists(path) then

begin

f := OpenFile(path, False);

ReadFileString(f, s, FileSize(f));

CloseFile(f);

TSA := Explode(#13#10, s);

if (Length(TSA) >= line) then

Result := TSA[(line - 1)];

SetLength(TSA, 0);

end;

end;

 

var

clPath: string;

clc, i: Integer;

 

begin

clPath := (Replace(AppPath, 'bin\', '') + 'changelog.txt');

clc := CountFileLines(clPath);

WriteLn('Changelog contains ' + IntToStr(CountFileLines(clPath)) + ' lines!');

WriteLn('Printing changelog LINE-BY-LINE in 3 seconds!');

Wait(3000);

for i := 1 to clc do

WriteLn(GetFileLine(clPath, i));

end.[/scar]

 

Edit: Here is another style to get data from line (GetStrLine() + ReadFileStr()), check below..

 

[scar]function GetStrLine(str: string; line: Integer): string;

var

TSA: TStringArray;

begin

if ((str <> '') and (line > 0)) then

begin

TSA := Explode(#13#10, str);

if (Length(TSA) >= line) then

Result := TSA[(line - 1)];

SetLength(TSA, 0);

end;

end;

 

function ReadFileStr(Path: string): string;

var

f: Integer;

begin

if not FileExists(Path) then

Exit;

f := OpenFile(Path, False);

ReadFileString(f, Result, FileSize(f));

CloseFile(f);

end;

 

var

str, clPath: string;

c, i: Integer;

 

begin

ClearDebug;

clPath := (Replace(AppPath, 'bin\', '') + 'changelog.txt');

str := ReadFileStr(clPath);

c := (CountStr(#13#10, str) + 1);

WriteLn('Changelog contains ' + IntToStr© + ' lines!');

WriteLn('Printing changelog LINE-BY-LINE in 3 seconds!');

Wait(3000);

for i := 1 to c do

WriteLn(GetStrLine(str, i));

str := '';

end.[/scar]

 

Of course, the best way is just to store string lines as TStringArray (or TStringList).

Then it is wayyy quicker to get the lines, because there wont be need for loading the data again every single time (like with these functions).

Edited by Janilabo
Improved GetFileLine() & GetStrLine() a bit.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...