summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2011-04-09 07:54:44 (GMT)
committerRemko Tronçon <git@el-tramo.be>2011-04-18 19:11:41 (GMT)
commite3d2137622cea23298f203801bc698eff08e0ea1 (patch)
treee8d02f3334a6422e25ca5f30606850d960b27cc7 /Swiften/Entity/PayloadPersister.cpp
parent01385d84f07466550fff702079555c65963463a8 (diff)
downloadswift-contrib-e3d2137622cea23298f203801bc698eff08e0ea1.zip
swift-contrib-e3d2137622cea23298f203801bc698eff08e0ea1.tar.bz2
Factor out payload persisting into utility classes.
Diffstat (limited to 'Swiften/Entity/PayloadPersister.cpp')
-rw-r--r--Swiften/Entity/PayloadPersister.cpp52
1 files changed, 52 insertions, 0 deletions
diff --git a/Swiften/Entity/PayloadPersister.cpp b/Swiften/Entity/PayloadPersister.cpp
new file mode 100644
index 0000000..2899edc
--- /dev/null
+++ b/Swiften/Entity/PayloadPersister.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2011 Remko Tronçon
+ * Licensed under the GNU General Public License v3.
+ * See Documentation/Licenses/GPLv3.txt for more information.
+ */
+
+#include <Swiften/Entity/PayloadPersister.h>
+
+#include <boost/filesystem/fstream.hpp>
+#include <boost/filesystem.hpp>
+
+#include <Swiften/Parser/PayloadParsers/UnitTest/PayloadParserTester.h>
+#include <Swiften/Base/ByteArray.h>
+#include <Swiften/Parser/PayloadParser.h>
+#include <Swiften/Parser/PayloadParserFactory.h>
+#include <Swiften/Serializer/PayloadSerializer.h>
+
+using namespace Swift;
+
+PayloadPersister::PayloadPersister() {
+}
+
+PayloadPersister::~PayloadPersister() {
+}
+
+void PayloadPersister::savePayload(boost::shared_ptr<Payload> payload, const boost::filesystem::path& path) {
+ if (!boost::filesystem::exists(path.parent_path())) {
+ try {
+ boost::filesystem::create_directories(path.parent_path());
+ }
+ catch (const boost::filesystem::filesystem_error& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ }
+ }
+ boost::filesystem::ofstream file(path);
+ file << getSerializer()->serialize(payload);
+ file.close();
+}
+
+boost::shared_ptr<Payload> PayloadPersister::loadPayload(const boost::filesystem::path& path) {
+ if (boost::filesystem::exists(path)) {
+ ByteArray data;
+ data.readFromFile(path.string());
+ std::auto_ptr<PayloadParser> parser(createParser());
+ PayloadParserTester tester(parser.get());
+ tester.parse(data.toString());
+ return parser->getPayload();
+ }
+ else {
+ return boost::shared_ptr<Payload>();
+ }
+}