diff options
author | David Robillard <d@drobilla.net> | 2019-03-07 22:41:35 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-03-08 09:07:18 +0100 |
commit | 8eba534e99cd8db29cca03035a115312935a22a3 (patch) | |
tree | cc13f89d5bdad33935fa4e8c396a740229be6b1e | |
parent | acb958e95d0e8ca1b0dd912fe8bbf2e14e5f74e9 (diff) | |
download | ingen-8eba534e99cd8db29cca03035a115312935a22a3.tar.gz ingen-8eba534e99cd8db29cca03035a115312935a22a3.tar.bz2 ingen-8eba534e99cd8db29cca03035a115312935a22a3.zip |
Use smart pointers to handle FILE streams
-rw-r--r-- | src/Configuration.cpp | 7 | ||||
-rw-r--r-- | src/SocketReader.cpp | 10 | ||||
-rw-r--r-- | tests/ingen_bench.cpp | 10 |
3 files changed, 15 insertions, 12 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); diff --git a/tests/ingen_bench.cpp b/tests/ingen_bench.cpp index c51b4d79..b51ec364 100644 --- a/tests/ingen_bench.cpp +++ b/tests/ingen_bench.cpp @@ -122,15 +122,15 @@ main(int argc, char** argv) const uint64_t t_end = clock.now_microseconds(); // Write log output - FILE* log = fopen(out_file.c_str(), "a"); - if (ftell(log) == 0) { - fprintf(log, "# n_threads\trun_time\treal_time\n"); + std::unique_ptr<FILE, decltype(&fclose)> log{fopen(out_file.c_str(), "a"), + &fclose}; + if (ftell(log.get()) == 0) { + fprintf(log.get(), "# n_threads\trun_time\treal_time\n"); } - fprintf(log, "%u\t%f\t%f\n", + fprintf(log.get(), "%u\t%f\t%f\n", world->conf().option("threads").get<int32_t>(), (t_end - t_start) / 1000000.0, (n_test_frames / 48000.0)); - fclose(log); // Shut down world->engine()->deactivate(); |