Coverage Report

Created: 2025-07-18 06:42

/src/aspell/common/config.hpp
Line
Count
Source (jump to first uncovered line)
1
// This file is part of The New Aspell
2
// Copyright (C) 2001 by Kevin Atkinson under the GNU LGPL license
3
// version 2.0 or 2.1.  You should have received a copy of the LGPL
4
// license along with this library if you did not you can find
5
// it at http://www.gnu.org/.
6
7
#ifndef ASPELL_CONFIG___HPP
8
#define ASPELL_CONFIG___HPP
9
10
#include "can_have_error.hpp"
11
#include "key_info.hpp"
12
#include "posib_err.hpp"
13
#include "string.hpp"
14
#include "vector.hpp"
15
16
namespace acommon {
17
18
  class OStream;
19
  class KeyInfoEnumeration;
20
  class StringPairEnumeration;
21
  class MutableContainer;
22
  class Cacheable;
23
  struct Conv;
24
25
  // The Config class is used to hold configuration information.
26
  // it has a set of keys which it will except.  Inserting or even
27
  // trying to look at a key that it does not know will produce
28
  // an error.  Extra accepted keys can be added with the set_extra 
29
  // method.
30
31
  // Keys tagged with KEYINFO_UTF8 are expected to be in UTF-8 format.
32
  // Keys with file/dir names may contain 8-bit characters and must
33
  //   remain untranslated
34
  // All other keys are expected to only contain ASCII characters.
35
36
  class Config;
37
38
  struct ConfigModule {
39
    const char * name; 
40
    const char * file; // path of shared object or dll
41
    const char * desc; // description of module
42
    const KeyInfo * begin;
43
    const KeyInfo * end;
44
  };
45
46
  class Notifier {
47
  public:
48
    // returns a copy if a copy should be made otherwise returns null
49
0
    virtual Notifier * clone(Config *) const {return 0;}
50
3.34k
    virtual ~Notifier() {}
51
52
2.05k
    virtual PosibErr<void> item_updated(const KeyInfo *, bool)    {return no_err;}
53
426
    virtual PosibErr<void> item_updated(const KeyInfo *, int)     {return no_err;}
54
0
    virtual PosibErr<void> item_updated(const KeyInfo *, ParmStr) {return no_err;}
55
0
    virtual PosibErr<void> list_updated(const KeyInfo *)          {return no_err;}
56
  };
57
58
  class PossibleElementsEmul;
59
  class NotifierEnumeration;
60
  class GetLine;
61
  class MDInfoListofLists;
62
63
  static const bool REPLACE = true;
64
  static const bool INSERT  = false;
65
66
  // prefixes
67
  //
68
  // reset - resets a value to the default
69
  //
70
  // enable - sets a boolean value to true
71
  // dont, disable - sets a boolean value to false
72
  // -- setting a boolean value to an empty string is the same as setting 
73
  //    it to true
74
  //
75
  // lset - sets a list, items separated by ':'
76
  // rem, remove - removes item from a list
77
  // add - add an item to a list
78
  // clear - removes all items from a list
79
  // -- setting a list item directly, ie with out a prefix, is the same as 
80
  //    setting it to a single value
81
82
  class Config : public CanHaveError {
83
    // copy and destructor provided
84
    friend class MDInfoListofLists;
85
86
  public:
87
    enum Action {NoOp, Set, Reset, Enable, Disable, 
88
                 ListSet, ListAdd, ListRemove, ListClear};
89
90
    struct Entry {
91
      Entry * next;
92
      String key;
93
      String value;
94
      String file;
95
      unsigned line_num;
96
      Action action;
97
      bool need_conv;
98
      bool secure; // if the value was set in a secure content
99
      short place_holder;
100
55.5k
      Entry() : line_num(0), action(NoOp), 
101
55.5k
                need_conv(false), secure(false), place_holder(-1) {}
102
    };
103
104
  private:
105
    static const int num_parms_[9];
106
107
  public:
108
32.2k
    static inline bool list_action(Action a) {return (int)a >= (int)ListAdd; }
109
83.7k
    static inline int num_parms(Action a) {return num_parms_[a];}
110
111
  private:
112
    String    name_;
113
114
    Entry * first_;
115
    Entry * * insert_point_;
116
    Entry * others_;
117
118
    bool committed_;
119
    bool attached_;    // if attached can't copy
120
    Vector<Notifier *> notifier_list;
121
122
    friend class PossibleElementsEmul;
123
124
    const KeyInfo       * keyinfo_begin;
125
    const KeyInfo       * keyinfo_end;
126
    const KeyInfo       * extra_begin;
127
    const KeyInfo       * extra_end;
128
129
    int md_info_list_index;
130
131
    void copy(const Config & other);
132
    void del();
133
134
    PosibErr<int> commit(Entry * entry, Conv * conv = 0);
135
136
    bool settings_read_in_;
137
138
  public:
139
140
    // the first
141
    // if the second parameter is set than flagged options will be
142
    // converted to utf-8 if needed
143
    PosibErr<void> commit_all(Vector<int> * = 0, const char * codeset = 0);
144
145
    PosibErr<void> replace(ParmStr, ParmStr);
146
    PosibErr<void> remove(ParmStr);
147
148
1.16k
    bool empty() const {return !first_;}
149
150
    PosibErr<void> merge(const Config & other);
151
152
    PosibErr<void> lang_config_merge(const Config & other,
153
                                     int which, ParmStr codeset);
154
155
0
    bool settings_read_in() {return settings_read_in_;}
156
157
    PosibErr<void> set_committed_state(bool val);
158
159
    const Entry * lookup(const char * key) const;
160
    PosibErr<void> lookup_list(const KeyInfo * ki, MutableContainer & m, 
161
                               bool include_default) const;
162
163
    String temp_str;
164
165
    PosibErr<const ConfigModule *> (* load_filter_hook)(Config * config, ParmStr value);
166
    Notifier * filter_mode_notifier;
167
168
    Vector<ConfigModule> filter_modules;
169
    Vector<Cacheable *>  filter_modules_ptrs;
170
171
    Config(ParmStr name,
172
     const KeyInfo * mainbegin, 
173
     const KeyInfo * mainend);
174
175
    Config(const Config &);
176
    ~Config();
177
    Config & operator= (const Config &);
178
179
0
    bool get_attached() const {return attached_;}
180
1.00k
    void set_attached(bool a) {attached_ = a;}
181
182
    Config * clone() const;
183
    void assign(const Config * other);
184
185
0
    const char * name() const {return name_.c_str();}
186
187
    NotifierEnumeration * notifiers() const;
188
  
189
    bool add_notifier    (      Notifier *);
190
    bool remove_notifier (const Notifier *);
191
    bool replace_notifier(const Notifier *, Notifier *);
192
193
    void set_extra(const KeyInfo * begin, const KeyInfo * end);
194
195
    void set_filter_modules(const ConfigModule * modbegin, const ConfigModule * modend);
196
197
    static const char * base_name(const char * name, Action * action = 0);
198
  
199
    PosibErr<const KeyInfo *> keyinfo(ParmStr key) const;
200
201
    KeyInfoEnumeration * possible_elements(bool include_extra = true,
202
                                           bool include_modules = true) const;
203
    
204
0
    StringPairEnumeration * elements() {return 0;} // FIXME
205
    
206
    String get_default(const KeyInfo * ki) const;
207
    PosibErr<String> get_default(ParmStr) const;
208
209
    PosibErr<String> retrieve(ParmStr key) const;
210
211
    struct Value {
212
      String val;
213
      bool secure;
214
0
      Value() {}
215
2.93k
      Value(const String & v, bool s = false) : val(v), secure(s) {}
216
    };
217
    PosibErr<Value> retrieve_value(ParmStr key) const;
218
219
    // will also retrieve a list, with one value per line
220
    PosibErr<String> retrieve_any(ParmStr key) const;
221
  
222
    bool have (ParmStr key) const;
223
224
    PosibErr<void> retrieve_list (ParmStr key, MutableContainer *) const;
225
    PosibErr<bool> retrieve_bool (ParmStr key) const;
226
    PosibErr<int>  retrieve_int  (ParmStr key) const;
227
    
228
    // will take ownership of entry, even if there is an error
229
    PosibErr<void> set(Entry * entry, bool do_unescape = false);
230
      
231
    void replace_internal (ParmStr, ParmStr);
232
    
233
    void write_to_stream(OStream & out, bool include_extra = false);
234
235
    PosibErr<bool> read_in_settings(const Config * = 0);
236
237
    PosibErr<void> read_in(IStream & in, ParmStr id = "");
238
    PosibErr<void> read_in_file(ParmStr file);
239
    PosibErr<void> read_in_string(ParmStr str, const char * what = "");
240
  };
241
242
  Config * new_config();
243
  Config * new_basic_config(); // config which doesn't require any
244
             // external symbols
245
246
  class NotifierEnumeration {
247
    // no copy and destructor needed
248
    Vector<Notifier *>::const_iterator i;
249
    Vector<Notifier *>::const_iterator end;
250
  public:
251
    NotifierEnumeration(const Vector<Notifier *> & b) 
252
0
      : i(b.begin()), end(b.end()) {}
253
0
    const Notifier * next() {
254
0
      const Notifier * temp = *i;
255
0
      if (i != end)
256
0
  ++i;
257
0
      return temp;
258
0
    }
259
0
    bool at_end() const {return i == end;}
260
  };
261
262
  class KeyInfoEnumeration {
263
  public:
264
    typedef const KeyInfo * Value;
265
    virtual KeyInfoEnumeration * clone() const = 0;
266
    virtual void assign(const KeyInfoEnumeration *) = 0;
267
    virtual bool at_end() const = 0;
268
    virtual const KeyInfo * next() = 0;
269
    virtual const char * active_filter_module_name(void) = 0;
270
    virtual const char * active_filter_module_desc(void) = 0;
271
    virtual bool active_filter_module_changed(void) = 0;
272
0
    virtual ~KeyInfoEnumeration() {}
273
  };
274
275
  static const unsigned KEYINFO_MAY_CHANGE = 1 << 0;
276
  static const unsigned KEYINFO_UTF8       = 1 << 1;
277
  static const unsigned KEYINFO_HIDDEN     = 1 << 2;
278
  static const unsigned KEYINFO_COMMON     = 1 << 4;
279
  
280
  class AddableContainer;
281
  class StringList;
282
283
  void separate_list(ParmStr value, AddableContainer & out, bool do_unescape = true);
284
  void combine_list(String & res, const StringList &);
285
286
287
}
288
289
#endif
290