Error appendToFile(const core::FilePath& filePath, const ContentType& content) { using namespace boost::system::errc ; // open the file stream boost::shared_ptr<std::ostream> pOfs; Error error = filePath.open_w(&pOfs, false); if (error) return error; try { pOfs->seekp(0, std::ios_base::end); // append the content *pOfs << content ; if (pOfs->fail()) return systemError(io_error, ERROR_LOCATION); } catch(const std::exception& e) { Error error = systemError(boost::system::errc::io_error, ERROR_LOCATION); error.addProperty("what", e.what()); error.addProperty("path", filePath.absolutePath()); return error; } return Success() ; }
Error writeCollectionToFile( const core::FilePath& filePath, const CollectionType& collection, boost::function<std::string( const typename CollectionType::value_type&)> stringifyFunction) { using namespace boost::system::errc ; // open the file stream boost::shared_ptr<std::ostream> pOfs; Error error = filePath.open_w(&pOfs, true); if (error) return error; try { // write each line for (typename CollectionType::const_iterator it = collection.begin(); it != collection.end(); ++it) { *pOfs << stringifyFunction(*it) << std::endl ; if (pOfs->fail()) return systemError(io_error, ERROR_LOCATION); } } catch(const std::exception& e) { Error error = systemError(boost::system::errc::io_error, ERROR_LOCATION); error.addProperty("what", e.what()); error.addProperty("path", filePath.absolutePath()); return error; } return Success() ; }