Changeset 11557 for trunk/Mars


Ignore:
Timestamp:
07/24/11 11:07:12 (13 years ago)
Author:
tbretz
Message:
Added exceptions if supported by the compiler; removed check for TELESCOP keyword
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Mars/mbase/fits.h

    r11550 r11557  
    1 #ifndef MARS_MFits
    2 #define MARS_MFits
     1#ifndef MARS_fits
     2#define MARS_fits
     3
     4#include <stdint.h>
    35
    46#include <map>
     
    79#include <algorithm>
    810
    9 #include <stdint.h>
     11#ifdef __EXCEPTIONS
     12#include <stdexcept>
     13#endif
    1014
    1115#ifdef __CINT__
     
    1418
    1519#ifndef __MARS__
    16 #include <vector>
    17 #include <fstream>
    1820#include <iomanip>
    19 #include <iostream>
    20 #define MZlib ifstream
    2121#define gLog cerr
    2222#define err ""
    2323#define all ""
    2424#else
    25 #include "MZlib.h"
    26 
    2725#include "MLog.h"
    2826#include "MLogManip.h"
    2927#endif
    3028
    31 using namespace std;
    32 
    33 class MFits : public MZlib
     29#include "izstream.h"
     30
     31namespace std
     32{
     33
     34class fits : public izstream
    3435{
    3536public:
     
    7879        {
    7980            // Trim Both leading and trailing spaces
    80             const size_t start = str.find_first_not_of(c); // Find the first character position after excluding leading blank spaces
    81             const size_t end   = str.find_last_not_of(c);  // Find the first character position from reverse af
     81            const size_t pstart = str.find_first_not_of(c); // Find the first character position after excluding leading blank spaces
     82            const size_t pend   = str.find_last_not_of(c);  // Find the first character position from reverse af
    8283
    8384            // if all spaces or empty return an empty string
    84             if (string::npos==start || string::npos==end)
     85            if (string::npos==pstart || string::npos==pend)
    8586                return string();
    8687
    87             return str.substr(start, end-start+1);
     88            return str.substr(pstart, pend-pstart+1);
    8889        }
    8990
     
    9394            if (it==keys.end())
    9495            {
    95                 gLog << err << "ERROR - Key '" << key << "' not found." << endl;
     96                ostringstream str;
     97                str << "Key '" << key << "' not found.";
     98#ifdef __EXCEPTIONS
     99                throw runtime_error(str.str());
     100#else
     101                gLog << err << "ERROR - " << str.str() << endl;
    96102                return false;
     103#endif
    97104            }
    98105
    99106            if (it->second.type!=type)
    100107            {
    101                 gLog << err << "ERROR - Wrong type for key '" << key << "': expected " << type << ", found " << it->second.type << "." << endl;
     108                ostringstream str;
     109                str << "Wrong type for key '" << key << "': expected " << type << ", found " << it->second.type << ".";
     110#ifdef __EXCEPTIONS
     111                throw runtime_error(str.str());
     112#else
     113                gLog << err << "ERROR - " << str.str() << endl;
    102114                return false;
     115#endif
    103116            }
    104117
    105118            if (!value.empty() && it->second.value!=value)
    106119            {
    107                 gLog << err << "ERROR - Wrong value for key '" << key << "': expected " << value << ", found " << it->second.value << "." << endl;
     120                ostringstream str;
     121                str << "Wrong value for key '" << key << "': expected " << value << ", found " << it->second.value << ".";
     122#ifdef __EXCEPTIONS
     123                throw runtime_error(str.str());
     124#else
     125                gLog << err << "ERROR - " << str.str() << endl;
    108126                return false;
     127#endif
    109128            }
    110129
     
    197216                    return;
    198217
    199                 const string name = Get<string>("TTYPE"+num.str());
    200                 const string fmt  = Get<string>("TFORM"+num.str());
    201 
    202                 istringstream in(fmt);
     218                const string id = Get<string>("TTYPE"+num.str());
     219                const string fmt = Get<string>("TFORM"+num.str());
     220
     221                istringstream sin(fmt);
    203222                int n = 0;
    204                 in >> n;
    205                 if (!in)
     223                sin >> n;
     224                if (!sin)
    206225                    n = 1;
    207226
     
    226245                // case 'Q': size = 16; break; // array descriptor (64bit)
    227246                default:
    228                     gLog << err << "FITS format '" << fmt << "' not yet supported." << endl;
    229                     continue;
     247                    {
     248                        ostringstream str;
     249                        str << "FITS format TFORM='" << fmt << "' not yet supported.";
     250#ifdef __EXCEPTIONS
     251                        throw runtime_error(str.str());
     252#else
     253                        gLog << err << "ERROR - " << str.str() << endl;
     254                        return;
     255#endif
     256                    }
    230257                }
    231258
    232259                const Table::Column col = { bytes, n, size, type };
    233260
    234                 cols[name] = col;
     261                cols[id] = col;
    235262                bytes  += n*size;
    236263            }
     
    238265            if (bytes!=bytes_per_row)
    239266            {
    240                 gLog << err << "Column size mismatch" << endl;
     267#ifdef __EXCEPTIONS
     268                throw runtime_error("Column size mismatch");
     269#else
     270                gLog << err << "ERROR - Column size mismatch" << endl;
    241271                return;
     272#endif
    242273            }
    243274
     
    275306            T Get(const string &key) const
    276307        {
    277             const ::map<string,Entry>::const_iterator it = keys.find(key);
     308            const map<string,Entry>::const_iterator it = keys.find(key);
    278309            if (it==keys.end())
    279310            {
    280                 gLog << err << "Key '" << key << "' not found." << endl;
     311                ostringstream str;
     312                str << "Key '" << key << "' not found." << endl;
     313#ifdef __EXCEPTIONS
     314                throw runtime_error(str.str());
     315#else
     316                gLog << err << "ERROR - " << str.str() << endl;
    281317                return 0;
     318#endif
    282319            }
    283320            return it->second.Get<T>();
     
    306343    vector<string> ReadBlock(vector<string> &vec)
    307344    {
    308         bool end = false;
     345        bool endtag = false;
    309346        for (int i=0; i<36; i++)
    310347        {
     
    324361
    325362            if (str=="END                                                                             ")
    326                 end = true;
    327 
    328             if (end)
     363                endtag = true;
     364
     365            if (endtag)
    329366                continue;
    330367
     
    346383
    347384public:
    348     MFits(const string &fname) : MZlib(fname.c_str())
     385    fits(const string &fname) : izstream(fname.c_str())
    349386    {
    350387        char simple[6];
     
    355392        if (memcmp(simple, "SIMPLE", 6))
    356393        {
    357             gLog << err << "File is not a FITS file." << endl;
    358394            clear(rdstate()|ios::badbit);
     395#ifdef __EXCEPTIONS
     396            throw runtime_error("File is not a FITS file.");
     397#else
     398            gLog << err << "ERROR - File is not a FITS file." << endl;
    359399            return;
     400#endif
    360401        }
    361402
     
    371412                if (!good())
    372413                {
    373                     gLog << err << "FITS file corrupted." << endl;
    374414                    clear(rdstate()|ios::badbit);
     415#ifdef __EXCEPTIONS
     416                    throw runtime_error("FITS file corrupted.");
     417#else
     418                    gLog << err << "ERROR - FITS file corrupted." << endl;
    375419                    return;
     420#endif
    376421                }
    377422
     
    396441
    397442                if (!fTable)
    398                     return;
    399 
    400                 if (!fTable.Check("TELESCOP", 'T', "FACT"))
    401443                {
    402                     gLog << err << "Not a valid FACT fits file." << endl;
     444                    clear(rdstate()|ios::badbit);
    403445                    return;
    404446                }
     
    427469    void ReadRow(size_t row)
    428470    {
    429         // if (row!=fRow+1) // Fast seeking is ensured by MZlib
     471        // if (row!=fRow+1) // Fast seeking is ensured by izstream
    430472        seekg(fTable.offset+row*fTable.bytes_per_row);
    431473
     
    439481        void revcpy(char *dest, const char *src, int num)
    440482    {
    441         const char *end = src + num*N;
    442         for (const char *ptr = src; ptr<end; ptr+=N, dest+=N)
     483        const char *pend = src + num*N;
     484        for (const char *ptr = src; ptr<pend; ptr+=N, dest+=N)
    443485            reverse_copy(ptr, ptr+N, dest);
    444486    }
     
    492534        if (fTable.cols.count(name)==0)
    493535        {
    494             gLog << err <<"SetPtrAddress('" << name << "') - Column not found." << endl;
     536            ostringstream str;
     537            str <<"SetPtrAddress('" << name << "') - Column not found." << endl;
     538#ifdef __EXCEPTIONS
     539            throw runtime_error(str.str());
     540#else
     541            gLog << err << "ERROR - " << str.str() << endl;
    495542            return false;
     543#endif
    496544        }
    497545
    498546        if (sizeof(T)!=fTable.cols[name].size)
    499547        {
    500             gLog << err << "SetPtrAddress('" << name << "') - Element size mismatch: expected "
     548            ostringstream str;
     549            str << "SetPtrAddress('" << name << "') - Element size mismatch: expected "
    501550                << fTable.cols[name].size << " from header, got " << sizeof(T) << endl;
    502 
     551#ifdef __EXCEPTIONS
     552            throw runtime_error(str.str());
     553#else
     554            gLog << err << "ERROR - " << str.str() << endl;
    503555            return false;
     556#endif
    504557        }
    505558
    506559        if (cnt!=fTable.cols[name].num)
    507560        {
    508             gLog << err << "SetPtrAddress('" << name << "') - Element count mismatch: expected "
     561            ostringstream str;
     562            str << "SetPtrAddress('" << name << "') - Element count mismatch: expected "
    509563                << fTable.cols[name].num << " from header, got " << cnt << endl;
    510 
     564#ifdef __EXCEPTIONS
     565            throw runtime_error(str.str());
     566#else
     567            gLog << err << "ERROR - " << str.str() << endl;
    511568            return false;
     569#endif
    512570        }
    513571
     
    543601        if (fTable.cols.count(name)==0)
    544602        {
    545             gLog << err <<"SetPtrAddress('" << name << "') - Column not found." << endl;
     603            ostringstream str;
     604            str <<"SetPtrAddress('" << name << "') - Column not found." << endl;
     605#ifdef __EXCEPTIONS
     606            throw runtime_error(str.str());
     607#else
     608            gLog << err << "ERROR - " << str.str() << endl;
    546609            return false;
     610#endif
    547611        }
    548612
     
    560624    uint64_t GetUInt(const string &key) const { return fTable.Get<uint64_t>(key); }
    561625    double   GetFloat(const string &key) const { return fTable.Get<double>(key); }
     626    string   GetStr(const string &key) const { return fTable.Get<string>(key); }
    562627
    563628    size_t GetN(const string &key) const
     
    574639    void PrintColumns() const { fTable.PrintColumns(); }
    575640};
    576 #endif
     641
     642};
     643#endif
Note: See TracChangeset for help on using the changeset viewer.