small improvements

This commit is contained in:
Martchus 2016-03-12 18:36:10 +01:00
parent 9942634510
commit cc6e16e513
6 changed files with 44 additions and 47 deletions

View File

@ -15,15 +15,6 @@ using namespace std;
namespace Media { namespace Media {
/*!
* \brief Constructs a new BasicFileInfo.
*/
BasicFileInfo::BasicFileInfo() :
m_size(0)
{
m_file.exceptions(fstream::failbit | fstream::badbit);
}
/*! /*!
* \brief Constructs a new BasicFileInfo for the specified file. * \brief Constructs a new BasicFileInfo for the specified file.
* *
@ -31,7 +22,8 @@ BasicFileInfo::BasicFileInfo() :
*/ */
BasicFileInfo::BasicFileInfo(const std::string &path) : BasicFileInfo::BasicFileInfo(const std::string &path) :
m_path(path), m_path(path),
m_size(0) m_size(0),
m_readOnly(false)
{ {
m_file.exceptions(fstream::failbit | fstream::badbit); m_file.exceptions(fstream::failbit | fstream::badbit);
} }
@ -48,26 +40,26 @@ BasicFileInfo::~BasicFileInfo()
/*! /*!
* \brief Opens a std::fstream for the current file. Does nothing a stream is already open. * \brief Opens a std::fstream for the current file. Does nothing a stream is already open.
* \param readonly Indicates whether the stream should be opend as read-only. * \param readOnly Indicates whether the stream should be opend as read-only.
* \throws Throws std::ios_base::failure when an IO error occurs. * \throws Throws std::ios_base::failure when an IO error occurs.
*/ */
void BasicFileInfo::open(bool readonly) void BasicFileInfo::open(bool readOnly)
{ {
if(!isOpen()) { if(!isOpen()) {
reopen(readonly); reopen(readOnly);
} }
} }
/*! /*!
* \brief Opens a std::fstream for the current file. Closes a possibly already opened stream and * \brief Opens a std::fstream for the current file. Closes a possibly already opened stream and
* clears all flags before. * clears all flags before.
* \param readonly Indicates whether the stream should be opend as read-only. * \param readOnly Indicates whether the stream should be opend as read-only.
* \throws Throws std::ios_base::failure when an IO error occurs. * \throws Throws std::ios_base::failure when an IO error occurs.
*/ */
void BasicFileInfo::reopen(bool readonly) void BasicFileInfo::reopen(bool readOnly)
{ {
close(); close();
m_file.open(m_path, readonly ? ios_base::in | ios_base::binary : ios_base::in | ios_base::out | ios_base::binary); m_file.open(m_path, (m_readOnly = readOnly) ? ios_base::in | ios_base::binary : ios_base::in | ios_base::out | ios_base::binary);
m_file.seekg(0, ios_base::end); m_file.seekg(0, ios_base::end);
m_size = m_file.tellg(); m_size = m_file.tellg();
m_file.seekg(0, ios_base::beg); m_file.seekg(0, ios_base::beg);

View File

@ -12,15 +12,15 @@ namespace Media {
class LIB_EXPORT BasicFileInfo class LIB_EXPORT BasicFileInfo
{ {
public: public:
BasicFileInfo(); BasicFileInfo(const std::string &path = std::string());
BasicFileInfo(const std::string &path);
BasicFileInfo(const BasicFileInfo &) = delete; BasicFileInfo(const BasicFileInfo &) = delete;
BasicFileInfo &operator=(const BasicFileInfo &) = delete; BasicFileInfo &operator=(const BasicFileInfo &) = delete;
virtual ~BasicFileInfo(); virtual ~BasicFileInfo();
void open(bool readonly = false); void open(bool readOnly = false);
void reopen(bool readonly = false); void reopen(bool readonly = false);
bool isOpen(); bool isOpen() const;
bool isReadOnly() const;
void close(); void close();
const std::string &path() const; const std::string &path() const;
@ -45,6 +45,7 @@ private:
std::string m_path; std::string m_path;
std::fstream m_file; std::fstream m_file;
uint64 m_size; uint64 m_size;
bool m_readOnly;
}; };
/*! /*!
@ -52,11 +53,19 @@ private:
* *
* \sa stream() * \sa stream()
*/ */
inline bool BasicFileInfo::isOpen() inline bool BasicFileInfo::isOpen() const
{ {
return m_file.is_open(); return m_file.is_open();
} }
/*!
* \brief Indicates whether the last open()/reopen() call was read-only.
*/
inline bool BasicFileInfo::isReadOnly() const
{
return m_readOnly;
}
/*! /*!
* \brief Returns the std::fstream for the current instance. * \brief Returns the std::fstream for the current instance.
*/ */

View File

@ -171,7 +171,7 @@ void MatroskaContainer::validateIndex()
if(clusterElement->id() != MatroskaIds::Cluster) { if(clusterElement->id() != MatroskaIds::Cluster) {
addNotification(NotificationType::Critical, "\"CueClusterPosition\" element at " + numberToString(subElement->startOffset()) + " does not point to \"Cluster\"-element (points to " + numberToString(clusterElement->startOffset()) + ").", context); addNotification(NotificationType::Critical, "\"CueClusterPosition\" element at " + numberToString(subElement->startOffset()) + " does not point to \"Cluster\"-element (points to " + numberToString(clusterElement->startOffset()) + ").", context);
} }
} catch(Failure &) { } catch(const Failure &) {
addNotifications(context, *clusterElement); addNotifications(context, *clusterElement);
} }
break; break;
@ -211,7 +211,7 @@ void MatroskaContainer::validateIndex()
default: default:
addNotification(NotificationType::Critical, "\"CueRelativePosition\" element does not point to \"Block\"-, \"BlockGroup\", or \"SimpleBlock\"-element (points to " + numberToString(referenceElement.startOffset()) + ").", context); addNotification(NotificationType::Critical, "\"CueRelativePosition\" element does not point to \"Block\"-, \"BlockGroup\", or \"SimpleBlock\"-element (points to " + numberToString(referenceElement.startOffset()) + ").", context);
} }
} catch(Failure &) { } catch(const Failure &) {
addNotifications(context, referenceElement); addNotifications(context, referenceElement);
} }
} }
@ -493,7 +493,7 @@ void MatroskaContainer::internalParseHeader()
default: default:
; ;
} }
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Can not parse element at " + numberToString(offset) + " (denoted using \"SeekHead\" element).", context); addNotification(NotificationType::Critical, "Can not parse element at " + numberToString(offset) + " (denoted using \"SeekHead\" element).", context);
} }
} }
@ -586,7 +586,7 @@ void MatroskaContainer::internalParseTags()
m_tags.back()->parse(*subElement); m_tags.back()->parse(*subElement);
} catch(NoDataFoundException &) { } catch(NoDataFoundException &) {
m_tags.pop_back(); m_tags.pop_back();
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Unable to parse tag " + ConversionUtilities::numberToString(m_tags.size()) + ".", context); addNotification(NotificationType::Critical, "Unable to parse tag " + ConversionUtilities::numberToString(m_tags.size()) + ".", context);
} }
break; break;
@ -597,7 +597,7 @@ void MatroskaContainer::internalParseTags()
addNotification(NotificationType::Warning, "\"Tags\"-element contains unknown child. It will be ignored.", context); addNotification(NotificationType::Warning, "\"Tags\"-element contains unknown child. It will be ignored.", context);
} }
} }
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Element structure seems to be invalid.", context); addNotification(NotificationType::Critical, "Element structure seems to be invalid.", context);
throw; throw;
} }
@ -620,7 +620,7 @@ void MatroskaContainer::internalParseTracks()
m_tracks.back()->parseHeader(); m_tracks.back()->parseHeader();
} catch(NoDataFoundException &) { } catch(NoDataFoundException &) {
m_tracks.pop_back(); m_tracks.pop_back();
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Unable to parse track " + ConversionUtilities::numberToString(m_tracks.size()) + ".", context); addNotification(NotificationType::Critical, "Unable to parse track " + ConversionUtilities::numberToString(m_tracks.size()) + ".", context);
} }
break; break;
@ -631,7 +631,7 @@ void MatroskaContainer::internalParseTracks()
addNotification(NotificationType::Warning, "\"Tracks\"-element contains unknown child element \"" + subElement->idToString() + "\". It will be ignored.", context); addNotification(NotificationType::Warning, "\"Tracks\"-element contains unknown child element \"" + subElement->idToString() + "\". It will be ignored.", context);
} }
} }
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Element structure seems to be invalid.", context); addNotification(NotificationType::Critical, "Element structure seems to be invalid.", context);
throw; throw;
} }
@ -654,7 +654,7 @@ void MatroskaContainer::internalParseChapters()
m_editionEntries.back()->parseNested(); m_editionEntries.back()->parseNested();
} catch(NoDataFoundException &) { } catch(NoDataFoundException &) {
m_editionEntries.pop_back(); m_editionEntries.pop_back();
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Unable to parse edition entry " + ConversionUtilities::numberToString(m_editionEntries.size()) + ".", context); addNotification(NotificationType::Critical, "Unable to parse edition entry " + ConversionUtilities::numberToString(m_editionEntries.size()) + ".", context);
} }
break; break;
@ -665,7 +665,7 @@ void MatroskaContainer::internalParseChapters()
addNotification(NotificationType::Warning, "\"Chapters\"-element contains unknown child element \"" + subElement->idToString() + "\". It will be ignored.", context); addNotification(NotificationType::Warning, "\"Chapters\"-element contains unknown child element \"" + subElement->idToString() + "\". It will be ignored.", context);
} }
} }
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Element structure seems to be invalid.", context); addNotification(NotificationType::Critical, "Element structure seems to be invalid.", context);
throw; throw;
} }
@ -688,7 +688,7 @@ void MatroskaContainer::internalParseAttachments()
m_attachments.back()->parse(subElement); m_attachments.back()->parse(subElement);
} catch(NoDataFoundException &) { } catch(NoDataFoundException &) {
m_attachments.pop_back(); m_attachments.pop_back();
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Unable to parse attached file " + ConversionUtilities::numberToString(m_attachments.size()) + ".", context); addNotification(NotificationType::Critical, "Unable to parse attached file " + ConversionUtilities::numberToString(m_attachments.size()) + ".", context);
} }
break; break;
@ -699,7 +699,7 @@ void MatroskaContainer::internalParseAttachments()
addNotification(NotificationType::Warning, "\"Attachments\"-element contains unknown child element \"" + subElement->idToString() + "\". It will be ignored.", context); addNotification(NotificationType::Warning, "\"Attachments\"-element contains unknown child element \"" + subElement->idToString() + "\". It will be ignored.", context);
} }
} }
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Element structure seems to be invalid.", context); addNotification(NotificationType::Critical, "Element structure seems to be invalid.", context);
throw; throw;
} }
@ -901,7 +901,7 @@ void MatroskaContainer::internalMakeFile()
} }
} }
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Unable to parse content in top-level element at " + numberToString(level0Element->startOffset()) + " of original file.", context); addNotification(NotificationType::Critical, "Unable to parse content in top-level element at " + numberToString(level0Element->startOffset()) + " of original file.", context);
throw; throw;
} }
@ -948,7 +948,7 @@ calculateSegmentData:
if((segment.cuesElement = level0Element->childById(MatroskaIds::Cues))) { if((segment.cuesElement = level0Element->childById(MatroskaIds::Cues))) {
try { try {
segment.cuesUpdater.parse(segment.cuesElement); segment.cuesUpdater.parse(segment.cuesElement);
} catch(Failure &) { } catch(const Failure &) {
addNotifications(segment.cuesUpdater); addNotifications(segment.cuesUpdater);
throw; throw;
} }
@ -1316,10 +1316,10 @@ nonRewriteCalculations:
} }
} }
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Parsing the original file failed.", context); addNotification(NotificationType::Critical, "Parsing the original file failed.", context);
throw; throw;
} catch(ios_base::failure &) { } catch(const ios_base::failure &) {
addNotification(NotificationType::Critical, "An IO error occured when parsing the original file.", context); addNotification(NotificationType::Critical, "An IO error occured when parsing the original file.", context);
throw; throw;
} }
@ -1495,7 +1495,7 @@ nonRewriteCalculations:
try { try {
segment.cuesUpdater.make(outputStream); segment.cuesUpdater.make(outputStream);
addNotifications(segment.cuesUpdater); addNotifications(segment.cuesUpdater);
} catch(Failure &) { } catch(const Failure &) {
addNotifications(segment.cuesUpdater); addNotifications(segment.cuesUpdater);
throw; throw;
} }
@ -1593,7 +1593,7 @@ nonRewriteCalculations:
try { try {
segment.cuesUpdater.make(outputStream); segment.cuesUpdater.make(outputStream);
addNotifications(segment.cuesUpdater); addNotifications(segment.cuesUpdater);
} catch(Failure &) { } catch(const Failure &) {
addNotifications(segment.cuesUpdater); addNotifications(segment.cuesUpdater);
throw; throw;
} }
@ -1668,7 +1668,7 @@ nonRewriteCalculations:
reset(); reset();
try { try {
parseHeader(); parseHeader();
} catch(Failure &) { } catch(const Failure &) {
addNotification(NotificationType::Critical, "Unable to reparse the header of the new file.", context); addNotification(NotificationType::Critical, "Unable to reparse the header of the new file.", context);
throw; throw;
} }

View File

@ -264,7 +264,7 @@ void MatroskaCuePositionUpdater::make(ostream &stream)
case MatroskaIds::CueRelativePosition: case MatroskaIds::CueRelativePosition:
try { try {
EbmlElement::makeSimpleElement(stream, cueTrackPositionsChild->id(), m_relativeOffsets.at(cueTrackPositionsChild).currentValue()); EbmlElement::makeSimpleElement(stream, cueTrackPositionsChild->id(), m_relativeOffsets.at(cueTrackPositionsChild).currentValue());
} catch(out_of_range &) { } catch(const out_of_range &) {
// we were not able parse the relative offset because the absolute offset is missing // we were not able parse the relative offset because the absolute offset is missing
// continue anyways // continue anyways
} }
@ -316,7 +316,7 @@ void MatroskaCuePositionUpdater::make(ostream &stream)
addNotification(NotificationType::Warning, "\"Cues\"-element contains a element which is not a \"CuePoint\"-element. It will be ignored.", context); addNotification(NotificationType::Warning, "\"Cues\"-element contains a element which is not a \"CuePoint\"-element. It will be ignored.", context);
} }
} }
} catch(out_of_range &) { } catch(const out_of_range &) {
addNotification(NotificationType::Critical, "Unable to write the file index because the index of the original file could not be parsed correctly.", context); addNotification(NotificationType::Critical, "Unable to write the file index because the index of the original file could not be parsed correctly.", context);
throw InvalidDataException(); throw InvalidDataException();
} }

View File

@ -38,10 +38,6 @@ void MatroskaSeekInfo::shift(uint64 start, int64 amount)
void MatroskaSeekInfo::parse(EbmlElement *seekHeadElement) void MatroskaSeekInfo::parse(EbmlElement *seekHeadElement)
{ {
static const string context("parsing \"SeekHead\"-element"); static const string context("parsing \"SeekHead\"-element");
//if(!seekHeadElement->parent()) {
// addNotification(NotificationType::Warning, "\"SeekHead\"-element without parent specified.", context);
// throw InvalidDataException();
//}
m_seekHeadElement = seekHeadElement; m_seekHeadElement = seekHeadElement;
m_info.clear(); m_info.clear();
EbmlElement *seekElement = seekHeadElement->firstChild(); EbmlElement *seekElement = seekHeadElement->firstChild();

View File

@ -72,7 +72,7 @@ KnownField MatroskaTag::knownField(const std::string &id) const
}); });
try { try {
return map.at(id); return map.at(id);
} catch(out_of_range &) { } catch(const out_of_range &) {
return KnownField::Invalid; return KnownField::Invalid;
} }
} }
@ -100,7 +100,7 @@ void MatroskaTag::parse(EbmlElement &tagElement)
field.invalidateNotifications(); field.invalidateNotifications();
field.reparse(*child, true); field.reparse(*child, true);
fields().insert(pair<string, MatroskaTagField>(field.id(), field)); fields().insert(pair<string, MatroskaTagField>(field.id(), field));
} catch(Failure &) { } catch(const Failure &) {
} }
addNotifications(context, field); addNotifications(context, field);
break; break;