EnviOpen.h 1.8 KB
#ifndef ENVIOPEN_H
#define ENVIOPEN_H
#include <string>
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>
#include <stdio.h>
#include "EnviHeader.h"
#include "EnviFid.h"
using namespace std;



//return value is incorrect
static EnviFidClass EnviOpen(string filename, char* mask = NULL, string mode = "r", int nBands = 0, int sX = 0)
{
	//create a file ID object
	EnviFidClass fid;

    //set the file read/write mode
    fid.mode = mode;

    //set the mask (if specified)
	fid.mask = mask;

	// load the header file only while reading
	if(mode=="r")
	{
        //load the header file
        int error = fid.header.LoadHeader(filename);

        //if the LoadHeader function is not zero, the header is invalid
        if (error != 0)
        {
            fid.isValid = 0;
            return fid;
        }

	}

	//default header to be used in case of writing a file
	else
	{

		fid.header.datatype= 4;
		fid.header.nF= nBands;
		fid.header.sX=0;
		fid.header.sY=1;
		fid.header.headersize=0;
	}
    //load the binary file associated with this header
        //get the binary file name

	 //find the binary file name

    //locate the position of the extension
    int pos = filename.find(".hdr");
    string binaryname = filename.substr(0, pos);
        //load the binary file using fopen()
        //store the resulting FILE pointer in the EnviFidClass variable
	mode += "b";
	fid.file = fopen (binaryname.c_str(), mode.c_str());
	if(fid.file == NULL)
	{
		cout<<"Error opening binary file."<<endl;
		fid.isValid = 0;
		return fid;
	}

	//set the file name
	fid.fileName = binaryname;

    fid.isValid = 1;
	return fid;
}

static EnviFidClass EnviOpen(EnviFidClass fid, char* mask = NULL, string mode = "r")
{
	string filename = fid.fileName;
	filename += ".hdr";

	return EnviOpen(filename, mask, mode);
}

#endif