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;
PostTask(const InnerEvent::Callback & callback)32 void PostTask(const InnerEvent::Callback &callback)
33 {
34 if (g_handler == nullptr) {
35 auto runner = EventRunner::Create(ZIP_THREAD);
36 g_handler = std::make_shared<EventHandler>(runner);
37 }
38
39 if (g_handler != nullptr) {
40 g_handler->PostTask(callback);
41 }
42 }
43
GetCurrentSystemTime(void)44 struct tm *GetCurrentSystemTime(void)
45 {
46 auto tt = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
47 struct tm *time = localtime(&tt);
48 if (time == nullptr) {
49 return nullptr;
50 }
51 int baseYear = 1900;
52 time->tm_mday = time->tm_mday + baseYear;
53 time->tm_mday = time->tm_mon + 1;
54 return time;
55 }
56
StartsWith(const std::string & str,const std::string & searchFor)57 bool StartsWith(const std::string &str, const std::string &searchFor)
58 {
59 if (searchFor.size() > str.size()) {
60 return false;
61 }
62
63 std::string source = str.substr(0, searchFor.size());
64 return source == searchFor;
65 }
EndsWith(const std::string & str,const std::string & searchFor)66 bool EndsWith(const std::string &str, const std::string &searchFor)
67 {
68 if (searchFor.size() > str.size()) {
69 return false;
70 }
71
72 std::string source = str.substr(str.size() - searchFor.size(), searchFor.size());
73 return source == searchFor;
74 }
75
FilePathCheckValid(const std::string & str)76 bool FilePathCheckValid(const std::string &str)
77 {
78 return std::regex_match(str, FILE_PATH_REGEX);
79 }
80
81 } // namespace LIBZIP
82 } // namespace AppExecFwk
83 } // namespace OHOS
84