summaryrefslogtreecommitdiffstats
path: root/kiten/dict.h
blob: 80fa81131fd1dc09193fa6159441c9ef90c0424b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
 This file is part of Kiten, a KDE Japanese Reference Tool...
 Copyright (C) 2001  Jason Katz-Brown <jason@katzbrown.com>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 USA
**/


#ifndef DICT_H
#define DICT_H

#include <qfile.h>
#include <qmemarray.h>
#include <qptrlist.h>
#include <qregexp.h>
#include <qstringlist.h>

#include <sys/types.h>
#ifdef __osf__
typedef unsigned int uint32_t;
typedef int int32_t;
#else
#include <inttypes.h>
#endif

namespace Dict
{

enum TextType { Text_Kanji, Text_Kana, Text_Latin };

// returns the TextType of the first part of the text
KDE_EXPORT TextType textType(const QString &text);

// File needs to be able to give out Arrays based on its mmap'd data.
// But, we don't want the users of the arrays to have to remember to
// resetRawData() after using them, since that's bound to fail sooner or later.
//
// This class handles it for us.
template<class T> class Array : public QMemArray<T>
{
public:
	Array(T *, int);
	virtual ~Array();

private:
	T *data;
	int dataSize;
};

template<class T> Array<T>::Array(T *d, int s)
	: QMemArray<T>()
	, data(d)
	, dataSize(s)
{
	setRawData(data, dataSize / sizeof(T));
}

template<class T> Array<T>::~Array()
{
	resetRawData(data, dataSize / sizeof(T));
}

// File manages all the files, pointers, and memory management associated
// with a single dictionary.
class File
{
public:
	File(QString path, QString name);
	~File();

	QString name(void);

	Array<const unsigned char> dict(void);
	Array<const uint32_t> index(void);

	int dictLength(void);
	int indexLength(void);

	// replacement for exceptions thrown in the constructor
	bool isValid(void);

	unsigned char lookup(unsigned i, int offset);
	QCString lookup(unsigned i);
private:
	QString myName;

	QFile dictFile;
	const unsigned char * dictPtr;

	QFile indexFile;
	const uint32_t * indexPtr;

	bool valid;
};

class KDE_EXPORT Entry
{
public:
	// EDict ctor
	Entry(const QString &, const QString &, const QStringList &);
	// Kanjidict ctor
	Entry(const QString &, QStringList &, QStringList &, unsigned int grade, unsigned int freq, unsigned int strokes, unsigned int miscount);
	// default (for containers)
	Entry(const QString & = QString::null);
	// for a heading
	Entry(const QString &, bool header);

	QString dictName();
	QString header();
	QStringList meanings();
	QStringList readings();
	QString firstReading();

	bool kanaOnly();
	QString kanji();

	bool extendedKanjiInfo();
	unsigned int grade();
	unsigned int strokes();
	unsigned int miscount();
	unsigned int freq();

protected:
	QString DictName;
	QString Header;
	QStringList Meanings;

	QString Kanji;
	bool KanaOnly;
	QStringList Readings;

	bool ExtendedKanjiInfo;
	unsigned int Grade;
	unsigned int Strokes;
	unsigned int Miscount;
	unsigned int Freq;
};

struct SearchResult
{
	QValueList<Entry> list;
	QStringList results;
	int count, outOf;
	bool common;
	QString text;
};

enum SearchType { Search_Beginning, Search_FullWord, Search_Anywhere };
enum DictionaryType { Edict, Kanjidict };

class KDE_EXPORT Index : public QObject
{
Q_OBJECT

public:
	Index();
	virtual ~Index();

	void setDictList(const QStringList &files, const QStringList &names);
	void setKanjiDictList(const QStringList &files, const QStringList &names);

	SearchResult search(QRegExp, const QString &, bool common);
	SearchResult searchKanji(QRegExp, const QString &, bool common);
	SearchResult searchPrevious(QRegExp, const QString &, SearchResult, bool common);

	// convenience function to create suitable regexps
	static QRegExp createRegExp(SearchType type, const QString &text, DictionaryType dictionaryType, bool caseSensitive = false);

private:
	QPtrList<File> dictFiles;
	QPtrList<File> kanjiDictFiles;

	void loadDictList(QPtrList<File> &fileList, const QStringList &dictList, const QStringList &dictNameList);

	QStringList doSearch(File &, const QString &);
	SearchResult scanResults(QRegExp regexp, QStringList results, bool common);
	SearchResult scanKanjiResults(QRegExp regexp, QStringList results, bool common);
	int stringCompare(File &, int index, QCString);
};

// lotsa helper functions
KDE_EXPORT QString prettyKanjiReading(QStringList);
KDE_EXPORT QString prettyMeaning(QStringList);
KDE_EXPORT Entry parse(const QString &);
KDE_EXPORT Entry kanjiParse(const QString &);
KDE_EXPORT Dict::Entry firstEntry(Dict::SearchResult);
KDE_EXPORT QString firstEntryText(Dict::SearchResult);

int eucStringCompare(const char *str1, const char *str2);
bool isEUC(unsigned char c);
}

#endif