#include "aaspi_framework.h" #include "aaspi_lexer.h" #include #include #include #include #ifdef _WIN32 # include #else # include # include #endif using namespace std; AASPI_Framework::AASPI_Framework() { } AASPI_Framework::~AASPI_Framework() { close_all_files(); } void AASPI_Framework::init() { setup_datapath(); } void AASPI_Framework::set_argvec(int argc, char **argv) { if (argc > 0) { prog_name=string(argv[0]); } int i; argvec.resize(argc-1); for (i=1; i 0) argvec.resize(len); } void AASPI_Framework::set_arg(int pos, char *arg) { string sarg=requote_arg(arg); if (argvec.size() >= size_t(pos)) argvec.resize(pos+1); argvec[pos]=sarg; if (pos == 0) prog_name=sarg; istringstream ssr(sarg); AASPI_Lexer clex(ssr); AASPI_Token tok=clex.lex(); if (tok.typ != AASPI_Lexer::FailEnd) { cmdline_dict.set_string(tok.Name,tok.Val); } } string AASPI_Framework::getHostName() { char hname[256]; int rval=gethostname(hname,256); //now working. Need to add ws2_32.lib to additional library dependencies in Fortran project hname[255]='\0'; if (rval < 0) return string(""); return string(hname); } string AASPI_Framework::getUserName() { #ifdef _WIN32 // This has not been tested. char buff[256]; DWORD sz=sizeof(buff); GetUserNameA(buff,&sz); //now working. Need to add advapi32.lib to additional library dependencies in Fortran project return string(buff); #else struct passwd *pwe=getpwuid(geteuid()); if (!pwe) return string("Unknown"); if (!pwe->pw_name) return string("Unknown"); return string(pwe->pw_name); #endif } string AASPI_Framework::getTime() { time_t tim=time(0); string str=string(ctime(&tim)); int pos=str.size()-1; if (str[pos] == '\n') str.erase(pos); return str; } // Just use the environment variable for now!! // Needs to be expanded for windows string AASPI_Framework::getHomeDir() { #ifdef _WIN32 return str_getenv("HOMEDRIVE")+'\\'+str_getenv("HOMEPATH"); #else return str_getenv("HOME"); #endif } void AASPI_Framework::setup_datapath() { string envpath =str_getenv("DATAPATH"); //cout << "ENV Datapath:" << envpath << endl; string dpathfile=".datapath"; string locpath=read_datapath_file(dpathfile); dpathfile=homeDir()+"/.datapath"; string hdirpath=read_datapath_file(dpathfile); //cout << "Local .datapath path="<isReadable()) { cerr << "Warning: File already open and not readable:" << endl; cerr << "File: " << fname << endl; } return inf; } // Need to add checks to see if file opened OK AASPI_File *inf=new AASPI_File; if ( ! inf->OpenIn(fname)) { delete inf; cerr << "Failed to open:" << fname << endl; return 0; // Return Null if it fails } file_map[fname]=inf; inf->setFramework(this); return inf; } AASPI_File *AASPI_Framework::get_file_out(const string &tag, int dform, int esz) { if (tag.empty()) { cerr << "Warning: Requested empty output file tag." << endl; return 0; } string fname; // Look for 'tag=history' on the command line. // Then Look for the file `tag' in the current directory. if (cmdline_dict.is_defined(tag)) { fname=cmdline_dict.get_string(tag); } else { fname=tag; } // Look in file_list file_map_iterator it=file_map.find(fname); if (it != file_map.end()) { AASPI_File *outf=(*it).second; if (!outf->isWritable()) { cerr << "Warning: File already open and not writable" << endl; cerr << "File: " << fname << endl; } return outf; } // Need to add checks to see if file opened OK AASPI_File *outf=new AASPI_File; if ( ! outf->OpenOut(fname,dform,esz,datapath())) { delete outf; return 0; // Return Null if it fails } file_map[fname]=outf; outf->setFramework(this); return outf; } // A function specifically for zgy import when user opt to use zgy binary format for AASPI // We don't want to overwrite the input zgy with a blank output zgy file!!! AASPI_File *AASPI_Framework::get_file_out_no_binary(const string &tag, int dform, int esz) { if (tag.empty()) { cerr << "Warning: Requested empty output file tag." << endl; return 0; } string fname; // Look for 'tag=history' on the command line. // Then Look for the file `tag' in the current directory. if (cmdline_dict.is_defined(tag)) { fname = cmdline_dict.get_string(tag); } else { fname = tag; } // Look in file_list file_map_iterator it = file_map.find(fname); if (it != file_map.end()) { AASPI_File *outf = (*it).second; if (!outf->isWritable()) { cerr << "Warning: File already open and not writable" << endl; cerr << "File: " << fname << endl; } return outf; } // Need to add checks to see if file opened OK AASPI_File *outf = new AASPI_File; if (!outf->OpenOutNoBinary(fname, dform, esz, datapath())) { delete outf; return 0; // Return Null if it fails } file_map[fname] = outf; outf->setFramework(this); return outf; } // A function to force writing out with AASPI binary. This is useful mostly for SEGY reader AASPI_File *AASPI_Framework::get_file_out_force_aaspi_binary(const string &tag, int dform, int esz) { if (tag.empty()) { cerr << "Warning: Requested empty output file tag." << endl; return 0; } string fname; // Look for 'tag=history' on the command line. // Then Look for the file `tag' in the current directory. if (cmdline_dict.is_defined(tag)) { fname = cmdline_dict.get_string(tag); } else { fname = tag; } // Look in file_list file_map_iterator it = file_map.find(fname); if (it != file_map.end()) { AASPI_File *outf = (*it).second; if (!outf->isWritable()) { cerr << "Warning: File already open and not writable" << endl; cerr << "File: " << fname << endl; } return outf; } // Need to add checks to see if file opened OK AASPI_File *outf = new AASPI_File; if (!outf->OpenOutForceAASPIBinary(fname, dform, esz, datapath())) { delete outf; return 0; // Return Null if it fails } file_map[fname] = outf; outf->setFramework(this); return outf; } void AASPI_Framework::close_file(const string &tag) { // Look for 'tag=history' on the command line. // Then Look for the file `tag' in the current directory. string fname; if (cmdline_dict.is_defined(tag)) { fname=cmdline_dict.get_string(tag); } else { fname=tag; } file_map_iterator it=file_map.find(fname); if (it != file_map.end()) { AASPI_File *fl=(*it).second; fl->Close(); (*it).second=0; delete fl; file_map.erase(it); } } void AASPI_Framework::close_all_files() { file_map_iterator it; for (it=file_map.begin(); it != file_map.end(); ++it) { AASPI_File *fl=(*it).second; cerr << "Closing file: " << (*it).first << endl; fl->Close(); // ****FIX THIS -- MEMORY LEAK****** // delete fl; (*it).second=0; } file_map.clear(); } string AASPI_Framework::requote_arg(const string &arg) { size_t first_space=arg.find_first_of(" \t"); if (first_space == string::npos) return arg; // No worries no spaces size_t slen=arg.size(); size_t first_eq=arg.find('='); if (first_eq == string::npos) return arg; // No = found size_t p1=first_eq+1; if (p1 == slen) return arg; // Ends in = return arg.substr(0,p1)+'"'+arg.substr(p1)+'"'; } // A function to check if a point fall inside a polygon // Might be needed in fortran later, so I'm putting it here. // Source: http://stackoverflow.com/questions/11716268/point-in-polygon-algorithm bool AASPI_Framework::InsidePolygon(float x, float y, float *pol_x, float *pol_y, int n) { int i, j; bool c = false; for (i = 0, j = n - 1; i < n; j = i++) { if (((pol_y[i] > y) != (pol_y[j] > y)) && (x < (pol_x[j] - pol_x[i])*(y - pol_y[i]) / (pol_y[j] - pol_y[i]) + pol_x[i])) c = !c; } return c; }