~ubuntu-branches/ubuntu/vivid/ddrescueview/vivid-backports

« back to all changes in this revision

Viewing changes to source/Parser.pas

  • Committer: Package Import Robot
  • Author(s): Graham Inggs
  • Date: 2015-05-11 11:31:36 UTC
  • Revision ID: package-import@ubuntu.com-20150511113136-7zdpjspc6jfd2nxz
Tags: upstream-0.4~alpha
ImportĀ upstreamĀ versionĀ 0.4~alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(*
 
2
   Parser.pas - Log file parser unit
 
3
 
 
4
   Copyright (C) 2013 - 2015 Martin Bittermann (martinbittermann@gmx.de)
 
5
 
 
6
   This file is part of ddrescueview.
 
7
 
 
8
   ddrescueview is free software: you can redistribute it and/or modify
 
9
   it under the terms of the GNU General Public License as published by
 
10
   the Free Software Foundation, either version 3 of the License, or
 
11
   (at your option) any later version.
 
12
 
 
13
   ddrescueview is distributed in the hope that it will be useful,
 
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
   GNU General Public License for more details.
 
17
 
 
18
   You should have received a copy of the GNU General Public License
 
19
   along with ddrescueview.  If not, see <http://www.gnu.org/licenses/>.
 
20
*)
 
21
 
 
22
unit Parser;
 
23
 
 
24
interface
 
25
uses Classes, Shared;
 
26
 
 
27
type
 
28
 
 
29
  { TLogParser }
 
30
 
 
31
  TLogParser = class(TObservablePersistent)
 
32
  private
 
33
    FLog : TLog;
 
34
    FRescueStatus : TRescueStatus;
 
35
    FComments : TStringList; // comment lines of logfile
 
36
    FLogStream : TStream;
 
37
    FFileName : String;
 
38
    procedure logMsg(msg : String);
 
39
    function isEmptyLine(strLine : String) : boolean;
 
40
    function isCommentLine(strLine : String) : boolean;
 
41
  public
 
42
    destructor Destroy; override;
 
43
    procedure OpenFile(filename : String);
 
44
    procedure CloseFile;
 
45
    procedure parse();
 
46
    function hasFile() : boolean;
 
47
    property log : TLog read FLog;
 
48
    property CommentLines : TStringList read FComments;
 
49
    property rescueStatus : TRescueStatus read FRescueStatus;
 
50
    property LogFileName :String read FFileName;
 
51
  end;
 
52
 
 
53
 
 
54
implementation
 
55
uses SysUtils, GUI;
 
56
 
 
57
destructor TLogParser.Destroy;
 
58
begin
 
59
  CloseFile();
 
60
  inherited;
 
61
end;
 
62
 
 
63
procedure TLogParser.OpenFile(filename : String);
 
64
begin
 
65
  try
 
66
    CloseFile(); // close already open file
 
67
    // open the log file using shared read access, so ddrescue can still write to it.
 
68
    FLogStream := TFileStream.Create(filename, fmOpenRead or fmShareDenyNone);
 
69
    FComments := TStringList.Create;
 
70
    FFileName := filename;
 
71
    parse();  // start parsing after opening
 
72
  except
 
73
    on E: Exception do begin
 
74
        CloseFile;
 
75
        logMsg('Error: '+E.Message);
 
76
    end;
 
77
  end;
 
78
end;
 
79
 
 
80
procedure TLogParser.CloseFile;
 
81
begin
 
82
  FFileName := '';
 
83
  FreeAndNil(FComments);
 
84
  FreeAndNil(FLogStream);
 
85
  FRescueStatus := emptyRescueStatus;
 
86
  SetLength(FLog, 0);
 
87
  FPONotifyObservers(self, ooDeleteItem, nil);
 
88
end;
 
89
 
 
90
(* returns true if a line is empty *)
 
91
function TLogParser.isEmptyLine(strLine : String) : boolean;
 
92
begin
 
93
  strLine := TrimLeft(strLine);
 
94
  result := (Length(strLine) = 0); // empty line
 
95
end;
 
96
 
 
97
(* returns true if a line is a comment line *)
 
98
function TLogParser.isCommentLine(strLine : String) : boolean;
 
99
begin
 
100
  result := (strLine[1] = '#'); // comment line
 
101
end;
 
102
 
 
103
 
 
104
(* add a log line to the application log *)
 
105
procedure TLogParser.logMsg(msg : String);
 
106
begin
 
107
  MainForm.DbgLog.Lines.Add(msg);
 
108
end;
 
109
 
 
110
(* Parse a log file.
 
111
   After parsing, the results are available in
 
112
   the properties log and rescueStatus *)
 
113
procedure TLogParser.parse();
 
114
var
 
115
  line : string;
 
116
  token : array[0..2] of string;
 
117
  i, logEntry, lineIdx, idx : Integer;
 
118
  logStrings : TStringList;
 
119
  comments_end : boolean;
 
120
begin
 
121
  if not Assigned(FLogStream) then begin
 
122
    logMsg('Parser: No log file opened.');
 
123
    FPONotifyObservers(self, ooChange, nil);
 
124
    exit;
 
125
  end;
 
126
 
 
127
  try
 
128
    FLogStream.Seek(0, soFromBeginning);
 
129
    logStrings := TStringList.Create;
 
130
    logStrings.LoadFromStream(FLogStream);
 
131
    logMsg('Parsing log file: ' +IntToStr(logStrings.Count) + ' lines.');
 
132
  except
 
133
    on E : Exception do begin
 
134
      logMsg('Error: Cannot read log file: '+E.Message+'('+E.ClassName+')');
 
135
      FPONotifyObservers(self, ooChange, nil);
 
136
      exit;
 
137
    end;
 
138
  end;
 
139
 
 
140
  FRescueStatus := emptyRescueStatus;
 
141
  lineIdx := -1;
 
142
  logEntry := 0;
 
143
  comments_end := false;
 
144
 
 
145
  try
 
146
    repeat
 
147
      inc(lineIdx);
 
148
      line:=logStrings[lineIdx];
 
149
      if isCommentLine(line) then begin
 
150
        if comments_end or (pos('# current_pos', line) > 0) then
 
151
          comments_end:=true
 
152
        else FComments.Add(line);
 
153
        // process comment info
 
154
        if pos('Command line:', line) > 0 then begin
 
155
           repeat // not a loop, but goto replacement
 
156
               // try to find ddrescue's block size argument
 
157
               idx := pos(' -b', line);
 
158
               if idx <> 0 then idx := idx+3 // point to start of number
 
159
               else begin
 
160
                 idx := pos(' --block-size=', line);
 
161
                 if idx <> 0 then idx := idx+14 // point to start of number
 
162
                 else break; // argument not found, jump over the rest
 
163
               end;
 
164
               token[0] :=TrimLeft(Copy(line, idx, Length(line)));
 
165
               idx := pos(' ', token[0]); //space after argument
 
166
               if idx <> 0 then token[0] :=Copy(token[0], 1, idx-1);
 
167
               FRescueStatus.suggestedBlockSize:=StrToIntDef(token[0], 0);
 
168
           until true;
 
169
        end;
 
170
      end else if not isEmptyLine(line) then begin
 
171
        // split line into maximum of 3 tokens
 
172
        line:=Trim(line);
 
173
        for i:= 0 to 2 do begin
 
174
          idx:= Pos(' ', line);
 
175
          if idx = 0 then begin
 
176
            token[i] := line;
 
177
            line := '';
 
178
          end else begin
 
179
            token[i] := Copy(line, 1, idx-1);
 
180
            line := TrimLeft(Copy(line, idx, Length(line)-idx+1));
 
181
          end;
 
182
        end;
 
183
        if (token[0] <> '') and (token[1] <> '') then begin
 
184
          if token[2] <> '' then begin // standard block line (3 tokens)
 
185
            if Length(FLog) = logEntry then SetLength(FLog, logEntry+256); // allocate more space
 
186
            FLog[logEntry].offset:=StrToInt64(token[0]);
 
187
            FLog[logEntry].length:=StrToInt64(token[1]);
 
188
            FLog[logEntry].status:=token[2][1];
 
189
            case FLog[logEntry].status of
 
190
              '?' : inc(FRescueStatus.nontried, FLog[logEntry].length);
 
191
              '+' : inc(FRescueStatus.rescued, FLog[logEntry].length);
 
192
              '*' : inc(FRescueStatus.nontrimmed, FLog[logEntry].length);
 
193
              '/' : inc(FRescueStatus.nonscraped, FLog[logEntry].length);
 
194
              '-' : inc(FRescueStatus.bad, FLog[logEntry].length);
 
195
            end;
 
196
            if FLog[logEntry].status in ['-', '*', '/'] then inc(FRescueStatus.errors);
 
197
            inc(logEntry);
 
198
          end else begin // found the status line (2 components)
 
199
            if FRescueStatus.pos = 0 then begin
 
200
              FRescueStatus.pos:=StrToInt64(token[0]);
 
201
              FRescueStatus.curOperation:=token[1][1];
 
202
              FRescueStatus.strCurOperation:=OperationToText(FRescueStatus.curOperation);
 
203
            end else begin
 
204
              logMsg('Parser: found more than one line with 2 tokens.');
 
205
            end;
 
206
          end;
 
207
        end;
 
208
      end;
 
209
    until lineIdx = logStrings.Count-1;
 
210
    SetLength(FLog, logEntry); // trim array to actually needed size
 
211
  except
 
212
    on E : Exception do logMsg('Error parsing log file: '+E.Message+'('+E.ClassName+')');
 
213
  end;
 
214
  FreeAndNil(logStrings);
 
215
 
 
216
  if logEntry > 0 then begin
 
217
    // calculate device's size from last block's offset and length
 
218
    FRescueStatus.devicesize:=FLog[logEntry-1].offset + FLog[logEntry-1].length;
 
219
  end else logMsg('Parser: No blocks in logfile!');
 
220
  // notify any observers of the changes
 
221
  FPONotifyObservers(self, ooChange, nil);
 
222
end;
 
223
 
 
224
function TLogParser.hasFile: boolean;
 
225
begin
 
226
  hasFile:=Length(Flog)<>0;
 
227
end;
 
228
 
 
229
end.