//
// https://www.ramecera.it/myscrewdriver.html
// e-mail: screwdriver@ramecera.it
//
/* This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License v1.0 which accompanies this
 * distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html */
package it.ramecera.salad.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import it.ramecera.salad.everyday.Candies;
import it.ramecera.salad.everyday.ExactException;
import it.ramecera.salad.everyday.Mix;

/**
 * <p>
 * Class for reading files. The class should not be instantiated.
 * </p>
 * <p>
 * Company: Ramecera™<br>
 * Creation date: 2020-04-09<br>
 * <p>
 * Changes:<br>
 * </p>
 * 
 * @author M. Maglioni */

public class ReadMyFile {

	/**
	 * <p>
	 * Reading a sequential file. It is intended as structured with
	 * lines terminated by CR / LF or LF; the method reads the lines on
	 * a List of strings.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * 
	 * @param g file name, complete with path
	 * @return a List of strings
	 * @throws FileNotFoundException is triggered when the file does not
	 *         exist
	 * @throws IOException is triggered when it is not possible to read,
	 *         for any other reason */
	public static List<String> zebra_A(final String g)

			throws IOException, FileNotFoundException {

		List<String> al = new ArrayList<String>();
		al.clear();
		String riga = null;
		FileReader fr = null;
		try {
			fr = new FileReader(g);
		} catch (FileNotFoundException enf) {
			throw enf;
		}
		BufferedReader in = new BufferedReader(fr);
		do {
			try {
				riga = in.readLine();
			} catch (IOException eio) {
				in.close();
				throw eio;
			}
			al.add(riga);
		} while (riga != null);
		in.close();
		al.remove(al.size() - 1);
		return al;
	}

	/**
	 * <p>
	 * Reading a sequential file. It is intended as structured with
	 * lines terminated by CR / LF or LF; the method reads the lines on
	 * a List of strings. It differs from zebra_A in that it ignores
	 * empty lines, which therefore do not weigh down the list.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * 
	 * @param g file name, complete with path
	 * @return a List of strings
	 * @throws FileNotFoundException is triggered when the file does not
	 *         exist
	 * @throws IOException is triggered when it is not possible to read,
	 *         for any other reason */
	public static List<String> zebra_B(final String g)

			throws IOException, FileNotFoundException {

		List<String> al = new ArrayList<String>();
		String line = null;
		FileReader fr = null;
		try {
			fr = new FileReader(g);
		} catch (FileNotFoundException enf) {
			throw enf;
		}
		BufferedReader in = new BufferedReader(fr);
		do {
			try {
				line = in.readLine();
			} catch (IOException eio) {
				in.close();
				throw eio;
			}
			if (line != null && !line.equals("")) {
				al.add(line);
			}
		} while (line != null);
		in.close();
		return al;
	}

	/**
	 * <p>
	 * Reading a sequential file. It is intended as structured with
	 * lines terminated by CR/LF or LF; the method reads the lines on a
	 * list of strings.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * 
	 * @param file the object File to be read
	 * @param code Unicode code to be adopted
	 * @return a List of strings
	 * @throws ExactException standard exception */
	public static List<String> zebra_C(File file, final String code)

			throws ExactException {

		String s = Equipment.oneStringUnicode_A(file, code);
		List<String> r = new ArrayList<String>();
		String[] p = s.split("\\x0a");
		for (String q : p) {
			if (q.endsWith(Candies.CR)) {
				q = q.substring(0, q.length() - 1);
			}
			r.add(q);
		}
		return r;
	}

	/**
	 * <p>
	 * As above, but the input provided is a File object.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * 
	 * @param f the file object
	 * @return a List of strings
	 * @throws FileNotFoundException is triggered when the file does not
	 *         exist
	 * @throws IOException is triggered when it is not possible to read,
	 *         for any other reason */
	public static List<String> zebra_A(File f)

			throws IOException, FileNotFoundException {

		return zebra_A(f.getPath());
	}

	/**
	 * <p>
	 * Production of the list of files of a directory, filtered
	 * according to a pattern.
	 * </p>
	 * <p>
	 * Creation date: 2020-04-09<br>
	 * Last updated: 2020-04-09
	 * </p>
	 * <p>
	 * Changes:<br>
	 * </p>
	 * 
	 * @return an array list of strings with the found files
	 * @param x the pattern to filter the files, according to the codes
	 *        of the java.util.regex.Pattern class.
	 * @param y the path to search for
	 * @since 1.0.0 files */
	public static List<String> listaFile(final String x,
			final String y) {

		// if necessary, I put a slash in the queue
		File f = new File(y);
		List<String> l = new ArrayList<String>();
		if (!f.exists() || !f.isDirectory()) // quality control
			return l;
		String[] sl = f.list(Mix.fileFilter(x));
		for (int i = 0; i < sl.length; i++)
			l.add(sl[i]);
		return l;
	}
}