1 /*
2  * Copyright (C) 2024 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 
16 #ifndef RINGTONE_FILE_UTILS_H
17 #define RINGTONE_FILE_UTILS_H
18 
19 #include <string>
20 #include <unordered_set>
21 
22 namespace OHOS {
23 namespace Media {
24 #define EXPORT __attribute__ ((visibility ("default")))
25 
26 EXPORT const std::string RINGTONE_FILEMODE_READONLY = "r";
27 EXPORT const std::string RINGTONE_FILEMODE_WRITEONLY = "w";
28 EXPORT const std::string RINGTONE_FILEMODE_READWRITE = "rw";
29 EXPORT const std::string RINGTONE_FILEMODE_WRITETRUNCATE = "wt";
30 EXPORT const std::string RINGTONE_FILEMODE_WRITEAPPEND = "wa";
31 EXPORT const std::string RINGTONE_FILEMODE_READWRITETRUNCATE = "rwt";
32 EXPORT const std::string RINGTONE_FILEMODE_READWRITEAPPEND = "rwa";
33 EXPORT const std::unordered_set<std::string> RINGTONE_OPEN_MODES = {
34     RINGTONE_FILEMODE_READONLY,
35     RINGTONE_FILEMODE_WRITEONLY,
36     RINGTONE_FILEMODE_READWRITE,
37     RINGTONE_FILEMODE_WRITETRUNCATE,
38     RINGTONE_FILEMODE_WRITEAPPEND,
39     RINGTONE_FILEMODE_READWRITETRUNCATE,
40     RINGTONE_FILEMODE_READWRITEAPPEND
41 };
42 
43 constexpr int64_t MSEC_TO_SEC = 1e3;
44 constexpr int64_t MSEC_TO_NSEC = 1e6;
45 constexpr size_t DEFAULT_TIME_SIZE = 32;
46 
47 class RingtoneFileUtils {
48 public:
49     EXPORT static int64_t Timespec2Millisecond(const struct timespec &time);
50     EXPORT static bool StartsWith(const std::string &str, const std::string &prefix);
51     EXPORT static int64_t UTCTimeMilliSeconds();
52     EXPORT static int64_t UTCTimeSeconds();
53     EXPORT static std::string StrCreateTimeByMilliseconds(const std::string &format, int64_t time);
54     EXPORT static std::string SplitByChar(const std::string &str, const char split);
55     EXPORT static std::string GetExtensionFromPath(const std::string &path);
56     EXPORT static std::string GetFileNameFromPath(const std::string &path);
57     EXPORT static std::string GetFileNameFromPathOrUri(const std::string &path, bool &isTitle);
58     EXPORT static std::string GetBaseNameFromPath(const std::string &path);
59     EXPORT static int32_t OpenFile(const std::string &filePath, const std::string &mode);
60     EXPORT static int32_t OpenVibrateFile(const std::string &filePath, const std::string &mode);
61     EXPORT static int32_t CreateFile(const std::string &filePath);
62     EXPORT static bool IsSameFile(const std::string &srcPath, const std::string &dstPath);
63     EXPORT static bool IsFileExists(const std::string &fileName);
64     EXPORT static bool DeleteFile(const std::string &fileName);
65     EXPORT static bool MoveFile(const std::string &oldPath, const std::string &newPath);
66     EXPORT static bool CopyFileUtil(const std::string &filePath, const std::string &newPath);
67     EXPORT static int32_t RemoveDirectory(const std::string &path);
68     EXPORT static bool Mkdir(const std::string &subStr, std::shared_ptr<int> errCodePtr = nullptr);
69     EXPORT static bool IsDirectory(const std::string &dirName, std::shared_ptr<int> errCodePtr = nullptr);
70     EXPORT static bool CreateDirectory(const std::string &dirPath, std::shared_ptr<int> errCodePtr = nullptr);
71     EXPORT static std::string UrlDecode(const std::string &url);
72     EXPORT static void CreateRingtoneDir();
73     EXPORT static int32_t CreatePreloadFolder(const std::string &path);
74     EXPORT static int32_t MoveDirectory(const std::string &srcDir, const std::string &dstDir);
75     EXPORT static void AccessRingtoneDir();
76     EXPORT static std::string GetFileExtension(const std::string &path);
77 };
78 } // namespace Media
79 } // namespace OHOS
80 
81 #endif // RINGTONE_FILE_UTILS_H
82