1 /*
2  * SPDX-FileCopyrightText: 2005 Takuro Ashie
3  * SPDX-FileCopyrightText: 2017-2017 CSSlayer <wengxt@gmail.com>
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  *
7  */
8 #ifndef _FCITX5_ANTHY_STYLE_FILE_H_
9 #define _FCITX5_ANTHY_STYLE_FILE_H_
10 
11 #include <string>
12 #include <vector>
13 
14 class Key2KanaTable;
15 class StyleLine;
16 class StyleSection;
17 class StyleFile;
18 
19 typedef std::vector<StyleLine> StyleLines;
20 typedef std::vector<StyleLines> StyleSections;
21 typedef std::vector<StyleFile> StyleFiles;
22 
23 enum class StyleLineType {
24     UNKNOWN,
25     SPACE,
26     COMMENT,
27     SECTION,
28     KEY,
29 };
30 
31 class StyleLine {
32 public:
33     StyleLine(StyleFile *style_file, std::string line);
34     ~StyleLine();
35 
36 public:
37     StyleLineType type();
line()38     std::string line() { return line_; }
39     bool get_section(std::string &section);
40     bool get_key(std::string &key);
41     bool get_value(std::string &value);
42     bool get_value_array(std::vector<std::string> &value);
43 
44 private:
45     StyleFile *styleFile_;
46     std::string line_;
47     StyleLineType type_;
48 };
49 
50 class StyleFile {
51 public:
52     StyleFile();
53     FCITX_INLINE_DEFINE_DEFAULT_DTOR_AND_MOVE_WITHOUT_SPEC(StyleFile);
54 
55 public:
56     bool load(const std::string &filename);
57 
58     const std::string &title() const;
59 
60     bool getKeyList(std::vector<std::string> &keys, std::string section);
61     bool getString(std::string &value, std::string section, std::string key);
62     bool getStringArray(std::vector<std::string> &value, std::string section,
63                         std::string key);
64 
65 public: // for getting specific data
66     Key2KanaTable key2kanaTable(std::string section);
67 
68 private:
69     void clear();
70     void setupDefaultEntries();
71     StyleLines *findSection(const std::string &section);
72 
73 private:
74     std::string title_;
75     StyleSections sections_;
76 };
77 
78 #endif // _FCITX5_ANTHY_STYLE_FILE_H_
79