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 "base/file_path.h" | |
13 #include "chrome/browser/sync/util/sync_types.h" | |
14 | |
15 extern const char kPathSeparator[]; | |
16 | |
17 template <typename StringType> | |
18 class PathSegmentIterator : public std::iterator<std::forward_iterator_tag, | |
19 StringType> { | |
20 public: | |
21 explicit PathSegmentIterator(const StringType& path) : | |
22 path_(path), segment_begin_(0), segment_end_(0) { | |
23 ++(*this); | |
24 } | |
25 | |
26 PathSegmentIterator() : segment_begin_(0), segment_end_(0) { } | |
27 | |
28 // Default copy constructors, constructors, etc. will all do the right thing. | |
29 PathSegmentIterator& operator ++() { | |
30 segment_begin_ = | |
31 std::min(path_.size(), | |
32 path_.find_first_not_of(kPathSeparator, segment_end_)); | |
33 segment_end_ = | |
34 std::min(path_.size(), | |
35 path_.find_first_of(kPathSeparator, segment_begin_)); | |
36 value_.assign(path_, segment_begin_, segment_end_ - segment_begin_); | |
37 return *this; | |
38 } | |
39 | |
40 PathSegmentIterator operator ++(int) { | |
41 PathSegmentIterator i(*this); | |
42 return ++i; | |
43 } | |
44 | |
45 const StringType& operator * () const { | |
46 return value_; | |
47 } | |
48 const StringType* operator -> () const { | |
49 return &value_; | |
50 } | |
51 | |
52 // If the current value and remaining path are equal, then we | |
53 // call the iterators equal. | |
54 bool operator == (const PathSegmentIterator& i) const { | |
55 return 0 == path_.compare(segment_begin_, | |
56 path_.size() - segment_begin_, | |
57 i.path_, i.segment_begin_, i.path_.size() - i.segment_begin_); | |
58 } | |
59 | |
60 bool operator != (const PathSegmentIterator& i) const { | |
61 return !(*this == i); | |
62 } | |
63 | |
64 protected: | |
65 StringType path_; | |
66 typename StringType::size_type segment_begin_; | |
67 typename StringType::size_type segment_end_; | |
68 StringType value_; | |
69 }; | |
70 | |
71 // Makes a path component legal for your OS, but doesn't handle collisions | |
72 // with other files in the same directory. it can do this by removing | |
73 // illegal characters and adding ~1 before the first '.' in the filename. | |
74 // returns PSTR("") if the name is fine as-is | |
75 // on mac/linux we let names stay unicode normalization form C in the system | |
76 // and convert to another normal form in fuse handlers. but, if a '/' is in | |
77 // a filename, we handle it here. | |
78 std::string MakePathComponentOSLegal(const std::string& component); | |
79 | |
80 #endif // CHROME_BROWSER_SYNC_UTIL_PATH_HELPERS_H_ | |
OLD | NEW |