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
16#include <algorithm>
17#include <string>
18
19#include "absl/strings/string_view.h"
20#include "absl/strings/strip.h"
21
22namespace cuttlefish {
23
25 public:
26 explicit CommandParser(const std::string& command) : copy_command_(command) {
28 };
29
30 ~CommandParser() = default;
31
34
35 inline void SkipPrefix();
36 inline void SkipPrefixAT();
37 inline void SkipComma();
38 inline void SkipWhiteSpace();
39
40 absl::string_view GetNextStr();
41 absl::string_view GetNextStr(char flag);
42 std::string GetNextStrDeciToHex(); /* for AT+CRSM */
43
44 int GetNextInt();
45 int GetNextHexInt();
46
47 const absl::string_view* operator->() const { return &command_; }
48 const absl::string_view& operator*() const { return command_; }
49 bool operator==(const std::string& rhs) const { return command_ == rhs; }
50 absl::string_view::const_reference& operator[](int index) const {
51 return command_[index];
52 }
53
54 private:
55 std::string copy_command_;
56 absl::string_view command_;
57};
58
65 auto pos = command_.find('=');
66 if (pos != absl::string_view::npos) {
67 command_.remove_prefix(std::min(pos + 1, command_.size()));
68 }
69}
70
76 absl::ConsumePrefix(&command_, absl::string_view("AT"));
77}
78
84 auto pos = command_.find(',');
85 if (pos != absl::string_view::npos) {
86 command_.remove_prefix(std::min(pos + 1, command_.size()));
87 }
88}
89
95 auto pos = command_.find(' ');
96 if (pos != absl::string_view::npos) {
97 command_.remove_prefix(std::min(pos + 1, command_.size()));
98 }
99}
100
101} // namespace cuttlefish
Definition: command_parser.h:24
CommandParser(CommandParser &&)=default
absl::string_view::const_reference & operator[](int index) const
Definition: command_parser.h:50
void SkipWhiteSpace()
Definition: command_parser.h:94
void SkipComma()
Definition: command_parser.h:83
std::string GetNextStrDeciToHex()
Definition: command_parser.cpp:71
int GetNextInt()
Definition: command_parser.cpp:107
absl::string_view GetNextStr()
Definition: command_parser.cpp:30
const absl::string_view * operator->() const
Definition: command_parser.h:47
void SkipPrefixAT()
Definition: command_parser.h:75
std::string copy_command_
Definition: command_parser.h:55
CommandParser & operator=(CommandParser &&)=default
absl::string_view command_
Definition: command_parser.h:56
const absl::string_view & operator*() const
Definition: command_parser.h:48
CommandParser(const std::string &command)
Definition: command_parser.h:26
void SkipPrefix()
Definition: command_parser.h:64
int GetNextHexInt()
Definition: command_parser.cpp:123
bool operator==(const std::string &rhs) const
Definition: command_parser.h:49
#define min(a, b)
Definition: ext4_utils.h:44
Definition: alloc_driver.h:21