1 /*
2  * SharedSettings.hpp
3  *
4  * Copyright (C) 2021 by RStudio, PBC
5  *
6  * Unless you have received this program directly from RStudio pursuant
7  * to the terms of a commercial license agreement with RStudio, then
8  * this program is licensed to you under the terms of version 3 of the
9  * GNU Affero General Public License. This program is distributed WITHOUT
10  * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT,
11  * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the
12  * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details.
13  *
14  */
15 
16 #ifndef CORE_SHARED_SETTINGS_HPP
17 #define CORE_SHARED_SETTINGS_HPP
18 
19 #include <string>
20 
21 #include <boost/algorithm/string/trim.hpp>
22 
23 #include <shared_core/Error.hpp>
24 #include <shared_core/FilePath.hpp>
25 #include <core/FileSerializer.hpp>
26 
27 namespace rstudio {
28 namespace core {
29 
30 class SharedSettings
31 {
32 public:
SharedSettings(const core::FilePath & settingsPath)33    explicit SharedSettings(const core::FilePath& settingsPath)
34       : settingsPath_(settingsPath)
35    {
36       Error error = settingsPath_.ensureDirectory();
37       if (error)
38          LOG_ERROR(error);
39    }
40 
41    // read setting from a folder
readSettingFromPath(const core::FilePath & settingsPath,const std::string & settingName)42    static std::string readSettingFromPath(const core::FilePath& settingsPath, const std::string& settingName)
43    {
44       using namespace rstudio::core;
45       FilePath readPath = settingsPath.completePath(settingName);
46       if (readPath.exists())
47       {
48          std::string value;
49          Error error = core::readStringFromFile(readPath, &value);
50          if (error)
51          {
52             LOG_ERROR(error);
53             return std::string();
54          }
55          boost::algorithm::trim(value);
56          return value;
57       }
58       else
59       {
60          return std::string();
61       }
62    }
63 
64    // write setting to a folder
writeSettingToPath(const core::FilePath & settingsPath,const std::string & settingName,const std::string & value)65    static void writeSettingToPath(const core::FilePath& settingsPath,
66                                   const std::string& settingName,
67                                   const std::string& value)
68    {
69       using namespace rstudio::core;
70       FilePath writePath = settingsPath.completePath(settingName);
71       Error error = core::writeStringToFile(writePath, value);
72       if (error)
73          LOG_ERROR(error);
74    }
75 
readSetting(const char * const settingName) const76    std::string readSetting(const char * const settingName) const
77    {
78       return readSettingFromPath(settingsPath_, settingName);
79    }
80 
writeSetting(const char * const settingName,const std::string & value)81    void writeSetting(const char * const settingName,
82                      const std::string& value)
83    {
84       writeSettingToPath(settingsPath_, settingName, value);
85    }
86 
87 private:
88    core::FilePath settingsPath_;
89 };
90 
91 
92 } // namespace core
93 } // namespace rstudio
94 
95 #endif // CORE_SHARED_SETTINGS_HPP
96 
97