package com.mamewo.malarm;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Vector;

/**
 * @author Takashi Masuyama <mamewotoko@gmail.com>
 */

public class M3UPlaylist implements Playlist{
	private int _nextindex = 0;
	String _basepath;
	private Vector<String> _playlist;
	
	public M3UPlaylist(String basepath, String playlistpath) {
		_basepath = basepath;
		String sep = System.getProperty("file.separator");
		if (! _basepath.endsWith(sep)) {
			_basepath = _basepath + sep;
		}
		try {
			_playlist = new Vector<String>();
			loadPlaylist(playlistpath);
		} catch (FileNotFoundException e) {
			//TODO: set default playlist?
			e.printStackTrace();
		} catch (IOException e) {
			//TODO: set default playlist?
			e.printStackTrace();
		}
	}
	
	@Override
	public boolean isEmpty() {
		return _playlist.isEmpty();
	}
	
	@Override
	public String next() {
		if (_playlist.size() <= _nextindex) {
			_nextindex = 0;
		}
		String result = _playlist.get(_nextindex);
		_nextindex++;
		return _basepath + result;
	}

	@Override
	public void reset() {
		_nextindex = 0;
	}

	protected void loadPlaylist(String filename) throws FileNotFoundException, IOException {
		BufferedReader br = new BufferedReader(new FileReader (filename));
		String music_filename;
		while ((music_filename = br.readLine()) != null) {
			_playlist.add(music_filename);
		}
		br.close();
	}
}

@@ -1074,26 +1074,118 @@
 ;%0A%09%09
-return _basepath +
+if (result.indexOf(System.getProperty(%22file.separator%22)) == -1) %7B%0A%09%09%09result = _basepath + result;%0A%09%09%7D%0A%09%09return
  res
@@ -1482,24 +1482,68 @@
  != null) %7B%0A
+%09%09%09if (! music_filename.startsWith(%22#%22)) %7B%0A%09
 %09%09%09_playlist
@@ -1564,16 +1564,21 @@
 ename);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%09%09br

support full path of music filename ignore directive in m3u file