Skip to content

Commit b7ca67c

Browse files
authored
Merge pull request #1190 from joto/cleanup-options-handling
Cleanup options handling
2 parents 4bb963d + f0be07c commit b7ca67c

12 files changed

+69
-74
lines changed

src/geometry-processor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "tagtransform.hpp"
1414

1515
struct middle_query_t;
16-
struct options_t;
16+
class options_t;
1717
class reprojection;
1818

1919
struct geometry_processor

src/middle-ram.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "middle.hpp"
1717

1818
class node_ram_cache;
19-
struct options_t;
19+
class options_t;
2020

2121
template <typename T, size_t N>
2222
class cache_block_t

src/node-persistent-cache.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "node-ram-cache.hpp"
1010
#include "osmtypes.hpp"
1111

12-
struct options_t;
12+
class options_t;
1313

1414
class node_persistent_cache
1515
{

src/options.cpp

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include <cstring>
99
#include <getopt.h>
1010
#include <osmium/version.hpp>
11-
#include <sstream>
1211
#include <stdexcept>
1312
#include <thread> // for number of threads
1413

@@ -86,7 +85,7 @@ void short_usage(char *arg0)
8685
"\t{} -h|--help\n"_format(program_name(arg0))};
8786
}
8887

89-
void long_usage(char const *arg0, bool verbose = false)
88+
void long_usage(char const *arg0, bool verbose)
9089
{
9190
char const *const name = program_name(arg0);
9291

@@ -257,33 +256,27 @@ void long_usage(char const *arg0, bool verbose = false)
257256

258257
} // anonymous namespace
259258

260-
database_options_t::database_options_t()
261-
: db(boost::none), username(boost::none), host(boost::none),
262-
password(boost::none), port(boost::none)
263-
{}
264-
265259
std::string database_options_t::conninfo() const
266260
{
267-
std::ostringstream out;
261+
std::string out{"fallback_application_name='osm2pgsql'"};
268262

269-
out << "fallback_application_name='osm2pgsql'";
270-
if (db) {
271-
out << " dbname='" << *db << "'";
263+
if (!db.empty()) {
264+
out += " dbname='{}'"_format(db);
272265
}
273-
if (username) {
274-
out << " user='" << *username << "'";
266+
if (!username.empty()) {
267+
out += " user='{}'"_format(username);
275268
}
276-
if (password) {
277-
out << " password='" << *password << "'";
269+
if (!password.empty()) {
270+
out += " password='{}'"_format(password);
278271
}
279-
if (host) {
280-
out << " host='" << *host << "'";
272+
if (!host.empty()) {
273+
out += " host='{}'"_format(host);
281274
}
282-
if (port) {
283-
out << " port='" << *port << "'";
275+
if (!port.empty()) {
276+
out += " port='{}'"_format(port);
284277
}
285278

286-
return out.str();
279+
return out;
287280
}
288281

289282
options_t::options_t()
@@ -302,8 +295,6 @@ options_t::options_t()
302295
}
303296
}
304297

305-
options_t::~options_t() {}
306-
307298
static osmium::Box parse_bbox(char const *bbox)
308299
{
309300
double minx, maxx, miny, maxy;
@@ -331,6 +322,8 @@ static osmium::Box parse_bbox(char const *bbox)
331322

332323
options_t::options_t(int argc, char *argv[]) : options_t()
333324
{
325+
bool help_verbose = false; // Will be set when -v/--verbose is set
326+
334327
int c;
335328

336329
//keep going while there are args left to handle
@@ -353,7 +346,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
353346
create = true;
354347
break;
355348
case 'v':
356-
verbose = true;
349+
help_verbose = true;
357350
break;
358351
case 's':
359352
slim = true;
@@ -472,21 +465,21 @@ options_t::options_t(int argc, char *argv[]) : options_t()
472465
extra_attributes = true;
473466
break;
474467
case 'k':
475-
if (hstore_mode != HSTORE_NONE) {
468+
if (hstore_mode != hstore_column::none) {
476469
throw std::runtime_error{"You can not specify both --hstore "
477470
"(-k) and --hstore-all (-j)\n"};
478471
}
479-
hstore_mode = HSTORE_NORM;
472+
hstore_mode = hstore_column::norm;
480473
break;
481474
case 208:
482475
hstore_match_only = true;
483476
break;
484477
case 'j':
485-
if (hstore_mode != HSTORE_NONE) {
478+
if (hstore_mode != hstore_column::none) {
486479
throw std::runtime_error{"You can not specify both --hstore "
487480
"(-k) and --hstore-all (-j)\n"};
488481
}
489-
hstore_mode = HSTORE_ALL;
482+
hstore_mode = hstore_column::all;
490483
break;
491484
case 'z':
492485
hstore_columns.emplace_back(optarg);
@@ -559,7 +552,7 @@ options_t::options_t(int argc, char *argv[]) : options_t()
559552

560553
//they were looking for usage info
561554
if (long_usage_bool) {
562-
long_usage(argv[0], verbose);
555+
long_usage(argv[0], help_verbose);
563556
return;
564557
}
565558

@@ -581,11 +574,9 @@ options_t::options_t(int argc, char *argv[]) : options_t()
581574
check_options();
582575

583576
if (pass_prompt) {
584-
char *prompt = simple_prompt("Password:", 100, 0);
585-
if (prompt == nullptr) {
586-
database_options.password = boost::none;
587-
} else {
588-
database_options.password = std::string(prompt);
577+
char const *prompt = simple_prompt("Password:", 100, 0);
578+
if (prompt != nullptr) {
579+
database_options.password = prompt;
589580
}
590581
}
591582
}
@@ -605,15 +596,15 @@ void options_t::check_options()
605596
throw std::runtime_error{"--drop only makes sense with --slim.\n"};
606597
}
607598

608-
if (hstore_mode == HSTORE_NONE && hstore_columns.empty() &&
599+
if (hstore_mode == hstore_column::none && hstore_columns.empty() &&
609600
hstore_match_only) {
610601
fprintf(stderr,
611602
"Warning: --hstore-match-only only makes sense with --hstore, "
612603
"--hstore-all, or --hstore-column; ignored.\n");
613604
hstore_match_only = false;
614605
}
615606

616-
if (enable_hstore_index && hstore_mode == HSTORE_NONE &&
607+
if (enable_hstore_index && hstore_mode == hstore_column::none &&
617608
hstore_columns.empty()) {
618609
fprintf(stderr, "Warning: --hstore-add-index only makes sense with "
619610
"hstore enabled.\n");

src/options.hpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,46 @@
1111
#include <string>
1212
#include <vector>
1313

14-
/* Variants for generation of hstore column */
15-
/* No hstore column */
16-
#define HSTORE_NONE 0
17-
/* create a hstore column for all tags which do not have an exclusive column */
18-
#define HSTORE_NORM 1
19-
/* create a hstore column for all tags */
20-
#define HSTORE_ALL 2
14+
/// Variants for generation of hstore column
15+
enum class hstore_column : char
16+
{
17+
/// no hstore column
18+
none = 0,
19+
/// create hstore column for all tags that don't have their own column
20+
norm = 1,
21+
/// create hstore column for all tags
22+
all = 2
23+
};
2124

2225
/**
2326
* Database options, not specific to a table
2427
*/
25-
class database_options_t
28+
struct database_options_t
2629
{
27-
public:
28-
database_options_t();
29-
boost::optional<std::string> db;
30-
boost::optional<std::string> username;
31-
boost::optional<std::string> host;
32-
boost::optional<std::string> password;
33-
boost::optional<std::string> port;
30+
std::string db;
31+
std::string username;
32+
std::string host;
33+
std::string password;
34+
std::string port;
3435

3536
std::string conninfo() const;
3637
};
3738

3839
/**
3940
* Structure for storing command-line and other options
4041
*/
41-
struct options_t
42+
class options_t
4243
{
4344
public:
44-
// fixme: bring back old comment
45+
/**
46+
* Constructor setting default values for all options. Used for testing.
47+
*/
4548
options_t();
49+
4650
/**
47-
* Parse the options from the command line
51+
* Constructor parsing the options from the command line.
4852
*/
4953
options_t(int argc, char *argv[]);
50-
virtual ~options_t();
5154

5255
std::string prefix{"planet_osm"}; ///< prefix for table names
5356
std::shared_ptr<reprojection> projection; ///< SRS of projection
@@ -80,7 +83,7 @@ struct options_t
8083
std::string expire_tiles_filename{"dirty_tiles"};
8184

8285
/// add an additional hstore column with objects key/value pairs, and what type of hstore column
83-
int hstore_mode = HSTORE_NONE;
86+
hstore_column hstore_mode = hstore_column::none;
8487

8588
bool enable_hstore_index = false; ///< add an index on the hstore column
8689

@@ -124,7 +127,6 @@ struct options_t
124127
std::string input_reader{"auto"};
125128
osmium::Box bbox;
126129
bool extra_attributes = false;
127-
bool verbose = false;
128130

129131
std::vector<std::string> input_files;
130132

src/output-multi.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@
1717
#include <memory>
1818
#include <string>
1919

20+
class options_t;
2021
class table_t;
2122
class tagtransform_t;
2223
struct export_list;
2324
struct middle_query_t;
24-
struct options_t;
2525

2626
class output_multi_t : public output_t
2727
{

src/output.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ parse_multi_single(pt::ptree const &conf,
7373
new_opts.tblsmain_index = conf.get("tablespace-index", "");
7474
new_opts.tblsmain_data = conf.get("tablespace-data", "");
7575

76-
override_if<int>(new_opts.hstore_mode, "enable-hstore", conf);
76+
if (conf.get<bool>("enable-hstore", false)) {
77+
new_opts.hstore_mode = hstore_column::norm;
78+
}
7779
override_if<bool>(new_opts.enable_hstore_index, "enable-hstore-index",
7880
conf);
7981
override_if<bool>(new_opts.enable_multi, "enable-multi", conf);

src/table.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515

1616
table_t::table_t(std::string const &name, std::string const &type,
1717
columns_t const &columns, hstores_t const &hstore_columns,
18-
int const srid, bool const append, int const hstore_mode,
18+
int const srid, bool const append, hstore_column hstore_mode,
1919
std::shared_ptr<db_copy_thread_t> const &copy_thread)
2020
: m_target(std::make_shared<db_target_descr_t>(name.c_str(), "osm_id")),
2121
m_type(type), m_srid(fmt::to_string(srid)), m_append(append),
2222
m_hstore_mode(hstore_mode), m_columns(columns),
2323
m_hstore_columns(hstore_columns), m_copy(copy_thread)
2424
{
2525
// if we dont have any columns
26-
if (m_columns.empty() && m_hstore_mode != HSTORE_ALL) {
26+
if (m_columns.empty() && m_hstore_mode != hstore_column::all) {
2727
throw std::runtime_error{
2828
"No columns provided for table {}"_format(name)};
2929
}
@@ -99,7 +99,7 @@ void table_t::start(std::string const &conninfo, std::string const &table_space)
9999
}
100100

101101
//add tags column
102-
if (m_hstore_mode != HSTORE_NONE) {
102+
if (m_hstore_mode != hstore_column::none) {
103103
sql += "\"tags\" hstore,";
104104
}
105105

@@ -163,7 +163,7 @@ void table_t::generate_copy_column_list()
163163
}
164164

165165
//add tags column and geom column
166-
if (m_hstore_mode != HSTORE_NONE) {
166+
if (m_hstore_mode != hstore_column::none) {
167167
m_target->rows += "tags,way";
168168
//or just the geom column
169169
} else {
@@ -264,7 +264,7 @@ void table_t::stop(bool updateable, bool enable_hstore_index,
264264
if (enable_hstore_index) {
265265
fmt::print(stderr, "Creating hstore indexes on {}\n",
266266
m_target->name);
267-
if (m_hstore_mode != HSTORE_NONE) {
267+
if (m_hstore_mode != hstore_column::none) {
268268
m_sql_conn->exec(
269269
"CREATE INDEX ON {} USING GIN (tags) {}"_format(
270270
m_target->name, tablespace_clause(table_space_index)));
@@ -303,18 +303,18 @@ void table_t::write_row(osmid_t id, taglist_t const &tags,
303303
// used to remember which columns have been written out already.
304304
std::vector<bool> used;
305305

306-
if (m_hstore_mode != HSTORE_NONE) {
306+
if (m_hstore_mode != hstore_column::none) {
307307
used.assign(tags.size(), false);
308308
}
309309

310310
//get the regular columns' values
311-
write_columns(tags, m_hstore_mode == HSTORE_NORM ? &used : nullptr);
311+
write_columns(tags, m_hstore_mode == hstore_column::norm ? &used : nullptr);
312312

313313
//get the hstore columns' values
314314
write_hstore_columns(tags);
315315

316316
//get the key value pairs for the tags column
317-
if (m_hstore_mode != HSTORE_NONE) {
317+
if (m_hstore_mode != hstore_column::none) {
318318
write_tags_column(tags, used);
319319
}
320320

src/table.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class table_t
1919
public:
2020
table_t(std::string const &name, std::string const &type,
2121
columns_t const &columns, hstores_t const &hstore_columns,
22-
int const srid, bool const append, int const hstore_mode,
22+
int const srid, bool const append, hstore_column hstore_mode,
2323
std::shared_ptr<db_copy_thread_t> const &copy_thread);
2424
table_t(table_t const &other,
2525
std::shared_ptr<db_copy_thread_t> const &copy_thread);
@@ -81,7 +81,7 @@ class table_t
8181
std::unique_ptr<pg_conn_t> m_sql_conn;
8282
std::string m_srid;
8383
bool m_append;
84-
int m_hstore_mode;
84+
hstore_column m_hstore_mode;
8585
columns_t m_columns;
8686
hstores_t m_hstore_columns;
8787
std::string m_table_space;

src/tagtransform-c.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ bool c_tagtransform_t::check_key(std::vector<taginfo> const &infos,
114114
// if we didn't find any tags that we wanted to export
115115
// and we aren't strictly adhering to the list
116116
if (!strict) {
117-
if (m_options->hstore_mode != HSTORE_NONE) {
117+
if (m_options->hstore_mode != hstore_column::none) {
118118
/* ... but if hstore_match_only is set then don't take this
119119
as a reason for keeping the object */
120120
if (!m_options->hstore_match_only) {

0 commit comments

Comments
 (0)