aboutsummaryrefslogtreecommitdiffstats
path: root/Tools/ScoreConverter/USongs.pas
blob: 8f20f44f568f937a46e538b5e25410a1dfe053f2 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
unit USongs;

interface

type
  TScore = record
    Name:       string;
    Score:      integer;
    Length:     string;
  end;
  
  TSong = record
    Path:       string;
    FileName:   string;

    Title:      string;
    Artist:     string;

    Score:      array[0..2] of array of TScore;
  end;

  TSongs = class
    LastCount: Integer;
    Song:       array of TSong; // array of songs

    function  ReadHeader(var rSong: TSong): boolean;
    procedure BrowseDir(Dir: string); // Browse a dir + subdirs for songfiles
  end;

  var Songs: TSongs;

implementation
uses Sysutils, UMainForm, Dialogs;

function TSongs.ReadHeader(var rSong: TSong): boolean;
var
  Line, Identifier, Value: String;
  Temp: word;
  Done: byte;
  SongFile: Textfile;
begin
  Result := False;


  //Open File and set File Pointer to the beginning
  AssignFile(SongFile, rSong.Path + rSong.FileName);
  Reset(SongFile);

  //Read Header
  Result := true;

  //Read first Line
  ReadLn (SongFile, Line);

  if (Length(Line)<=0) then
  begin
    Result := False;
    Exit;
  end;
  Done := 0;
  //Read Lines while Line starts with #
  While (Line[1] = '#') do
  begin
    Temp := Pos(':', Line);

    //Line has a Seperator-> Headerline
    if (Temp <> 0) then
    begin
      //Read Identifier and Value
      Identifier  := Uppercase(Trim(Copy(Line, 2, Temp - 2))); //Uppercase is for Case Insensitive Checks
      Value       := Trim(Copy(Line, Temp + 1,Length(Line) - Temp));

      //Check the Identifier (If Value is given)
      if (Length(Value) <> 0) then
      begin

        //-----------
        //Required Attributes
        //-----------

        //Title
        if (Identifier = 'TITLE') then
        begin
          rSong.Title := Value;

          //Add Title Flag to Done
          Done := Done or 1;
        end

        //Artist
        else if (Identifier = 'ARTIST') then
        begin
          rSong.Artist := Value;

          //Add Artist Flag to Done
          Done := Done or 2;
        end;

      end;
    end;

    if not EOf(SongFile) then
      ReadLn (SongFile, Line)
    else
    begin
      Result := False;
      break;
    end;

    //End on first empty Line
    if (Length(Line) = 0) then
      break;
  end;

  //Check if all Required Values are given
  if (Done <> 3) then
  begin
    Result := False;
  end;

  //And Close File
  CloseFile(SongFile);
end;

procedure TSongs.BrowseDir(Dir: string);
var
  SR:     TSearchRec;   // for parsing Songs Directory
  SLen:   integer;
begin
  if FindFirst(Dir + '*', faDirectory, SR) = 0 then begin
    repeat
      if (SR.Name <> '.') and (SR.Name <> '..') then
        BrowseDir(Dir + Sr.Name + '\');
    until FindNext(SR) <> 0;
  end;
  FindClose(SR);

 if FindFirst(Dir + '*.txt', 0, SR) = 0 then begin
    repeat
      SLen := Length(Song);
      SetLength(Song, SLen + 1);

      Song[SLen].Path := Dir;
      Song[SLen].FileName := SR.Name;

      if (ReadHeader(Song[SLen]) = false) then SetLength(Song, SLen);

      //update Songs Label
      if LastCount <> SLen div 30 then
      begin
        LastCount := SLen div 30;
        MainForm.UpdateLoadedSongs(Dir, SLen);
      end;

    until FindNext(SR) <> 0;
    end; // if FindFirst
  FindClose(SR);
end;

end.