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 #ifndef CHROME_BROWSER_SYNC_UTIL_PATH_HELPERS_H_ |
| 6 #define CHROME_BROWSER_SYNC_UTIL_PATH_HELPERS_H_ |
| 7 |
| 8 #include <algorithm> |
| 9 #include <iterator> |
| 10 #include <string> |
| 11 |
| 12 #include "chrome/browser/sync/util/compat-file.h" |
| 13 #include "chrome/browser/sync/util/sync_types.h" |
| 14 |
| 15 template <typename StringType> |
| 16 class PathSegmentIterator : public std::iterator<std::forward_iterator_tag, |
| 17 StringType> { |
| 18 public: |
| 19 explicit PathSegmentIterator(const StringType& path) : |
| 20 path_(path), segment_begin_(0), segment_end_(0) { |
| 21 ++(*this); |
| 22 } |
| 23 |
| 24 PathSegmentIterator() : segment_begin_(0), segment_end_(0) { } |
| 25 |
| 26 // Default copy constructors, constructors, etc. will all do the right thing. |
| 27 PathSegmentIterator& operator ++() { |
| 28 segment_begin_ = |
| 29 std::min(path_.size(), |
| 30 path_.find_first_not_of(kPathSeparator, segment_end_)); |
| 31 segment_end_ = |
| 32 std::min(path_.size(), |
| 33 path_.find_first_of(kPathSeparator, segment_begin_)); |
| 34 value_.assign(path_, segment_begin_, segment_end_ - segment_begin_); |
| 35 return *this; |
| 36 } |
| 37 |
| 38 PathSegmentIterator operator ++(int) { |
| 39 PathSegmentIterator i(*this); |
| 40 return ++i; |
| 41 } |
| 42 |
| 43 const StringType& operator * () const { |
| 44 return value_; |
| 45 } |
| 46 const StringType* operator -> () const { |
| 47 return &value_; |
| 48 } |
| 49 |
| 50 // If the current value and remaining path are equal, then we |
| 51 // call the iterators equal. |
| 52 bool operator == (const PathSegmentIterator& i) const { |
| 53 return 0 == path_.compare(segment_begin_, |
| 54 path_.size() - segment_begin_, |
| 55 i.path_, i.segment_begin_, i.path_.size() - i.segment_begin_); |
| 56 } |
| 57 |
| 58 bool operator != (const PathSegmentIterator& i) const { |
| 59 return !(*this == i); |
| 60 } |
| 61 |
| 62 protected: |
| 63 StringType path_; |
| 64 typename StringType::size_type segment_begin_; |
| 65 typename StringType::size_type segment_end_; |
| 66 StringType value_; |
| 67 }; |
| 68 |
| 69 // NOTE: The functions (Strip)LastPathSegment always return values without a |
| 70 // trailing slash. |
| 71 PathString LastPathSegment(const PathString& path); |
| 72 std::string LastPathSegment(const std::string& path); |
| 73 PathString AppendSlash(const PathString& path); |
| 74 PathString GetFullPath(const PathString& path); |
| 75 PathString LowercasePath(const PathString& path); |
| 76 PathString ExpandTilde(const PathString& path); |
| 77 |
| 78 inline bool HasSuffixPathString(const PathString& str, |
| 79 const PathString& suffix) { |
| 80 return str.find(suffix, str.size() - suffix.size()) != PathString::npos; |
| 81 } |
| 82 |
| 83 inline PathString StripSuffixPathString(const PathString& str, |
| 84 const PathString& suffix) { |
| 85 PathString ret(str); |
| 86 if (HasSuffixPathString(str, suffix)) { |
| 87 ret.resize(str.size() - suffix.size()); |
| 88 } |
| 89 return ret; |
| 90 } |
| 91 |
| 92 // Returns a string with length or fewer elements, careful to |
| 93 // not truncate a string mid-surrogate pair. |
| 94 PathString TruncatePathString(const PathString& original, int length); |
| 95 |
| 96 // Makes a path component legal for your OS, but doesn't handle collisions |
| 97 // with other files in the same directory. it can do this by removing |
| 98 // illegal characters and adding ~1 before the first '.' in the filename. |
| 99 // returns PSTR("") if the name is fine as-is |
| 100 // on mac/linux we let names stay unicode normalization form C in the system |
| 101 // and convert to another normal form in fuse handlers. but, if a '/' is in |
| 102 // a filename, we handle it here. |
| 103 PathString MakePathComponentOSLegal(const PathString& component); |
| 104 |
| 105 #endif // CHROME_BROWSER_SYNC_UTIL_PATH_HELPERS_H_ |
OLD | NEW |