22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 #include "PropertyFile.h" 35 36 #include "Helpers.h" 37 #include "FilePath.h" 38 39 #include <string> 40 41 42 PropertyFile::PropertyFile(void) : PropertyContainer() { 43 FReadOnly = false; 44 FModified = false; 45 } 46 47 PropertyFile::PropertyFile(const TString FileName) : PropertyContainer() { 48 FReadOnly = true; 49 FModified = false; 50 LoadFromFile(FileName); 51 } 52 53 PropertyFile::PropertyFile(std::map<TString, TString> Value) : PropertyContainer() { 54 FData.insert(Value.begin(), Value.end()); 55 } 56 57 PropertyFile::~PropertyFile(void) { 58 } 59 60 void PropertyFile::SetModified(bool Value) { 61 FModified = Value; 62 } 63 64 bool PropertyFile::IsModified() { 65 return FModified; 66 } 67 68 bool PropertyFile::GetReadOnly() { 69 return FReadOnly; 70 } 71 72 void PropertyFile::SetReadOnly(bool Value) { 73 FReadOnly = Value; 74 } 75 76 void PropertyFile::Assign(std::map<TString, TString> Value) { 77 FData.clear(); 78 FData.insert(Value.begin(), Value.end()); 79 SetModified(true); 80 } 81 82 bool PropertyFile::LoadFromFile(const TString FileName) { 83 bool result = false; 84 Platform& platform = Platform::GetInstance(); 85 86 std::list<TString> contents = platform.LoadFromFile(FileName); 87 88 if (contents.empty() == false) { 89 for (std::list<TString>::const_iterator iterator = contents.begin(); iterator != contents.end(); iterator++) { 90 TString line = *iterator; 91 TString name; 92 TString value; 93 94 if (Helpers::SplitOptionIntoNameValue(line, name, value) == true) { 95 FData.insert(std::map<TString, TString>::value_type(name, value)); 96 } 97 } 98 99 SetModified(false); 100 result = true; 101 } 102 103 return result; 104 } 105 106 bool PropertyFile::SaveToFile(const TString FileName, bool ownerOnly) { 107 bool result = false; 108 109 if (GetReadOnly() == false && IsModified()) { 110 std::list<TString> contents; 111 112 for (std::map<TString, TString>::iterator iterator = FData.begin(); 113 iterator != FData.end(); 114 iterator++) { 115 116 TString name = iterator->first; 117 TString value = iterator->second; 118 TString line = name + _T('=') + value; 119 contents.push_back(line); 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 bool result = false; 134 std::map<TString, TString>::const_iterator iterator = FData.find(Key); 135 136 if (iterator != FData.end()) { 137 Value = iterator->second; 138 result = true; 139 } 140 141 return result; 142 } 143 144 bool PropertyFile::SetValue(const TString Key, TString Value) { 145 bool result = false; 146 147 if (GetReadOnly() == false) { 148 FData[Key] = Value; 149 SetModified(true); 150 result = true; 151 } 152 153 return result; 154 } 155 156 bool PropertyFile::RemoveKey(const TString Key) { 157 bool result = false; 158 159 if (GetReadOnly() == false) { 160 std::map<TString, TString>::iterator iterator = FData.find(Key); 161 162 if (iterator != FData.end()) { 163 FData.erase(iterator); 164 SetModified(true); 165 result = true; 166 } 167 } 168 169 return result; 170 } 171 172 size_t PropertyFile::GetCount() { 173 return FData.size(); 174 } 175 176 std::map<TString, TString> PropertyFile::GetData() { 177 return FData; 178 } | 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 34 #include "PropertyFile.h" 35 36 #include "Helpers.h" 37 #include "FilePath.h" 38 39 #include <string> 40 41 42 PropertyFile::PropertyFile(void) : IPropertyContainer() { 43 FReadOnly = false; 44 FModified = false; 45 } 46 47 PropertyFile::PropertyFile(const TString FileName) : IPropertyContainer() { 48 FReadOnly = true; 49 FModified = false; 50 LoadFromFile(FileName); 51 } 52 53 PropertyFile::PropertyFile(OrderedMap<TString, TString> Value) { 54 FData.Append(Value); 55 } 56 57 //PropertyFile::PropertyFile(std::map<TString, TString> Value) : PropertyContainer() { 58 // FData.Append(Value); 59 //} 60 61 PropertyFile::PropertyFile(const PropertyFile &Value) { 62 FData = Value.FData; 63 FReadOnly = Value.FReadOnly; 64 FModified = Value.FModified; 65 } 66 67 PropertyFile::~PropertyFile(void) { 68 FData.Clear(); 69 } 70 71 void PropertyFile::SetModified(bool Value) { 72 FModified = Value; 73 } 74 75 bool PropertyFile::IsModified() { 76 return FModified; 77 } 78 79 bool PropertyFile::GetReadOnly() { 80 return FReadOnly; 81 } 82 83 void PropertyFile::SetReadOnly(bool Value) { 84 FReadOnly = Value; 85 } 86 87 //void PropertyFile::Assign(std::map<TString, TString> Value) { 88 // FData.Clear(); 89 // FData.Assign(Value); 90 // SetModified(true); 91 //} 92 93 bool PropertyFile::LoadFromFile(const TString FileName) { 94 bool result = false; 95 Platform& platform = Platform::GetInstance(); 96 97 std::list<TString> contents = platform.LoadFromFile(FileName); 98 99 if (contents.empty() == false) { 100 for (std::list<TString>::const_iterator iterator = contents.begin(); iterator != contents.end(); iterator++) { 101 TString line = *iterator; 102 TString name; 103 TString value; 104 105 if (Helpers::SplitOptionIntoNameValue(line, name, value) == true) { 106 FData.Append(name, value); 107 } 108 } 109 110 SetModified(false); 111 result = true; 112 } 113 114 return result; 115 } 116 117 bool PropertyFile::SaveToFile(const TString FileName, bool ownerOnly) { 118 bool result = false; 119 120 if (GetReadOnly() == false && IsModified()) { 121 std::list<TString> contents; 122 std::vector<TString> keys = FData.GetKeys(); 123 124 for (size_t index = 0; index < keys.size(); index++) { 125 TString name = keys[index]; 126 127 try { 128 TString value;// = FData[index]; 129 130 if (FData.GetValue(name, value) == true) { 131 TString line = name + _T('=') + value; 132 contents.push_back(line); 133 } 134 } 135 catch (std::out_of_range) { 136 } 137 } 138 139 Platform& platform = Platform::GetInstance(); 140 platform.SaveToFile(FileName, contents, ownerOnly); 141 142 SetModified(false); 143 result = true; 144 } 145 146 return result; 147 } 148 149 bool PropertyFile::GetValue(const TString Key, TString& Value) { 150 return FData.GetValue(Key, Value); 151 } 152 153 bool PropertyFile::SetValue(const TString Key, TString Value) { 154 bool result = false; 155 156 if (GetReadOnly() == false) { 157 FData.SetValue(Key, Value); 158 SetModified(true); 159 result = true; 160 } 161 162 return result; 163 } 164 165 bool PropertyFile::RemoveKey(const TString Key) { 166 bool result = false; 167 168 if (GetReadOnly() == false) { 169 result = FData.RemoveByKey(Key); 170 171 if (result == true) { 172 SetModified(true); 173 } 174 } 175 176 return result; 177 } 178 179 size_t PropertyFile::GetCount() { 180 return FData.Count(); 181 } 182 183 //std::vector<TString> PropertyFile::GetKeys() { 184 // return FData.GetKeys(); 185 //} 186 187 OrderedMap<TString, TString> PropertyFile::GetData() { 188 return FData; 189 } |