diff --git a/lib/configuration.cc b/lib/configuration.cc index cd8edb7fd9297789b85fe8208697518fd90233fd..a9fb2e01ea9a888fad4b05f40ca52de10dae77c4 100644 --- a/lib/configuration.cc +++ b/lib/configuration.cc @@ -33,10 +33,8 @@ #include "lib/configuration.h" #include "lib/message.h" -#include <iostream> -#include <fstream> -#include <string> -#include <string.h> +#include <cstring> +#include <stdexcept> namespace transport { @@ -94,52 +92,53 @@ Configuration::Configuration(std::ifstream &file) } // Get the command - // This is pretty horrible, but C++ does promise that &line[0] - // is going to be a mutable contiguous buffer... - char *cmd = strtok(&line[0], " \t"); + unsigned int t1 = line.find_first_of(" \t"); + string cmd = line.substr(0, t1); - if (strcasecmp(cmd, "f") == 0) { - char *arg = strtok(NULL, " \t"); - if (!arg) { + if (strcasecmp(cmd.c_str(), "f") == 0) { + unsigned int t2 = line.find_first_not_of(" \t", t1); + if (t2 == string::npos) { Panic ("'f' configuration line requires an argument"); } - char *strtolPtr; - f = strtoul(arg, &strtolPtr, 0); - if ((*arg == '\0') || (*strtolPtr != '\0')) { + + try { + f = stoul(line.substr(t2, string::npos)); + } catch (std::invalid_argument& ia) { Panic("Invalid argument to 'f' configuration line"); } - } else if (strcasecmp(cmd, "replica") == 0) { - char *arg = strtok(NULL, " \t"); - if (!arg) { + } else if (strcasecmp(cmd.c_str(), "replica") == 0) { + unsigned int t2 = line.find_first_not_of(" \t", t1); + if (t2 == string::npos) { Panic ("'replica' configuration line requires an argument"); } - char *host = strtok(arg, ":"); - char *port = strtok(NULL, ""); - - if (!host || !port) { + unsigned int t3 = line.find_first_of(":", t2); + if (t3 == string::npos) { Panic("Configuration line format: 'replica host:port'"); } - replicas.push_back(ReplicaAddress(string(host), string(port))); - } else if (strcasecmp(cmd, "multicast") == 0) { - char *arg = strtok(NULL, " \t"); - if (!arg) { + string host = line.substr(t2, t3-t2); + string port = line.substr(t3+1, string::npos); + + replicas.push_back(ReplicaAddress(host, port)); + } else if (strcasecmp(cmd.c_str(), "multicast") == 0) { + unsigned int t2 = line.find_first_not_of(" \t", t1); + if (t2 == string::npos) { Panic ("'multicast' configuration line requires an argument"); } - char *host = strtok(arg, ":"); - char *port = strtok(NULL, ""); - - if (!host || !port) { - Panic("Configuration line format: 'multicast host:port'"); + unsigned int t3 = line.find_first_of(":", t2); + if (t3 == string::npos) { + Panic("Configuration line format: 'replica host:port'"); } - multicastAddress = new ReplicaAddress(string(host), - string(port)); + string host = line.substr(t2, t3-t2); + string port = line.substr(t3+1, string::npos); + + multicastAddress = new ReplicaAddress(host, port); hasMulticast = true; } else { - Panic("Unknown configuration directive: %s", cmd); + Panic("Unknown configuration directive: %s", cmd.c_str()); } } diff --git a/lib/configuration.h b/lib/configuration.h index 48fd00814fcbac495e84d0ebcd2211723a9fbc52..76e6d7d2d3e2544a9ce7c4cd3a3b786fb156a9b9 100644 --- a/lib/configuration.h +++ b/lib/configuration.h @@ -84,6 +84,7 @@ private: } // namespace transport + namespace std { template <> struct hash<transport::ReplicaAddress> { @@ -110,5 +111,4 @@ template <> struct hash<transport::Configuration> }; } - #endif /* _LIB_CONFIGURATION_H_ */ diff --git a/lib/tcptransport.cc b/lib/tcptransport.cc index 24b7c2fdb1a972e08986886676a032eb21e22ffb..3ebd780358bda89d214674f2e9c0a705f18f8998 100644 --- a/lib/tcptransport.cc +++ b/lib/tcptransport.cc @@ -154,9 +154,9 @@ TCPTransport::TCPTransport(double dropRate, double reorderRate, lastTimerId = 0; // Set up libevent + evthread_use_pthreads(); event_set_log_callback(LogCallback); event_set_fatal_callback(FatalCallback); - evthread_use_pthreads(); libeventBase = event_base_new(); evthread_make_base_notifiable(libeventBase); diff --git a/lib/udptransport.cc b/lib/udptransport.cc index 879726a558932cf7d8c64df7fc0d230299ba67ed..f66312e15d0f15fbb5e9f867a44e6f4a529f5a2d 100644 --- a/lib/udptransport.cc +++ b/lib/udptransport.cc @@ -196,9 +196,9 @@ UDPTransport::UDPTransport(double dropRate, double reorderRate, } // Set up libevent + evthread_use_pthreads(); event_set_log_callback(LogCallback); event_set_fatal_callback(FatalCallback); - evthread_use_pthreads(); libeventBase = event_base_new(); evthread_make_base_notifiable(libeventBase);