Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chromeos / system / name_value_pairs_parser.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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 "chromeos/system/name_value_pairs_parser.h"
6
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/process/launch.h"
12 #include "base/stl_util.h"
13 #include "base/strings/string_tokenizer.h"
14 #include "base/strings/string_util.h"
15 #include "base/sys_info.h"
16
17 namespace chromeos {  // NOLINT
18 namespace system {
19
20 namespace {
21
22 const char kQuoteChars[] = "\"";
23 const char kTrimChars[] = "\" ";
24
25 bool GetToolOutput(int argc, const char* argv[], std::string& output) {
26   DCHECK_GE(argc, 1);
27
28   if (!base::PathExists(base::FilePath(argv[0]))) {
29     LOG(WARNING) << "Tool for statistics not found: " << argv[0];
30     return false;
31   }
32
33   std::vector<std::string> args;
34   for (int argn = 0; argn < argc; ++argn)
35     args.push_back(argv[argn]);
36   if (!base::GetAppOutput(args, &output)) {
37     LOG(WARNING) << "Error executing " << argv[0];
38     return false;
39   }
40
41   return true;
42 }
43
44 }  // namespace
45
46 NameValuePairsParser::NameValuePairsParser(NameValueMap* map)
47     : map_(map) {
48 }
49
50 void NameValuePairsParser::AddNameValuePair(const std::string& key,
51                                             const std::string& value) {
52   if (!ContainsKey(*map_, key)) {
53     (*map_)[key] = value;
54     VLOG(1) << "name: " << key << ", value: " << value;
55   } else {
56     LOG(WARNING) << "Key " << key << " already has value " << (*map_)[key]
57                  << ", ignoring new value: " << value;
58   }
59 }
60
61 bool NameValuePairsParser::ParseNameValuePairs(const std::string& in_string,
62                                                const std::string& eq,
63                                                const std::string& delim) {
64   return ParseNameValuePairsWithComments(in_string, eq, delim, "");
65 }
66
67 bool NameValuePairsParser::ParseNameValuePairsWithComments(
68     const std::string& in_string,
69     const std::string& eq,
70     const std::string& delim,
71     const std::string& comment_delim) {
72   bool all_valid = true;
73   // Set up the pair tokenizer.
74   base::StringTokenizer pair_toks(in_string, delim);
75   pair_toks.set_quote_chars(kQuoteChars);
76   // Process token pairs.
77   while (pair_toks.GetNext()) {
78     std::string pair(pair_toks.token());
79     // Anything before the first |eq| is the key, anything after is the value.
80     // |eq| must exist.
81     size_t eq_pos = pair.find(eq);
82     if (eq_pos != std::string::npos) {
83       // First |comment_delim| after |eq_pos| starts the comment.
84       // A value of |std::string::npos| means that the value spans to the end
85       // of |pair|.
86       size_t value_size = std::string::npos;
87       if (!comment_delim.empty()) {
88         size_t comment_pos = pair.find(comment_delim, eq_pos + 1);
89         if (comment_pos != std::string::npos)
90           value_size = comment_pos - eq_pos - 1;
91       }
92
93       std::string key;
94       std::string value;
95       base::TrimString(pair.substr(0, eq_pos), kTrimChars, &key);
96       base::TrimString(pair.substr(eq_pos + 1, value_size), kTrimChars, &value);
97
98       if (!key.empty()) {
99         AddNameValuePair(key, value);
100         continue;
101       }
102     }
103
104     LOG(WARNING) << "Invalid token pair: " << pair << ". Ignoring.";
105     all_valid = false;
106   }
107   return all_valid;
108 }
109
110 bool NameValuePairsParser::GetSingleValueFromTool(int argc,
111                                                   const char* argv[],
112                                                   const std::string& key) {
113   std::string output_string;
114   if (!GetToolOutput(argc, argv, output_string))
115     return false;
116
117   base::TrimWhitespaceASCII(output_string, base::TRIM_ALL, &output_string);
118   AddNameValuePair(key, output_string);
119   return true;
120 }
121
122 bool NameValuePairsParser::GetNameValuePairsFromFile(
123     const base::FilePath& file_path,
124     const std::string& eq,
125     const std::string& delim) {
126   std::string contents;
127   if (base::ReadFileToString(file_path, &contents)) {
128     return ParseNameValuePairs(contents, eq, delim);
129   } else {
130     if (base::SysInfo::IsRunningOnChromeOS())
131       LOG(WARNING) << "Unable to read statistics file: " << file_path.value();
132     return false;
133   }
134 }
135
136 bool NameValuePairsParser::ParseNameValuePairsFromTool(
137     int argc,
138     const char* argv[],
139     const std::string& eq,
140     const std::string& delim,
141     const std::string& comment_delim) {
142   std::string output_string;
143   if (!GetToolOutput(argc, argv, output_string))
144     return false;
145
146   return ParseNameValuePairsWithComments(
147       output_string, eq, delim, comment_delim);
148 }
149
150 }  // namespace system
151 }  // namespace chromeos