Ignore:
Timestamp:
04/18/13 12:08:14 (11 years ago)
Author:
tbretz
Message:
Implemented the possibility to stream the input file immediately also to an output file while reading.
File:
1 edited

Legend:

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

    r15267 r15364  
    1717#include <map>
    1818#include <string>
     19#include <fstream>
    1920#include <sstream>
    2021#include <algorithm>
     
    420421
    421422private:
     423    ofstream fCopy;
     424
    422425    Table fTable;
    423426
     
    497500    }
    498501
    499 public:
    500     fits(const string &fname, bool force=false) : izstream(fname.c_str())
     502    void Constructor(const string &fname, string fout, bool force)
    501503    {
    502504        char simple[10];
     
    595597            }
    596598        }
     599
     600        if (fout.empty())
     601            return;
     602
     603        if (*fout.rbegin()=='/')
     604        {
     605            const size_t p = fname.find_last_of('/');
     606            fout.append(fname.substr(p+1));
     607        }
     608
     609        fCopy.open(fout);
     610        if (!fCopy)
     611        {
     612            clear(rdstate()|ios::badbit);
     613#ifdef __EXCEPTIONS
     614            throw runtime_error("Could not open output file.");
     615#else
     616            gLog << ___err___ << "ERROR - Failed to open output file." << endl;
     617#endif
     618        }
     619
     620        const streampos p = tellg();
     621        seekg(0);
     622
     623        vector<char> buf(p);
     624        read(buf.data(), p);
     625
     626        fCopy.write(buf.data(), p);
     627        if (!fCopy)
     628            clear(rdstate()|ios::badbit);
     629    }
     630
     631public:
     632    fits(const string &fname, bool force=false) : izstream(fname.c_str())
     633    {
     634        Constructor(fname, "", force);
     635    }
     636
     637    fits(const string &fname, const string &fout, bool force=false) : izstream(fname.c_str())
     638    {
     639        Constructor(fname, fout, force);
     640    }
     641
     642    ~fits()
     643    {
     644        copy(istreambuf_iterator<char>(*this),
     645             istreambuf_iterator<char>(),
     646             ostreambuf_iterator<char>(fCopy));
    597647    }
    598648
     
    625675
    626676        if (row==fRow+1)
     677        {
    627678            fChkData.add(fBufferRow);
     679            if (fCopy.is_open() && fCopy.good())
     680                fCopy.write(fBufferRow.data()+offset, fTable.bytes_per_row);
     681            if (!fCopy)
     682                clear(rdstate()|ios::badbit);
     683        }
     684        else
     685            if (fCopy.is_open())
     686                clear(rdstate()|ios::badbit);
    628687
    629688        fRow = row;
Note: See TracChangeset for help on using the changeset viewer.