#ifndef AASPI_FILE_H #define AASPI_FILE_H #include "aaspi_file_io.h" #include "aaspi_hist_file.h" #include // This class is really the manager for the group of files: // 1) The history file // 2) The associated data file // 3) The header history file (if any) // 4) The header data file (if any) // // And possibly in the future: // 5) The grid history file // 6) The grid data file class AASPI_Framework; class AASPI_File { public: enum DataType { xdr_float=1, xdr_int=2, xdr_byte=3, native_float=4, native_int=5, native_byte=6 }; enum { read_bit=0x1, write_bit=0x2 }; AASPI_File() { init(); } ~AASPI_File() { Close(); } void setFramework (AASPI_Framework *safw) { afw=safw; hist.setFramework(afw); } bool OpenIn(const string &fname); bool OpenOut(const string &fname,int dformat, int esz, const string &dpath = "" ); bool OpenOutNoBinary(const string &fname, int dformat, int esz, const string &dpath = ""); bool OpenOutForceAASPIBinary(const string &fname, int dformat, int esz, const string &dpath = ""); bool Close(); bool CopyHistFrom(const AASPI_File &hfile) { bool rv=hist.CopyFrom(hfile.hist); write_hist_stamp(); return rv; } bool CopyDataPointer(AASPI_File &hfile); aaspi_ssize_t Read(void *dst, size_t len); aaspi_ssize_t Write(char *src, size_t len); bool Seek(aaspi_off_t pos, int whence=SEEK_SET) { #ifdef _WIN32 if (zgy_binary) { return binf.SeekZGY(pos, whence); } else { #endif return binf.Seek(pos, whence); #ifdef _WIN32 } #endif } aaspi_off_t Tell() { #ifdef _WIN32 if (zgy_binary) { return binf.TellZGY(); } else { #endif return binf.Tell(); #ifdef _WIN32 } #endif } const string &dataFilename() { return binf.Filename(); } int get_num_axes() { return hist.get_num_axes(); } int get_axis_par(int iax, int &n, float &o, float &d, string &label) { return hist.get_axis_par(iax,n,o,d,label); } int put_axis_par(int iax, int n, float o, float d, const string &label) { return hist.put_axis_par(iax,n,o,d,label); } AASPI_Dictionary &getDict() { return hist.getDict(); } bool isReadable() { return flags & read_bit; } bool isWritable() { return flags & write_bit; } int get_esize() const { return esize; } int get_hdr_esize() const { return hdr_esize; } int get_data_format() const { return data_format; } // Passthru to history file template inline void getParam(const string &tag, _T &val) { hist.get_par(tag,val); } template inline void putParam(const string &tag, _T val) { write_hist_stamp(); hist.put_par(tag,val); } // Need this explicit overload to keep the compiler // from making the wrong choice of type inline void putParam(const string &tag, bool val) { write_hist_stamp(); hist.put_par_bool(tag,val); } inline bool hasParam(const string &tag) { return hist.has_par(tag); } // Header file functions void get_number_keys(int &numkeys); int get_header_axis_par(int iax, int &n, float &o, float &d, string &label) { return hdr_hist.get_axis_par(iax,n,o,d,label); } void get_key_name(const int keyindex, string &keyname); void get_key_index(const string &keyname, int &keyindex); void get_key_format(const int keyindex, int &keyfmt); void get_key_format(const int keyindex, string &keyfmt); void get_key_type(const int keyindex, string &keytyp); void get_key_val_by_index(int recno, const int keyindex, int nval, int *vals); void get_key_val_by_name(int recno, const string &keyname, int nval, int *vals); void get_val_headers(int recno, int nhdr, int *vals); bool CopyHeaderFrom(const AASPI_File &hfile) { if (!hdr_hist.isOpen()) OpenOutHeader(); bool rv=hdr_hist.CopyFrom(hfile.hdr_hist); hdr_esize=hfile.hdr_esize; hdr_n1=hfile.hdr_n1; write_header_stamp(); return rv; } void put_number_keys(int numkeys); void put_header_axis_par(int iax, int n, float o, float d, const string &label); void put_key(const string &keyname, const string &keytyp, const string &keyfmt, int keyindex); void put_val_by_index(int recno, int keyindex, int nval, int *vals); void put_val_headers(int recno, int nhdr, int *vals); // Statics static string DatatypeToString(int dtyp); static int StringToDatatype(string dstr); #ifdef _WIN32 bool zgy_binary; bool first_write; // whether to initialize Writer. // Thang Ha, 20180613: note: binf is a protected variable, and therefore cannot be passed to a function. // Better define these void procedures to be used in-house only void update_zgy_meta(); // update binary zgy metadata before writing. Hopefully at this point the zgy_meta.size array is set up. Otherwise we are doomed. void updateDataRange(); int get_zgy_data_type(); #endif protected: AASPI_Framework *afw; AASPI_HistoryFile hist, hdr_hist; AASPI_BinFile binf, hdr_binf; AASPI_HistoryFile *hfp; // Used for borrowed history files string hist_name, hf_name; string data_path; bool has_header_file; bool hist_stamp_written; bool head_stamp_written; int flags; int esize; // 4 or 8 (for complex) ??? int hdr_esize; // for completeness int hdr_n1; // number of keys; int data_format; void init() { afw=0; hfp=0; flags=0; data_format=xdr_float; esize=4; hdr_esize=4; hdr_n1=0; has_header_file=false; hist_stamp_written=false; head_stamp_written=false; #ifdef _WIN32 zgy_binary = false; // default binary is assumed to be of AASPI type (.H@) first_write = true; #endif } void translate_from(void *buf, size_t len); void translate_to(void *buf, size_t len); bool OpenInHeader(); bool OpenOutHeader(); string historyFilename() { return hist.filename(); } string headerFilename() { return hdr_hist.filename(); } void write_hist_stamp() { if (hist_stamp_written) return; string dfname=binf.Filename(); hist.put_par("in",dfname); hist_stamp_written=true; } void write_header_stamp() { if (head_stamp_written) return; string hdf_name=hdr_binf.Filename(); hdr_hist.put_par("in",hdf_name); head_stamp_written=true; } #ifdef _WIN32 bool write_zgy_binary(); // determine whether to write data binary as zgy format based on default param file #endif }; #endif