1 /* 2 Gri - A language for scientific graphics programming 3 Copyright (C) 2008 Daniel Kelley 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along 16 with this program; if not, write to the Free Software Foundation, Inc., 17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 */ 19 20 // Object for data files 21 #if !defined(_DATAFILE_HH_) 22 #define _DATAFILE_HH_ 23 #include <string> 24 #include <vector> // part of STL 25 #include <stdio.h> 26 27 #include "types.hh" 28 #include "macro.hh" 29 #include "CmdFile.hh" 30 31 class DataFile 32 { 33 public: 34 enum type {from_cmdfile, ascii, bin_unknown, bin_uchar, bin_16bit, bin_int, bin_float, bin_double, bin_netcdf}; DataFile()35 DataFile() { 36 extern std::vector<CmdFile> _cmdFILE; 37 name.assign("stdin"); 38 if (_cmdFILE.size() > 0) { 39 fp = _cmdFILE.end()->get_fp(); 40 if (superuser() & FLAG_AUT1)printf(" DEBUG: %s:%d construct from cmdFILE fp= %lx ",__FILE__,__LINE__,(long unsigned int)(fp)); 41 } else { 42 if (superuser() & FLAG_AUT1)printf(" DEBUG: %s:%d construct from stdin fp= %lx ",__FILE__,__LINE__,(long unsigned int)(stdin)); 43 fp = stdin; 44 } 45 if (fp == (FILE*)NULL) {printf("ERROR. Null fp [blank-constructor phase] %s:%d\n",__FILE__, __LINE__);} 46 the_type = from_cmdfile; 47 line = 1; 48 delete_when_close = false; 49 if (superuser() & FLAG_AUT1)printf(" ... %s:%d name= '%s'\t fp= %lx this= %lx\n",__FILE__,__LINE__,name.c_str(),long(fp),long(this)); 50 } DataFile(const DataFile & d)51 DataFile(const DataFile& d) { 52 name.assign(d.get_name()); 53 fp = d.get_fp(); 54 //if (fp == (FILE*)NULL) {printf("ERROR. Null fp [copy-constructor phase] %s:%d\n",__FILE__, __LINE__);} 55 netCDF_id = d.get_netCDF_id(); 56 the_type = d.get_type(); 57 line = d.get_line(); 58 delete_when_close = d.get_delete_when_close(); 59 if (superuser() & FLAG_AUT1)printf(" DEBUG: %s:%d DataFile(%lx) name= '%s'\t fp= %lx this= %lx\n",__FILE__,__LINE__,long(&d),name.c_str(),long(fp),long(this)); 60 } DataFile(FILE * a_fp,const char * a_name,int a_netCDF_id,type a_the_type,bool a_delete_when_close)61 DataFile(FILE* a_fp, const char* a_name, int a_netCDF_id, type a_the_type, bool a_delete_when_close) { 62 fp = a_fp; 63 name.assign(a_name); 64 netCDF_id = a_netCDF_id; 65 the_type = a_the_type; 66 line = 0; 67 delete_when_close = a_delete_when_close; 68 if (superuser() & FLAG_AUT1)printf(" DEBUG: %s:%d DataFile(fp= %lx, name= '%s', ...) this= %lx\n",__FILE__,__LINE__,long(a_fp),a_name,long(this)); 69 } ~DataFile()70 ~DataFile() { 71 if (superuser() & FLAG_AUT1)printf(" DEBUG: %s:%d DataFile::~DataFile() name= '%s'\t fp= %lx this= %lx\n",__FILE__,__LINE__, name.c_str(), long(fp), long(this)); 72 #if 0 // BUG 2001-feb-17 -- not sure on next 2 lines 73 name.string::~string(); // not executed 74 #endif 75 } operator =(const DataFile & d)76 DataFile& operator=(const DataFile& d) { 77 name.assign(d.get_name()); 78 fp = d.get_fp(); 79 if (fp == (FILE*)NULL) {printf("ERROR. Null fp [operator= phase] %s:%d\n",__FILE__, __LINE__);} 80 netCDF_id = d.get_netCDF_id(); 81 the_type = d.get_type(); 82 line = d.get_line(); 83 delete_when_close = d.get_delete_when_close(); 84 if (superuser() & FLAG_AUT1)printf(" DEBUG: %s:%d DataFile::operator= name= '%s'\t fp= %lx this= %lx\n",__FILE__,__LINE__, name.c_str(), long(fp), long(this)); 85 return *this; 86 } set_line(int new_line)87 void set_line(int new_line) {line = new_line > 1 ? new_line : 1;} increment_line()88 void increment_line() {line++; } get_name() const89 const char *get_name() const {return name.c_str(); } get_fp() const90 FILE* get_fp() const {return fp; } get_netCDF_id() const91 int get_netCDF_id() const {return netCDF_id; } get_type() const92 type get_type() const {return the_type; } get_line() const93 int get_line() const {return line; } get_delete_when_close() const94 bool get_delete_when_close() const {return delete_when_close; } 95 private: 96 FILE* fp; // file pointer 97 std::string name; // name of file, or stdin 98 int netCDF_id; // only used if FILE_BIN_NETCDF 99 type the_type; // 100 int line; // current line number 101 bool delete_when_close; // for open "...|" 102 }; 103 #endif // _DATAFILE_HH_ 104