1 /* 2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include "PropertyFile.h" 27 28 #include "Helpers.h" 29 #include "FilePath.h" 30 31 #include <string> 32 33 34 PropertyFile::PropertyFile(void) : IPropertyContainer() { 35 FReadOnly = false; 36 FModified = false; 37 } 38 39 PropertyFile::PropertyFile(const TString FileName) : IPropertyContainer() { 40 FReadOnly = true; 41 FModified = false; 42 LoadFromFile(FileName); 43 } 44 45 PropertyFile::PropertyFile(OrderedMap<TString, TString> Value) { 46 FData.Append(Value); 47 } 48 49 PropertyFile::PropertyFile(PropertyFile &Value) { 50 FData = Value.FData; 51 FReadOnly = Value.FReadOnly; 52 FModified = Value.FModified; 53 } 54 55 PropertyFile::~PropertyFile(void) { 56 FData.Clear(); 57 } 58 59 void PropertyFile::SetModified(bool Value) { 60 FModified = Value; 61 } 62 63 bool PropertyFile::IsModified() { 64 return FModified; 65 } 66 67 bool PropertyFile::GetReadOnly() { 68 return FReadOnly; 69 } 70 71 void PropertyFile::SetReadOnly(bool Value) { 72 FReadOnly = Value; 73 } 74 75 bool PropertyFile::LoadFromFile(const TString FileName) { 76 bool result = false; 77 Platform& platform = Platform::GetInstance(); 78 79 std::list<TString> contents = platform.LoadFromFile(FileName); 80 81 if (contents.empty() == false) { 82 for (std::list<TString>::const_iterator iterator = contents.begin(); 83 iterator != contents.end(); iterator++) { 84 TString line = *iterator; 85 TString name; 86 TString value; 87 88 if (Helpers::SplitOptionIntoNameValue(line, name, value) == true) { 89 FData.Append(name, value); 90 } 91 } 92 93 SetModified(false); 94 result = true; 95 } 96 97 return result; 98 } 99 100 bool PropertyFile::SaveToFile(const TString FileName, bool ownerOnly) { 101 bool result = false; 102 103 if (GetReadOnly() == false && IsModified()) { 104 std::list<TString> contents; 105 std::vector<TString> keys = FData.GetKeys(); 106 107 for (size_t index = 0; index < keys.size(); index++) { 108 TString name = keys[index]; 109 110 try { 111 TString value;// = FData[index]; 112 113 if (FData.GetValue(name, value) == true) { 114 TString line = name + _T('=') + value; 115 contents.push_back(line); 116 } 117 } 118 catch (std::out_of_range &) { 119 } 120 } 121 122 Platform& platform = Platform::GetInstance(); 123 platform.SaveToFile(FileName, contents, ownerOnly); 124 125 SetModified(false); 126 result = true; 127 } 128 129 return result; 130 } 131 132 bool PropertyFile::GetValue(const TString Key, TString& Value) { 133 return FData.GetValue(Key, Value); 134 } 135 136 bool PropertyFile::SetValue(const TString Key, TString Value) { 137 bool result = false; 138 139 if (GetReadOnly() == false) { 140 FData.SetValue(Key, Value); 141 SetModified(true); 142 result = true; 143 } 144 145 return result; 146 } 147 148 bool PropertyFile::RemoveKey(const TString Key) { 149 bool result = false; 150 151 if (GetReadOnly() == false) { 152 result = FData.RemoveByKey(Key); 153 154 if (result == true) { 155 SetModified(true); 156 } 157 } 158 159 return result; 160 } 161 162 size_t PropertyFile::GetCount() { 163 return FData.Count(); 164 } 165 166 OrderedMap<TString, TString> PropertyFile::GetData() { 167 return FData; 168 }