• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef FOUNDATION_AAFWK_STANDARD_TOOLS_ZIP_FILE_PATH_H
16 #define FOUNDATION_AAFWK_STANDARD_TOOLS_ZIP_FILE_PATH_H
17 #include <string>
18 #include <vector>
19 namespace OHOS {
20 namespace AAFwk {
21 namespace LIBZIP {
22 
23 #define FILE_PATH_LITERAL(x) x
24 #define PRFilePath "s"
25 
26 template<typename T, size_t N>
27 char (&ArraySizeHelper(T (&array)[N]))[N];
28 #define arraysize(array) (sizeof(ArraySizeHelper(array)))
29 
30 class FilePath {
31 public:
32     using CharType = std::string::value_type;
33 
34     FilePath();
35     FilePath(const FilePath &that);
36     explicit FilePath(const std::string &path);
37     ~FilePath();
38     FilePath &operator=(const FilePath &that);
39     bool operator==(const FilePath &that) const;
40     bool operator!=(const FilePath &that) const;
41     bool operator<(const FilePath &that) const;
42 
43     static const CharType kSeparators[];
44     static const size_t kSeparatorsLength;
45     static const CharType kCurrentDirectory[];
46     static const CharType kParentDirectory[];
47     static const CharType kExtensionSeparator;
48 
49     static FilePath FromUTF8Unsafe(const std::string &utf8);
50     static bool IsSeparator(CharType character);
51     static bool CreateDirectory(const FilePath &fullPath);
52     static bool DirectoryExists(const FilePath &path);
53     static bool PathIsValid(const FilePath &path);
54 
55     // Returns a FilePath by appending a separator and the supplied path
56     // component to this object's path.  Append takes care to avoid adding
57     // excessive separators if this object's path already ends with a separator.
58     // If this object's path is kCurrentDirectory, a new FilePath corresponding
59     // only to |component| is returned.  |component| must be a relative path;
60     // it is an error to pass an absolute path.
61     FilePath Append(const std::string &component);
62     FilePath Append(FilePath &component);
63     // If IsParent(child) holds, appends to path (if non-NULL) the
64     // relative path to child and returns true.  For example, if parent
65     // holds "/Users/johndoe/Library/Application Support", child holds
66     // "/Users/johndoe/Library/Application Support/Google/Chrome/Default",
67     // and *path holds "/Users/johndoe/Library/Caches", then after
68     // parent.AppendRelativePath(child, path) is called *path will hold
69     // "/Users/johndoe/Library/Caches/Google/Chrome/Default".  Otherwise,
70     // returns false.
71     bool AppendRelativePath(const FilePath &child, FilePath *path);
72 
73     bool ReferencesParent();
74     void GetComponents(std::vector<std::string> &components);
75     FilePath DirName();
76     FilePath BaseName();
77     bool IsAbsolute();
78     std::string Value();
79 
80 private:
81     std::string path_;
82 
83     void StripTrailingSeparatorsInternal();
84     bool AreAllSeparators(const std::string &input);
85 };
86 }  // namespace LIBZIP
87 }  // namespace AAFwk
88 }  // namespace OHOS
89 
90 #endif  // FOUNDATION_AAFWK_STANDARD_TOOLS_ZIP_FILE_PATH_H