1e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block/*
2e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * Copyright (C) 2010 Google Inc. All rights reserved.
3e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *
4e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * Redistribution and use in source and binary forms, with or without
5e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * modification, are permitted provided that the following conditions are
6e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * met:
7e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *
8e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *     * Redistributions of source code must retain the above copyright
9e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * notice, this list of conditions and the following disclaimer.
10e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *     * Redistributions in binary form must reproduce the above
11e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * copyright notice, this list of conditions and the following disclaimer
12e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * in the documentation and/or other materials provided with the
13e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * distribution.
14e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *     * Neither the name of Google Inc. nor the names of its
15e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * contributors may be used to endorse or promote products derived from
16e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * this software without specific prior written permission.
17e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block *
18e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block */
30e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
31e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#ifndef DOMFilePath_h
32e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#define DOMFilePath_h
33e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
34e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#include "PlatformString.h"
35e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#include <wtf/PassRefPtr.h>
36e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#include <wtf/RefCounted.h>
37e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
38e8b154fd68f9b33be40a3590e58347f353835f5cSteve Blocknamespace WebCore {
39e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
40e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block// DOMFileSystem path utilities. All methods in this class are static.
41e8b154fd68f9b33be40a3590e58347f353835f5cSteve Blockclass DOMFilePath {
42e8b154fd68f9b33be40a3590e58347f353835f5cSteve Blockpublic:
43e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static const char separator;
44e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static const char root[];
45e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
46e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    // Returns the name part from the given path.
47e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static String getName(const String& path);
48e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
49e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    // Returns the parent directory path of the given path.
50e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static String getDirectory(const String& path);
51e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
52e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    // Checks if a given path is a parent of mayBeChild. This method assumes given paths are absolute and do not have extra references to a parent (i.e. "../").
53e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static bool isParentOf(const String& path, const String& mayBeChild);
54e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
55e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    // Appends the separator at the end of the path if it's not there already.
56e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static String ensureDirectoryPath(const String& path);
57e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
58e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    // Returns a new path by appending a separator and the supplied path component to the path.
59e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static String append(const String& path, const String& component);
60e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
61e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static bool isAbsolute(const String& path)
62e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    {
63e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block        return path.startsWith(DOMFilePath::root);
64e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    }
65e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
66e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static bool endsWithSeparator(const String& path)
67e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    {
68e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block        return path[path.length() - 1] == DOMFilePath::separator;
69e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    }
70e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
71e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    // Evaluates all "../" and "./" segments.  Note that "/../" expands to "/", so you can't ever refer to anything above the root directory.
72e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static String removeExtraParentReferences(const String& path);
73e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
74e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    // Checks if the given path follows the FileSystem API naming restrictions.
75e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static bool isValidPath(const String& path);
76e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
77e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    // Checks if the given name follows the FileSystem API naming restrictions.
78e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    static bool isValidName(const String& name);
79e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
80e8b154fd68f9b33be40a3590e58347f353835f5cSteve Blockprivate:
81e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block    DOMFilePath() { }
82e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block};
83e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
84e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block} // namespace WebCore
85e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block
86e8b154fd68f9b33be40a3590e58347f353835f5cSteve Block#endif // DOMFilePath_h
87