summaryrefslogtreecommitdiffstats
path: root/kiten/dict.h
blob: f510d0c149793c90abf948e86469c59bf40b3d5a (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 <tqfile.h>
#include <tqmemarray.h>
#include <tqptrlist.h>
#include <tqregexp.h>
#include <tqstringlist.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 TQString &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 TQMemArray<T>
{
public:
	Array(T *, int);
	virtual ~Array();

private:
	T *data;
	int dataSize;
};

template<class T> Array<T>::Array(T *d, int s)
	: TQMemArray<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(TQString path, TQString name);
	~File();

	TQString 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);
	TQCString lookup(unsigned i);
private:
	TQString myName;

	TQFile dictFile;
	const unsigned char * dictPtr;

	TQFile indexFile;
	const uint32_t * indexPtr;

	bool valid;
};

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

	TQString dictName();
	TQString header();
	TQStringList meanings();
	TQStringList readings();
	TQString firstReading();

	bool kanaOnly();
	TQString kanji();

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

protected:
	TQString DictName;
	TQString Header;
	TQStringList Meanings;

	TQString Kanji;
	bool KanaOnly;
	TQStringList Readings;

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

struct SearchResult
{
	TQValueList<Entry> list;
	TQStringList results;
	int count, outOf;
	bool common;
	TQString 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 TQStringList &files, const TQStringList &names);
	void setKanjiDictList(const TQStringList &files, const TQStringList &names);

	SearchResult search(TQRegExp, const TQString &, bool common);
	SearchResult searchKanji(TQRegExp, const TQString &, bool common);
	SearchResult searchPrevious(TQRegExp, const TQString &, SearchResult, bool common);

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

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

	void loadDictList(TQPtrList<File> &fileList, const TQStringList &dictList, const TQStringList &dictNameList);

	TQStringList doSearch(File &, const TQString &);
	SearchResult scanResults(TQRegExp regexp, TQStringList results, bool common);
	SearchResult scanKanjiResults(TQRegExp regexp, TQStringList results, bool common);
	int stringCompare(File &, int index, TQCString);
};

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

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

#endif