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 POSIX_PLATFORM_CONFIG_FILE_HPP_
30 #define 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  * This class provides read/write/clear methods for key/value configuration files.
41  *
42  */
43 class ConfigFile
44 {
45 public:
46     /**
47      * This method 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      * This method 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      * @param[in]      aValueLength  The max length of the data pointed to by @p aValue.
62      *
63      * @retval OT_ERROR_NONE          The given configuration was found and fetched successfully.
64      * @retval OT_ERROR_NOT_FOUND     The given key or iterator was not found in the configuration file.
65      * @retval OT_ERROR_INVALID_ARGS  If @p aKey or @p aValue was NULL.
66      *
67      */
68     otError Get(const char *aKey, int &aIterator, char *aValue, int aValueLength);
69 
70     /**
71      * This method adds a configuration to the configuration file.
72      *
73      * @param[in]  aKey    The key string associated with the requested configuration.
74      * @param[in]  aValue  A pointer to where the new value string of the configuration should be written.
75      *
76      * @retval OT_ERROR_NONE          The given key was found and removed successfully.
77      * @retval OT_ERROR_INVALID_ARGS  If @p aKey or @p aValue was NULL.
78      *
79      */
80     otError Add(const char *aKey, const char *aValue);
81 
82     /**
83      * This function removes all configurations with the same key string from the configuration file.
84      *
85      * @param[in]  aKey  The key string associated with the requested configuration.
86      *
87      * @retval OT_ERROR_NONE          The given key was found and removed successfully.
88      * @retval OT_ERROR_INVALID_ARGS  If @p aKey was NULL.
89      *
90      */
91     otError Clear(const char *aKey);
92 
93 private:
94     const char               *kCommentDelimiter = "#";
95     const char               *kSwapSuffix       = ".swap";
96     static constexpr uint16_t kLineMaxSize      = 512;
97     static constexpr uint16_t kFileNameMaxSize  = 255;
98 
99     void Strip(char *aString);
100 
101     const char *mFilePath;
102 };
103 
104 } // namespace Posix
105 } // namespace ot
106 
107 #endif // POSIX_PLATFORM_CONFIG_FILE_HPP_
108