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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use std::ffi::{CString, NulError};
use std::fs::File;
use std::io::prelude::*;
use std::io;
use std::path::PathBuf;

use libc;
use cue_sys as libcue;
pub use cue_sys::DiscMode;

use cd_text::CDText;
use rem::REM;
use track::Track;

/// The CD struct represents the entirety of a CD, which is the core unit of
/// a CUE sheet. This struct contains the parsing methods used as the primary
/// entry point to libcue's functionality.
///
/// A CD can be a pure CD audio disc, a pure data disc, or a mixed-mode disc
/// containing both data and audio tracks in arbitrary order. A CD will
/// always have at least one track.
///
/// Here's an example of a simple function which parses a CUE sheet and
/// prints information about its contents:
///
/// ```rust
/// use cue::cd::{CD, DiscMode};
/// use cue::track::{TrackMode, TrackSubMode};
///
/// let cue_sheet = "FILE \"example.img\" BINARY
///   TRACK 01 MODE1/2352
///     INDEX 01 00:00:00
///   TRACK 02 AUDIO
///     PREGAP 00:02:00
///     INDEX 01 58:41:36
///   TRACK 03 AUDIO
///     INDEX 00 61:06:08
///     INDEX 01 61:08:08
/// ";
///
/// let cd = CD::parse(cue_sheet.to_string()).unwrap();
///
/// println!("Number of tracks: {}", cd.get_track_count());
/// let mode = match cd.get_mode() {
///     DiscMode::CD_DA => "CD-DA",
///     DiscMode::CD_ROM => "CD-ROM",
///     DiscMode::CD_ROM_XA => "CD-ROM XA",
/// };
/// println!("Mode: {}", mode);
/// println!("");
///
/// for (index, track) in cd.tracks().iter().enumerate() {
///     println!("Track {}", index + 1);
///     println!("Filename: {}", track.get_filename());
///     println!("Start: {}", track.get_start());
///     println!("Length: {}", track.get_length());
///     println!("Pregap: {}", track.get_zero_pre());
///     println!("Postgap: {}", track.get_zero_post());
///     println!("");
/// }
/// ```
pub struct CD {
    cd: *mut libcue::CdPointer,
}

impl CD {
    /// Parses a string containing a CUE sheet and returns a `CD` struct.
    ///
    /// # Errors
    /// Returns a `NulError` if the provided string contains any null bytes.
    pub fn parse(string: String) -> Result<CD, NulError> {
        let c_string = CString::new(string)?;
        let cd;
        unsafe {
            cd = libcue::cue_parse_string(c_string.as_ptr());
        }
        let cd_type = CD {
            cd: cd,
        };
        return Ok(cd_type);
    }

    /// Reads the file contained at `path` and parses it like the [`parse`](#method.parse) function
    /// above.
    pub fn parse_file(path: PathBuf) -> Result<CD, io::Error> {
        let mut cue_sheet = vec![];
        File::open(&path)?.read_to_end(&mut cue_sheet)?;
        return Ok(CD::parse(String::from_utf8_lossy(&cue_sheet).into_owned()).unwrap());
    }

    /// Returns a `DiscMode` value indicating the type of disc represented by this
    /// CUE sheet.
    /// Individual tracks also have types; see [`Track.get_mode`] and [`Track.get_sub_mode`].
    ///
    /// [`Track.get_mode`]: ../track/struct.Track.html#method.get_mode
    /// [`Track.get_sub_mode`]: ../track/struct.Track.html#method.get_sub_mode
    pub fn get_mode(&self) -> DiscMode {
        unsafe {
            return libcue::cd_get_mode(self.cd);
        }
    }

    /// Returns the path on disc to the sidecar metadata file containing CD-TEXT
    /// metadata, if any.
    ///
    /// # Safety
    /// This may segfault if there is no CD-TEXT sidecar.
    pub fn get_cdtextfile(&self) -> Option<String> {
        let c_string;
        unsafe {
            let raw_string = libcue::cd_get_cdtextfile(self.cd);
            if raw_string.is_null() {
                return None;
            }
            c_string = CString::from_raw(raw_string);
        }
        return Some(c_string.to_string_lossy().into_owned());
    }

    /// Returns the total number of tracks in this CD.
    pub fn get_track_count(&self) -> isize {
        unsafe {
            return libcue::cd_get_ntrack(self.cd) as isize;
        }
    }

    /// Returns a [`Track`] struct for the track at the requested index.
    /// Note that track numbering starts from 1; there is no track 0.
    ///
    /// # Errors
    /// If the requested track doesn't exist in the CD, returns `Err`
    /// with a string containing an error message.
    ///
    /// [`Track`]: ../track/struct.Track.html
    pub fn get_track(&self, index: isize) -> Result<Track, String> {
        let track_count = self.get_track_count();
        if index > track_count {
            return Err(format!("Invalid index; CD has {} tracks", track_count));
        }

        let track;
        unsafe {
            track = libcue::cd_get_track(self.cd, index as libc::c_int);
        }

        return Ok(Track::from(track));
    }

    /// Returns a `Vec` containing every track in the CD.
    pub fn tracks(&self) -> Vec<Track> {
        let mut tracks = vec![];
        let mut index = 1;
        while index <= self.get_track_count() {
            tracks.push(self.get_track(index).unwrap());
            index += 1;
        }

        return tracks;
    }

    /// Returns a [`CDText`] representing the CD-TEXT data stored within
    /// this CUE sheet.
    ///
    /// [`CDText`]: ../cd_text/struct.CDText.html
    pub fn get_cdtext(&self) -> CDText {
        let cdtext;
        unsafe {
            cdtext = libcue::cd_get_cdtext(self.cd);
        }
        return CDText::from(cdtext);
    }

    /// Returns a [`REM`] representing the comments stored within
    /// this CUE sheet.
    ///
    /// [`REM`]: ../rem/struct.REM.html
    pub fn get_rem(&self) -> REM {
        let rem;
        unsafe {
            rem = libcue::cd_get_rem(self.cd);
        }
        return REM::from(rem);
    }
}

impl Drop for CD {
    fn drop(&mut self) {
        unsafe {
            libcue::cd_delete(self.cd);
        }
    }
}