PageRenderTime 34ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/o3d/utils/cross/file_path_utils.cc

https://github.com/akesling/chromium
C++ | 161 lines | 91 code | 16 blank | 54 comment | 13 complexity | df2aa5bf48438e4da63bde38df0bf192 MD5 | raw file
  1. /*
  2. * Copyright 2009, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above
  12. * copyright notice, this list of conditions and the following disclaimer
  13. * in the documentation and/or other materials provided with the
  14. * distribution.
  15. * * Neither the name of Google Inc. nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. // This file contains the definitions for some convenience functions
  32. // used to make FilePaths more useful.
  33. #include "utils/cross/file_path_utils.h"
  34. #include "base/file_util.h"
  35. #include "base/string_util.h"
  36. #include "base/file_path.h"
  37. namespace o3d {
  38. std::wstring FilePathToWide(const FilePath& input) {
  39. #if defined(OS_WIN)
  40. return input.value();
  41. #else
  42. return UTF8ToWide(input.value());
  43. #endif
  44. }
  45. FilePath WideToFilePath(const std::wstring& input) {
  46. #if defined(OS_WIN)
  47. return FilePath(input);
  48. #else
  49. return FilePath(WideToUTF8(input));
  50. #endif
  51. }
  52. String FilePathToUTF8(const FilePath& input) {
  53. #if defined(OS_WIN)
  54. return WideToUTF8(input.value());
  55. #else
  56. return input.value();
  57. #endif
  58. }
  59. FilePath UTF8ToFilePath(const String& input) {
  60. #if defined(OS_WIN)
  61. return FilePath(UTF8ToWide(input));
  62. #else
  63. return FilePath(input);
  64. #endif
  65. }
  66. bool AbsolutePath(FilePath* absolute_path) {
  67. #if defined(OS_WIN)
  68. return file_util::AbsolutePath(absolute_path);
  69. #else
  70. // On the Posix implementation of file_util::AbsolutePath,
  71. // realpath() is used, which only works if the path actually exists.
  72. // So, we try using AbsolutePath, and if it doesn't work, we fake it
  73. // by just prepending the current working direcory if it's not
  74. // already absolute.
  75. if (absolute_path->IsAbsolute()) {
  76. return true;
  77. } else {
  78. FilePath new_absolute_path = FilePath(*absolute_path);
  79. if (file_util::AbsolutePath(&new_absolute_path)) {
  80. *absolute_path = new_absolute_path;
  81. return true;
  82. } else {
  83. // OK, so we failed to make an absolute path above, and we know
  84. // it's not an absolute path to begin with, so we just prepend
  85. // the current working directory.
  86. FilePath cwd_path;
  87. if (!file_util::GetCurrentDirectory(&cwd_path)) {
  88. return false;
  89. }
  90. *absolute_path = cwd_path.Append(*absolute_path);
  91. return true;
  92. }
  93. }
  94. #endif
  95. }
  96. bool GetRelativePathIfPossible(const FilePath& base_dir,
  97. const FilePath& candidate,
  98. FilePath *result) {
  99. FilePath parent = FilePath(base_dir.StripTrailingSeparators());
  100. FilePath child = FilePath(candidate.StripTrailingSeparators());
  101. // If we can't convert the child to an absolute path for some
  102. // reason, then we just do nothing and return the candidate.
  103. if (!child.IsAbsolute() && !AbsolutePath(&child)) {
  104. *result = candidate;
  105. return false;
  106. }
  107. // If we can't convert the parent to an absolute path for some
  108. // reason, then we just do nothing and return the absolute path to
  109. // the child.
  110. if (!parent.IsAbsolute() && !AbsolutePath(&parent)) {
  111. *result = child;
  112. return false;
  113. }
  114. FilePath::StringType child_str = child.value();
  115. FilePath::StringType parent_str = parent.value();
  116. // If the child is too short, it can't be a child of parent, and if
  117. // it doesn't have a separator in the right place, then it also
  118. // can't be a child, so we return the absolute path to the child.
  119. // file_util::AbsolutePath() normalizes '/' to '\' on Windows, so we
  120. // only need to check kSeparators[0].
  121. if (child_str.length() <= parent_str.length() ||
  122. child_str[parent_str.length()] != FilePath::kSeparators[0]) {
  123. *result = child;
  124. return false;
  125. }
  126. if (
  127. #if defined(OS_WIN)
  128. // file_util::AbsolutePath() does not flatten case on Windows,
  129. // so we must do a case-insensitive compare.
  130. StartsWith(child_str, parent_str, false)
  131. #else
  132. StartsWithASCII(child_str, parent_str, true)
  133. #endif
  134. ) {
  135. // Add one to skip over the dir separator.
  136. child_str = child_str.substr(parent_str.size() + 1);
  137. // Now we know we can return the relative path.
  138. *result = FilePath(child_str);
  139. return true;
  140. } else {
  141. *result = FilePath(child_str);
  142. return false;
  143. }
  144. }
  145. } // end namespace o3d