Home | History | Annotate | Download | only in androidfw
      1 /*
      2  * Copyright (C) 2017 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 "androidfw/ResourceUtils.h"
     18 
     19 namespace android {
     20 
     21 bool ExtractResourceName(const StringPiece& str, StringPiece* out_package, StringPiece* out_type,
     22                          StringPiece* out_entry) {
     23   *out_package = "";
     24   *out_type = "";
     25   bool has_package_separator = false;
     26   bool has_type_separator = false;
     27   const char* start = str.data();
     28   const char* end = start + str.size();
     29   if (start[0] == '@') {
     30       start++;
     31   }
     32   const char* current = start;
     33   while (current != end) {
     34     if (out_type->size() == 0 && *current == '/') {
     35       has_type_separator = true;
     36       out_type->assign(start, current - start);
     37       start = current + 1;
     38     } else if (out_package->size() == 0 && *current == ':') {
     39       has_package_separator = true;
     40       out_package->assign(start, current - start);
     41       start = current + 1;
     42     }
     43     current++;
     44   }
     45   out_entry->assign(start, end - start);
     46 
     47   return !(has_package_separator && out_package->empty()) &&
     48          !(has_type_separator && out_type->empty());
     49 }
     50 
     51 }  // namespace android
     52