xref: /aosp_15_r20/external/cronet/testing/android/native_test/native_test_util.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "testing/android/native_test/native_test_util.h"
6 
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/strings/string_tokenizer.h"
10 #include "base/strings/string_util.h"
11 
12 namespace testing {
13 namespace android {
14 
ParseArgsFromString(const std::string & command_line,std::vector<std::string> * args)15 void ParseArgsFromString(const std::string& command_line,
16                          std::vector<std::string>* args) {
17   base::StringTokenizer tokenizer(command_line, base::kWhitespaceASCII);
18   tokenizer.set_quote_chars("\"");
19   while (tokenizer.GetNext()) {
20     std::string token;
21     base::RemoveChars(tokenizer.token(), "\"", &token);
22     args->push_back(token);
23   }
24 }
25 
ParseArgsFromCommandLineFile(const char * path,std::vector<std::string> * args)26 void ParseArgsFromCommandLineFile(
27     const char* path, std::vector<std::string>* args) {
28   base::FilePath command_line(path);
29   std::string command_line_string;
30   if (base::ReadFileToString(command_line, &command_line_string)) {
31     ParseArgsFromString(command_line_string, args);
32   }
33 }
34 
ArgsToArgv(const std::vector<std::string> & args,std::vector<char * > * argv)35 int ArgsToArgv(const std::vector<std::string>& args,
36                 std::vector<char*>* argv) {
37   // We need to pass in a non-const char**.
38   int argc = args.size();
39 
40   argv->resize(argc + 1);
41   for (int i = 0; i < argc; ++i) {
42     (*argv)[i] = const_cast<char*>(args[i].c_str());
43   }
44   (*argv)[argc] = NULL;  // argv must be NULL terminated.
45 
46   return argc;
47 }
48 
49 }  // namespace android
50 }  // namespace testing
51