Home | History | Annotate | Download | only in test
      1 /*
      2  * Copyright (C) 2015 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 <string.h>
     18 
     19 #include <string>
     20 #include <unordered_map>
     21 
     22 #include <sys/system_properties.h>
     23 
     24 std::unordered_map<std::string, std::string> g_properties;
     25 
     26 extern "C" int property_set(const char* name, const char* value) {
     27   if (g_properties.count(name) != 0) {
     28     g_properties.erase(name);
     29   }
     30   g_properties[name] = value;
     31   return 0;
     32 }
     33 
     34 extern "C" int property_get(const char* key, char* value, const char* default_value) {
     35   if (g_properties.count(key) == 0) {
     36     if (default_value == nullptr) {
     37       return 0;
     38     }
     39     strncpy(value, default_value, PROP_VALUE_MAX-1);
     40   } else {
     41     strncpy(value, g_properties[key].c_str(), PROP_VALUE_MAX-1);
     42   }
     43   value[PROP_VALUE_MAX-1] = '\0';
     44   return strlen(value);
     45 }
     46