Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / local_discovery / storage / path_util.cc
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.
4
5 #include "chrome/browser/local_discovery/storage/path_util.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10 #include "base/strings/utf_string_conversions.h"
11
12 namespace local_discovery {
13
14 namespace {
15
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();
33       }
34     } else {
35       output += str[i];
36     }
37   }
38
39   return output;
40 }
41
42 const size_t kNumComponentsInBasePrivetPath = 4;
43 const int kIndexOfServiceNameInComponentList = 2;
44
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
51 }
52
53 }  // namespace
54
55 base::FilePath NormalizeFilePath(const base::FilePath& path) {
56 #if defined(OS_WIN)
57   base::FilePath::StringType path_updated_string = path.value();
58
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
67 }
68
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]));
75
76
77   for (size_t i = kNumComponentsInBasePrivetPath; i < components.size(); i++) {
78     path += '/' + PathStringToString(components[i]);
79   }
80
81   if (path.empty()) path = "/";
82 }
83
84 ParsedPrivetPath::~ParsedPrivetPath() {
85 }
86
87 }  // namespace local_discovery