aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/Classes/USong_TextFile.pas
blob: a3e605de64966f8f37d3768182100dd33cc79c11 (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
unit USong_TextFile;

interface

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

{$I switches.inc}

uses
  Classes,
  SysUtils,
  USong;

type
  {*******************
    Child of the new TSong class.
    implements filehandling to load a song from a text file
   *******************}
  TSong_TextFile = class(TSong)
    protected
      SongFile: TextFile;
      
      Function OpenSongFile: Boolean;
      Function IsDataAvailable: Boolean;
      Function GetNextLine(): String;
      Procedure CloseSongFile;
  end;
  
implementation

uses
  ULog;

//--------
// Open the SongFile
//--------
Function TSong_TextFile.OpenSongFile: Boolean;
begin
  Result := False;
  
  if not FileExists(FilePath + FileName) then
    Log.LogError('File does not exsist', FilePath + FileName)
  else
  begin
    try
      AssignFile(SongFile, FilePath + FileName);
      Reset(SongFile);
      Result := True;
    except
      Log.LogError('Faild to open file', FilePath + FileName)
    end;
  end;
end;

//--------
// More data in songfile available?
//--------
Function TSong_TextFile.IsDataAvailable: Boolean;
begin
  Result := not eof(SongFile);
end;

//--------
// Returns the next line from the SongFile
//--------
Function TSong_TextFile.GetNextLine(): String;
begin
  ReadLn(SongFile, Result);
  Result := Trim(Result);
end;

//--------
// Close the SongFile
//--------
Procedure TSong_TextFile.CloseSongFile;
begin
  try
    CloseFile(SongFile);
  except
    Log.LogError('Error closing file', FilePath + FileName);
  end;
end;

end.