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 #define LOG_TAG "URIUtils"
16
17 #include "uri_utils.h"
18
19 #include <vector>
20
21 #include "log_print.h"
22 #include "string_ex.h"
23 #include "uri.h"
24
25 namespace OHOS::DataShare {
GetInfoFromURI(const std::string & uri,UriInfo & uriInfo)26 bool URIUtils::GetInfoFromURI(const std::string &uri, UriInfo &uriInfo)
27 {
28 Uri uriTemp(uri);
29 std::vector<std::string> splitUri;
30 SplitStr(uriTemp.GetPath(), "/", splitUri);
31 if (splitUri.size() < URI_INDEX_MAX) {
32 ZLOGE("Invalid uri: %{public}s", uri.c_str());
33 return false;
34 }
35
36 if (splitUri[URI_INDEX_BUNLDENAME].empty() || splitUri[URI_INDEX_MODULENAME].empty() ||
37 splitUri[URI_INDEX_STORENAME].empty() || splitUri[URI_INDEX_TABLENAME].empty()) {
38 ZLOGE("Uri has empty field!");
39 return false;
40 }
41
42 uriInfo.bundleName = splitUri[URI_INDEX_BUNLDENAME];
43 uriInfo.moduleName = splitUri[URI_INDEX_MODULENAME];
44 uriInfo.storeName = splitUri[URI_INDEX_STORENAME];
45 uriInfo.tableName = splitUri[URI_INDEX_TABLENAME];
46 return true;
47 }
operator ==(const UriInfo & rhs) const48 bool UriInfo::operator==(const UriInfo &rhs) const
49 {
50 return bundleName == rhs.bundleName && moduleName == rhs.moduleName && storeName == rhs.storeName &&
51 tableName == rhs.tableName;
52 }
operator !=(const UriInfo & rhs) const53 bool UriInfo::operator!=(const UriInfo &rhs) const
54 {
55 return !(rhs == *this);
56 }
operator <(const UriInfo & rhs) const57 bool UriInfo::operator<(const UriInfo &rhs) const
58 {
59 if (bundleName < rhs.bundleName) {
60 return true;
61 }
62 if (rhs.bundleName < bundleName) {
63 return false;
64 }
65 if (moduleName < rhs.moduleName) {
66 return true;
67 }
68 if (rhs.moduleName < moduleName) {
69 return false;
70 }
71 if (storeName < rhs.storeName) {
72 return true;
73 }
74 if (rhs.storeName < storeName) {
75 return false;
76 }
77 return tableName < rhs.tableName;
78 }
operator >(const UriInfo & rhs) const79 bool UriInfo::operator>(const UriInfo &rhs) const
80 {
81 return rhs < *this;
82 }
operator <=(const UriInfo & rhs) const83 bool UriInfo::operator<=(const UriInfo &rhs) const
84 {
85 return !(rhs < *this);
86 }
operator >=(const UriInfo & rhs) const87 bool UriInfo::operator>=(const UriInfo &rhs) const
88 {
89 return !(*this < rhs);
90 }
91 } // namespace OHOS::DataShare