Regex::Regex(const char * regex, int flags) { auto errCode = regcomp(&exp, regex, flags); if (errCode != 0) { auto size = regerror(errCode, &exp, nullptr, 0); if (size) { std::string msg(size, '\0'); regerror(errCode, &exp, &msg.front(), size); throw LibraryException(errCode, msg); } throw LibraryException(errCode, ""); } }
/* Return a book copy and get number of days late (if exists). */ const int Library::returnCopy (const long long& barcode) { /* Find borrowed book with specified barcode. */ for (std::map<BookCopy*, Borrower*>::iterator iterator = borrows.begin(); iterator != borrows.end(); iterator++) { /* If book found, return it. */ BookCopy* copy = iterator->first; if (copy->getBarcode() == barcode) { /* Get number of days late (if exists). */ int days = copy->returnCopy(); /* Proceed return procedure. */ (iterator->second)->returnBook(days > 0); borrows.erase(iterator); /* Return number of days late (if exists). */ return days; } } /* Else, throw an exception. */ std::stringstream message; message << "Book with barcode " << std::setw(13) << std::setfill('0') << barcode << " is currently not borrowed!"; throw LibraryException(message.str()); }
/* Get book copy by barcode. */ BookCopy* Library::getBookCopy (const long long& barcode) const { /* Loop through all books to find copy. */ for (std::map<long long, Book*>::const_iterator iterator = books.begin(); iterator != books.end(); iterator++) { try { /* If copy found, return it. */ return (iterator->second)->getBookCopy(barcode); } catch (LibraryException& exception) { /* Not handling exception. */ } } /* If book copy not found, throw an exception. */ std::stringstream message; message << "Book with barcode " << std::setw(13) << std::setfill('0') << barcode << " not found!"; throw LibraryException(message.str()); }
PROPVARIANT CodecLibrary::GetMethodProperty( UINT32 index, PROPID propId) const { PROPVARIANT result = {}; if (S_OK != m_getMethodProperty(index, propId, &result)) { throw LibraryException("Cannot get method property."); } return result; }
LibraryPropertyReader::LibraryPropertyReader( GetHandlerPropertyFunc getHandlerProperty, GetHandlerProperty2Func getHandlerProperty2) : m_getHandlerProperty(getHandlerProperty) , m_getHandlerProperty2(getHandlerProperty2) { if (!getHandlerProperty && !getHandlerProperty2) { throw LibraryException("Assigned two empty functions."); } }
CLSID CodecInfo::__GetCodecClass(UINT32 unIndex, PROPID propId) { PropertyVariant propVariant = m_codecLibrary.GetMethodProperty( unIndex, propId); switch (propVariant.vt) { case VT_BSTR: return *reinterpret_cast<const GUID *>(propVariant.bstrVal); case VT_EMPTY: return CLSID(); default: throw LibraryException("Cannot get the codec class."); } }
/* Add a copy of this book. */ void Book::addBookCopy (BookCopy*& copy) { /* If first 10 digits of barcode matches the ISBN, * attempt to add copy for book. */ if (copy->getBarcode() / 1000 == isbn) { /* Get return pair of insertion operation. */ std::pair<std::map<long long, BookCopy*>::iterator, bool> result = copies.insert( std::pair<long long, BookCopy*>(copy->getBarcode(), copy)); /* If copy already existed, throw an exception. */ if (!result.second) { std::stringstream message; message << "Book with barcode " << std::setw(13) << std::setfill('0') << copy->getBarcode() << " existed!"; throw LibraryException(message.str()); } } else { /* Else, throw an exception. */ std::stringstream message; message << "Copy with barcode " << std::setw(13) << std::setfill('0') << copy->getBarcode() << " does not belong to the book with ISBN " << isbn << '!'; throw LibraryException(message.str()); } }
/* Get borrower by ID. */ Borrower* Library::getBorrower (const std::string& id) const { /* Return borrower if found. */ std::map<std::string, Borrower*>::const_iterator iterator = borrowers.find(id); if (iterator != borrowers.end()) { return iterator->second; } /* Else, throw an exception. */ std::stringstream message; message << "Borrower with ID " << id << " not found!"; throw LibraryException(message.str()); }
/* Add new borrower. */ void Library::addBorrower (Borrower*& borrower) { /* Add borrower and get status. */ std::pair < std::map<std::string, Borrower*>::iterator, bool> result = borrowers.insert( std::pair<std::string, Borrower*>(borrower->getId(), borrower)); /* If borrower already existed, throw an exception. */ if (!result.second) { std::stringstream message; message << "Borrower with ID " << borrower->getId() << " already existed!"; throw LibraryException(message.str()); } }
/* Get a book by ISBN. */ Book* Library::getBook (const long long& isbn) const { /* If book found, return it. */ std::map<long long, Book*>::const_iterator iterator = books.find(isbn); if (iterator != books.end()) { return iterator->second; } /* If no book found, throw an exception. */ std::stringstream message; message << "Book with ISBN " << std::setw(10) << std::setfill('0') << isbn << " not existed!"; throw LibraryException(message.str()); }
/* Add new book to library. */ void Library::addBook (Book*& book) { /* Add book to library and get status. */ std::pair < std::map<long long, Book*>::iterator, bool> result = books.insert(std::pair<long long, Book*>(book->getIsbn(), book)); /* If book already existed, throw an exception. */ if (!result.second) { std::stringstream message; message << "Book with ISBN " << std::setw(10) << std::setfill('0') << book->getIsbn() << " already existed!"; throw LibraryException(message.str()); } }
/* Get a copy of this book by barcode. */ BookCopy* Book::getBookCopy (const long long& barcode) const { /* If copy found, return it. */ std::map<long long, BookCopy*>::const_iterator iterator = copies.find(barcode); if (iterator != copies.end()) { return iterator->second; } /* If no copy found, throw exception. */ std::stringstream message; message << "No books with barcode " << std::setw(13) << std::setfill('0') << barcode << " found!"; throw LibraryException(message.str()); }
PROPVARIANT LibraryPropertyReader::ReadProperty( UINT32 unIndex, PROPID propId) const { PROPVARIANT result = {}; HRESULT hResult = NULL; if (!m_getHandlerProperty) { hResult = m_getHandlerProperty2(unIndex, propId, &result); } else { hResult = m_getHandlerProperty(propId, &result); } if (hResult != S_OK) { throw LibraryException("Cannot read property."); } return result; }
UINT32 Codecs::__LoadCodecs(const CodecLibrary &codecLibrary) { auto getNumberOfMethods = codecLibrary.GetProc<GetNumberOfMethodsFunc>( "GetNumberOfMethods"); UINT32 unNumberOfMethods; if (getNumberOfMethods(&unNumberOfMethods) != S_OK) { throw LibraryException("Cannot get number of methods."); } UINT32 result = 0; for (UINT32 i = 0; i != unNumberOfMethods; ++i) { try { m_vupCodecInfo.push_back( make_unique<CodecInfo>(codecLibrary, i)); ++result; } catch (const LibraryException &) {} } return result; }
/* Borrow a book copy. */ void Library::borrowCopy (Borrower*& borrower, BookCopy*& copy) { /* Add new borrow and get status. */ std::pair < std::map<BookCopy*, Borrower*>::iterator, bool> result = borrows.insert( std::pair<BookCopy*, Borrower*>(copy, borrower)); /* If book copy already borrowed, throw an exception. */ if (result.second) { borrower->borrowBook(); copy->borrowCopy(); } else { std::stringstream message; message << "Book with barcode " << std::setw(13) << std::setfill('0') << copy->getBarcode() << " already borrowed!"; throw LibraryException(message.str()); } }