1 /* 2 * Copyright (c) 2022 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_APPEXECFWK_STANDARD_TOOLS_ZIP_FILE_PATH_H 16 #define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_FILE_PATH_H 17 #include <string> 18 #include <vector> 19 namespace OHOS { 20 namespace AppExecFwk { 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 static bool PathIsReadable(const FilePath &path); 55 static bool IsDir(const FilePath &path); 56 static bool GetZipAllDirFiles(const std::string &path, std::vector<std::string> &files); 57 // Returns a FilePath by appending a separator and the supplied path 58 // component to this object's path. Append takes care to avoid adding 59 // excessive separators if this object's path already ends with a separator. 60 // If this object's path is kCurrentDirectory, a new FilePath corresponding 61 // only to |component| is returned. |component| must be a relative path; 62 // it is an error to pass an absolute path. 63 FilePath Append(const std::string &component); 64 FilePath Append(FilePath &component); 65 void AppendSeparator(void); 66 // If IsParent(child) holds, appends to path (if non-NULL) the 67 // relative path to child and returns true. 68 bool AppendRelativePath(const FilePath &child, FilePath *path); 69 70 bool ReferencesParent(); 71 void GetComponents(std::vector<std::string> &components); 72 FilePath DirName(); 73 FilePath BaseName(); 74 bool IsAbsolute(); 75 std::string Value(); 76 std::string CheckDestDirTail(); 77 private: 78 std::string path_; 79 80 void StripTrailingSeparatorsInternal(); 81 bool AreAllSeparators(const std::string &input); 82 }; 83 } // namespace LIBZIP 84 } // namespace AppExecFwk 85 } // namespace OHOS 86 87 #endif // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_ZIP_FILE_PATH_H 88