Changeset 280 in pacpusframework for trunk/src/FileLib


Ignore:
Timestamp:
Mar 20, 2014, 11:59:05 AM (11 years ago)
Author:
Marek Kurdej
Message:

Major: DbiteException uses boost::exception.

Location:
trunk/src/FileLib/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/FileLib/src/DbiteException.cpp

    r156 r280  
    1010using namespace pacpus;
    1111
    12 DbiteException::DbiteException(const std::string& what)
     12DbiteException::DbiteException(std::string const& what)
    1313    : PacpusException(what)
    1414{
  • trunk/src/FileLib/src/DbiteFile.cpp

    r141 r280  
    1010#include <Pacpus/kernel/Log.h>
    1111
     12#include <boost/exception/detail/exception_ptr.hpp>
     13#include <boost/exception/errinfo_api_function.hpp>
     14#include <boost/exception/errinfo_file_name.hpp>
     15#include <boost/exception/errinfo_nested_exception.hpp>
    1216#include <cassert>
    1317#include <cmath>
     
    1620#include <sstream>
    1721
     22using boost::copy_exception;
     23using boost::errinfo_api_function;
     24using boost::errinfo_file_name;
     25using boost::errinfo_nested_exception;
     26using boost::exception_ptr;
    1827using namespace pacpus;
    19 using namespace std;
     28using std::fstream;
     29using std::ios_base;
     30using std::min;
     31using std::max;
     32using std::numeric_limits;
     33using std::streamsize;
     34using std::string;
     35using std::stringstream;
    2036
    2137// FIXME: record size is only 8-bit long!
     
    3248}
    3349
    34 bool doesFileExist(const string & path);
     50bool doesFileExist(string const& path);
    3551
    3652DbiteFile::DbiteFile()
     
    5773    stringstream os;
    5874
    59     os << "Signature    : " << getSignature() << endl;
    60     os << "Version      : " << getVersion() << endl;
    61     os << "Header size  : " << getDataOffset() << " [bytes]" << endl;
    62     os << "Record size  : " << getRecordSize() << " [bytes]" << endl;
    63     os << "Data type    : " << getType() << endl;
    64     os << "Time min.    : " << getTimeMin() << endl;
    65     os << "Time max.    : " << getTimeMax() << endl;
    66     os << "Duration     : " << (getTimeMax() - getTimeMin()) / kMicrosInSecond << " [seconds]" << endl;
    67     os << "File size    : " << getFileSize() << " [bytes]" << endl;
    68     os << "Record count : " << getRecordCount() << endl;
     75    os << "Signature    : " << getSignature() << "\n";
     76    os << "Version      : " << getVersion() << "\n";
     77    os << "Header size  : " << getDataOffset() << " [bytes]" << "\n";
     78    os << "Record size  : " << getRecordSize() << " [bytes]" << "\n";
     79    os << "Data type    : " << getType() << "\n";
     80    os << "Time min.    : " << getTimeMin() << "\n";
     81    os << "Time max.    : " << getTimeMax() << "\n";
     82    os << "Duration     : " << (getTimeMax() - getTimeMin()) / kMicrosInSecond << " [seconds]" << "\n";
     83    os << "File size    : " << getFileSize() << " [bytes]" << "\n";
     84    os << "Record count : " << getRecordCount() << "\n";
    6985
    7086    return os.str();
     
    91107    setPath(path);
    92108
    93     mFile.open(getPath().c_str(), ios_base::binary | ios_base::in);
     109    ios_base::openmode openmode = ios_base::binary | ios_base::in;
     110    mFile.open(getPath().c_str(), openmode);
    94111    try {
    95112        checkFileOpen();
     
    98115        LOG_ERROR("Dbite exception: " << e.what());
    99116        close();
    100         throw DbiteException("cannot open file for reading");
     117        BOOST_THROW_EXCEPTION(DbiteException("cannot open file for reading")
     118            << errinfo_file_name(mPath)
     119            << errinfo_ios_base_open_mode(openmode)
     120        );
    101121    }
    102122
    103123    try {
    104124        readHeader();
    105     } catch (DbiteException & e) {
     125    } catch (DbiteException& e) {
    106126        (void) e; // unused
    107127        LOG_ERROR("Dbite exception: " << e.what());
    108128        // TODO: readAndDiagnoseHeader();
    109129        close();
    110         throw DbiteException("cannot read header");
     130        BOOST_THROW_EXCEPTION(DbiteException("cannot read header")
     131            << errinfo_nested_exception(copy_exception(e))
     132        );
    111133    }
    112134    verifyHeader();
     
    156178    if (!isOpen()) {
    157179        LOG_ERROR("cannot open file for writing");
    158         throw DbiteException("cannot open file for writing");
     180        BOOST_THROW_EXCEPTION(DbiteException("cannot open file for writing")
     181            << errinfo_ios_base_open_mode(openmode)
     182        );
    159183    }
    160184
     
    170194            // TODO: readAndDiagnoseHeader();
    171195            close();
    172             throw DbiteException("cannot read header");
     196            BOOST_THROW_EXCEPTION(DbiteException("cannot read header"));
    173197        }
    174198    } else {
     
    336360    if (getVersion() != VERSION_NUMBER) {
    337361        LOG_ERROR("bad version number");
    338         throw DbiteException("bad version number");
     362        BOOST_THROW_EXCEPTION(DbiteException("bad version number"));
    339363    }
    340364
    341365    if (!isSignatureCorrect()) {
    342366        LOG_ERROR("bad signature");
    343         throw DbiteException("bad signature");
     367        BOOST_THROW_EXCEPTION(DbiteException("bad signature"));
    344368    }
    345369
     
    511535    if (isVariableDataSize()) {
    512536        LOG_ERROR("cannot read nth record in a variable");
    513         throw DbiteException("cannot read nth record in a variable");
     537        BOOST_THROW_EXCEPTION(DbiteException("cannot read nth record in a variable"));
    514538    }
    515539
     
    518542    if (getFileSize() < nthRecordPosition) {
    519543        LOG_WARN("wanted to read past the file end");
    520         //throw DbiteException("wanted to read past the file end");
     544        //BOOST_THROW_EXCEPTION(DbiteException("wanted to read past the file end"));
    521545    }
    522546    mFile.clear();
     
    530554    if (!buffer) {
    531555        LOG_WARN("data buffer must be a non-NULL pointer");
    532         throw DbiteException("data buffer must be a non-NULL pointer");
     556        BOOST_THROW_EXCEPTION(DbiteException("data buffer must be a non-NULL pointer"));
    533557    }
    534558
     
    582606        }
    583607        LOG_ERROR("cannot read acquisition time");
    584         throw DbiteException("cannot read acquisition time");
     608        BOOST_THROW_EXCEPTION(DbiteException("cannot read acquisition time")
     609            << errinfo_file_name(mPath)
     610            << errinfo_api_function("read")
     611        );
    585612    }
    586613
     
    613640            LOG_INFO("buffer size = " << bufferSize);
    614641            LOG_INFO("data   size = " << dataSize);
    615             throw DbiteException("buffer is smaller than the data size");
     642            BOOST_THROW_EXCEPTION(DbiteException("buffer is smaller than the data size"));
    616643            // TODO: don't throw exception, but read only as much data as possible
    617644        }
     
    682709        // read forward
    683710        readResult = readRecordForward(time, timeRange, buffer, bufferSize);
    684     } catch (DbiteException & e) {
     711    } catch (DbiteException& /*e*/) {
    685712        setReadPosition(initialPosition);
    686         throw e;
     713        throw;
    687714    }
    688715
     
    703730        if (dataSize > static_cast<size_t>(numeric_limits<VariableDataSizeT>::max())) {
    704731            LOG_ERROR("variable data size too big");
    705             throw DbiteException("variable data size too big");
     732            BOOST_THROW_EXCEPTION(DbiteException("variable data size too big")
     733                << errinfo_file_name(mPath)
     734            );
    706735        }
    707736    }
     
    734763            writtenBytes += sizeof(VariableDataSizeT);
    735764        }
    736     } catch (DbiteException & e) {
     765    } catch (DbiteException& e) {
    737766        LOG_ERROR("Dbite exception: " << e.what());
    738767        LOG_INFO("restoring initial write position");
     
    740769        // on error, go back to record start
    741770        setWritePosition(- (int64_t) writtenBytes, ios_base::cur);
    742         throw e;
     771        throw;
    743772    }
    744773 
     
    766795    if (!mFile) {
    767796        LOG_ERROR("cannot read data");
    768         throw DbiteException("cannot read data");
     797        BOOST_THROW_EXCEPTION(DbiteException("cannot read data")
     798            << errinfo_file_name(mPath)
     799            << errinfo_api_function("read")
     800        );
    769801    }
    770802}
     
    775807    if (!mFile) {
    776808        LOG_ERROR("cannot write data");
    777         throw DbiteException("cannot write data");
     809        BOOST_THROW_EXCEPTION(DbiteException("cannot write data")
     810            << errinfo_file_name(mPath)
     811            << errinfo_api_function("write")
     812        );
    778813    }
    779814}
     
    789824        string errorMessage = "file is not open";
    790825        LOG_WARN(errorMessage);
    791         throw DbiteException(errorMessage.c_str());
     826        BOOST_THROW_EXCEPTION(DbiteException(errorMessage.c_str())
     827            << errinfo_file_name(mPath)
     828        );
    792829    }
    793830}
Note: See TracChangeset for help on using the changeset viewer.