1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/cast/test/utility/input_helper.h"
10 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h"
17 InputBuilder::InputBuilder(const std::string& title,
18 const std::string& default_value,
22 default_value_(default_value),
23 low_range_(low_range),
24 high_range_(high_range) {}
26 InputBuilder::~InputBuilder() {}
28 std::string InputBuilder::GetStringInput() const {
29 printf("\n%s\n", title_.c_str());
30 if (!default_value_.empty())
31 printf("Hit enter for default (%s):\n", default_value_.c_str());
36 if (!fgets(raw_input, 128, stdin)) {
41 std::string input = raw_input;
42 input = input.substr(0, input.size() - 1); // Strip last \n.
43 if (input.empty() && !default_value_.empty())
44 return default_value_;
46 if (!ValidateInput(input)) {
47 printf("Invalid input. Please try again.\n");
48 return GetStringInput();
53 int InputBuilder::GetIntInput() const {
54 std::string string_input = GetStringInput();
56 CHECK(base::StringToInt(string_input, &int_value));
60 bool InputBuilder::ValidateInput(const std::string input) const {
61 // Check for a valid range.
62 if (low_range_ == INT_MIN && high_range_ == INT_MAX) return true;
64 if (!base::StringToInt(input, &value))
66 return value >= low_range_ && value <= high_range_;