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
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
#[macro_use]
extern crate log;
extern crate libc;
use std::ffi::CStr;
use std::ffi::CString;
use std::ffi::NulError;
use std::os::raw::c_char;
use std::path::PathBuf;
use std::str::Utf8Error;
#[derive(Debug)]
pub enum FileError {
OpenFailure,
PathAsString,
NullPathString(NulError),
InvalidTagFile,
}
#[derive(Debug)]
pub enum StringError {
StringAsStr,
NulPtr,
Utf8Err(Utf8Error),
NullString(NulError),
}
type StringReadResult = Result<String, StringError>;
type MultiStringReadResult = Result<Vec<String>, StringError>;
#[derive(Debug)]
pub struct TalamelFile {
file_handle: *mut tml_TalamelFile,
}
impl TalamelFile {
pub fn new<P: Into<PathBuf>>(filename: P) -> Result<TalamelFile, FileError> {
let cs_filename = filename
.into()
.to_str()
.ok_or(FileError::PathAsString)
.and_then(|filename| CString::new(filename).map_err(FileError::NullPathString))?;
unsafe {
let file_ptr = tml_open_file(cs_filename.as_ptr());
if file_ptr.is_null() {
debug!("Got null file pointer (bad filename, or bad tags?)");
return Err(FileError::InvalidTagFile);
}
return Ok(TalamelFile {
file_handle: file_ptr,
});
}
}
fn read_and_parse(c_string_pointer: *mut c_char) -> StringReadResult {
if c_string_pointer.is_null() {
debug!("Got null c string");
return Err(StringError::NulPtr);
}
unsafe {
let str_slice = CStr::from_ptr(c_string_pointer);
let str_res: StringReadResult = str_slice
.to_str()
.map(|s| s.to_owned())
.map_err(StringError::Utf8Err);
tml_free_str(c_string_pointer);
str_res
}
}
pub fn read_property_values<S: Into<String>>(self: &Self, key: S) -> MultiStringReadResult {
let cs_key = CString::new(key.into().as_str()).map_err(StringError::NullString)?;
unsafe {
let pcnt = tml_count_property_values(self.file_handle, cs_key.as_ptr());
let mut v: Vec<String> = Vec::with_capacity(pcnt as usize);
for i in 0..pcnt {
let comment = Self::read_and_parse(tml_read_property_value(
self.file_handle,
cs_key.as_ptr(),
i as u32,
));
match comment {
Ok(s) => v.push(s),
Err(e) => return Err(e),
}
}
return Ok(v);
}
}
pub fn title(self: &Self) -> StringReadResult {
unsafe { Self::read_and_parse(tml_read_title(self.file_handle)) }
}
pub fn artist(self: &Self) -> StringReadResult {
unsafe { Self::read_and_parse(tml_read_artist(self.file_handle)) }
}
pub fn bpm(self: &Self) -> Option<u32> {
unsafe {
match tml_read_bpm(self.file_handle) {
0 => None,
v => Some(v),
}
}
}
pub fn comments(self: &Self) -> MultiStringReadResult {
unsafe {
let comment_count = tml_count_comments(self.file_handle);
let mut v: Vec<String> = Vec::with_capacity(comment_count as usize);
for i in 0..comment_count {
let comment = Self::read_and_parse(tml_get_comment(self.file_handle, i as u32));
match comment {
Ok(s) => v.push(s),
Err(e) => return Err(e),
}
}
return Ok(v);
}
}
}
impl Drop for TalamelFile {
fn drop(&mut self) {
unsafe {
tml_free_file(self.file_handle);
}
}
}