1 /* 2 * Copyright (c) 2022, The OpenThread Authors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. Neither the name of the copyright holder nor the 13 * names of its contributors may be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #ifndef OT_POSIX_PLATFORM_CONFIG_FILE_HPP_ 30 #define OT_POSIX_PLATFORM_CONFIG_FILE_HPP_ 31 32 #include <assert.h> 33 #include <stdint.h> 34 #include <openthread/error.h> 35 36 namespace ot { 37 namespace Posix { 38 39 /** 40 * Provides read/write/clear methods for key/value configuration files. 41 * 42 */ 43 class ConfigFile 44 { 45 public: 46 /** 47 * Initializes the configuration file path. 48 * 49 * @param[in] aFilePath A pointer to the null-terminated file path. 50 * 51 */ 52 explicit ConfigFile(const char *aFilePath); 53 54 /** 55 * Gets a configuration from the configuration file. 56 * 57 * @param[in] aKey The key string associated with the requested configuration. 58 * @param[in,out] aIterator A reference to an iterator. MUST be initialized to 0 or the behavior is undefined. 59 * @param[out] aValue A pointer to where the new value string of the configuration should be read from. 60 * The @p aValue string will be terminated with `\0` if this method returns success. 61 * May be `nullptr` if performing a presence check. 62 * @param[in] aValueLength The max length of the data pointed to by @p aValue. 63 * 64 * @retval OT_ERROR_NONE The given configuration was found and fetched successfully. 65 * @retval OT_ERROR_NOT_FOUND The given key or iterator was not found in the configuration file. 66 * @retval OT_ERROR_INVALID_ARGS If @p aKey was NULL. 67 * 68 */ 69 otError Get(const char *aKey, int &aIterator, char *aValue, int aValueLength) const; 70 71 /** 72 * Adds a configuration to the configuration file. 73 * 74 * @param[in] aKey The key string associated with the requested configuration. 75 * @param[in] aValue A pointer to where the new value string of the configuration should be written. 76 * 77 * @retval OT_ERROR_NONE The given key was found and removed successfully. 78 * @retval OT_ERROR_INVALID_ARGS If @p aKey or @p aValue was NULL. 79 * 80 */ 81 otError Add(const char *aKey, const char *aValue); 82 83 /** 84 * Removes all configurations with the same key string from the configuration file. 85 * 86 * @param[in] aKey The key string associated with the requested configuration. 87 * 88 * @retval OT_ERROR_NONE The given key was found and removed successfully. 89 * @retval OT_ERROR_INVALID_ARGS If @p aKey was NULL. 90 * 91 */ 92 otError Clear(const char *aKey); 93 94 /** 95 * Indicates whether the given key exists. 96 * 97 * @param[in] aKey The key string associated with the requested configuration. 98 * 99 * @retval TRUE If the key exists in the configuration file. 100 * @retval FALSE If the key does not exist in the configuration file. 101 * 102 */ 103 bool HasKey(const char *aKey) const; 104 105 /** 106 * Indicates whether the configuration file exists. 107 * 108 * @retval TRUE If the configuration file exists. 109 * @retval FALSE If the configuration file does not exist. 110 * 111 */ 112 bool DoesExist(void) const; 113 114 private: 115 const char *kCommentDelimiter = "#"; 116 const char *kSwapSuffix = ".swap"; 117 static constexpr uint16_t kLineMaxSize = 512; 118 static constexpr uint16_t kFileNameMaxSize = 255; 119 120 void Strip(char *aString) const; 121 122 const char *mFilePath; 123 }; 124 125 } // namespace Posix 126 } // namespace ot 127 128 #endif // OT_POSIX_PLATFORM_CONFIG_FILE_HPP_ 129