1 /*
2  * Copyright (C) 2022 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 
17 #ifndef ART_LIBARTTOOLS_INCLUDE_TOOLS_CMDLINE_BUILDER_H_
18 #define ART_LIBARTTOOLS_INCLUDE_TOOLS_CMDLINE_BUILDER_H_
19 
20 #include <algorithm>
21 #include <iterator>
22 #include <string>
23 #include <string_view>
24 #include <vector>
25 
26 #include "android-base/stringprintf.h"
27 
28 namespace art {
29 namespace tools {
30 
31 namespace internal {
32 
ContainsOneFormatSpecifier(std::string_view format,char specifier)33 constexpr bool ContainsOneFormatSpecifier(std::string_view format, char specifier) {
34   int count = 0;
35   size_t pos = 0;
36   while ((pos = format.find('%', pos)) != std::string_view::npos) {
37     if (pos == format.length() - 1) {
38       // Invalid trailing '%'.
39       return false;
40     }
41     if (format[pos + 1] == specifier) {
42       count++;
43     } else if (format[pos + 1] != '%') {
44       // "%%" is okay. Otherwise, it's a wrong specifier.
45       return false;
46     }
47     pos += 2;
48   }
49   return count == 1;
50 }
51 
52 }  // namespace internal
53 
54 // A util class that builds cmdline arguments.
55 class CmdlineBuilder {
56  public:
57   // Returns all arguments.
Get()58   const std::vector<std::string>& Get() const { return elements_; }
59 
60   // Adds an argument as-is.
Add(std::string_view arg)61   CmdlineBuilder& Add(std::string_view arg) {
62     elements_.push_back(std::string(arg));
63     return *this;
64   }
65 
66   // Same as above but adds a runtime argument.
AddRuntime(std::string_view arg)67   CmdlineBuilder& AddRuntime(std::string_view arg) { return Add("--runtime-arg").Add(arg); }
68 
69   // Adds a string value formatted by the format string.
70   //
71   // Usage: Add("--flag=%s", "value")
Add(const char * arg_format,const std::string & value)72   CmdlineBuilder& Add(const char* arg_format, const std::string& value)
73       __attribute__((enable_if(internal::ContainsOneFormatSpecifier(arg_format, 's'),
74                                "'arg' must be a string literal that contains '%s'"))) {
75     return Add(android::base::StringPrintf(arg_format, value.c_str()));
76   }
77 
78   // Same as above but adds a runtime argument.
AddRuntime(const char * arg_format,const std::string & value)79   CmdlineBuilder& AddRuntime(const char* arg_format, const std::string& value)
80       __attribute__((enable_if(internal::ContainsOneFormatSpecifier(arg_format, 's'),
81                                "'arg' must be a string literal that contains '%s'"))) {
82     return AddRuntime(android::base::StringPrintf(arg_format, value.c_str()));
83   }
84 
85   // Adds an integer value formatted by the format string.
86   //
87   // Usage: Add("--flag=%d", 123)
Add(const char * arg_format,int value)88   CmdlineBuilder& Add(const char* arg_format, int value)
89       __attribute__((enable_if(internal::ContainsOneFormatSpecifier(arg_format, 'd'),
90                                "'arg' must be a string literal that contains '%d'"))) {
91     return Add(android::base::StringPrintf(arg_format, value));
92   }
93 
94   // Same as above but adds a runtime argument.
AddRuntime(const char * arg_format,int value)95   CmdlineBuilder& AddRuntime(const char* arg_format, int value)
96       __attribute__((enable_if(internal::ContainsOneFormatSpecifier(arg_format, 'd'),
97                                "'arg' must be a string literal that contains '%d'"))) {
98     return AddRuntime(android::base::StringPrintf(arg_format, value));
99   }
100 
101   // Adds a string value formatted by the format string if the value is non-empty. Does nothing
102   // otherwise.
103   //
104   // Usage: AddIfNonEmpty("--flag=%s", "value")
AddIfNonEmpty(const char * arg_format,const std::string & value)105   CmdlineBuilder& AddIfNonEmpty(const char* arg_format, const std::string& value)
106       __attribute__((enable_if(internal::ContainsOneFormatSpecifier(arg_format, 's'),
107                                "'arg' must be a string literal that contains '%s'"))) {
108     if (!value.empty()) {
109       Add(android::base::StringPrintf(arg_format, value.c_str()));
110     }
111     return *this;
112   }
113 
114   // Same as above but adds a runtime argument.
AddRuntimeIfNonEmpty(const char * arg_format,const std::string & value)115   CmdlineBuilder& AddRuntimeIfNonEmpty(const char* arg_format, const std::string& value)
116       __attribute__((enable_if(internal::ContainsOneFormatSpecifier(arg_format, 's'),
117                                "'arg' must be a string literal that contains '%s'"))) {
118     if (!value.empty()) {
119       AddRuntime(android::base::StringPrintf(arg_format, value.c_str()));
120     }
121     return *this;
122   }
123 
124   // Adds an argument as-is if the boolean value is true. Does nothing otherwise.
AddIf(bool value,std::string_view arg)125   CmdlineBuilder& AddIf(bool value, std::string_view arg) {
126     if (value) {
127       Add(arg);
128     }
129     return *this;
130   }
131 
132   // Same as above but adds a runtime argument.
AddRuntimeIf(bool value,std::string_view arg)133   CmdlineBuilder& AddRuntimeIf(bool value, std::string_view arg) {
134     if (value) {
135       AddRuntime(arg);
136     }
137     return *this;
138   }
139 
140   // Concatenates this builder with another. Returns the concatenated result and nullifies the input
141   // builder.
Concat(CmdlineBuilder && other)142   CmdlineBuilder& Concat(CmdlineBuilder&& other) {
143     elements_.reserve(elements_.size() + other.elements_.size());
144     std::move(other.elements_.begin(), other.elements_.end(), std::back_inserter(elements_));
145     other.elements_.clear();
146     return *this;
147   }
148 
149  private:
150   std::vector<std::string> elements_;
151 };
152 
153 }  // namespace tools
154 }  // namespace art
155 
156 #endif  // ART_LIBARTTOOLS_INCLUDE_TOOLS_CMDLINE_BUILDER_H_
157