Android-cuttlefish cvd tool
command_parser.h
Go to the documentation of this file.
1//
2// Copyright (C) 2020 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
17
18#include <algorithm>
19#include <string>
20
21namespace cuttlefish {
22
24 public:
25 explicit CommandParser(const std::string& command) : copy_command_(command) {
27 };
28
29 ~CommandParser() = default;
30
33
34 inline void SkipPrefix();
35 inline void SkipPrefixAT();
36 inline void SkipComma();
37 inline void SkipWhiteSpace();
38
39 std::string_view GetNextStr();
40 std::string_view GetNextStr(char flag);
41 std::string GetNextStrDeciToHex(); /* for AT+CRSM */
42
43 int GetNextInt();
44 int GetNextHexInt();
45
46 const std::string_view* operator->() const { return &command_; }
47 const std::string_view& operator*() const { return command_; }
48 bool operator==(const std::string &rhs) const { return command_ == rhs; }
49 std::string_view::const_reference& operator[](int index) const { return command_[index]; }
50
51 private:
52 std::string copy_command_;
53 std::string_view command_;
54};
55
62 auto pos = command_.find('=');
63 if (pos != std::string_view::npos) {
64 command_.remove_prefix(std::min(pos + 1, command_.size()));
65 }
66}
67
73 android::base::ConsumePrefix(&command_, std::string_view("AT"));
74}
75
81 auto pos = command_.find(',');
82 if (pos != std::string_view::npos) {
83 command_.remove_prefix(std::min(pos + 1, command_.size()));
84 }
85}
86
92 auto pos = command_.find(' ');
93 if (pos != std::string_view::npos) {
94 command_.remove_prefix(std::min(pos + 1, command_.size()));
95 }
96}
97
98} // namespace cuttlefish
Definition: command_parser.h:23
CommandParser(CommandParser &&)=default
void SkipWhiteSpace()
Definition: command_parser.h:91
void SkipComma()
Definition: command_parser.h:80
std::string GetNextStrDeciToHex()
Definition: command_parser.cpp:69
std::string_view command_
Definition: command_parser.h:53
int GetNextInt()
Definition: command_parser.cpp:105
void SkipPrefixAT()
Definition: command_parser.h:72
const std::string_view * operator->() const
Definition: command_parser.h:46
std::string copy_command_
Definition: command_parser.h:52
CommandParser & operator=(CommandParser &&)=default
std::string_view::const_reference & operator[](int index) const
Definition: command_parser.h:49
std::string_view GetNextStr()
Definition: command_parser.cpp:28
CommandParser(const std::string &command)
Definition: command_parser.h:25
void SkipPrefix()
Definition: command_parser.h:61
int GetNextHexInt()
Definition: command_parser.cpp:121
bool operator==(const std::string &rhs) const
Definition: command_parser.h:48
const std::string_view & operator*() const
Definition: command_parser.h:47
#define min(a, b)
Definition: ext4_utils.h:44
bool ConsumePrefix(std::string_view *s, std::string_view prefix)
Definition: strings.h:132
Definition: alloc_utils.cpp:23