Skip to content

Commit f4d1ad1

Browse files
committed
C++14 compliance. Minor fixes.
1 parent 33de18e commit f4d1ad1

File tree

4 files changed

+30
-34
lines changed

4 files changed

+30
-34
lines changed

src/handler.cc

+9-10
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class NetworkTimeout : public std::exception
6161
public:
6262
virtual const char* what() const noexcept {
6363
return "network timeout";
64-
};
64+
}
6565
};
6666

6767
class NetworkError : public std::exception
@@ -158,7 +158,7 @@ void write_line(const int32_t sockfd, string&& line)
158158
{
159159
string output = line + "\r\n";
160160
const char* msg = output.c_str();
161-
if (-1 == send(sockfd, (void*) msg, output.size(), 0))
161+
if (-1 == send(sockfd, reinterpret_cast<const void*>(msg), output.size(), 0))
162162
throw NetworkError();
163163
}
164164

@@ -191,14 +191,15 @@ auto tokenize(string&& line, char character = ' ')
191191
return rv;
192192
}
193193

194-
string generate_response(auto begin, auto end)
194+
string generate_response(vector<string>::const_iterator begin,
195+
vector<string>::const_iterator end)
195196
{
196197
string rv = "OK ";
197198

198199
for (auto i = begin ; i != end ; ++i)
199200
{
200-
bool present = binary_search(hashes.begin(),
201-
hashes.end(),
201+
bool present = binary_search(hashes.cbegin(),
202+
hashes.cend(),
202203
to_pair64(*i));
203204

204205
rv += present ? "1" : "0";
@@ -242,7 +243,6 @@ void handle_client(const int32_t fd)
242243
case Command::Bye:
243244
close(fd);
244245
return;
245-
break;
246246

247247
case Command::Status:
248248
write_line(fd, "NOT SUPPORTED");
@@ -259,17 +259,16 @@ void handle_client(const int32_t fd)
259259
case Command::Downshift:
260260
write_line(fd, "NOT OK");
261261
break;
262-
263-
default:
262+
263+
case Command::Unknown:
264264
write_line(fd, "NOT OK");
265265
close(fd);
266266
return;
267-
break;
268267
}
269268
commands = tokenize(read_line(fd));
270269
}
271270
}
272-
catch (std::exception& e)
271+
catch (std::exception&)
273272
{
274273
}
275274
}

src/main.cc

+8-14
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void load_hashes()
8383
{
8484
hash_set.reserve(45000000);
8585
}
86-
catch (std::bad_alloc& ba)
86+
catch (std::bad_alloc&)
8787
{
8888
log(LogLevel::ALERT, "couldn't reserve enough memory");
8989
exit(EXIT_FAILURE);
@@ -112,7 +112,6 @@ void load_hashes()
112112
log(LogLevel::ALERT,
113113
"shutting down!");
114114
exit(EXIT_FAILURE);
115-
return;
116115
}
117116

118117
try
@@ -140,12 +139,11 @@ void load_hashes()
140139
"loaded " + howmany + " million hashes");
141140
}
142141
}
143-
catch (std::bad_alloc& ba)
142+
catch (std::bad_alloc&)
144143
{
145144
log(LogLevel::ALERT,
146145
"couldn't allocate enough memory");
147146
exit(EXIT_FAILURE);
148-
return;
149147
}
150148
}
151149
string howmany {to_string(hash_count)};
@@ -171,7 +169,6 @@ void load_hashes()
171169
"hash file contains duplicates -- "
172170
"shutting down!");
173171
exit(EXIT_FAILURE);
174-
return;
175172
}
176173
foo = *iter;
177174
}
@@ -280,7 +277,6 @@ void parse_options(int argc, char* argv[])
280277
<< PACKAGE_VERSION
281278
<< "\n\n";
282279
exit(EXIT_SUCCESS);
283-
break;
284280

285281
case 'b':
286282
cout << argv[0]
@@ -294,11 +290,10 @@ void parse_options(int argc, char* argv[])
294290
"operating system, and a detailed description of how to recreate\n"
295291
"your bug.\n\n";
296292
exit(EXIT_SUCCESS);
297-
break;
298293

299294
case 'f':
300295
{
301-
hashes_location = string((const char*) optarg);
296+
hashes_location = string(static_cast<const char*>(optarg));
302297
ifstream infile { hashes_location.c_str() };
303298
if (not infile)
304299
{
@@ -311,14 +306,14 @@ void parse_options(int argc, char* argv[])
311306
case 'h':
312307
show_usage(argv[0]);
313308
exit(EXIT_SUCCESS);
314-
break;
315309

316310
case 'p':
317311
try
318312
{
319-
auto port_num = stoi(optarg);
320-
if (port < 1024 || port > 65535)
313+
auto port_num = static_cast<uint16_t>(stoi(optarg));
314+
if (port_num < 1024 || port_num > 65535)
321315
throw new std::exception();
316+
port = port_num;
322317
}
323318
catch (...)
324319
{
@@ -329,7 +324,6 @@ void parse_options(int argc, char* argv[])
329324
default:
330325
show_usage(argv[0]);
331326
exit(EXIT_FAILURE);
332-
break;
333327
}
334328
}
335329
}
@@ -344,9 +338,9 @@ const vector<pair64>& hashes { hash_set };
344338
@param level The priority of the message
345339
@param msg The message to write
346340
*/
347-
void log(const LogLevel level, const string msg)
341+
void log(const LogLevel level, const string&& msg)
348342
{
349-
syslog(LOG_MAKEPRI(LOG_USER, static_cast<int>(level)), msg.c_str());
343+
syslog(LOG_MAKEPRI(LOG_USER, static_cast<int>(level)), "%s", msg.c_str());
350344
}
351345

352346
/** Entry point for the application.

src/main.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1414
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515
*/
1616

17-
#ifndef __MAIN_H
18-
#define __MAIN_H
17+
#ifndef MAIN_H
18+
#define MAIN_H
1919

2020
#include "../config.h"
2121
#include <sys/types.h>
@@ -36,10 +36,13 @@ enum class LogLevel
3636
EMERGENCY = LOG_EMERG
3737
};
3838

39-
void log(const LogLevel, const std::string);
39+
void log(const LogLevel, const std::string&&);
4040
void handle_client(const int32_t);
4141
pair64 to_pair64(std::string);
4242
std::string from_pair64(pair64);
43+
bool operator<(const pair64& lhs, const pair64& rhs);
44+
bool operator==(const pair64& lhs, const pair64& rhs);
45+
bool operator>(const pair64& lhs, const pair64& rhs);
4346

4447
#endif
4548

src/to_pair64.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ pair64 to_pair64(string input)
4848
{
4949
ulong64 left { 0ULL };
5050
ulong64 right { 0ULL };
51-
int index {0};
51+
uint8_t val1 { 0ULL };
52+
uint8_t val2 { 0ULL };
53+
size_t index {0};
5254
char ch1 { 0 };
5355
char ch2 { 0 };
54-
int val1 { 0 };
55-
int val2 { 0 };
5656

5757
transform(input.begin(),
5858
input.end(),
@@ -65,11 +65,11 @@ pair64 to_pair64(string input)
6565
ch2 = input[index + 16];
6666

6767
val1 = (ch1 >= '0' and ch1 <= '9') ?
68-
ch1 - '0' :
69-
ch1 - 'a' + 10;
68+
static_cast<uint8_t>(ch1 - '0') :
69+
static_cast<uint8_t>(ch1 - 'a') + 10;
7070
val2 = (ch2 >= '0' and ch2 <= '9') ?
71-
ch2 - '0' :
72-
ch2 - 'a' + 10;
71+
static_cast<uint8_t>(ch2 - '0') :
72+
static_cast<uint8_t>(ch2 - 'a') + 10;
7373

7474
left = (left << 4) + val1;
7575
right = (right << 4) + val2;

0 commit comments

Comments
 (0)