Componentize AccountReconcilor.
[chromium-blink-merge.git] / chrome / browser / local_discovery / storage / path_util.cc
blob0225c9ca6ad66930125ac2343d5a3681c7de643a
1 // Copyright 2014 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.
5 #include "chrome/browser/local_discovery/storage/path_util.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "base/strings/utf_string_conversions.h"
12 namespace local_discovery {
14 namespace {
16 std::string UnescapeSlashes(const std::string& str) {
17 std::string output = "";
18 for (size_t i = 0; i < str.length(); i++) {
19 if (str[i] == '$') {
20 i++;
21 switch (str[i]) {
22 case 's':
23 output += '/';
24 break;
25 case 'b':
26 output += '\\';
27 break;
28 case '$':
29 output += '$';
30 break;
31 default:
32 NOTREACHED();
34 } else {
35 output += str[i];
39 return output;
42 const size_t kNumComponentsInBasePrivetPath = 4;
43 const int kIndexOfServiceNameInComponentList = 2;
45 std::string PathStringToString(const base::FilePath::StringType& string) {
46 #if defined(OS_WIN)
47 return base::UTF16ToUTF8(string);
48 #else
49 return string;
50 #endif
53 } // namespace
55 base::FilePath NormalizeFilePath(const base::FilePath& path) {
56 #if defined(OS_WIN)
57 base::FilePath::StringType path_updated_string = path.value();
59 std::replace(path_updated_string.begin(),
60 path_updated_string.end(),
61 static_cast<base::FilePath::CharType>('\\'),
62 static_cast<base::FilePath::CharType>('/'));
63 return base::FilePath(path_updated_string);
64 #else
65 return path;
66 #endif
69 ParsedPrivetPath::ParsedPrivetPath(const base::FilePath& file_path) {
70 std::vector<base::FilePath::StringType> components;
71 file_path.GetComponents(&components);
72 DCHECK(components.size() >= kNumComponentsInBasePrivetPath);
73 service_name = UnescapeSlashes(PathStringToString(
74 components[kIndexOfServiceNameInComponentList]));
77 for (size_t i = kNumComponentsInBasePrivetPath; i < components.size(); i++) {
78 path += '/' + PathStringToString(components[i]);
81 if (path.empty()) path = "/";
84 ParsedPrivetPath::~ParsedPrivetPath() {
87 } // namespace local_discovery