OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 "net/ftp/ftp_util.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/string_tokenizer.h" |
| 11 #include "base/string_util.h" |
| 12 |
| 13 // For examples of Unix<->VMS path conversions, see the unit test file. On VMS |
| 14 // a path looks differently depending on whether it's a file or directory. |
| 15 |
| 16 namespace net { |
| 17 |
| 18 // static |
| 19 std::string FtpUtil::UnixFilePathToVMS(const std::string& unix_path) { |
| 20 if (unix_path.empty()) |
| 21 return std::string(); |
| 22 |
| 23 StringTokenizer tokenizer(unix_path, "/"); |
| 24 std::vector<std::string> tokens; |
| 25 while (tokenizer.GetNext()) |
| 26 tokens.push_back(tokenizer.token()); |
| 27 |
| 28 if (unix_path[0] == '/') { |
| 29 // It's an absolute path. |
| 30 |
| 31 if (tokens.empty()) { |
| 32 DCHECK_EQ(1U, unix_path.length()); |
| 33 return "[]"; |
| 34 } |
| 35 |
| 36 if (tokens.size() == 1) |
| 37 return unix_path.substr(1); // Drop the leading slash. |
| 38 |
| 39 std::string result(tokens[0] + ":["); |
| 40 if (tokens.size() == 2) { |
| 41 // Don't ask why, it just works that way on VMS. |
| 42 result.append("000000"); |
| 43 } else { |
| 44 result.append(tokens[1]); |
| 45 for (size_t i = 2; i < tokens.size() - 1; i++) |
| 46 result.append("." + tokens[i]); |
| 47 } |
| 48 result.append("]" + tokens[tokens.size() - 1]); |
| 49 return result; |
| 50 } |
| 51 |
| 52 if (tokens.size() == 1) |
| 53 return unix_path; |
| 54 |
| 55 std::string result("["); |
| 56 for (size_t i = 0; i < tokens.size() - 1; i++) |
| 57 result.append("." + tokens[i]); |
| 58 result.append("]" + tokens[tokens.size() - 1]); |
| 59 return result; |
| 60 } |
| 61 |
| 62 // static |
| 63 std::string FtpUtil::UnixDirectoryPathToVMS(const std::string& unix_path) { |
| 64 if (unix_path.empty()) |
| 65 return std::string(); |
| 66 |
| 67 std::string path(unix_path); |
| 68 |
| 69 if (path[path.length() - 1] != '/') |
| 70 path.append("/"); |
| 71 |
| 72 // Reuse logic from UnixFilePathToVMS by appending a fake file name to the |
| 73 // real path and removing it after conversion. |
| 74 path.append("x"); |
| 75 path = UnixFilePathToVMS(path); |
| 76 return path.substr(0, path.length() - 1); |
| 77 } |
| 78 |
| 79 // static |
| 80 std::string FtpUtil::VMSPathToUnix(const std::string& vms_path) { |
| 81 if (vms_path.empty()) |
| 82 return "."; |
| 83 |
| 84 if (vms_path == "[]") |
| 85 return "/"; |
| 86 |
| 87 std::string result(vms_path); |
| 88 if (vms_path[0] == '[') { |
| 89 // It's a relative path. |
| 90 ReplaceFirstSubstringAfterOffset(&result, 0, "[.", ""); |
| 91 } else { |
| 92 // It's an absolute path. |
| 93 result.insert(0, "/"); |
| 94 ReplaceSubstringsAfterOffset(&result, 0, ":[000000]", "/"); |
| 95 ReplaceSubstringsAfterOffset(&result, 0, ":[", "/"); |
| 96 } |
| 97 std::replace(result.begin(), result.end(), '.', '/'); |
| 98 std::replace(result.begin(), result.end(), ']', '/'); |
| 99 |
| 100 // Make sure the result doesn't end with a slash. |
| 101 if (result[result.length() - 1] == '/') |
| 102 result = result.substr(0, result.length() - 1); |
| 103 |
| 104 return result; |
| 105 } |
| 106 |
| 107 } // namespace |
OLD | NEW |