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/strip.h"
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 {
50 return command_[index];
51 }
52
53 private:
54 std::string copy_command_;
55 std::string_view command_;
56};
57
64 auto pos = command_.find('=');
65 if (pos != std::string_view::npos) {
66 command_.remove_prefix(std::min(pos + 1, command_.size()));
67 }
68}
69
75 absl::ConsumePrefix(&command_, std::string_view("AT"));
76}
77
83 auto pos = command_.find(',');
84 if (pos != std::string_view::npos) {
85 command_.remove_prefix(std::min(pos + 1, command_.size()));
86 }
87}
88
94 auto pos = command_.find(' ');
95 if (pos != std::string_view::npos) {
96 command_.remove_prefix(std::min(pos + 1, command_.size()));
97 }
98}
99
100} // namespace cuttlefish
Definition: command_parser.h:23
CommandParser(CommandParser &&)=default
void SkipWhiteSpace()
Definition: command_parser.h:93
void SkipComma()
Definition: command_parser.h:82
std::string GetNextStrDeciToHex()
Definition: command_parser.cpp:69
std::string_view command_
Definition: command_parser.h:55
int GetNextInt()
Definition: command_parser.cpp:105
void SkipPrefixAT()
Definition: command_parser.h:74
const std::string_view * operator->() const
Definition: command_parser.h:46
std::string copy_command_
Definition: command_parser.h:54
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:63
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_driver.h:20