Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2014 Google, Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | #define LOG_TAG "bt_btif_config_transcode" |
| 20 | |
| 21 | #include <tinyxml2.h> |
| 22 | |
Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 23 | #include "osi/include/config.h" |
| 24 | #include "osi/include/log.h" |
Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 25 | |
| 26 | using namespace tinyxml2; |
| 27 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame^] | 28 | config_t* btif_config_transcode(const char* xml_filename) { |
Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 29 | XMLDocument document; |
| 30 | int error = document.LoadFile(xml_filename); |
| 31 | if (error != XML_SUCCESS) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame^] | 32 | LOG_ERROR(LOG_TAG, "%s unable to load XML file '%s': %d", __func__, |
| 33 | xml_filename, error); |
Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 34 | return NULL; |
| 35 | } |
| 36 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame^] | 37 | XMLElement* rootElement = document.RootElement(); |
Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 38 | if (!rootElement) { |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame^] | 39 | LOG_ERROR(LOG_TAG, |
| 40 | "%s unable to find root element; assuming corrupted config file.", |
| 41 | __func__); |
Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 42 | return NULL; |
| 43 | } |
| 44 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame^] | 45 | config_t* config = config_new_empty(); |
Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 46 | if (!config) { |
Bluetooth Build Test | b4a42e7 | 2016-04-07 14:10:40 +0200 | [diff] [blame] | 47 | LOG_ERROR(LOG_TAG, "%s unable to allocate config object.", __func__); |
Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 48 | return NULL; |
| 49 | } |
| 50 | |
Myles Watson | 6bd442f | 2016-10-19 09:50:22 -0700 | [diff] [blame^] | 51 | for (XMLElement* i = rootElement->FirstChildElement(); i != NULL; |
| 52 | i = i->NextSiblingElement()) |
| 53 | for (XMLElement* j = i->FirstChildElement(); j != NULL; |
| 54 | j = j->NextSiblingElement()) { |
| 55 | const char* section = j->Attribute("Tag"); |
| 56 | for (XMLElement* k = j->FirstChildElement(); k != NULL; |
| 57 | k = k->NextSiblingElement()) { |
| 58 | const char* key = k->Attribute("Tag"); |
| 59 | const char* value = k->GetText(); |
Alain Vongsouvanh | 9a58e68 | 2016-03-29 17:33:55 -0700 | [diff] [blame] | 60 | if (section && key && value) |
| 61 | config_set_string(config, section, key, value); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return config; |
| 66 | } |