-
Notifications
You must be signed in to change notification settings - Fork 3
/
CueFile.cs
77 lines (67 loc) · 2.74 KB
/
CueFile.cs
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
using System;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
namespace BinChunker
{
public class CueFile
{
public readonly ArrayList TrackList = new ArrayList();
public readonly string BinFileName;
private readonly string cueFilePath;
public CueFile( string cueFilePath )
{
this.cueFilePath = cueFilePath;
Console.WriteLine("Reading the CUE file:");
string cueLines;
using (TextReader cueReader = new StreamReader( cueFilePath ) )
{
BinFileName = GetBinFileName( cueReader.ReadLine() );
cueLines = cueReader.ReadToEnd();
}
Regex trackRegex = new Regex( @"track\s+?(\d+?)\s+?(\S+?)[\s$]+?index\s+?\d+?\s+?(\S*)",
RegexOptions.IgnoreCase | RegexOptions.Multiline );
var matches = trackRegex.Matches( cueLines );
if (matches.Count==0)
throw new ApplicationException( "No tracks was found." );
Track track = null;
Track prevTrack = null;
foreach ( Match trackMatch in matches )
{
track = new Track(
trackMatch.Groups[1].Value,
trackMatch.Groups[2].Value,
trackMatch.Groups[3].Value );
if (BinChunk.Verbose)
Console.WriteLine(" (StartSector {0} ofs {1})", track.StartSector, track.StartPosition);
if (prevTrack != null)
{
prevTrack.Stop = track.StartPosition - 1;
prevTrack.StopSector = track.StartSector;
}
TrackList.Add( track );
prevTrack = track;
}
if (track == null) return;
track.Stop = GetBinFileLength();
track.StopSector = track.Stop / BinChunk.SectorLength;
TrackList[ TrackList.Count -1 ] = track;
}
private long GetBinFileLength()
{
var fileInfo = new FileInfo( BinFileName );
return fileInfo.Length;
}
private string GetBinFileName( string cueFirstLine )
{
Regex binRegex = new Regex( @"file\s+?""(.*?)""", RegexOptions.IgnoreCase | RegexOptions.Multiline );
Match match = binRegex.Match( cueFirstLine );
string res = match.Groups[1].Value;
var cueDirectory = Path.GetDirectoryName( cueFilePath );
res = Path.Combine( cueDirectory, Path.GetFileName( res ) );
if (!File.Exists( res ) )
res = Path.Combine( cueDirectory, Path.GetFileNameWithoutExtension( cueFilePath ) + ".bin" );
return res;
}
}
}