Coverage Report

Created: 2025-05-21 07:12

/src/aspell/modules/speller/default/data.hpp
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2000,2011 by Kevin Atkinson under the terms of the LGPL
2
3
#ifndef ASPELLER_DATA__HPP
4
#define ASPELLER_DATA__HPP
5
6
#include "ndebug.hpp"
7
#include <assert.h>
8
9
#include "copy_ptr.hpp"
10
#include "enumeration.hpp"
11
#include "language.hpp"
12
#include "posib_err.hpp"
13
#include "string.hpp"
14
#include "string_enumeration.hpp"
15
#include "word_list.hpp"
16
#include "cache.hpp"
17
#include "wordinfo.hpp"
18
19
using namespace acommon;
20
21
namespace acommon {
22
  class Config;
23
  class FStream;
24
  class OStream;
25
  class Convert;
26
}
27
28
namespace aspeller {
29
30
  class Dictionary;
31
  class DictList;
32
  typedef Enumeration<WordEntry *> WordEntryEnumeration;
33
  typedef Enumeration<Dictionary *> DictsEnumeration;
34
35
  class SoundslikeEnumeration 
36
  {
37
  public:
38
    virtual WordEntry * next(int) = 0;
39
64.6k
    virtual ~SoundslikeEnumeration() {}
40
64.6k
    SoundslikeEnumeration() {}
41
  private:
42
    SoundslikeEnumeration(const SoundslikeEnumeration &);
43
    void operator=(const SoundslikeEnumeration &);
44
  };
45
46
  class Dictionary : public Cacheable, public WordList {
47
    friend class SpellerImpl;
48
  private:
49
    CachePtr<const Language> lang_;
50
    PosibErr<void> attach(const Language &);
51
  public:
52
    class FileName {
53
      void copy(const FileName & other);
54
      String       path_;
55
      const char * name_;
56
    public:
57
15.4k
      const String & path() const {return path_;}
58
14.4k
      const char *   name() const {return name_;}
59
      
60
      void clear();
61
      void set(ParmString);
62
      
63
11.9k
      FileName() {clear();}
64
6.79k
      explicit FileName(ParmString str) {set(str);}
65
0
      FileName(const FileName & other) {copy(other);}
66
0
      FileName & operator=(const FileName & other) {copy(other); return *this;}
67
    };
68
    class Id;
69
  protected:
70
    CopyPtr<Id> id_;
71
3.38k
    virtual void set_lang_hook(Config &) {}
72
    
73
  public:
74
    typedef Id CacheKey;
75
    bool cache_key_eq(const Id &);
76
77
    enum BasicType {no_type, basic_dict, replacement_dict, multi_dict};
78
    const BasicType basic_type;
79
    const char * const class_name;
80
81
  protected:
82
    Dictionary(BasicType,const char *);
83
  public:
84
    virtual ~Dictionary();
85
86
32.0k
    const Id & id() {return *id_;}
87
    PosibErr<void> check_lang(ParmString lang);
88
    PosibErr<void> set_check_lang(ParmString lang, Config &);
89
71.7k
    const LangImpl * lang() const {return lang_;};
90
0
    const Language * language() const {return lang_;};
91
    const char * lang_name() const;
92
93
  private:
94
    FileName file_name_;
95
  protected:
96
    PosibErr<void> set_file_name(ParmString name);
97
    PosibErr<void> update_file_info(FStream & f);
98
  public:
99
    bool compare(const Dictionary &);
100
101
3.40k
    const char * file_name() const {return file_name_.path().c_str();}
102
    // returns any additional dictionaries that are also used
103
    virtual PosibErr<void> load(ParmString, Config &, DictList * = 0, 
104
                                SpellerImpl * = 0);
105
106
    virtual PosibErr<void> merge(ParmString);
107
    virtual PosibErr<void> synchronize();
108
    virtual PosibErr<void> save_noupdate();
109
    virtual PosibErr<void> save_as(ParmString);
110
    virtual PosibErr<void> clear();
111
112
    bool validate_words; // valid new words added via a file of the
113
                         // add method
114
115
    bool affix_compressed;
116
    bool invisible_soundslike; // true when words are grouped by the
117
                               // soundslike but soundslike data is not
118
                               // actually stored with the word
119
    bool soundslike_root_only; // true when affix compression is used AND
120
                               // the stored soundslike corresponds to the
121
                               // root word only
122
    bool fast_scan;  // can effectively scan for all soundslikes (or
123
                     // clean words if have_soundslike is false)
124
                     // with an edit distance of 1 or 2
125
    bool fast_lookup; // can effectively find all words with a given soundslike
126
                      // when the SoundslikeWord is not given
127
    
128
    typedef WordEntryEnumeration        Enum;
129
    typedef const char *                Value;
130
    typedef unsigned int                Size;
131
132
    StringEnumeration * elements() const;
133
134
    virtual Enum * detailed_elements() const;
135
    virtual Size   size()     const;
136
    virtual bool   empty()    const;
137
  
138
    virtual bool lookup (ParmString word, const SensitiveCompare *, 
139
                         WordEntry &) const;
140
    
141
    virtual bool clean_lookup(ParmString, WordEntry &) const;
142
143
    virtual bool soundslike_lookup(const WordEntry &, WordEntry &) const;
144
    virtual bool soundslike_lookup(ParmString, WordEntry & o) const;
145
146
    // the elements returned are only guaranteed to remain valid
147
    // guaranteed to return all soundslike and all words 
148
    // however an individual soundslike may appear multiple
149
    // times in the list....
150
    virtual SoundslikeEnumeration * soundslike_elements() const;
151
152
    virtual PosibErr<void> add(ParmString w, ParmString s);
153
    PosibErr<void> add(ParmString w);
154
155
    virtual PosibErr<void> remove(ParmString w);
156
157
    virtual bool repl_lookup(const WordEntry &, WordEntry &) const;
158
    virtual bool repl_lookup(ParmString, WordEntry &) const;
159
160
    virtual PosibErr<void> add_repl(ParmString mis, ParmString cor, ParmString s);
161
    PosibErr<void> add_repl(ParmString mis, ParmString cor);
162
163
    virtual PosibErr<void> remove_repl(ParmString mis, ParmString cor);
164
165
    virtual DictsEnumeration * dictionaries() const;
166
  };
167
168
  typedef Dictionary Dict;
169
  typedef Dictionary ReplacementDict;
170
  typedef Dictionary MultiDict;
171
172
  bool operator==(const Dictionary::Id & rhs, const Dictionary::Id & lhs);
173
174
  inline bool operator!=(const  Dictionary::Id & rhs, const Dictionary::Id & lhs)
175
0
  {
176
0
    return !(rhs == lhs);
177
0
  }
178
179
  class DictList {
180
    // well a stack at the moment but it may eventually become a list
181
    // NOT necessarily first in first out
182
    Vector<Dict *> data;
183
  private:
184
    DictList(const DictList &);
185
    void operator= (const DictList &);
186
  public:
187
    // WILL take ownership of the dict
188
881
    DictList() {}
189
3.38k
    void add(Dict * o) {data.push_back(o);}
190
6.77k
    Dict * last() {return data.back();}
191
3.38k
    void pop() {data.pop_back();}
192
5.13k
    bool empty() {return data.empty();}
193
885
    ~DictList() {for (; !empty(); pop()) last()->release();}
194
  };
195
196
  typedef unsigned int DataType;
197
  static const DataType DT_ReadOnly     = 1<<0;
198
  static const DataType DT_Writable     = 1<<1;
199
  static const DataType DT_WritableRepl = 1<<2;
200
  static const DataType DT_Multi        = 1<<3;
201
  static const DataType DT_Any          = 0xFF;
202
203
  // any new extra dictionaries that were loaded will be ii
204
  PosibErr<Dict *> add_data_set(ParmString file_name,
205
                                Config &,
206
                                DictList * other_dicts = 0,
207
                                SpellerImpl * = 0,
208
                                ParmString dir = 0,
209
                                DataType allowed = DT_Any);
210
  
211
  // implemented in readonly_ws.cc
212
  Dictionary * new_default_readonly_dict();
213
  
214
  PosibErr<void> create_default_readonly_dict(StringEnumeration * els,
215
                                              Config & config);
216
  
217
  // implemented in multi_ws.cc
218
  MultiDict * new_default_multi_dict();
219
220
  // implemented in writable.cpp
221
  Dictionary * new_default_writable_dict(const Config & cfg);
222
223
  // implemented in writable.cpp
224
  ReplacementDict * new_default_replacement_dict(const Config & cfg);
225
}
226
227
#endif
228