forked from osm2pgsql-dev/osm2pgsql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.cpp
More file actions
88 lines (72 loc) · 1.98 KB
/
Copy pathutil.cpp
File metadata and controls
88 lines (72 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* SPDX-License-Identifier: GPL-2.0-or-later
*
* This file is part of osm2pgsql (https://osm2pgsql.org/).
*
* Copyright (C) 2006-2021 by the osm2pgsql developer community.
* For a full list of authors see the git log.
*/
#include "config.h"
#include "util.hpp"
#include <iostream>
#include <iterator>
#ifdef _WIN32
#include <windows.h>
#elif defined(HAVE_TERMIOS_H)
#include <termios.h>
#include <unistd.h>
#endif
namespace util {
void string_id_list_t::add(osmid_t id)
{
fmt::format_to(std::back_inserter(m_list), "{},", id);
}
std::string const &string_id_list_t::get()
{
assert(!empty());
m_list.back() = '}';
return m_list;
}
std::string human_readable_duration(uint64_t seconds)
{
if (seconds < 60) {
return "{}s"_format(seconds);
}
if (seconds < (60 * 60)) {
return "{}s ({}m {}s)"_format(seconds, seconds / 60, seconds % 60);
}
auto const secs = seconds % 60;
auto const mins = seconds / 60;
return "{}s ({}h {}m {}s)"_format(seconds, mins / 60, mins % 60, secs);
}
std::string human_readable_duration(std::chrono::milliseconds ms)
{
return human_readable_duration(static_cast<uint64_t>(
std::chrono::duration_cast<std::chrono::seconds>(ms).count()));
}
std::string get_password()
{
#ifdef _WIN32
HANDLE const handle_stdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(handle_stdin, &mode);
SetConsoleMode(handle_stdin, mode & (~ENABLE_ECHO_INPUT));
#elif defined(HAVE_TERMIOS_H)
termios orig_flags{};
tcgetattr(STDIN_FILENO, &orig_flags);
termios flags = orig_flags;
flags.c_lflag &= ~static_cast<unsigned int>(ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &flags);
#endif
std::string password;
std::cout << "Password:";
std::getline(std::cin, password);
std::cout << "\n";
#ifdef _WIN32
SetConsoleMode(handle_stdin, mode);
#elif defined(HAVE_TERMIOS_H)
tcsetattr(STDIN_FILENO, TCSANOW, &orig_flags);
#endif
return password;
}
} // namespace util