return ret;
}
-int main(int argc, char **argv)
+// Note: Limits are inclusive.
+int fetch_config_int(const vector<ConfigLine> &config, const string &keyword, int min_limit, int max_limit)
{
- fprintf(stderr, "\nCubemap starting.\n");
-
- string config_filename = (argc == 1) ? "cubemap.config" : argv[1];
- vector<ConfigLine> config = parse_config(config_filename);
-
- // Go through each (parsed) configuration line.
- int port = -1, num_servers = -1;
+ bool value_found = false;
+ int value = -1;
for (unsigned i = 0; i < config.size(); ++i) {
- if (config[i].keyword == "port") {
- if (config[i].parameters.size() > 0 ||
- config[i].arguments.size() != 1) {
- fprintf(stderr, "ERROR: 'port' takes one argument and no parameters\n");
- exit(1);
- }
- port = atoi(config[i].arguments[0].c_str());
+ if (config[i].keyword != keyword) {
+ continue;
}
- if (config[i].keyword == "num_servers") {
- if (config[i].parameters.size() > 0 ||
- config[i].arguments.size() != 1) {
- fprintf(stderr, "ERROR: 'num_servers' takes one argument and no parameters\n");
- exit(1);
- }
- num_servers = atoi(config[i].arguments[0].c_str());
+ if (config[i].parameters.size() > 0 ||
+ config[i].arguments.size() != 1) {
+ fprintf(stderr, "ERROR: '%s' takes one argument and no parameters\n", keyword.c_str());
+ exit(1);
}
+ value_found = true;
+ value = atoi(config[i].arguments[0].c_str()); // TODO: verify int validity.
}
- if (port <= 0 || port > 65535) {
- fprintf(stderr, "ERROR: Missing or invalid 'port' statement in config file\n");
+ if (!value_found) {
+ fprintf(stderr, "ERROR: Missing '%s' statement in config file.\n",
+ keyword.c_str());
exit(1);
}
- if (num_servers <= 0 || num_servers > 20000) { // 20000 is an insanely high max limit.
- fprintf(stderr, "ERROR: Missing or invalid 'num_servers' statement in config file\n");
+ if (value < min_limit || value > max_limit) {
+ fprintf(stderr, "ERROR: '%s' is set to %d, must be in [%d,%d]\n",
+ keyword.c_str(), value, min_limit, max_limit);
exit(1);
}
+ return value;
+}
+
+int main(int argc, char **argv)
+{
+ fprintf(stderr, "\nCubemap starting.\n");
+
+ string config_filename = (argc == 1) ? "cubemap.config" : argv[1];
+ vector<ConfigLine> config = parse_config(config_filename);
+
+ int port = fetch_config_int(config, "port", 1, 65535);
+ int num_servers = fetch_config_int(config, "num_servers", 1, 20000); // Insanely high max limit.
servers = new ServerPool(num_servers);