moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kiten / dict.h
blob5fac3a642a4e6b59d784ad44cecc2c78d0b44823
1 /**
2 This file is part of Kiten, a KDE Japanese Reference Tool...
3 Copyright (C) 2001 Jason Katz-Brown <jason@katzbrown.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 USA
19 **/
22 #ifndef DICT_H
23 #define DICT_H
25 #include <qfile.h>
26 #include <qmemarray.h>
27 #include <qptrlist.h>
28 #include <qregexp.h>
29 #include <qstringlist.h>
31 #include <sys/types.h>
32 #ifdef __osf__
33 typedef unsigned int uint32_t;
34 typedef int int32_t;
35 #else
36 #include <inttypes.h>
37 #endif
39 namespace Dict
42 enum TextType { Text_Kanji, Text_Kana, Text_Latin };
44 // returns the TextType of the first part of the text
45 KDE_EXPORT TextType textType(const QString &text);
47 // File needs to be able to give out Arrays based on its mmap'd data.
48 // But, we don't want the users of the arrays to have to remember to
49 // resetRawData() after using them, since that's bound to fail sooner or later.
51 // This class handles it for us.
52 template<class T> class Array : public QMemArray<T>
54 public:
55 Array(T *, int);
56 virtual ~Array();
58 private:
59 T *data;
60 int dataSize;
63 template<class T> Array<T>::Array(T *d, int s)
64 : QMemArray<T>()
65 , data(d)
66 , dataSize(s)
68 setRawData(data, dataSize / sizeof(T));
71 template<class T> Array<T>::~Array()
73 resetRawData(data, dataSize / sizeof(T));
76 // File manages all the files, pointers, and memory management associated
77 // with a single dictionary.
78 class File
80 public:
81 File(QString path, QString name);
82 ~File();
84 QString name(void);
86 Array<const unsigned char> dict(void);
87 Array<const uint32_t> index(void);
89 int dictLength(void);
90 int indexLength(void);
92 // replacement for exceptions thrown in the constructor
93 bool isValid(void);
95 unsigned char lookup(unsigned i, int offset);
96 QCString lookup(unsigned i);
97 private:
98 QString myName;
100 QFile dictFile;
101 const unsigned char * dictPtr;
103 QFile indexFile;
104 const uint32_t * indexPtr;
106 bool valid;
109 class KDE_EXPORT Entry
111 public:
112 // EDict ctor
113 Entry(const QString &, const QString &, const QStringList &);
114 // Kanjidict ctor
115 Entry(const QString &, QStringList &, QStringList &, unsigned int grade, unsigned int freq, unsigned int strokes, unsigned int miscount);
116 // default (for containers)
117 Entry(const QString & = QString::null);
118 // for a heading
119 Entry(const QString &, bool header);
121 QString dictName();
122 QString header();
123 QStringList meanings();
124 QStringList readings();
125 QString firstReading();
127 bool kanaOnly();
128 QString kanji();
130 bool extendedKanjiInfo();
131 unsigned int grade();
132 unsigned int strokes();
133 unsigned int miscount();
134 unsigned int freq();
136 protected:
137 QString DictName;
138 QString Header;
139 QStringList Meanings;
141 QString Kanji;
142 bool KanaOnly;
143 QStringList Readings;
145 bool ExtendedKanjiInfo;
146 unsigned int Grade;
147 unsigned int Strokes;
148 unsigned int Miscount;
149 unsigned int Freq;
152 struct SearchResult
154 QValueList<Entry> list;
155 QStringList results;
156 int count, outOf;
157 bool common;
158 QString text;
161 enum SearchType { Search_Beginning, Search_FullWord, Search_Anywhere };
162 enum DictionaryType { Edict, Kanjidict };
164 class KDE_EXPORT Index : public QObject
166 Q_OBJECT
168 public:
169 Index();
170 virtual ~Index();
172 void setDictList(const QStringList &files, const QStringList &names);
173 void setKanjiDictList(const QStringList &files, const QStringList &names);
175 SearchResult search(QRegExp, const QString &, bool common);
176 SearchResult searchKanji(QRegExp, const QString &, bool common);
177 SearchResult searchPrevious(QRegExp, const QString &, SearchResult, bool common);
179 // convenience function to create suitable regexps
180 static QRegExp createRegExp(SearchType type, const QString &text, DictionaryType dictionaryType, bool caseSensitive = false);
182 private:
183 QPtrList<File> dictFiles;
184 QPtrList<File> kanjiDictFiles;
186 void loadDictList(QPtrList<File> &fileList, const QStringList &dictList, const QStringList &dictNameList);
188 QStringList doSearch(File &, const QString &);
189 SearchResult scanResults(QRegExp regexp, QStringList results, bool common);
190 SearchResult scanKanjiResults(QRegExp regexp, QStringList results, bool common);
191 int stringCompare(File &, int index, QCString);
194 // lotsa helper functions
195 KDE_EXPORT QString prettyKanjiReading(QStringList);
196 KDE_EXPORT QString prettyMeaning(QStringList);
197 KDE_EXPORT Entry parse(const QString &);
198 KDE_EXPORT Entry kanjiParse(const QString &);
199 KDE_EXPORT Dict::Entry firstEntry(Dict::SearchResult);
200 KDE_EXPORT QString firstEntryText(Dict::SearchResult);
202 int eucStringCompare(const char *str1, const char *str2);
203 bool isEUC(unsigned char c);
206 #endif