OLD | NEW |
---|---|
(Empty) | |
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/dbus/ibus/ibus_util.h" | |
6 | |
7 #include "base/environment.h" | |
8 #include "base/file_util.h" | |
9 #include "base/memory/scoped_ptr.h" | |
10 #include "base/string_util.h" | |
11 | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 namespace chromeos { | |
16 | |
17 namespace { | |
18 const char kIBusAddressFileEnvKey[] = "IBUS_ADDRESS_FILE"; | |
19 const char kIBusAddressKey[] = "IBUS_ADDRESS"; | |
20 } // namespace | |
21 | |
22 bool IBusUtil::GetIBusAddress(std::string* address) { | |
23 DCHECK(address); | |
24 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
25 std::string address_file_path; | |
26 if (!env->GetVar(kIBusAddressFileEnvKey, &address_file_path)) { | |
27 LOG(ERROR) << "Can not find " << kIBusAddressFileEnvKey | |
28 << " in environment variables."; | |
29 return false; | |
30 } | |
31 | |
32 std::string address_file_contents; | |
33 if (!file_util::ReadFileToString(FilePath(address_file_path), | |
satorux1
2012/04/20 23:18:40
Ugh, you cannot do this here. DBusThreadManager is
Seigo Nonaka
2012/04/21 05:35:14
I found a command line flag to specify opening add
Seigo Nonaka
2012/04/23 23:03:21
I made another CL for above.
http://codereview.chr
| |
34 &address_file_contents)) { | |
35 LOG(ERROR) << "Can not read IBus address file:" << address_file_path; | |
36 return false; | |
37 } | |
38 | |
39 std::vector<std::string> lines; | |
40 Tokenize(address_file_contents, "\r\n", &lines); | |
satorux1
2012/04/20 23:18:40
Use SplitString instead? \r won't be used in the f
Seigo Nonaka
2012/04/23 23:03:21
Yes, but I removed this file.
Thanks
On 2012/04/20
| |
41 | |
42 // Prfix matching is enought for ibus address file parsing, because address | |
43 // file is generated by ibus with fixed format. | |
44 // https://github.com/ibus/ibus/blob/master/src/ibusshare.c#L234 | |
45 const std::string address_entry_prefix = std::string(kIBusAddressKey) | |
46 + std::string("="); | |
47 for (std::vector<std::string>::const_iterator ite = lines.begin(); | |
48 ite != lines.end(); ++ite) { | |
satorux1
2012/04/20 23:18:40
matter of taste, but for (size_t i = 0; i < lines.
Seigo Nonaka
2012/04/23 23:03:21
I have not strong policy about iterator:)
I will d
| |
49 | |
50 if (!StartsWithASCII(*ite, address_entry_prefix, true)) { | |
51 continue; | |
52 } | |
53 | |
54 address->assign(ite->substr(address_entry_prefix.size())); | |
55 return true; | |
56 } | |
57 LOG(ERROR) << "Can not find " << kIBusAddressKey << " entry."; | |
58 return false; | |
59 } | |
60 | |
61 } // namespace chromeos | |
OLD | NEW |