Author Topic: How many lines of text  (Read 1226 times)

0 Members and 3 Guests are viewing this topic.

Offline shantec

  • Soldier
  • **
  • Posts: 140
  • Get ANGREH!!
How many lines of text
« on: April 26, 2009, 09:47:24 am »
Any way to check from text / ini file how many lines of text there is?
« Last Edit: April 26, 2009, 09:50:19 am by shantec »
Also Known As REIMA


Lol Happles (happy apples)

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Re: How many lines of text
« Reply #1 on: April 26, 2009, 09:55:59 am »
« Last Edit: April 26, 2009, 09:57:32 am by Wookash »
"My senses are so powerful that I can hear the blood pumping through your veins."

Offline shantec

  • Soldier
  • **
  • Posts: 140
  • Get ANGREH!!
Re: How many lines of text
« Reply #2 on: April 26, 2009, 10:00:15 am »
I meant with script
I want SCRIPT to check how many lines there is so it can do stuff
Also Known As REIMA


Lol Happles (happy apples)

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: How many lines of text
« Reply #3 on: April 26, 2009, 10:01:35 am »
something along these lines:
writeln(inttostr(getarraylength(xsplit(readfile('path/to/file'),#10))));

Or just count the linebreaks with a loop similar to xsplit, cba writing it all up for you ;)

Offline shantec

  • Soldier
  • **
  • Posts: 140
  • Get ANGREH!!
Re: How many lines of text
« Reply #4 on: April 26, 2009, 10:49:29 am »
Code: [Select]
function GetLines(FileDir: string): integer;
var i,lines: integer;
file: string;
begin
Lines := 0;
file :=ReadFile(Filedir);
For i := 1 to 1000 do begin
If GetPiece(file,chr(13) + chr(10),i) <> '' then begin
lines := i+1;
end else break;
end;
Result := lines;
end;

Okay fully working script
there is just one problem.. if there are whitespaces in the file, it stops
Anyone, how could i fix that?
Also Known As REIMA


Lol Happles (happy apples)

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: How many lines of text
« Reply #5 on: April 26, 2009, 12:32:32 pm »
Code: [Select]
function DoesFileExist(Name: string): boolean;
begin
  if (GetSystem() = 'windows') then
  begin
    if (FileExists(Name)) then
    begin
      result:= true;
    end;
  end else
  begin
    if ((FileExists(Name)) or (ReadFile(Name) <> '')) then
    begin
      result:= true;
    end;
  end;
end;

function GetLines(const PATH: string): integer;
var
  i, x: integer;
  s, FILE: string;
begin
  i:= 1;
  if (DoesFileExist(PATH)) then
  begin
    FILE:= ReadFile(PATH);
    while (i <= length(FILE)) do
    begin
      s:= Copy(FILE, i, 2);
      if (s = #13#10) then
      begin
        Inc(Result, 1);
        Inc(i, 2);
      end else
      begin
        Inc(i, 1);
      end;
    end;
  end;
end;

Always check if the file exists you want to access before reading it.
Soldat Global Account System: #soldat.sgas @ quakenet

Offline JFK

  • Camper
  • ***
  • Posts: 255
    • My TraxInSpace Account
Re: How many lines of text
« Reply #6 on: April 27, 2009, 06:12:57 am »
Code: [Select]
function GetLines(FileDir: string): integer;
var i,lines: integer;
file: string;
begin
Lines := 0;
file :=ReadFile(Filedir);
For i := 1 to 1000 do begin
If GetPiece(file,chr(13) + chr(10),i) <> '' then begin
lines := i+1;
end else break;
end;
Result := lines;
end;

Okay fully working script
there is just one problem.. if there are whitespaces in the file, it stops
Anyone, how could i fix that?

I asume you mean a white line. That's pretty logical. You're checking everything between two newline chars. A white line is 'nothing' between two newline chars, so your for-loop will break.
Im not sure if it will work, but you could try out something with chr(3) wich should be the ascii number for end-of-text.
Would be a bit more elegant solution then checking every character.. still very nice function, curry ;)

Come join: EliteCTF
Listen to: My Music

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: How many lines of text
« Reply #7 on: April 27, 2009, 07:56:45 am »
Would be a bit more elegant solution then checking every character.
thats what i meant by something similar to xsplit...

@ shantec: Also, make sure your linebreaks are #13#10, not just #10 ;) But i assume your server runs on windows, so you should be fine

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: How many lines of text
« Reply #8 on: April 27, 2009, 09:27:50 am »
Code: [Select]
function StringOccurs(needle, haystack: string): integer;
begin
   while strpos(needle,haystack) <> 0 do begin
      Result := Result + 1;
      haystack := copy(haystack,strpos(needle,haystack)+1,length(haystack));
   end;
end;

function CountLines(Filename: string): integer;
begin
   Result := StringOccurs(chr(13)+chr(10), ReadFile(Filename));
end;

Not tested, but you get the idea.

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: How many lines of text
« Reply #9 on: April 27, 2009, 11:24:21 am »
I asume you mean a white line. That's pretty logical. You're checking everything between two newline chars. A white line is 'nothing' between two newline chars, so your for-loop will break.
Im not sure if it will work, but you could try out something with chr(3) wich should be the ascii number for end-of-text.
Would be a bit more elegant solution then checking every character.. still very nice function, curry ;)

Yeah, I just wanted him to show how a function similar to xsplit may look like.
Soldat Global Account System: #soldat.sgas @ quakenet