Skip to content

Commit

Permalink
Fix reading from STDIN for sort command
Browse files Browse the repository at this point in the history
Reading from STDIN did not work. See #278.

This also fixes another problem when using the multipass strategy:
In that case reading from STDIN is not allowed and we now check for
that.
  • Loading branch information
joto committed Nov 5, 2024
1 parent ab0e236 commit e34ac30
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/command_sort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,21 @@ bool CommandSort::setup(const std::vector<std::string>& arguments) {
setup_input_files(vm);
setup_output_file(vm);

if (vm.count("input-filenames")) {
m_filenames = vm["input-filenames"].as<std::vector<std::string>>();
}

if (vm.count("strategy")) {
m_strategy = vm["strategy"].as<std::string>();
if (m_strategy != "simple" && m_strategy != "multipass") {
throw argument_error{"Unknown strategy: " + m_strategy};
}
}

if (m_strategy == "multipass") {
if (std::any_of(m_input_filenames.cbegin(), m_input_filenames.cend(), [&](const std::string& name) {
return name == "-";
})) {
throw argument_error{"Can not read from STDIN when using multipass strategy"};
}
}

return true;
}

Expand All @@ -110,8 +114,8 @@ bool CommandSort::run_single_pass() {

m_vout << "Reading contents of input files...\n";
osmium::ProgressBar progress_bar{file_size_sum(m_input_files), display_progress()};
for (const std::string& file_name : m_filenames) {
osmium::io::Reader reader{file_name, osmium::osm_entity_bits::object};
for (const auto& file : m_input_files) {
osmium::io::Reader reader{file, osmium::osm_entity_bits::object};
const osmium::io::Header header{reader.header()};
bounding_box.extend(header.joined_boxes());
while (osmium::memory::Buffer buffer = reader.read()) {
Expand Down Expand Up @@ -167,8 +171,8 @@ bool CommandSort::run_multi_pass() {
osmium::Box bounding_box;

m_vout << "Reading input file headers...\n";
for (const std::string& file_name : m_filenames) {
osmium::io::Reader reader{file_name, osmium::osm_entity_bits::nothing};
for (const auto& file : m_input_files) {
osmium::io::Reader reader{file, osmium::osm_entity_bits::nothing};
const osmium::io::Header header{reader.header()};
bounding_box.extend(header.joined_boxes());
reader.close();
Expand Down Expand Up @@ -196,8 +200,8 @@ bool CommandSort::run_multi_pass() {

m_vout << "Pass " << pass++ << "...\n";
m_vout << "Reading contents of input files...\n";
for (const std::string& file_name : m_filenames) {
osmium::io::Reader reader{file_name, entity};
for (const auto& file : m_input_files) {
osmium::io::Reader reader{file, entity};
const osmium::io::Header read_header{reader.header()};
bounding_box.extend(read_header.joined_boxes());
while (osmium::memory::Buffer buffer = reader.read()) {
Expand Down
1 change: 0 additions & 1 deletion src/command_sort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

class CommandSort : public CommandWithMultipleOSMInputs, public with_osm_output {

std::vector<std::string> m_filenames;
std::string m_strategy{"simple"};

public:
Expand Down

0 comments on commit e34ac30

Please sign in to comment.