Skip to content

Commit e34ac30

Browse files
committed
Fix reading from STDIN for sort command
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.
1 parent ab0e236 commit e34ac30

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/command_sort.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,21 @@ bool CommandSort::setup(const std::vector<std::string>& arguments) {
7676
setup_input_files(vm);
7777
setup_output_file(vm);
7878

79-
if (vm.count("input-filenames")) {
80-
m_filenames = vm["input-filenames"].as<std::vector<std::string>>();
81-
}
82-
8379
if (vm.count("strategy")) {
8480
m_strategy = vm["strategy"].as<std::string>();
8581
if (m_strategy != "simple" && m_strategy != "multipass") {
8682
throw argument_error{"Unknown strategy: " + m_strategy};
8783
}
8884
}
8985

86+
if (m_strategy == "multipass") {
87+
if (std::any_of(m_input_filenames.cbegin(), m_input_filenames.cend(), [&](const std::string& name) {
88+
return name == "-";
89+
})) {
90+
throw argument_error{"Can not read from STDIN when using multipass strategy"};
91+
}
92+
}
93+
9094
return true;
9195
}
9296

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

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

169173
m_vout << "Reading input file headers...\n";
170-
for (const std::string& file_name : m_filenames) {
171-
osmium::io::Reader reader{file_name, osmium::osm_entity_bits::nothing};
174+
for (const auto& file : m_input_files) {
175+
osmium::io::Reader reader{file, osmium::osm_entity_bits::nothing};
172176
const osmium::io::Header header{reader.header()};
173177
bounding_box.extend(header.joined_boxes());
174178
reader.close();
@@ -196,8 +200,8 @@ bool CommandSort::run_multi_pass() {
196200

197201
m_vout << "Pass " << pass++ << "...\n";
198202
m_vout << "Reading contents of input files...\n";
199-
for (const std::string& file_name : m_filenames) {
200-
osmium::io::Reader reader{file_name, entity};
203+
for (const auto& file : m_input_files) {
204+
osmium::io::Reader reader{file, entity};
201205
const osmium::io::Header read_header{reader.header()};
202206
bounding_box.extend(read_header.joined_boxes());
203207
while (osmium::memory::Buffer buffer = reader.read()) {

src/command_sort.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
3030

3131
class CommandSort : public CommandWithMultipleOSMInputs, public with_osm_output {
3232

33-
std::vector<std::string> m_filenames;
3433
std::string m_strategy{"simple"};
3534

3635
public:

0 commit comments

Comments
 (0)