1 // Copyright 2014 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_builder.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
18 static const char kEnablePromptsSwitch
[] = "enable-prompts";
20 InputBuilder::InputBuilder(const std::string
& title
,
21 const std::string
& default_value
,
25 default_value_(default_value
),
26 low_range_(low_range
),
27 high_range_(high_range
) {}
29 InputBuilder::~InputBuilder() {}
31 std::string
InputBuilder::GetStringInput() const {
32 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(kEnablePromptsSwitch
))
33 return default_value_
;
35 printf("\n%s\n", title_
.c_str());
36 if (!default_value_
.empty())
37 printf("Hit enter for default (%s):\n", default_value_
.c_str());
42 if (!fgets(raw_input
, 128, stdin
)) {
47 std::string input
= raw_input
;
48 input
= input
.substr(0, input
.size() - 1); // Strip last \n.
49 if (input
.empty() && !default_value_
.empty())
50 return default_value_
;
52 if (!ValidateInput(input
)) {
53 printf("Invalid input. Please try again.\n");
54 return GetStringInput();
59 int InputBuilder::GetIntInput() const {
60 std::string string_input
= GetStringInput();
62 CHECK(base::StringToInt(string_input
, &int_value
));
66 bool InputBuilder::ValidateInput(const std::string input
) const {
67 // Check for a valid range.
68 if (low_range_
== INT_MIN
&& high_range_
== INT_MAX
)
71 if (!base::StringToInt(input
, &value
))
73 return value
>= low_range_
&& value
<= high_range_
;