5 Copyright (C) 2009 Laurent Aimar
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 if( not string.match( string.upper( vlc.path ), ".CUE$" ) ) then
27 header = vlc.peek( 2048 )
28 return string.match( header, "FILE.*WAVE%s*[\r\n]+" ) or
29 string.match( header, "FILE.*AIFF%s*[\r\n]+" ) or
30 string.match( header, "FILE.*MP3%s*[\r\n]+" )
34 function is_utf8( src )
35 return vlc.strings.from_charset( "UTF-8", src ) == src
38 function cue_string( src )
39 if not is_utf8( src ) then
40 -- Convert to UTF-8 since it's probably Latin1
41 src = vlc.strings.from_charset( "ISO_8859-1", src )
43 local sub = string.match( src, "^\"(.*)\".*$" );
47 return string.match( src, "^(%S+).*$" )
50 function cue_path( src )
51 if( string.match( src, "^/" ) or
52 string.match( src, "^\\" ) or
53 string.match( src, "^[%l%u]:\\" ) ) then
54 return vlc.strings.make_uri(src)
57 local slash = string.find( string.reverse( vlc.path ), '/' )
58 local prefix = vlc.access .. "://" .. string.sub( vlc.path, 1, -slash )
59 -- FIXME: postfix may not be encoded correctly (esp. slashes)
60 local postfix = vlc.strings.encode_uri_component(src)
61 return prefix .. postfix
64 function cue_track( global, track )
65 if( track.index01 == nil ) then
70 t.path = cue_path( track.file or global.file )
72 t.album = global.title
73 t.artist = track.performer or global.performer
74 t.genre = track.genre or global.genre
75 t.date = track.date or global.date
76 t.description = global.comment
77 t.tracknum = track.num
78 t.options = { ":start-time=" .. track.index01}
83 function cue_append( tracks, global, track )
84 local t = cue_track( global, track )
86 if( #tracks > 0 ) then
87 local prev = tracks[#tracks]
88 table.insert( prev.options, ":stop-time=" .. track.index01)
90 table.insert( tracks, t )
103 line = vlc.readline()
104 if not line then break end
106 cmd, arg = string.match( line, "^%s*(%S+)%s*(.*)$" )
108 if( cmd == "REM" and arg ) then
109 subcmd, value = string.match( arg, "^(%S+)%s*(.*)$" )
110 if( subcmd == "GENRE" and value ) then
111 data.genre = cue_string( value )
112 elseif( subcmd == "DATE" and value ) then
113 data.date = cue_string( value )
114 elseif( subcmd == "COMMENT" and value ) then
115 data.comment = cue_string( value )
117 elseif( cmd == "PERFORMER" and arg ) then
118 data.performer = cue_string( arg )
119 elseif( cmd == "TITLE" and arg ) then
120 data.title = cue_string( arg )
121 elseif( cmd == "FILE" ) then
122 file = cue_string( arg )
123 elseif( cmd == "TRACK" ) then
124 if( not global_data ) then
127 cue_append( p, global_data, data )
129 data = { file = file, num = string.match( arg, "^(%d+)" ) }
130 elseif( cmd == "INDEX" ) then
131 local idx, m, s, f = string.match( arg, "(%d+)%s+(%d+):(%d+):(%d+)" )
132 if( idx == "01" and m ~= nil and s ~= nil and f ~= nil ) then
133 data.index01 = m * 60 + s + f / 75
138 cue_append( p, global_data, data )