summaryrefslogtreecommitdiffstats
path: root/kvoctrain/kvoctrain/spotlight2kvtml.cpp
blob: fa407605d471a877b9205eae4f5021b02d6d2336 (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
/***************************************************************************

                 convert spotlight list to kvtml

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

    begin                : Thu Sep 30 20:00:00 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 <tqfile.h>

#include <vector>
#include <iostream>
#include <stdlib.h>

using namespace std;


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

#define KVD_VERS_PREFIX " v"     //#include "kvoctraindoc.h"
#define TQM_VERB         "v"      //#include "QueryManager.h"

#include "kvoctraincore.h"

#define SPOT_VERSION   "v0.2.1"

vector<TQString> lesson_names;

struct spotty {
  TQString  type;
  int      lesson;
  TQString  en_rem,      // possibly remarks
           de_rem;
  TQString  en,
           de;
};

vector<spotty> spottys;

void writeSpotty (TQTextStream &os, spotty& spot_line, bool first) {
   os << " <e";

   if (spot_line.lesson != 0 )
     os << " m=\"" << spot_line.lesson << "\"";

   if (!spot_line.type.isEmpty() )
     os << " t=\"" << spot_line.type << "\"";

   os << "><o";

   if (first)
     os << " l=\"en\"";

   if (!spot_line.en_rem.isEmpty() )
     os << " r=\"" << spot_line.en_rem.stripWhiteSpace() << "\"";

   os << ">" << spot_line.en.stripWhiteSpace() << "</o><t";

   if (first)
     os << " l=\"de\"";

   if (!spot_line.de_rem.isEmpty() )
     os << " r=\"" << spot_line.de_rem.stripWhiteSpace() << "\"";

   os << ">" << spot_line.de.stripWhiteSpace() << "</t></e>\n";
}


void readToMem (TQTextStream &is, TQString month, TQString year)
{
   TQString line;
   bool    lesson_pending = false;
   TQString lesson_str;
   spotty spot;

   bool first_line = true;
   while (!is.eof() && is.tqdevice()->status() == IO_Ok) {
     line = is.readLine();

     int pos;
     while ((pos = line.find("  ")) >= 0)
       line.remove (pos, 1);

     bool head_line = (!line.stripWhiteSpace().isEmpty() && line == line.upper() );

     if (first_line && !head_line) {
       line = is.readLine();
       head_line = (!line.stripWhiteSpace().isEmpty() && line == line.upper() );
     }
     first_line = false;

     if (   line.find ("see p.") >= 0
         || line.find ("see pp.") >= 0
         || line.find ("see also p") >= 0) {
       line = "";
     }

     int c = 0x92;
     while ((pos = line.find (c)) >= 0)
       line[pos] = '\'';

     c = 0x94;
     while ((pos = line.find (c)) >= 0)
       line[pos] = ' ';

     pos = 0;
     while ((pos = line.find ('&', pos)) >= 0) {
       line.insert (pos+1, "amp;");
       pos += 5; // skip &amp;
     }

     while ((pos = line.find ('<')) >= 0) {
       line.remove(pos, 1);
       line.insert (pos, "&lt;");
     }

     while ((pos = line.find ('>')) >= 0) {
       line.remove(pos, 1);
       line.insert (pos, "&gt;");
     }

     if (!line.isEmpty()) {

       if (head_line) {
         lesson_pending = true;
         lesson_str = line + ", "+month+" "+year;
       }
       else {
         pos = line.find ('\t');
         if (pos < 0)
           pos = line.find ('|');
         if (pos >= 0) {
           if (lesson_pending) {
             lesson_pending = false;
             lesson_names.push_back(lesson_str.stripWhiteSpace());
           }

           spot.lesson = lesson_names.size();
           spot.type = "";
           spot.en_rem = "";
           spot.de_rem = "";

           if (line.left (3) == "to ") {
             spot.type = TQM_VERB;
             line.remove (0, 3);
             pos -= 3;
           }
           spot.en = line.mid(0, pos);
           spot.de = line.mid(pos+1, line.length()-pos-1);

           if ((pos = spot.en.find(" UK") ) >= 0) {
             spot.en_rem+= i18n("UK ").local8Bit();
             spot.en.remove (pos, 3);
           }
           if ((pos = spot.en.find("(UK)") ) >= 0) {
             spot.en_rem+= i18n("UK ").local8Bit();
             spot.en.remove (pos, 4);
           }

           if ((pos = spot.en.find(" N. Am.") ) >= 0) {
             spot.en_rem+= i18n("N. Am. ").local8Bit();
             spot.en.remove (pos, 7);
           }
           if ((pos = spot.en.find("(N. Am.)") ) >= 0) {
             spot.en_rem+= i18n("N. Am. ").local8Bit();
             spot.en.remove (pos, 8);
           }

           if ((pos = spot.en.find(" US") ) >= 0) {
             spot.en_rem+= i18n("US ").local8Bit();
             spot.en.remove (pos, 3);
           }
           if ((pos = spot.en.find("(US)") ) >= 0) {
             spot.en_rem+= i18n("US ").local8Bit();
             spot.en.remove (pos, 4);
           }

           if ((pos = spot.en.find("ifml.") ) >= 0) {
             spot.en_rem+= i18n("ifml. ").local8Bit();
             spot.en.remove (pos, 5);
           }
           if ((pos = spot.en.find("(ifml.)") ) >= 0) {
             spot.en_rem+= i18n("ifml. ").local8Bit();
             spot.en.remove (pos, 7);
           }

           if ((pos = spot.en.find("vulg.") ) >= 0) {
             spot.en_rem+= i18n("vulg. ").local8Bit();
             spot.en.remove (pos, 5);
           }
           if ((pos = spot.en.find("(vulg.)") ) >= 0) {
             spot.en_rem+= i18n("vulg. ").local8Bit();
             spot.en.remove (pos, 7);
           }

           if (!spot.en.isEmpty() && !spot.de.isEmpty())
             spottys.push_back(spot);
         }
       }
     }
   }
}

void writeToKvtml(TQTextStream &os, TQString month, TQString year)
{
   os <<
           "<?xml version=\"1.0\"?>\n"
           "<!doctype kvtml system \"kvoctrain.dtd\">\n"
           "<!--\n"
           "This is a machine generated file.\n"
           "Be careful when editing here.\n"
           "\n"
           "Short definition:\n"
           "\n"
           "lesson       lesson group\n"
           " desc        name\n"
           "   %no       its index\n"
           "   %current  is current lesson\n"
           "type         type group\n"
           " desc        name\n"
           "   %no       its index\n"
           "e            entry of dictionary\n"
           "  %s         is selected\n"
           "  %m         lesson member\n"
           "  %t         common expression type\n"
           " o           original\n"
           "   %q        in query (\"o\" is given, \"t\" is wanted)\n"
           "   %l        language code\n"
           "   %r        remark\n"
           "   %p        pronunciation\n"
           "   %width    column width\n"
           "   %t        expression type\n"
           " t           translation ..\n"
           "   %q        in query (\"t\" is given, \"o\" is wanted)\n"
           "   %l        language code\n"
           "   %r        remark\n"
           "   %p        pronunciation\n"
           "   %width    column width\n"
           "   %t        expression type\n"
           "\n"
           "   %d        last query date (from;to)\n"
           "   %w        dito, compressed\n"
           "   %g        grade (from;to)\n"
           "   %c        count (from;to)\n"
           "   %b        bad count (from;to)\n"
           "\n"
           "\n"
           "Valid xml means:\n"
           " - Close all tags\n"
           " - Keep proper hierarchy\n"
           " - All options are quoted\n"
           "\n"
           "--\n"
           ">\n"
           "<kvtml\n"
           "  generator=\"spotlight2kvtml " SPOT_VERSION " kvoctrain" KVD_VERS_PREFIX "0.5.0\"\n"
           "  cols=\"2\"\n"
           "  lines=\""  << spottys.size() << "\"\n" <<
           "  title=\"" << i18n("Spotlight Online, issue ").local8Bit() << month << " " << "\'" << year << "\"\n"
           << "  author=" << i18n("\"Spotlight Online, www.spotlight-online.de (converted by spotlight2kvtml)").local8Bit()
           << "\">\n\n"
           << " <lesson>\n";

           for (int i = 0; i < (int) lesson_names.size(); i++) {
             os << "  <desc no=\"" << i+1 << "\">"
                << lesson_names[i] << "</desc>\n";
           }
           os << " </lesson>\n\n";

   if (spottys.size() != 0)
     writeSpotty (os, spottys[0], true);

   int l = 1;
   while (os.tqdevice()->status() == IO_Ok && l < (int) spottys.size() ) {
     writeSpotty (os, spottys[l], false);
     l++;
   }

   os << "</kvtml>\n";
}


int main(int argc, char **argv)
{
  // use i18n-strings from kvoctrain
  KApplication app(argc,argv, "kvoctrain");

  if (argc != 4) {
    cerr << i18n("usage: spotlight2kvtml spotfile month year\n\n").local8Bit().data();
    exit (1);
  }

  TQString spot (argv[1]);
  TQString kvtml;

  int dot = spot.findRev('.');
  if (dot < 0) {
    kvtml = spot + "." KVTML_EXT;
  }
  else {
    kvtml = spot;
    kvtml.remove (dot, kvtml.length()-dot);
    kvtml += "." KVTML_EXT;
  }

  TQFile fs(spot);
  fs.open( IO_ReadOnly );
  TQTextStream is( &fs );
  readToMem (is, argv[2], argv[3]);
  fs.close();

  if (is.tqdevice()->status() != IO_Ok ) {
    cerr << i18n("Could not read ").local8Bit().data() << spot.local8Bit().data() << endl;
    exit (1);
  }

  TQFile fk(kvtml);
  fk.open( IO_WriteOnly );
  TQTextStream os( &fk );
  writeToKvtml(os, argv[2], argv[3]);
  fk.close();

  if (os.tqdevice()->status() != IO_Ok ) {
    cerr << i18n("Could not write ").local8Bit().data() << kvtml.local8Bit().data() << endl;
    exit (1);
  }

  exit (0);
}