Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: chrome/browser/sync/util/path_helpers-posix.cc

Issue 194065: Initial commit of sync engine code to browser/sync.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Fixes to gtest include path, reverted syncapi. Created 11 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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 <pwd.h>
6
7 #include <string.h>
8
9 #include "base/port.h"
10 #include "chrome/browser/sync/util/path_helpers.h"
11 #include "strings/strutil.h"
12
13 #if ((!defined(OS_LINUX)) && (!defined(OS_MACOSX)))
14 #error Compile this file on Mac OS X or Linux only.
15 #endif
16
17 PathString ExpandTilde(const PathString& path) {
18 if (path.empty())
19 return path;
20 if ('~' != path[0])
21 return path;
22 PathString ret;
23 // TODO(sync): Consider using getpwuid_r.
24 ret.insert(0, getpwuid(getuid())->pw_dir);
25 ret.append(++path.begin(), path.end());
26 return ret;
27 }
28
29 namespace {
30 // TODO(sync): We really should use char[].
31 string cache_dir_;
32 }
33
34 void set_cache_dir(string cache_dir) {
35 CHECK(cache_dir_.empty());
36 cache_dir_ = cache_dir;
37 }
38
39 string get_cache_dir() {
40 CHECK(!cache_dir_.empty());
41 return cache_dir_;
42 }
43
44 // On Posix, PathStrings are UTF-8, not UTF-16 as they are on Windows.
45 // Thus, this function is different from the Windows version.
46 PathString TruncatePathString(const PathString& original, int length) {
47 if (original.size() <= length)
48 return original;
49 if (length <= 0)
50 return original;
51 PathString ret(original.begin(), original.begin() + length);
52 COMPILE_ASSERT(sizeof(PathChar) == sizeof(uint8), PathStringNotUTF8);
53 PathString::reverse_iterator last_char = ret.rbegin();
54
55 // Values taken from
56 // http://en.wikipedia.org/w/index.php?title=UTF-8&oldid=252875566
57 if (0 == (*last_char & 0x80))
58 return ret;
59
60 for (; last_char != ret.rend(); ++last_char) {
61 if (0 == (*last_char & 0x80)) {
62 // got malformed UTF-8; bail
63 return ret;
64 }
65 if (0 == (*last_char & 0x40)) {
66 // got another trailing byte
67 continue;
68 }
69 break;
70 }
71
72 if (ret.rend() == last_char) {
73 // We hit the beginning of the string. bail.
74 return ret;
75 }
76
77 int last_codepoint_len = last_char - ret.rbegin() + 1;
78
79 if (((0xC0 == (*last_char & 0xE0)) && (2 == last_codepoint_len)) ||
80 ((0xE0 == (*last_char & 0xF0)) && (3 == last_codepoint_len)) ||
81 ((0xF0 == (*last_char & 0xF8)) && (4 == last_codepoint_len))) {
82 // Valid utf-8.
83 return ret;
84 }
85
86 // Invalid utf-8. chop off last "codepoint" and return.
87 ret.resize(ret.size() - last_codepoint_len);
88 return ret;
89 }
90
91 // Convert /s to :s .
92 PathString MakePathComponentOSLegal(const PathString& component) {
93 if (PathString::npos == component.find("/"))
94 return PSTR("");
95 PathString new_name;
96 new_name.reserve(component.size());
97 StringReplace(component, "/", ":", true, &new_name);
98 return new_name;
99 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698