RDK Documentation (Open Sourced RDK Components)
IniFile.cpp
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 #include "IniFile.h"
21 #include "hostIf_main.h"
22 #include <fstream>
23 #include <algorithm>
24 
25 using namespace std;
26 
27 IniFile::IniFile()
28 {
29 }
30 
31 bool IniFile::load(const string &filename)
32 {
33  m_filename = filename;
34 
35  // get rid of quotes, it is quite common with properties files
36  m_filename.erase( remove(m_filename.begin(), m_filename.end(), '\"' ), m_filename.end() );
37 
38  m_dict.clear();
39 
40  ifstream inputStream(m_filename.c_str());
41 
42  if (!inputStream.is_open()) {
43  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "%s: Trying to open a non-existent file [%s] \n", __FUNCTION__, m_filename.c_str());
44  return false;
45  }
46 
47  string line;
48  while (getline(inputStream, line)) {
49  size_t splitterPos = line.find('=');
50  if (splitterPos < line.length()) {
51  string key = line.substr(0, splitterPos);
52  string value = line.substr(splitterPos+1, line.length());
53  m_dict[key] = value;
54  }
55  }
56 
57  return true;
58 }
59 
60 bool IniFile::clear()
61 {
62  m_dict.clear();
63  return flush();
64 }
65 
66 string IniFile::value(const string &key, const string &defaultValue) const
67 {
68  map<string,string>::const_iterator it = m_dict.find(key);
69  if (it == m_dict.end()) {
70  return defaultValue;
71  }
72 
73  return it->second;
74 }
75 
76 bool IniFile::setValue(const string &key, const string &value)
77 {
78  if (m_filename.empty()) {
79  return false;
80  }
81 
82  m_dict[key] = value;
83 
84  // set results in a write back.
85  return flush();
86 }
87 
88 // writeout the file
89 bool IniFile::flush()
90 {
91  // FIXME: truncating everytime is bad for flash ingeneral
92  ofstream outputStream(m_filename.c_str()); // default is out|truncate
93  if (!outputStream.is_open()) {
94  RDK_LOG (RDK_LOG_ERROR, LOG_TR69HOSTIF, "%s: Will not write, open on file [%s] failed \n", __FUNCTION__, m_filename.c_str());
95  return false;
96  }
97 
98  for (map<string,string>::const_iterator it = m_dict.begin(); it != m_dict.end(); ++it) {
99  outputStream << it->first << '=' << it->second << endl;
100  }
101 
102  // even though desctructor closes it, for clarity purpose good to close
103  outputStream.close();
104 
105  return true;
106 }
hostIf_main.h
hostIf_main API.
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258