1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  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 }