Skip to content

Commit

Permalink
separate tmp_file.h
Browse files Browse the repository at this point in the history
  • Loading branch information
felixguendling committed Sep 6, 2023
1 parent fa3bbd1 commit 53fa409
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
43 changes: 43 additions & 0 deletions include/tiles/osm/tmp_file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <cstdio>
#include <string>

#include "boost/filesystem.hpp"

#include "utl/verify.h"

namespace tiles {

struct tmp_file {
explicit tmp_file(std::string path)
: path_{std::move(path)},
#ifdef _MSC_VER
file_(std::fopen(path_.c_str(), "wb+"))
#else
file_(std::fopen(path_.c_str(), "wb+e"))
#endif
{
utl::verify(file_ != nullptr, "tmp_file: unable to open file {}", path_);
}

~tmp_file() {
if (file_ != nullptr) {
std::fclose(file_);
boost::filesystem::remove(path_);
}
file_ = nullptr;
}

tmp_file(tmp_file const&) = default;
tmp_file(tmp_file&&) = default;
tmp_file& operator=(tmp_file const&) = default;
tmp_file& operator=(tmp_file&&) = default;

int fileno() const { return ::fileno(file_); }

std::string path_;
FILE* file_;
};

} // namespace tiles
32 changes: 1 addition & 31 deletions src/osm/load_osm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "tiles/db/tile_database.h"
#include "tiles/osm/feature_handler.h"
#include "tiles/osm/hybrid_node_idx.h"
#include "tiles/osm/tmp_file.h"
#include "tiles/util.h"
#include "tiles/util_parallel.h"

Expand All @@ -29,37 +30,6 @@ namespace orel = osmium::relations;
namespace om = osmium::memory;
namespace oeb = osmium::osm_entity_bits;

struct tmp_file {
explicit tmp_file(std::string path)
: path_{std::move(path)},
#ifdef _MSC_VER
file_(std::fopen(path_.c_str(), "wb+"))
#else
file_(std::fopen(path_.c_str(), "wb+e"))
#endif
{
utl::verify(file_ != nullptr, "tmp_file: unable to open file {}", path_);
}

~tmp_file() {
if (file_ != nullptr) {
std::fclose(file_);
boost::filesystem::remove(path_);
}
file_ = nullptr;
}

tmp_file(tmp_file const&) = default;
tmp_file(tmp_file&&) = default;
tmp_file& operator=(tmp_file const&) = default;
tmp_file& operator=(tmp_file&&) = default;

int fileno() const { return ::fileno(file_); }

std::string path_;
FILE* file_;
};

void load_osm(tile_db_handle& db_handle, feature_inserter_mt& inserter,
std::string const& osm_fname, std::string const& osm_profile,
std::string const& tmp_dname) {
Expand Down

0 comments on commit 53fa409

Please sign in to comment.