This source file includes following definitions.
- high_range_
- GetStringInput
- GetIntInput
- ValidateInput
#include "media/cast/test/utility/input_builder.h"
#include <stdlib.h>
#include <cstdio>
#include "base/command_line.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
namespace media {
namespace cast {
namespace test {
static const char kEnablePromptsSwitch[] = "enable-prompts";
InputBuilder::InputBuilder(const std::string& title,
const std::string& default_value,
int low_range,
int high_range)
: title_(title),
default_value_(default_value),
low_range_(low_range),
high_range_(high_range) {}
InputBuilder::~InputBuilder() {}
std::string InputBuilder::GetStringInput() const {
if (!CommandLine::ForCurrentProcess()->HasSwitch(kEnablePromptsSwitch))
return default_value_;
printf("\n%s\n", title_.c_str());
if (!default_value_.empty())
printf("Hit enter for default (%s):\n", default_value_.c_str());
printf("# ");
fflush(stdout);
char raw_input[128];
if (!fgets(raw_input, 128, stdin)) {
NOTREACHED();
return std::string();
}
std::string input = raw_input;
input = input.substr(0, input.size() - 1);
if (input.empty() && !default_value_.empty())
return default_value_;
if (!ValidateInput(input)) {
printf("Invalid input. Please try again.\n");
return GetStringInput();
}
return input;
}
int InputBuilder::GetIntInput() const {
std::string string_input = GetStringInput();
int int_value;
CHECK(base::StringToInt(string_input, &int_value));
return int_value;
}
bool InputBuilder::ValidateInput(const std::string input) const {
if (low_range_ == INT_MIN && high_range_ == INT_MAX)
return true;
int value;
if (!base::StringToInt(input, &value))
return false;
return value >= low_range_ && value <= high_range_;
}
}
}
}