File: Aiksaurus.cpp

package info (click to toggle)
aiksaurus 1.2.1%2Bdev-0.12-7.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 99,564 kB
  • sloc: cpp: 9,872; sh: 8,411; objc: 466; php: 375; makefile: 214
file content (362 lines) | stat: -rw-r--r-- 8,748 bytes parent folder | download | duplicates (8)
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * Aiksaurus - An English-language thesaurus library
 * Copyright (C) 2001-2002 by Jared Davis
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "Aiksaurus.h"
#include "MeaningsFile.h"
#include "WordsFile.h"
#include "WordStream.h"

#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;

#if defined WIN32
	#include <windows.h>
	#define WIN32_LEAN_AND_MEAN
	// Default aik_data_dir is local directory
	std::string AIK_DATA_DIR(".\\");
	// Regestry Reading for AIK_DATA_DIR
	void ReadRegistry()
	{
		HKEY hKey;
		unsigned long lType;	
		DWORD dwSize;
		unsigned char* szValue = NULL;
		if( ::RegOpenKeyEx( HKEY_LOCAL_MACHINE, "Software\\Aiksaurus", 0, KEY_READ, &hKey) == ERROR_SUCCESS )
		{
			// Determine size of string
			if( ::RegQueryValueEx( hKey, "Data_Dir", NULL, &lType, NULL, &dwSize) == ERROR_SUCCESS )
			{
				szValue = new unsigned char[dwSize + 1];
				::RegQueryValueEx( hKey, "Data_Dir", NULL, &lType, szValue, &dwSize);
				AIK_DATA_DIR = (char*) szValue;
				delete[] szValue;
			}
		}
	}
	#if defined _DLL_BUILD
		//Add a DllMain Entry point
		BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) { return TRUE; }
	#endif
#endif

namespace AiksaurusImpl
{
    class ThesaurusImpl
    {
        private:      
            
        // Prevent copying and assignment.          
            ThesaurusImpl(const ThesaurusImpl& rhs);
            ThesaurusImpl& operator=(const ThesaurusImpl& rhs);
 
        // Data Members
            MeaningsFile d_meanings;        // the meanings file
            WordsFile d_words;              // the words file
            std::string d_word;             // current search term.
            int d_id;                       // id of d_word
            const int* d_links;             // ids of meaning sets for d_word 
            std::string d_currentSynonym;   // last synonym that was read
            std::string d_currentSimilar;   // last similar word that was read
            int d_similarID;                // current id of similar words
            int d_similarStop;              // id to stop similar words at
            
        // For each link, we will read a meaning set from the Meanings
        // file.  We'll receive a vector of words that are associated
        // with the meaning.  These will get put into meaning streams,
        // one for each meaning.  We also keep track of which meaning
        // we're currently on.
            vector<WordStream*> d_meaningStreams;
            vector<WordStream*>::size_type d_currentStream;
           
            
        public:

            ThesaurusImpl(const char* mfile, const char* wfile)
                throw(AiksaurusException);
            
            ~ThesaurusImpl() throw();

            const char* word() const throw();

            bool find(const char* word) throw(AiksaurusException);
            const char* next(int& id) throw(AiksaurusException);
            const char* similar() throw(AiksaurusException);
    };


    
ThesaurusImpl::ThesaurusImpl(const char* mfile, const char* wfile)
throw(AiksaurusException)
    : d_meanings(mfile), 
      d_words(wfile), 
      d_links(NULL), 
      d_similarID(0),
      d_similarStop(0),
      d_currentStream(0)
{
    try
    {
        const int size = WordsFile::maxWordLength() + 1;
        d_currentSimilar.reserve( size );
        d_currentSynonym.reserve( size );
    }
    
    catch(std::bad_alloc)
    {
        throw AiksaurusException(
                AiksaurusException::CANNOT_ALLOCATE_MEMORY
        );
    }
}


ThesaurusImpl::~ThesaurusImpl() throw()
{
    for(unsigned int i = 0;i < d_meaningStreams.size();++i)
        delete d_meaningStreams[i];
}


const char* 
ThesaurusImpl::word() const throw()
{
    return d_word.c_str();
}



bool 
ThesaurusImpl::find(const char* word) throw(AiksaurusException)
{
    try
    {
        d_word = word;  // might throw std::bad_alloc
    
        for(unsigned int i = 0;i < d_meaningStreams.size();++i)
            delete d_meaningStreams[i];
        d_meaningStreams.clear();
    
        d_currentStream = 0;
        d_links = 0;
 
        bool found = d_words.findWord(word, d_id);  // might throw AikException
       
        if (found) 
        {
            d_words.loadWord(d_id);         // might throw AiksaurusException
            d_links = d_words.getLinks();   
        
            for(int i = 0;d_links[i] != -1;++i)
            {
                // this might throw std::bad_alloc
                d_meaningStreams.push_back( 
                    new WordStream( d_meanings.getWords(d_links[i]) )
                );
            }
        }
        
        d_similarID = max(d_id - 10, 0);
        d_similarStop = min(d_similarID + 20, d_words.getSize());    
    
        return found; 
    }

    catch(std::bad_alloc)
    {
        throw AiksaurusException(
                AiksaurusException::CANNOT_ALLOCATE_MEMORY
        );
    }
}



const char* ThesaurusImpl::next(int& id) throw(AiksaurusException)
{
    if (d_currentStream >= d_meaningStreams.size())
        return "";
    
    int x = d_meaningStreams[d_currentStream]->next();    
    if (x < 0)
    {
        d_currentStream++;
        return next(id);
    }

    else
    {
        id = d_currentStream;
        d_words.loadWord(x); // might throw AiksaurusException
        d_currentSynonym = d_words.getWord();
    }    

    return d_currentSynonym.c_str();
}


const char* ThesaurusImpl::similar() throw(AiksaurusException)
{
    if (d_similarID < d_similarStop)
    {
        d_words.loadWord(d_similarID++); // might throw AiksaurusException
        d_currentSimilar = d_words.getWord();
    }

    else
    {
        d_currentSimilar = "";
    }

    return d_currentSimilar.c_str();
}

}


///////////////////////////////////////////////////////////////////////
//
//  AikSaurus Class -- Wraps around Implementation
//
///////////////////////////////////////////////////////////////////////

static const char* EMPTY_STRING = "";

Aiksaurus::Aiksaurus() throw()
: d_impl_ptr(0), d_error(EMPTY_STRING)
{
    try
    {
#if defined WIN32
		ReadRegistry();
#endif
	std::string base(AIK_DATA_DIR);
#ifdef HAVE_GETENV
	char * aikdatadir_envvar = getenv ("AIK_DATA_DIR");
	if (aikdatadir_envvar) base = std::string(aikdatadir_envvar);
#endif
	std::string mfile(base + "meanings.dat");
        std::string wfile(base + "words.dat");
        d_impl_ptr = new ThesaurusImpl(mfile.c_str(), wfile.c_str());
    }
    catch(std::bad_alloc)
    {
        d_error = AiksaurusException::MemoryError;
    }
    catch(AiksaurusException& e)
    {
        d_error = e.getDescription();
    }
}

Aiksaurus::Aiksaurus(const char * path_meanings, const char * path_words) throw()
: d_impl_ptr(0), d_error(EMPTY_STRING)
{
    try
    {
        d_impl_ptr = new ThesaurusImpl(path_meanings,path_words);
    }
    catch(std::bad_alloc)
    {
        d_error = AiksaurusException::MemoryError;
    }
    catch(AiksaurusException& e)
    {
        d_error = e.getDescription();
    }
}


Aiksaurus::~Aiksaurus() throw()
{
    delete d_impl_ptr;
}


const char* 
Aiksaurus::word() const throw()
{
    return (d_impl_ptr) 
        ? (d_impl_ptr->word())
        : (EMPTY_STRING);   
}


const char* 
Aiksaurus::error() const throw()
{
    return d_error.c_str();
}


bool 
Aiksaurus::find(const char* word) throw()
{
    try
    {
        return d_impl_ptr->find(word);
    }

    catch(AiksaurusException& e)
    {
        d_error = e.getDescription();
        return false;
    }
}

const char* 
Aiksaurus::next(int& meaning) throw()
{
    try
    {
        return d_impl_ptr->next(meaning);
    }

    catch(AiksaurusException& e)
    {
        d_error = e.getDescription();
        return EMPTY_STRING;
    }
}

const char* 
Aiksaurus::similar() throw()
{
    try
    {    
        return d_impl_ptr->similar();
    }

    catch(AiksaurusException& e)
    {
        d_error = e.getDescription();
        return EMPTY_STRING;
    }
}