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

import java.io.*;
import java.text.ParseException;
import java.util.*;

import org.abarhub.filerw.ascii.LineContentAscii;
import org.abarhub.filerw.ascii.StructAsciiReader;

/**
 *
 * @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=Tools.convClassEnum(clazz);
    }

	public FileContentAscii<T> readFile() throws FileNotFoundException, IOException, ParseException
    {
        FileContentAscii<T> res;
        LineContentAscii<T> ligne;
        String ligne2;
        StructAsciiReader<T> buf=null;
        int i;
        res=new FileContentAscii<T>();
        try{
        	buf=new StructAsciiReader<T>(new BufferedReader(new FileReader(file)),fieldsList);
        	loop:{
            while((ligne=buf.readLn())!=null)
            {
            	res.add(ligne);
            	if(separator==Separator.NewLine)
                {
            		i=buf.read();
            		switch(i)
            		{
            		case -1:// EOF
            			break loop;
            		case '\n':
            			break;
            		case '\r':
            			i=buf.read();
            			switch(i)
            			{
            			case -1: // EOF
            				break loop;
            			case '\n':
            				break;
            			default:
                			throw new IOException("Bad format");
            			}
            			break;
            		default:
            			throw new IOException("Bad format");
            		}
                }
            	else if(separator==Separator.NoSeparator)
                {// nothing to do
            		
                }
                else
                {
                	assert(false);
                }
            }
        	}
		}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())
    				{
    					if(separator==Separator.NewLine)
    					{
    						out.println(ligne.getLine());
    					}
    					else if(separator==Separator.NoSeparator)
    					{
    						out.print(ligne.getLine());
    					}
    					else if(separator==Separator.String)
    					{
    						out.print(ligne.getLine());
    						if(stringSeparator!=null&&stringSeparator.length()>0)
    							out.print(stringSeparator);
    					}
    					else
    					{
    						assert(false);
    					}
    				}
    			}
    		}
    	}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;
    }
}

@@ -306,16 +306,68 @@
 Reader;%0D
+%0Aimport org.abarhub.filerw.ascii.StructAsciiWriter;%0D
 %0A%0D%0A/**%0D%0A
@@ -2754,27 +2754,36 @@
 %7B%0D%0A    %09
-Print
+StructAscii
 Writer
+%3CT%3E
  out=nul
@@ -2815,19 +2815,28 @@
 new 
-Print
+StructAscii
 Writer
+%3CT%3E
 (new
@@ -2876,16 +2876,27 @@
 leName))
+,fieldsList
 );%0D%0A    
@@ -3062,24 +3062,56 @@
 %0A    %09%09%09%09%7B%0D%0A
+    %09%09%09%09%09out.writeLine(ligne);%0D%0A
     %09%09%09%09%09if(
@@ -3175,31 +3175,16 @@
 println(
-ligne.getLine()
 );%0D%0A    
@@ -3269,35 +3269,8 @@
 %09%09%09%09
-out.print(ligne.getLine());
 %0D%0A  
@@ -3342,47 +3342,8 @@
 %09%7B%0D%0A
-    %09%09%09%09%09%09out.print(ligne.getLine());%0D%0A
     

integrate writer to readwriteascii