/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package usda.weru.util.gis;

//import gisviewer.shapefile.Swab;
import java.io.DataInputStream;
import java.io.EOFException;
import de.schlichtherle.truezip.file.TFileInputStream;
import java.io.IOException;
import java.util.Vector;
import org.apache.log4j.Logger;
import usda.weru.util.Util;

/**
 *
 * @author wjr
 */
public class ReadDBFFile {
    private static final Logger LOGGER = Logger.getLogger(ReadDBFFile.class);
    Vector<DBFFieldHeader> V_fh = new Vector<DBFFieldHeader>();
    Vector<String> V_data = new Vector<String>();

	/**
	 *
	 * @param fileName
	 */
	public ReadDBFFile(String fileName) {
        DataInputStream in = null;
        int multiPartPolygons = 0;

//        ReadShapefile rsf = new ReadShapefile();
        try {
            fileName = Util.purgeExtensions(fileName, Util.getExtensions(fileName));
            
            if (!fileName.endsWith(".dbf")) fileName += ".dbf";
            in = new DataInputStream(new TFileInputStream(fileName));
            DBFFileHeader fh = new DBFFileHeader();
            fh.read(in);
//            System.out.println(fh);
            while (true) {
                DBFFieldHeader fldh = new DBFFieldHeader();
                fldh.read(in);
                if (fldh.name.length() == 0) {
                    break;
                }
                V_fh.add(fldh);
//                System.out.println(fldh + " avail " + in.available());
            }
            readData(V_fh, in);
        } catch (EOFException eofe) {
//            System.out.println("done " + multiPartPolygons);
        } catch (IOException e) {
            //System.err.println("RunFileData: " + e);
            e.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException e) {
            }
        }

    }

	/**
	 *
	 * @param vec
	 * @param dis
	 * @throws IOException
	 */
	public void readData(Vector<DBFFieldHeader> vec, DataInputStream dis) throws IOException {
        while (true) {
            byte delFlg = dis.readByte();
            if (delFlg == 0x1A) throw new EOFException();
            String prtStr = "" + delFlg;
            for (DBFFieldHeader fh : vec) {
                byte[] buf = new byte[fh.recLen];
                dis.read(buf);
                prtStr += "|" + new String(buf);
            }
//            System.out.println(prtStr);
            V_data.add(prtStr);
        }
    }

	/**
	 *
	 * @param name
	 * @return
	 */
	public int getHeaderIndex(String name){
        for (int i = 0; i < V_fh.size(); i++){
            if (name.equals(V_fh.get(i).name)){
                return i + 1;
            }
        }        
        return -1;
    }

	/**
	 *
	 * @param idx
	 * @return
	 */
	public String getRecord(int idx) {
        try {
        return V_data.get(idx).toString();
        } catch (ArrayIndexOutOfBoundsException aioobe) {
            return "no record";
        }
    }

	/**
	 *
	 * @param args
	 */
	public static void main(String[] args) {
        if (args.length == 0) {
            args = new String[1];
            args[0] = "db/wind_gen/WINGEN_20090508/WINGEN_20090508";
//            args[0] = "data/CLIGEN_areas_04_04_08/CLIGEN_areas_04_04_08";
//            args[0] = "C:/weps.svn/GISViewer/uDig/data-v1_1/cities.shp";
//            args[0] = "C:/weps.svn/GISViewer/uDig/data-v1_1/countries.shp";
        }
        new ReadDBFFile(args[0]);

    }
}
