Elliott Hughes | 338698e | 2021-07-13 17:15:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2005 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "Utils.h" |
| 18 | |
| 19 | #include <utils/Compat.h> |
| 20 | |
| 21 | // Separator used by resource paths. This is not platform dependent contrary |
| 22 | // to OS_PATH_SEPARATOR. |
| 23 | #define RES_PATH_SEPARATOR '/' |
| 24 | |
| 25 | using android::String8; |
| 26 | |
| 27 | void convertToResPath([[maybe_unused]] String8& s) { |
| 28 | #if OS_PATH_SEPARATOR != RES_PATH_SEPARATOR |
| 29 | size_t len = s.length(); |
| 30 | if (len > 0) { |
| 31 | char* buf = s.lockBuffer(len); |
| 32 | for (char* end = buf + len; buf < end; ++buf) { |
| 33 | if (*buf == OS_PATH_SEPARATOR) *buf = RES_PATH_SEPARATOR; |
| 34 | } |
| 35 | s.unlockBuffer(len); |
| 36 | } |
| 37 | #endif |
| 38 | } |
Tomasz Wasilczyk | 804e819 | 2023-08-23 02:22:53 +0000 | [diff] [blame^] | 39 | |
| 40 | String8 walkPath(const String8& path, String8* outRemains) { |
| 41 | const char* cp; |
| 42 | const char* const str = path.c_str(); |
| 43 | const char* buf = str; |
| 44 | |
| 45 | cp = strchr(buf, OS_PATH_SEPARATOR); |
| 46 | if (cp == buf) { |
| 47 | // don't include a leading '/'. |
| 48 | buf = buf + 1; |
| 49 | cp = strchr(buf, OS_PATH_SEPARATOR); |
| 50 | } |
| 51 | |
| 52 | if (cp == nullptr) { |
| 53 | String8 res = buf != str ? String8(buf) : path; |
| 54 | if (outRemains) *outRemains = String8(); |
| 55 | return res; |
| 56 | } |
| 57 | |
| 58 | String8 res(buf, cp - buf); |
| 59 | if (outRemains) *outRemains = String8(cp + 1); |
| 60 | return res; |
| 61 | } |