/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.abarhub.filerw;

import java.io.*;
import java.util.*;

/**
 *
 * @author abarhub
 */
public class ReadWriteAscii<T extends Field> {
	//<T extends Enum<T>&Field>
    // le enum n'est pas necessaire, et peut être enlevé
    private File file;
    private final List<T> fieldsList;
    private enum Separator{NewLine,NoSeparator,String};
    private Separator separator=Separator.NewLine;
    private String stringSeparator;
    
    public ReadWriteAscii(File file,List<T> liste_champs){
        this.file=file;
        this.fieldsList=liste_champs;
    }
        
    public ReadWriteAscii(File file,Class<T> clazz){
        this.file=file;
        this.fieldsList=new ArrayList<T>();
        for (T option : clazz.getEnumConstants()) {
            fieldsList.add(option);
        }
    }
    
    public FileContentAscii<T> readFile() throws FileNotFoundException, IOException
    {
        FileContentAscii<T> res;
        BufferedReader buf=null;
        LineContentAscii<T> ligne;
        String ligne2;
        res=new FileContentAscii<T>();
        try{
            buf=new BufferedReader(new FileReader(file));
            if(separator==Separator.NewLine)
            {
	            while((ligne2=buf.readLine())!=null)
	            {
	                if(ligne2!=null&&ligne2.length()>0)
	                {
	                	ligne=new LineContentAscii<T>(fieldsList, ligne2);
	                	res.add(ligne);
	                }
	            }
            }
            else
            {
            	char buf2[]=new char[getSize()];
            	int len;
            	while((len=buf.read(buf2))!=-1)
	            {
	                if(len>0)
	                {
	                	ligne2=new String(buf2);
	                	ligne=new LineContentAscii<T>(fieldsList, ligne2);
	                	res.add(ligne);
	                }
	            }
            }
        }finally{
            if(buf!=null)
            {
                buf.close();
                buf=null;
            }
        }
        return res;
    }

    public void WriteFile(File fileName,FileContentAscii<T> contenu_fichier) throws IOException
    {
    	PrintWriter out=null;
    	try{
    		out=new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
    		if(contenu_fichier!=null)
    		{
    			if(contenu_fichier.getListe()!=null)
    			{
    				for(LineContentAscii<T> ligne:contenu_fichier.getListe())
    				{
    					out.println(ligne.getLine());
    				}
    			}
    		}
    	}finally{
    		if(out!=null)
    		{
    			out.flush();
    			out.close();
    		}
    	}
    }
    
    public void setNewLineSeparator()
    {
    	separator=Separator.NewLine;
    	stringSeparator=null;
    }
    
    public boolean isNewLineException()
    {
    	return separator==Separator.NewLine;
    }
    
    public void setNoSeparator()
    {
    	separator=Separator.NoSeparator;
    	stringSeparator=null;
    }

    public boolean isNoSeparator()
    {
    	return separator==Separator.NoSeparator;
    }
    
    public void setStringSeparator(String sep)
    {
    	if(sep==null)
    		throw new IllegalArgumentException();
    	separator=Separator.String;
    	stringSeparator=sep;
    }
    
    public boolean isStringSeparator()
    {
    	return separator==Separator.String;
    }
    
    private int getSize()
    {
    	int res=0;
    	for(T field:fieldsList)
    	{
    		res=Math.max(res, field.getPosition()+field.getLength());
    	}
    	return res;
    }
}

@@ -1641,18 +1641,55 @@
     else
+ if(separator==Separator.NoSeparator)
 %0D%0A
-
         
@@ -2067,32 +2067,109 @@
 %0A            %7D%0D%0A
+            else%0D%0A            %7B%0D%0A            %09assert(false);%0D%0A            %7D%0D%0A
         %7Dfinally
@@ -2731,37 +2731,500 @@
 %09%09%09%09
-out.println(ligne.getLine());
+if(separator==Separator.NewLine)%0D%0A    %09%09%09%09%09%7B%0D%0A    %09%09%09%09%09%09out.println(ligne.getLine());%0D%0A    %09%09%09%09%09%7D%0D%0A    %09%09%09%09%09else if(separator==Separator.NoSeparator)%0D%0A    %09%09%09%09%09%7B%0D%0A    %09%09%09%09%09%09out.print(ligne.getLine());%0D%0A    %09%09%09%09%09%7D%0D%0A    %09%09%09%09%09else if(separator==Separator.String)%0D%0A    %09%09%09%09%09%7B%0D%0A    %09%09%09%09%09%09out.print(ligne.getLine());%0D%0A    %09%09%09%09%09%09if(stringSeparator!=null&&stringSeparator.length()%3E0)%0D%0A    %09%09%09%09%09%09%09out.print(stringSeparator);%0D%0A    %09%09%09%09%09%7D%0D%0A    %09%09%09%09%09else%0D%0A    %09%09%09%09%09%7B%0D%0A    %09%09%09%09%09%09assert(false);%0D%0A    %09%09%09%09%09%7D
 %0D%0A  

fix test