summaryrefslogtreecommitdiff
path: root/fpcsrc/tests/bench/shootout/obsolete/wc.pp
blob: fb096a203ed737c057ad152e9359d0f235f93489 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

{ Count Lines/Words/Chars }

program wc;


uses SysUtils;

var
    nl, nw, nc: longint;
    Buf: array[1..4096] of byte;
    NumRead: Integer;

    A: Integer;
    Tmp: String;
    TmpPos : Byte;
    Ch: String;
    InWord: Boolean;
begin
    nl := 0;
    nc := 0;
    nw := 0;
    InWord := False;
    NumRead := FileRead(StdInputHandle, Buf, 4096);
    While NumRead > 0 Do
    begin
        Inc(nc, NumRead);
        For A := 1 To NumRead Do
        begin
            if Buf[A] = 10 Then Inc(nl);
            if Buf[A] = 13 Then Dec(nc);
            if (Buf[A] = 32) Or (Buf[A] = 10) Or (Buf[A] = 13) Or (Buf[A] = 9) Then
                InWord := False
            else
            begin
                If InWord = False Then
                begin
                    Inc(nw);
                    InWord := True;
                end;
            end;
        end;
        NumRead := FileRead(StdInputHandle, Buf, 4096);
    end;
    WriteLn(IntToStr(nl) + ' ' + IntToStr(nw) + ' ' + IntToStr(nc));
end.