summaryrefslogtreecommitdiffstats
path: root/kvoctrain/kvoctrain/kvt-core/kvd_lex.cpp
blob: feb160cf8c19512e31b057cc3d35c5482f948994 (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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/***************************************************************************

            maintain a *.lex document (vocabulary trainer for Win)

    -----------------------------------------------------------------------

    begin                : Sat Sep 04 11:00:53 MET 1999

    copyright            : (C) 1999-2001 Ewald Arnold
                           (C) 2001 The KDE-EDU team
    email                : kvoctrain@ewald-arnold.de

    -----------------------------------------------------------------------

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include <kapplication.h>
#include <klocale.h>
#include <kmessagebox.h>

#include "kvoctraindoc.h"


#include <vector>
using namespace std;

#define LEX_MAX_ATTR    20
#define LEX_MAX_LESSON  9

/*  sample for a lex-file

Vocabulary Trainer V5.0
LEX|Deutsch|Englisch| 259 260 1 1 1 0 0 268 0 1 1 1 1 145 6 23 1999 255 1 1 653 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 1 1 0 0 |
-11 700
System
Energie- und Automatisierungstechnik. V. Manuela Lutz. Danke.
1 1 der die das|he she it|
1 1 eine einer eines einen einem|has have had is be|
1 1 er sie es|his him her its|
1 1 wir ihr euch|in to into on at too|
1 1 in an auf bei|the a an|
1 1 zu um zur zum|out by with of off|
1 1 ist sind seid|something someone some|
1 1 dies dieser dieses|under below|
1 1 ||
1 1 ||
der die das ein eine einer eines einen einem|the a an|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Sachgebiete|
|
|
|
|
|
|
|
|
32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32
1 0 255 255 255 255 255 255 0 0 1 Abbildung||||mapping||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
1 0 255 255 255 255 255 255 0 0 1 Abblendkappe||||anti-dazzle device||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
...
*/


static QString extract (QString &line)
{
   QString ret;
   int lim = line.find ("|");
   if (lim < 0) {
     ret = line;
     line = "";
   }
   else {
     ret = line.left(lim);
     line.remove (0, lim+1);
   }

   return ret;
}


bool kvoctrainDoc::saveTypeNameLex (QTextStream &os)
{
  int i;
  for (i = 0; i < (int) type_descr.size() && i < LEX_MAX_ATTR; i++)
    os << getTypeName(i) << "|\n";

  while (i++ < LEX_MAX_ATTR)
    os << "|\n";

  return os.device()->status() == IO_Ok;
}


bool kvoctrainDoc::loadTypeNameLex (QTextStream &is)
{
  QString s;
  QString attr;

  type_descr.clear();

  for (int i = 0; i < LEX_MAX_ATTR; i++) {
    s = is.readLine();
    attr = extract (s);
/*
    if (attr.stripWhiteSpace().isEmpty()) {
      attr.setNum (i+1);
      attr.insert (0, "#");
    }
*/
    type_descr.push_back (attr);
  }

  return is.device()->status() == IO_Ok;
}


bool kvoctrainDoc::saveLessonLex (QTextStream &os)
{
  int i;
  for (i = 0; i < (int) lesson_descr.size() && i < LEX_MAX_LESSON; i++)
    os << lesson_descr[i] << "|\n";

  while (i++ < LEX_MAX_LESSON)
    os << "|\n";

  return os.device()->status() == IO_Ok;
}


bool kvoctrainDoc::loadLessonLex (QTextStream &is)
{
  QString s;
  QString lesson;

  lesson_descr.clear();

  for (int i = 0; i < LEX_MAX_LESSON; i++) {
    s = is.readLine();
    lesson = extract (s);
    if (lesson.stripWhiteSpace().isEmpty()) {
      lesson = "#" + QString::number(i+1);
    }
    lesson_descr.push_back (lesson);
  }

  return is.device()->status() == IO_Ok;
}


bool kvoctrainDoc::saveToLex (QTextStream& os, QString & /*title*/)
{
  os << LEX_IDENT_50 "\n";
  os << "LEX|" << getOriginalIdent() << "|" << getIdent(1) << "|"
     << "259 260 1 1 1 0 0 268 0 1 1 1 1 ";
  os << (int) vocabulary.size();
  os << " 9 5 1999 255 1 1 653 0 0 0 0 0 0 0 0 0 0 1 1 1 0 1 13 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 1 1 0 0 |#3"  << "\n";
  os << "-11 700 0 0 0 0 0 0 0 0 0 0" << "\n";
  os << "System\n";
  os << getTitle() << "\n";
  os << "1 1 der die das|he she it|" << "\n";
  os << "1 1 eine einer eines einen einem|has have had is be|" << "\n";
  os << "1 1 er sie es|his him her its|" << "\n";
  os << "1 1 wir ihr euch|in to into on at too|" << "\n";
  os << "1 1 in an auf bei|the a an|" << "\n";
  os << "1 1 zu um zur zum|out by with of off|" << "\n";
  os << "1 1 ist sind seid|something someone some|" << "\n";
  os << "1 1 dies dieser dieses|under below|" << "\n";
  os << "1 1 ||" << "\n";
  os << "1 1 ||" << "\n";
  os << "der die das ein eine einer eines einen einem|the a an|" << "\n";

  if (!saveTypeNameLex (os))
    return false;

  if (!saveLessonLex (os))
    return false;

  os << "32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32" << "\n";

  int ent_no = 0;
  int ent_percent = (int) vocabulary.size () / 100;
  float f_ent_percent = (int) vocabulary.size () / 100.0;
  emit progressChanged(this, 0);

  vector<kvoctrainExpr>::const_iterator first =  vocabulary.begin ();
  while (first != vocabulary.end ()) {

    ent_no++;
    if (ent_percent != 0 && (ent_no % ent_percent) == 0 )
      emit progressChanged(this, int(ent_no / f_ent_percent));

    os << "0" // type
       <<  " " << "255 255 255 255 255 255 255"
       <<  " " << QMIN (2, (*first).getGrade(false))
       <<  " " << QMIN (2, (*first).getGrade(true))
       <<  " " << (*first).getLesson()
       <<  " " << (*first).getOriginal()
       <<  " " << "||||"  // synonyms
       << (*first).getTranslation(1)
       << "||||"  // synonyms
       << "||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
       << "\n";

    first++;
  }

  setModified (false);
  return os.device()->status() == IO_Ok;
}


bool kvoctrainDoc::loadFromLex (QTextStream& is)
{

  langs.clear();
  vocabulary.clear();
  QString version,
          info1,
          s_dummy,
          title,
          f_keys,
          ident,
          orgID,
          transID;

  int     i_dummy,
          type,
          f_grade,
          t_grade,
          lesson;

  int lines = 10000000;

  version = is.readLine();
  info1 = is.readLine();
  ident = extract (info1);

  orgID = extract (info1);
  if (orgID.isEmpty())
    orgID = "original";

  transID = extract (info1);
  if (transID.isEmpty())
    transID = "translation";

  if (ident != "LEX" || version != LEX_IDENT_50) {
    errorLex (1, "invalid file format");
    return false;
  }

  // first two chars usually give language code:
  // english  ==> en
  // Deutsch  ==> de
  // italano  ==> it

  langs.push_back (orgID.left(2).lower());
  langs.push_back (transID.left(2).lower());

  QTextStream params (info1, IO_ReadOnly);
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> i_dummy;
  params >> lines;

  s_dummy = is.readLine();  // Font
  s_dummy = is.readLine();  // Font
  title = is.readLine();
  setTitle (title);
  s_dummy = is.readLine();
  s_dummy = is.readLine();
  s_dummy = is.readLine();
  s_dummy = is.readLine();
  s_dummy = is.readLine();
  s_dummy = is.readLine();
  s_dummy = is.readLine();
  s_dummy = is.readLine();
  s_dummy = is.readLine();
  s_dummy = is.readLine();
  s_dummy = is.readLine();

  if (!loadTypeNameLex (is))
    return false;

  if (!loadLessonLex (is))
    return false;

  f_keys = is.readLine();

  int ent_percent = (int) lines / 100;
  float f_ent_percent = (int) lines / 100.0;
  emit progressChanged(this, 0);

  for (int i = 0; !is.eof() && i < lines; i++) {
    if (ent_percent != 0 && (i % ent_percent) == 0 ) {
      emit progressChanged(this, int(i / f_ent_percent));
    }

    is >> type;
    is >> i_dummy;
    is >> i_dummy;
    is >> i_dummy;
    is >> i_dummy;
    is >> i_dummy;
    is >> i_dummy;
    is >> i_dummy;
    is >> t_grade;
    is >> f_grade;
    is >> lesson;

    QString line = is.readLine();
    QString s;

    QString original = extract (line);
    for (int i = 0; i < 3; i++) {
      s = extract (line);
      if (!s.isEmpty())
        original += ", " +s;
    }

    QString translation = extract (line);
    for (int i = 0; i < 3; i++) {
      s = extract (line);
      if (!s.isEmpty())
        translation += ", " +s;
    }

/*  ignore rest
       >> "||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
       >> "\n";
*/
    kvoctrainExpr kve (original);
    kve.setGrade (1, t_grade, false);
    kve.setGrade (1, f_grade, true);
    kve.setLesson (lesson);
    kve.addTranslation (translation);

// FIXME: convert attribute to word type ?

    vocabulary.push_back(kve);

  }

  setModified (false);
  return is.device()->status() == IO_Ok;
}


void kvoctrainDoc::errorLex (int /*line*/, const QString &text )
{
   unknown_elem = true;
   QApplication::setOverrideCursor( arrowCursor, true );
   QString s = kapp->makeStdCaption(i18n("Error in lex file"));
   QString msg = text;
   KMessageBox::error(0, msg, s);
   QApplication::restoreOverrideCursor();
}