diff --git a/base/BUILD.gn b/base/BUILD.gn index 924d1d460971272d2384ea1b5062b20647e0ace2..b23ecde7ae70111d6f8f781465eab25677ad18b8 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -73,6 +73,21 @@ sources_utils = [ "src/rwlock.cpp", ] +if (current_os == "mingw" || current_os == "mac" || current_os == "linux") { + sources_utils -= [ + "src/ashmem.cpp", + "src/datetime_ex.cpp", + "src/event_demultiplexer.cpp", + "src/event_handler.cpp", + "src/event_reactor.cpp", + "src/file_ex.cpp", + "src/semaphore_ex.cpp", + "src/thread_ex.cpp", + "src/timer.cpp", + "src/timer_event_handler.cpp", + ] +} + ohos_static_library("utilsbase") { sources = sources_utils configs = [ ":utils_coverage_config" ] @@ -118,5 +133,13 @@ ohos_shared_library("utils") { "system", "updater", ] + + if (current_os == "mingw") { + cflags = [ "-Wno-ignored-attributes" ] # need because stdio.h dont have dllexport, but SECUREC_API have + defines += [ + "SECUREC_DLL_EXPORT", + "SECUREC_IS_DLL_LIBRARY", + ] + } } ############################################################################### diff --git a/base/include/directory_ex.h b/base/include/directory_ex.h index ee7d46c5cf02ae62fcb8c06391b3a1ce310408d5..12aba372b9e981d2e4b48fd4acf9e5458055d0b8 100644 --- a/base/include/directory_ex.h +++ b/base/include/directory_ex.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -25,90 +25,106 @@ namespace OHOS { /** * The GetCurrentProcFullFileName function get the current process exe name. */ +OHOS_EXPORT std::string GetCurrentProcFullFileName(); /** * The GetCurrentProcPath function get the current process exe path. */ +OHOS_EXPORT std::string GetCurrentProcPath(); /** * The ExtractFilePath function extract the input file path. */ +OHOS_EXPORT std::string ExtractFilePath(const std::string& fileFullName); /** * The ExtractFilePath function extract the input file name. */ +OHOS_EXPORT std::string ExtractFileName(const std::string& fileFullName); /** * The ExtractFileExt function extract the input file name type. */ +OHOS_EXPORT std::string ExtractFileExt(const std::string& fileName); /** * The ExcludeTrailingPathDelimiter function exclude the end '/' from the strPath, * return the path without the end '/'. */ +OHOS_EXPORT std::string ExcludeTrailingPathDelimiter(const std::string& path); /** * The IncludeTrailingPathDelimiter function include the end '/' from the strPath, * return the path with the end '/'. */ +OHOS_EXPORT std::string IncludeTrailingPathDelimiter(const std::string& path); /** * The GetDirFiles function get all files in the path. */ +OHOS_EXPORT void GetDirFiles(const std::string& path, std::vector& files); /** * The IsEmptyFolder function judge the path is empty, * return true if is empty, else false. */ +OHOS_EXPORT bool IsEmptyFolder(const std::string& path); /** * The ForceCreateDirectory function is force create the dir with subdir, * return true if create succ, else false. */ +OHOS_EXPORT bool ForceCreateDirectory(const std::string& path); /** * The ForceRemoveDirectory function is force delete the dir with subdir and files, * return true if remove succ, else false. */ +OHOS_EXPORT bool ForceRemoveDirectory(const std::string& path); /** * The RemoveFile function is remove the input strFileName, * return true if remove succ, else false. */ +OHOS_EXPORT bool RemoveFile(const std::string& fileName); /** * The GetFolderSize function is get the folder size(bytes). */ +OHOS_EXPORT uint64_t GetFolderSize(const std::string& path); /** * The ChangeModeFile function is change the input file authority, * return true if change succ, else false. */ +OHOS_EXPORT bool ChangeModeFile(const std::string& fileName, const mode_t& mode); /** * The ChangeModeDirectory function is change the input Directory authority, include subdir, * return true if change succ, else false. */ +OHOS_EXPORT bool ChangeModeDirectory(const std::string& path, const mode_t& mode); /** * The PathToRealPath function is get real path from relative path, * return true if change succ, else false. */ +OHOS_EXPORT bool PathToRealPath(const std::string& path, std::string& realPath); } // OHOS #endif diff --git a/base/include/flat_obj.h b/base/include/flat_obj.h index 0c7dd7cee766f434c9404cead448ff6347b3a076..da17568bf9f53f9cca0c1928defb205cf7e225a5 100644 --- a/base/include/flat_obj.h +++ b/base/include/flat_obj.h @@ -1,43 +1,69 @@ -/* - * Copyright (c) 2021 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef UTILS_BASE_FLAT_OBJ_H -#define UTILS_BASE_FLAT_OBJ_H - -#include -#include - -#ifdef BINDER_IPC_32BIT - typedef __u32 binder_size_t; - typedef __u32 binder_uintptr_t; -#else - typedef __u64 binder_size_t; - typedef __u64 binder_uintptr_t; -#endif - -struct parcel_binder_object_header { - __u32 type; -}; -struct parcel_flat_binder_object { - struct parcel_binder_object_header hdr; - __u32 flags; - union { - binder_uintptr_t binder; - __u32 handle; - }; - binder_uintptr_t cookie; -}; - -#endif +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef UTILS_BASE_FLAT_OBJ_H +#define UTILS_BASE_FLAT_OBJ_H + +#if defined(__APPLE__) || defined(_WIN32) +#include +#include + +#ifdef BINDER_IPC_32BIT + typedef uint32_t binder_size_t; + typedef uint32_t binder_uintptr_t; +#else + typedef uint64_t binder_size_t; + typedef uint64_t binder_uintptr_t; +#endif + +struct parcel_binder_object_header { + uint32_t type; +}; +struct parcel_flat_binder_object { + struct parcel_binder_object_header hdr; + uint32_t flags; + union { + binder_uintptr_t binder; + uint32_t handle; + }; + binder_uintptr_t cookie; +}; +#else // !_WIN32 +#include +#include + +#ifdef BINDER_IPC_32BIT + typedef __u32 binder_size_t; + typedef __u32 binder_uintptr_t; +#else + typedef __u64 binder_size_t; + typedef __u64 binder_uintptr_t; +#endif + +struct parcel_binder_object_header { + __u32 type; +}; +struct parcel_flat_binder_object { + struct parcel_binder_object_header hdr; + __u32 flags; + union { + binder_uintptr_t binder; + __u32 handle; + }; + binder_uintptr_t cookie; +}; +#endif // _WIN32 + +#endif // UTILS_BASE_FLAT_OBJ_H diff --git a/base/include/parcel.h b/base/include/parcel.h index 93de8257fe22a1b787fa7feb1d3adb9ea2505d16..3aa66ca35b2236a12089fb638ce8a283c422ba84 100644 --- a/base/include/parcel.h +++ b/base/include/parcel.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -26,7 +26,7 @@ namespace OHOS { class Parcel; -class Parcelable : public virtual RefBase { +class OHOS_EXPORT Parcelable : public virtual RefBase { public: virtual ~Parcelable() = default; @@ -85,7 +85,7 @@ private: void *Realloc(void *data, size_t newSize) override; }; -class Parcel { +class OHOS_EXPORT Parcel { public: Parcel(); explicit Parcel(Allocator *allocator); diff --git a/base/include/refbase.h b/base/include/refbase.h index d551030e5b63116c93e12a902862a872f71fec0c..506c7f4ed6109f681436fc3e1347d1d1f9f16e9d 100644 --- a/base/include/refbase.h +++ b/base/include/refbase.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -142,7 +142,7 @@ private: }; #endif -class WeakRefCounter { +class OHOS_EXPORT WeakRefCounter { public: WeakRefCounter(RefCounter *counter, void *cookie); @@ -162,7 +162,7 @@ private: void *cookie_ = nullptr; }; -class RefBase { +class OHOS_EXPORT RefBase { public: RefBase(); diff --git a/base/include/rwlock.h b/base/include/rwlock.h index fde028d7b2bf6057d98ba938925b472f949b8bea..cf9ba13e86ad5f1449fa5ff84de2e9e5d97c1ea5 100644 --- a/base/include/rwlock.h +++ b/base/include/rwlock.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -24,7 +24,7 @@ namespace OHOS { namespace Utils { -class RWLock : NoCopyable { +class OHOS_EXPORT RWLock : NoCopyable { public: enum LockStatus { LOCK_STATUS_WRITE = -1, diff --git a/base/include/string_ex.h b/base/include/string_ex.h index 41be2adcd377d03e36d21f2fb118e04c9bfc6eeb..a7135a9c46df962877eacf7833b95be9faddd6ee 100644 --- a/base/include/string_ex.h +++ b/base/include/string_ex.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,31 +23,37 @@ namespace OHOS { /** * The UpperStr function convert all letters of str to uppercase. */ +OHOS_EXPORT std::string UpperStr(const std::string& str); /** * The LowerStr function convert all letters of str to lowercase. */ +OHOS_EXPORT std::string LowerStr(const std::string& str); /** * The ReplaceStr function will replace src with dst int base. */ +OHOS_EXPORT std::string ReplaceStr(const std::string& str, const std::string& src, const std::string& dst); /** * The TrimStr function will trim str by cTrim front and end. */ +OHOS_EXPORT std::string TrimStr(const std::string& str, const char cTrim = ' '); /** * The DexToHexString function convert decimal to hexadecimal string. */ +OHOS_EXPORT std::string DexToHexString(int value, bool upper = true); /** * The SplitStr function will split str by strSep. */ +OHOS_EXPORT void SplitStr(const std::string& str, const std::string& sep, std::vector& strs, bool canEmpty = false, bool needTrim = true); @@ -63,48 +69,56 @@ inline std::string ToString(T iValue) /** * The StrToInt function convert str to int. */ +OHOS_EXPORT bool StrToInt(const std::string& str, int& value); /** * The IsNumericStr function judge all characters of the string are numbers, * return true if all are numbers, else false. */ +OHOS_EXPORT bool IsNumericStr(const std::string& str); /** * The IsAlphaStr function judge all characters of the string are alphabet, * return true if all are alphabet, else false. */ +OHOS_EXPORT bool IsAlphaStr(const std::string& str); /** * The IsUpperStr function judge all characters of the string are uppercase, * return true if all are uppercase, else false. */ +OHOS_EXPORT bool IsUpperStr(const std::string& str); /** * The IsLowerStr function judge all characters of the string are lowercase, * return true if all are lowercase, else false. */ +OHOS_EXPORT bool IsLowerStr(const std::string& str); /** * The IsSubStr function judge the sub in str, * return true if sub in str, else false. */ +OHOS_EXPORT bool IsSubStr(const std::string& str, const std::string& sub); /** * The GetFirstSubStrBetween function get the first sub_str between left and right * return the rightstr pos, if failed return string::npos. */ +OHOS_EXPORT std::string::size_type GetFirstSubStrBetween(const std::string& str, const std::string& left, const std::string& right, std::string& sub); /** * The GetSubStrBetween function get the sub strings between left and right. */ +OHOS_EXPORT void GetSubStrBetween(const std::string& str, const std::string& left, const std::string& right, std::vector& sub); @@ -112,20 +126,24 @@ void GetSubStrBetween(const std::string& str, const std::string& left, * The IsSameTextStr function judge the first's letter is same with second, * return true if same, else false. */ +OHOS_EXPORT bool IsSameTextStr(const std::string& first, const std::string& second); +OHOS_EXPORT bool IsAsciiString(const std::string& str); /** * The str16ToStr8 function convert string16 to string8. * If convert failed, return an empty string. */ +OHOS_EXPORT std::string Str16ToStr8(const std::u16string& str16); /** * The Str8ToStr16 function convert string8 to string16. * If convert failed, return an empty u16string. */ +OHOS_EXPORT std::u16string Str8ToStr16(const std::string& str); } // namespace OHOS diff --git a/base/src/directory_ex.cpp b/base/src/directory_ex.cpp index fd8a366018cd079b483621e2bf5e73bdcf3480ea..438b188637255e3aec3f658445b2f556f47ddb82 100644 --- a/base/src/directory_ex.cpp +++ b/base/src/directory_ex.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include "directory_ex.h" #include #include @@ -23,6 +24,7 @@ using namespace std; namespace OHOS { +#if !defined(__APPLE__) && !defined(_WIN32) string GetCurrentProcFullFileName() { char procFile[PATH_MAX + 1] = {0}; @@ -34,6 +36,13 @@ string GetCurrentProcFullFileName() procFile[ret] = '\0'; return string(procFile); } +#else +string GetCurrentProcFullFileName() +{ + assert("GetCurrentProcFullFileName Not Implement"); + return ""; +} +#endif string GetCurrentProcPath() { @@ -82,6 +91,7 @@ string IncludeTrailingPathDelimiter(const std::string& path) return path; } +#if !defined(__APPLE__) && !defined(_WIN32) void GetDirFiles(const string& path, vector& files) { string pathStringWithDelimiter; @@ -108,7 +118,14 @@ void GetDirFiles(const string& path, vector& files) } closedir(dir); } +#else +void GetDirFiles(const string& path, vector& files) +{ + assert("GetCurrentProcFullFileName Not Implement"); +} +#endif +#if !defined(__APPLE__) && !defined(_WIN32) bool ForceCreateDirectory(const string& path) { string::size_type index = 0; @@ -130,7 +147,15 @@ bool ForceCreateDirectory(const string& path) return access(path.c_str(), F_OK) == 0; } +#else +bool ForceCreateDirectory(const string& path) +{ + assert("GetCurrentProcFullFileName Not Implement"); + return false; +} +#endif +#if !defined(__APPLE__) && !defined(_WIN32) bool ForceRemoveDirectory(const string& path) { string subPath; @@ -173,6 +198,7 @@ bool ForceRemoveDirectory(const string& path) return ret && (access(path.c_str(), F_OK) != 0); } +#endif bool RemoveFile(const string& fileName) { @@ -220,6 +246,7 @@ bool ChangeModeFile(const string& fileName, const mode_t& mode) return ChangeMode(fileName, mode); } +#if !defined(__APPLE__) && !defined(_WIN32) bool ChangeModeDirectory(const string& path, const mode_t& mode) { string subPath; @@ -262,6 +289,7 @@ bool ChangeModeDirectory(const string& path, const mode_t& mode) } return ret; } +#endif bool PathToRealPath(const string& path, string& realPath) { @@ -276,7 +304,12 @@ bool PathToRealPath(const string& path, string& realPath) } char tmpPath[PATH_MAX] = {0}; - if (realpath(path.c_str(), tmpPath) == nullptr) { +#ifdef _WIN32 + auto ret = _fullpath(tmpPath, path.c_str(), sizeof(tmpPath)); +#else + auto ret = realpath(path.c_str(), tmpPath); +#endif + if (ret == nullptr) { UTILS_LOGD("path to realpath error"); return false; } diff --git a/base/src/event_reactor.h b/base/src/event_reactor.h index 794e44a50ef93a470ae7c315f1dd70cee1679e3d..c628e760c535cd8b1f0faacc84c129e1f9e44b9d 100644 --- a/base/src/event_reactor.h +++ b/base/src/event_reactor.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -18,6 +18,7 @@ #include #include +#include #include #include #include diff --git a/base/src/file_ex.cpp b/base/src/file_ex.cpp index 578b1beacfbc2f1405ce07d6c31c36758b999647..488da8ac3ea009cc8f93709c3a4f3cb3edc56b51 100644 --- a/base/src/file_ex.cpp +++ b/base/src/file_ex.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -24,6 +24,7 @@ #include #include #include +#include #include "directory_ex.h" #include "utils_log.h" @@ -262,7 +263,7 @@ bool SaveBufferToFile(const string& filePath, const vector& content, bool } // if the file is not exist,create it first! - uint32_t mode = truncated ? (ios::out | ios::binary | ios::trunc) : (ios::out | ios::binary | ios::app); + auto mode = truncated ? (ios::out | ios::binary | ios::trunc) : (ios::out | ios::binary | ios::app); ofstream file; file.open(filePath.c_str(), mode); if (!file.is_open()) { diff --git a/base/src/parcel.cpp b/base/src/parcel.cpp index 65b15f4ea1ed3e4b463f01753583a98ce433eeb6..51ff846cd8fdc0bc571e37643e9af77b99542449 100644 --- a/base/src/parcel.cpp +++ b/base/src/parcel.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,6 +14,7 @@ */ #include "parcel.h" +#include #include "securec.h" #include "utils_log.h" diff --git a/base/src/string_ex.cpp b/base/src/string_ex.cpp index 32340ae1a502828a1d955904ccba03674def6857..8facfe573a65c16ab68fed5bc25536ecaec8547f 100644 --- a/base/src/string_ex.cpp +++ b/base/src/string_ex.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,6 +17,8 @@ #include "unicode_ex.h" #include "utils_log.h" #include "securec.h" +#include +#include #include #include #include diff --git a/base/src/thread_pool.cpp b/base/src/thread_pool.cpp index 9951929fd05e0bcc4595d35df9dd49ad8cdab98c..fe884accb07a3636746fe5de77c969b9d409b49c 100644 --- a/base/src/thread_pool.cpp +++ b/base/src/thread_pool.cpp @@ -17,6 +17,7 @@ #include "errors.h" #include "utils_log.h" +#include #include #include @@ -48,11 +49,13 @@ uint32_t ThreadPool::Start(int numThreads) for (int i = 0; i < numThreads; ++i) { std::thread t(&ThreadPool::WorkInThread, this); +#ifndef _WIN32 // Give the name of ThreadPool to threads created by the ThreadPool. int err = pthread_setname_np(t.native_handle(), (myName_ + std::to_string(i)).c_str()); if (err != 0) { UTILS_LOGW("Failed to set name to thread. %{public}s", strerror(err)); } +#endif threads_.push_back(std::move(t)); } return ERR_OK;