• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #include "jdwpargs.h"
18 
19 #include <algorithm>
20 #include <sstream>
21 
22 #include "base/logging.h"  // For VLOG.
23 
24 namespace adbconnection {
25 
JdwpArgs(const std::string & opts)26 JdwpArgs::JdwpArgs(const std::string& opts) {
27   std::stringstream ss(opts);
28 
29   // Split on ',' character
30   while (!ss.eof()) {
31     std::string w;
32     getline(ss, w, ',');
33 
34     // Trim spaces
35     w.erase(std::remove_if(w.begin(), w.end(), ::isspace), w.end());
36 
37     // Extract key=value
38     auto pos = w.find('=');
39 
40     // Check for bad format such as no '=' or '=' at either extremity
41     if (pos == std::string::npos || w.back() == '=' || w.front() == '=') {
42       VLOG(jdwp) << "Skipping jdwp parameters '" << opts << "', token='" << w << "'";
43       continue;
44     }
45 
46     // Set
47     std::string key = w.substr(0, pos);
48     std::string value = w.substr(pos + 1);
49     put(key, value);
50     VLOG(jdwp) << "Found jdwp parameters '" << key << "'='" << value << "'";
51   }
52 }
53 
put(const std::string & key,const std::string & value)54 void JdwpArgs::put(const std::string& key, const std::string& value) {
55   if (store.find(key) == store.end()) {
56     keys.emplace_back(key);
57   }
58 
59   store[key] = value;
60 }
61 
join()62 std::string JdwpArgs::join() {
63   std::string opts;
64   for (const auto& key : keys) {
65     opts += key + "=" + store[key] + ",";
66   }
67 
68   // Remove the trailing comma if there is one
69   if (opts.length() >= 2) {
70     opts = opts.substr(0, opts.length() - 1);
71   }
72 
73   return opts;
74 }
75 }  // namespace adbconnection
76