import java.io.*;
import java.util.ArrayList;
public class CSVReader {
BufferedReader _reader;
int _delimiter;
public CSVReader(BufferedReader reader) {
this._reader = reader;
this._delimiter = ',';
}
public CSVReader(BufferedReader reader,int delimiter) {
this._reader = reader;
this._delimiter = delimiter;
}
public String[] readLine(){
try {
String line = this._reader.readLine();
if(line == null) return null;
String[] fields = parseLine(line);
return fields;
} catch (Exception e) {
e.printStackTrace(System.err);
return null;
}
}
void close(){
try {
this._reader.close();
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
String[] parseLine(String s){
try {
java.util.ArrayList<String> elements = new ArrayList<String>();
StringReader reader = new StringReader(s);
StringWriter elementWriter = new StringWriter();
int c = 0;
boolean inQuots = false;
while( (c = reader.read()) != -1){
if(c == 0) continue;
if(c == this._delimiter){
if(!inQuots){
elements.add(elementWriter.toString() + "");
elementWriter = new StringWriter();
continue;
}
elementWriter.write(c);
continue;
}
if(c == '"'){
if(!inQuots){
inQuots = true;
continue;
}
else{
inQuots = false;
continue;
}
}
if(c == '\\'){
c = reader.read();
if(c == -1) break;
if(c == 'r'){
elementWriter.write('\r');
continue;
}
if(c == 'n'){
elementWriter.write('\n');
continue;
}
if(c == 't'){
elementWriter.write('\t');
continue;
}
if(c == 'f'){
elementWriter.write('\f');
continue;
}
if(c == 'b'){
elementWriter.write('\b');
continue;
}
if(c == 'u'){
//読み飛ばす!
reader.read();
reader.read();
reader.read();
reader.read();
continue;
}
if(c == '\\'){
elementWriter.write('\\');
continue;
}
}
elementWriter.write(c);
}
String sss = elementWriter.toString();
elements.add(sss + "");
String[] result = elements.toArray(new String[elements.size()]);
return result;
}
catch (Exception e) {
// TODO: handle exception
e.printStackTrace(System.err);
return null;
}
}
}