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 #include "zip_utils.h"
16 
17 #include <regex>
18 
19 #include "event_handler.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace LIBZIP {
24 namespace {
25 const std::string SEPARATOR = "/";
26 const std::regex FILE_PATH_REGEX(".*");
27 const std::string ZIP_THREAD = "ZipThread";
28 }  // namespace
29 using namespace OHOS::AppExecFwk;
30 
31 std::shared_ptr<EventHandler> g_handler = nullptr;
32 std::mutex mutex;
PostTask(const InnerEvent::Callback & callback)33 void PostTask(const InnerEvent::Callback &callback)
34 {
35     std::lock_guard<std::mutex> lock(mutex);
36     if (g_handler == nullptr) {
37         auto runner = EventRunner::Create(ZIP_THREAD, ThreadMode::FFRT);
38         g_handler = std::make_shared<EventHandler>(runner);
39     }
40     g_handler->PostTask(callback);
41 }
42 
GetCurrentSystemTime(void)43 struct tm *GetCurrentSystemTime(void)
44 {
45     auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
46     struct tm *time = localtime(&tt);
47     return time;
48 }
49 
StartsWith(const std::string & str,const std::string & searchFor)50 bool StartsWith(const std::string &str, const std::string &searchFor)
51 {
52     if (searchFor.size() > str.size()) {
53         return false;
54     }
55 
56     std::string source = str.substr(0, searchFor.size());
57     return source == searchFor;
58 }
EndsWith(const std::string & str,const std::string & searchFor)59 bool EndsWith(const std::string &str, const std::string &searchFor)
60 {
61     if (searchFor.size() > str.size()) {
62         return false;
63     }
64 
65     std::string source = str.substr(str.size() - searchFor.size(), searchFor.size());
66     return source == searchFor;
67 }
68 
FilePathCheckValid(const std::string & str)69 bool FilePathCheckValid(const std::string &str)
70 {
71     return std::regex_match(str, FILE_PATH_REGEX);
72 }
73 
74 }  // namespace LIBZIP
75 }  // namespace AppExecFwk
76 }  // namespace OHOS
77