aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2019-03-07 22:41:35 +0100
committerDavid Robillard <d@drobilla.net>2019-03-08 09:07:18 +0100
commit8eba534e99cd8db29cca03035a115312935a22a3 (patch)
treecc13f89d5bdad33935fa4e8c396a740229be6b1e /src
parentacb958e95d0e8ca1b0dd912fe8bbf2e14e5f74e9 (diff)
downloadingen-8eba534e99cd8db29cca03035a115312935a22a3.tar.gz
ingen-8eba534e99cd8db29cca03035a115312935a22a3.tar.bz2
ingen-8eba534e99cd8db29cca03035a115312935a22a3.zip
Use smart pointers to handle FILE streams
Diffstat (limited to 'src')
-rw-r--r--src/Configuration.cpp7
-rw-r--r--src/SocketReader.cpp10
2 files changed, 10 insertions, 7 deletions
diff --git a/src/Configuration.cpp b/src/Configuration.cpp
index c05c516e..d0d606e8 100644
--- a/src/Configuration.cpp
+++ b/src/Configuration.cpp
@@ -18,6 +18,7 @@
#include <cerrno>
#include <cstring>
#include <iostream>
+#include <memory>
#include <thread>
#include "ingen/Configuration.hpp"
@@ -279,7 +280,8 @@ Configuration::save(URIMap& uri_map,
}
// Attempt to open file for writing
- FILE* file = fopen(path.c_str(), "w");
+ std::unique_ptr<FILE, decltype(&fclose)> file{fopen(path.c_str(), "w"),
+ &fclose};
if (!file) {
throw FileError((fmt("Failed to open file %1% (%2%)")
% path % strerror(errno)).str());
@@ -302,7 +304,7 @@ Configuration::save(URIMap& uri_map,
env,
&base_uri,
serd_file_sink,
- file);
+ file.get());
// Write a prefix directive for each prefix in the environment
serd_env_foreach(env, (SerdPrefixSink)serd_writer_set_prefix, writer);
@@ -334,7 +336,6 @@ Configuration::save(URIMap& uri_map,
serd_writer_free(writer);
serd_env_free(env);
serd_node_free(&base);
- fclose(file);
return path;
}
diff --git a/src/SocketReader.cpp b/src/SocketReader.cpp
index de2f3342..590b40cd 100644
--- a/src/SocketReader.cpp
+++ b/src/SocketReader.cpp
@@ -15,6 +15,7 @@
*/
#include <cerrno>
+#include <memory>
#include <poll.h>
@@ -93,7 +94,8 @@ SocketReader::run()
LV2_URID_Map* map = &_world.uri_map().urid_map_feature()->urid_map;
// Open socket as a FILE for reading directly with serd
- FILE* f = fdopen(_socket->fd(), "r");
+ std::unique_ptr<FILE, decltype(&fclose)> f{fdopen(_socket->fd(), "r"),
+ &fclose};
if (!f) {
_world.log().error(fmt("Failed to open connection (%1%)\n")
% strerror(errno));
@@ -134,7 +136,7 @@ SocketReader::run()
nullptr);
serd_env_set_base_uri(_env, sord_node_to_serd_node(base_uri));
- serd_reader_start_stream(reader, f, (const uint8_t*)"(socket)", false);
+ serd_reader_start_stream(reader, f.get(), (const uint8_t*)"(socket)", false);
// Make an AtomReader to call Ingen Interface methods based on Atom
AtomReader ar(_world.uri_map(), _world.uris(), _world.log(), _iface);
@@ -145,7 +147,7 @@ SocketReader::run()
pfd.revents = 0;
while (!_exit_flag) {
- if (feof(f)) {
+ if (feof(f.get())) {
break; // Lost connection
}
@@ -187,7 +189,7 @@ SocketReader::run()
std::lock_guard<std::mutex> lock(_world.rdf_mutex());
// Destroy everything
- fclose(f);
+ f.reset();
sord_inserter_free(_inserter);
serd_reader_end_stream(reader);
sratom_free(sratom);