Skip to content

Commit 8cad37c

Browse files
committed
Use return or throw instead of std::exit where possible
1 parent b89da13 commit 8cad37c

19 files changed

Lines changed: 54 additions & 65 deletions

examples/osmium_amenity_list.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626

2727
#include <cstdio> // for std::printf
28-
#include <cstdlib> // for std::exit
2928
#include <iostream> // for std::cerr
3029
#include <string> // for std::string
3130

@@ -114,7 +113,7 @@ class AmenityHandler : public osmium::handler::Handler {
114113
int main(int argc, char* argv[]) {
115114
if (argc != 2) {
116115
std::cerr << "Usage: " << argv[0] << " OSMFILE\n";
117-
std::exit(1);
116+
return 1;
118117
}
119118

120119
try {
@@ -175,7 +174,7 @@ int main(int argc, char* argv[]) {
175174
} catch (const std::exception& e) {
176175
// All exceptions used by the Osmium library derive from std::exception.
177176
std::cerr << e.what() << '\n';
178-
std::exit(1);
177+
return 1;
179178
}
180179
}
181180

examples/osmium_area_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
2626
*/
2727

28-
#include <cstdlib> // for std::exit
2928
#include <cstring> // for std::strcmp
3029
#include <iostream> // for std::cout, std::cerr
3130

@@ -93,18 +92,18 @@ void print_help() {
9392

9493
void print_usage(const char* prgname) {
9594
std::cerr << "Usage: " << prgname << " [OPTIONS] OSMFILE\n";
96-
std::exit(1);
9795
}
9896

9997
int main(int argc, char* argv[]) {
10098
if (argc > 1 && (!std::strcmp(argv[1], "-h") ||
10199
!std::strcmp(argv[1], "--help"))) {
102100
print_help();
103-
std::exit(0);
101+
return 0;
104102
}
105103

106104
if (argc != 3) {
107105
print_usage(argv[0]);
106+
return 1;
108107
}
109108

110109
try {
@@ -120,6 +119,7 @@ int main(int argc, char* argv[]) {
120119
handler.set<osmium::handler::Dump>(std::cout);
121120
} else {
122121
print_usage(argv[0]);
122+
return 1;
123123
}
124124

125125
osmium::io::File input_file{argv[2]};
@@ -198,7 +198,7 @@ int main(int argc, char* argv[]) {
198198
} catch (const std::exception& e) {
199199
// All exceptions used by the Osmium library derive from std::exception.
200200
std::cerr << e.what() << '\n';
201-
std::exit(1);
201+
return 1;
202202
}
203203
}
204204

examples/osmium_change_tags.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
2323
*/
2424

25-
#include <cstdlib> // for std::exit
2625
#include <cstring> // for std::strcmp
2726
#include <exception> // for std::exception
2827
#include <iostream> // for std::cout, std::cerr
@@ -150,7 +149,7 @@ class RewriteHandler : public osmium::handler::Handler {
150149
int main(int argc, char* argv[]) {
151150
if (argc != 3) {
152151
std::cerr << "Usage: " << argv[0] << " INFILE OUTFILE\n";
153-
std::exit(1);
152+
return 1;
154153
}
155154

156155
// Get input and output file names from command line.
@@ -197,7 +196,7 @@ int main(int argc, char* argv[]) {
197196
} catch (const std::exception& e) {
198197
// All exceptions used by the Osmium library derive from std::exception.
199198
std::cerr << e.what() << '\n';
200-
std::exit(1);
199+
return 1;
201200
}
202201
}
203202

examples/osmium_convert.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
1818
*/
1919

20-
#include <cstdlib> // for std::exit
2120
#include <cstring> // for std::strcmp
2221
#include <exception> // for std::exception
2322
#include <iostream> // for std::cout, std::cerr
@@ -53,18 +52,18 @@ void print_help() {
5352

5453
void print_usage(const char* prgname) {
5554
std::cerr << "Usage: " << prgname << " [OPTIONS] [INFILE [OUTFILE]]\n";
56-
std::exit(0);
5755
}
5856

5957
int main(int argc, char* argv[]) {
6058
if (argc == 1) {
6159
print_usage(argv[0]);
60+
return 0;
6261
}
6362

6463
if (argc > 1 && (!std::strcmp(argv[1], "-h") ||
6564
!std::strcmp(argv[1], "--help"))) {
6665
print_help();
67-
std::exit(0);
66+
return 0;
6867
}
6968

7069
// Input and output format are empty by default. Later this will mean that
@@ -83,6 +82,7 @@ int main(int argc, char* argv[]) {
8382
input_format = argv[i];
8483
} else {
8584
print_usage(argv[0]);
85+
return 1;
8686
}
8787
} else if (!std::strncmp(argv[i], "--from-format=", 14)) {
8888
input_format = argv[i] + 14;
@@ -92,6 +92,7 @@ int main(int argc, char* argv[]) {
9292
output_format = argv[i];
9393
} else {
9494
print_usage(argv[0]);
95+
return 1;
9596
}
9697
} else if (!std::strncmp(argv[i], "--to-format=", 12)) {
9798
output_format = argv[i] + 12;
@@ -101,6 +102,7 @@ int main(int argc, char* argv[]) {
101102
output_file_name = argv[i];
102103
} else {
103104
print_usage(argv[0]);
105+
return 1;
104106
}
105107
}
106108

@@ -148,7 +150,7 @@ int main(int argc, char* argv[]) {
148150
} catch (const std::exception& e) {
149151
// All exceptions used by the Osmium library derive from std::exception.
150152
std::cerr << e.what() << '\n';
151-
std::exit(1);
153+
return 1;
152154
}
153155
}
154156

examples/osmium_count.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
#include <cstdint> // for std::uint64_t
21-
#include <cstdlib> // for std::exit
2221
#include <iostream> // for std::cout, std::cerr
2322

2423
// Allow any format of input files (XML, PBF, ...)
@@ -63,7 +62,7 @@ struct CountHandler : public osmium::handler::Handler {
6362
int main(int argc, char* argv[]) {
6463
if (argc != 2) {
6564
std::cerr << "Usage: " << argv[0] << " OSMFILE\n";
66-
std::exit(1);
65+
return 1;
6766
}
6867

6968
try {
@@ -95,7 +94,7 @@ int main(int argc, char* argv[]) {
9594
} catch (const std::exception& e) {
9695
// All exceptions used by the Osmium library derive from std::exception.
9796
std::cerr << e.what() << '\n';
98-
std::exit(1);
97+
return 1;
9998
}
10099
}
101100

examples/osmium_create_pois.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
2020
*/
2121

22-
#include <cstdlib> // for std::exit
2322
#include <cstring> // for std::strcmp
2423
#include <ctime> // for std::time
2524
#include <exception> // for std::exception
@@ -37,7 +36,7 @@
3736
int main(int argc, char* argv[]) {
3837
if (argc != 2) {
3938
std::cerr << "Usage: " << argv[0] << " OUTFILE\n";
40-
std::exit(1);
39+
return 1;
4140
}
4241

4342
// Get output file name from command line.
@@ -94,7 +93,7 @@ int main(int argc, char* argv[]) {
9493
} catch (const std::exception& e) {
9594
// All exceptions used by the Osmium library derive from std::exception.
9695
std::cerr << e.what() << '\n';
97-
std::exit(1);
96+
return 1;
9897
}
9998
}
10099

examples/osmium_debug.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
1818
*/
1919

20-
#include <cstdlib> // for std::exit
2120
#include <iostream> // for std::cout, std::cerr
2221
#include <string> // for std::string
2322

@@ -34,7 +33,7 @@ int main(int argc, char* argv[]) {
3433
if (argc < 2 || argc > 3) {
3534
std::cerr << "Usage: " << argv[0] << " OSMFILE [TYPES]\n";
3635
std::cerr << "TYPES can be any combination of 'n', 'w', 'r', and 'c' to indicate what types of OSM entities you want (default: all).\n";
37-
std::exit(1);
36+
return 1;
3837
}
3938

4039
try {
@@ -84,7 +83,7 @@ int main(int argc, char* argv[]) {
8483
} catch (const std::exception& e) {
8584
// All exceptions used by the Osmium library derive from std::exception.
8685
std::cerr << e.what() << '\n';
87-
std::exit(1);
86+
return 1;
8887
}
8988
}
9089

examples/osmium_dump_internal.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030

3131
#include <cerrno> // for errno
32-
#include <cstdlib> // for std::exit
3332
#include <cstring> // for std::strerror
3433
#include <iostream> // for std::cout, std::cerr
3534
#include <string> // for std::string
@@ -73,8 +72,7 @@ class IndexFile {
7372
explicit IndexFile(const std::string& filename) :
7473
m_fd(::open(filename.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666)) { // NOLINT(hicpp-signed-bitwise)
7574
if (m_fd < 0) {
76-
std::cerr << "Can't open index file '" << filename << "': " << std::strerror(errno) << "\n";
77-
std::exit(2);
75+
throw std::system_error{errno, std::system_category(), "Can't open index file '" + filename};
7876
}
7977
#ifdef _WIN32
8078
_setmode(m_fd, _O_BINARY);
@@ -102,7 +100,7 @@ class IndexFile {
102100
int main(int argc, char* argv[]) {
103101
if (argc != 3) {
104102
std::cerr << "Usage: " << argv[0] << " OSMFILE DIR\n";
105-
std::exit(2);
103+
return 2;
106104
}
107105

108106
try {
@@ -117,15 +115,15 @@ int main(int argc, char* argv[]) {
117115
#endif
118116
if (result == -1 && errno != EEXIST) {
119117
std::cerr << "Problem creating directory '" << output_dir << "': " << std::strerror(errno) << "\n";
120-
std::exit(2);
118+
return 2;
121119
}
122120

123121
// Create the output file which will contain our serialized OSM data
124122
const std::string data_file{output_dir + "/data.osm.ser"};
125123
const int data_fd = ::open(data_file.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0666); // NOLINT(hicpp-signed-bitwise)
126124
if (data_fd < 0) {
127125
std::cerr << "Can't open data file '" << data_file << "': " << std::strerror(errno) << "\n";
128-
std::exit(2);
126+
return 2;
129127
}
130128

131129
#ifdef _WIN32
@@ -196,7 +194,7 @@ int main(int argc, char* argv[]) {
196194
} catch (const std::exception& e) {
197195
// All exceptions used by the Osmium library derive from std::exception.
198196
std::cerr << e.what() << '\n';
199-
std::exit(1);
197+
return 1;
200198
}
201199
}
202200

examples/osmium_filter_discussions.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
*/
2323

2424
#include <algorithm> // for std::copy_if
25-
#include <cstdlib> // for std::exit
2625
#include <iostream> // for std::cout, std::cerr
2726

2827
// We want to read OSM files in XML format
@@ -43,7 +42,7 @@
4342
int main(int argc, char* argv[]) {
4443
if (argc != 3) {
4544
std::cout << "Usage: " << argv[0] << " INFILE OUTFILE\n";
46-
std::exit(1);
45+
return 1;
4746
}
4847

4948
try {
@@ -92,7 +91,7 @@ int main(int argc, char* argv[]) {
9291
} catch (const std::exception& e) {
9392
// All exceptions used by the Osmium library derive from std::exception.
9493
std::cerr << e.what() << '\n';
95-
std::exit(1);
94+
return 1;
9695
}
9796
}
9897

examples/osmium_index_lookup.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ int main(int argc, char* argv[]) {
338338
if (fd < 0) {
339339
std::cerr << "Can not open file '" << options.filename()
340340
<< "': " << std::strerror(errno) << '\n';
341-
std::exit(2);
341+
return 2;
342342
}
343343

344344
#ifdef _WIN32
@@ -365,7 +365,7 @@ int main(int argc, char* argv[]) {
365365
} catch (const std::exception& e) {
366366
// All exceptions used by the Osmium library derive from std::exception.
367367
std::cerr << e.what() << '\n';
368-
std::exit(1);
368+
return 1;
369369
}
370370
}
371371

0 commit comments

Comments
 (0)