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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
/* KPilot
**
** Copyright (C) 2002-2003 by Reinhold Kainhofer
**
** The doc converter synchronizes text files on the PC with DOC databases on the Palm
*/
/*
** 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 in a file called COPYING; if not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
** MA 02110-1301, USA.
*/
/*
** Bug reports and questions can be sent to [email protected]
*/
#include "options.h"
#include "DOC-converter.moc"
#include <tqdir.h>
#include <tqfileinfo.h>
#include <tqregexp.h>
#include <tqsortedlist.h>
#include <pilotDatabase.h>
#include <pilotLocalDatabase.h>
#include <pilotSerialDatabase.h>
#include "pilotDOCHead.h"
#include "pilotDOCEntry.h"
#include "pilotDOCBookmark.h"
#define min(a,b) (a<b)?(a):(b)
/****************************************************************************************************
* various bookmark classes. Most important is the bmkList findMatches(TQString) function,
* which needs to return a list of all bookmarks found for the given bookmark expression.
* A bookmark usually consists of a bookmark text and an offset into the text document.
****************************************************************************************************/
bool docBookmark::compare_pos=true;
bool operator< ( const docBookmark &s1, const docBookmark &s2)
{
if (docBookmark::compare_pos) { return s1.position<s2.position;}
else {return s2.bmkName<s2.bmkName;}
}
bool operator== ( const docBookmark &s1, const docBookmark &s2)
{
return (s1.position==s2.position) && (s1.bmkName==s2.bmkName);
}
int docMatchBookmark::findMatches(TQString doctext, bmkList &fBookmarks) {
FUNCTIONSETUP;
// bmkList res;
int pos = 0, nr=0, found=0;
#ifdef DEBUG
DEBUGKPILOT<<"Finding matches of "<<pattern<<endl;
#endif
while (pos >= 0 && found<to) {
pos = doctext.find(pattern, pos);
#ifdef DEBUG
DEBUGKPILOT<<"Result of search: pos="<<pos<<endl;
#endif
if (pos >= 0)
{
++found;
if (found>=from && found<=to) {
fBookmarks.append(new docBookmark(pattern, pos));
++nr;
}
++pos;
}
}
return nr;
}
int docRegExpBookmark::findMatches(TQString doctext, bmkList &fBookmarks)
{
FUNCTIONSETUP;
// bmkList res;
TQRegExp rx(pattern);
int pos = 0, nr=0, found=0;
while (pos>=0 && found<=to) {
#ifdef DEBUG
DEBUGKPILOT<<"Searching for bookmark "<<pattern<<endl;
#endif
pos=rx.search(doctext, pos);
if (pos > -1) {
++found;
if (found>=from && found<to) {
if (capSubexpression>=0) {
fBookmarks.append(new docBookmark(/*bmkName.left(16)*/rx.cap(capSubexpression), pos));
} else {
// TODO: use the subexpressions from the regexp for the bmk name ($1..$9) (given as separate regexp)
TQString bmkText(bmkName);
for (int i=0; i<=rx.numCaptures(); ++i) {
bmkText.replace(CSL1("$%1").tqarg(i), rx.cap(i));
bmkText.replace(CSL1("\\%1").tqarg(i), rx.cap(i));
}
fBookmarks.append(new docBookmark(bmkText.left(16), pos));
}
++nr;
}
++pos;
}
}
return nr;
}
/*********************************************************************
C O N S T R U C T O R
*********************************************************************/
DOCConverter::DOCConverter(TQObject *parent, const char *name):TQObject(parent,name) {
FUNCTIONSETUP;
docdb=0L;
eSortBookmarks=eSortNone;
fBookmarks.setAutoDelete( TRUE );
}
DOCConverter::~DOCConverter() {
FUNCTIONSETUP;
}
/*********************************************************************
S Y N C S T R U C T U R E
*********************************************************************/
void DOCConverter::setTXTpath(TQString path, TQString file) {
TQDir dr(path);
TQFileInfo pth(dr, file);
if (!file.isEmpty())
txtfilename = pth.absFilePath();
}
void DOCConverter::setTXTpath(TQString filename) {
if (!filename.isEmpty()) txtfilename = filename;
}
void DOCConverter::setPDB(PilotDatabase * dbi) {
if (dbi) docdb = dbi;
}
TQString DOCConverter::readText() {
FUNCTIONSETUP;
if (txtfilename.isEmpty()) return TQString();
TQFile docfile(txtfilename);
if (!docfile.open(IO_ReadOnly))
{
emit logError(i18n("Unable to open text file %1 for reading.").tqarg(txtfilename));
return TQString();
}
TQTextStream docstream(&docfile);
TQString doc = docstream.read();
docfile.close();
return doc;
}
int DOCConverter::findBmkEndtags(TQString &text, bmkList&fBmks) {
FUNCTIONSETUP;
// Start from the end of the text
int pos = text.length() - 1, nr=0;
bool doSearch=true;
while (pos >= 0/* && doSearch*/) {
DEBUGKPILOT<<"Current character is \'"<<text[pos].latin1()<<"\'"<<endl;
// skip whitespace until we reach a >
while (text[pos].isSpace() && pos >= 0) {
DEBUGKPILOT<<"Skipping whitespaces at the end of the file"<<endl;
pos--;
}
// every other character than a > is assumed to belong to the text, so there are no more bookmarks.
if (pos < 0 || text[pos] != '>') {
DEBUGKPILOT<<"Current character \'"<<text[pos].latin1()<<"\' at position "<<pos<<" is not and ending >. Finish searching for bookmarks."<<endl;
pos=-1;
break;
} else {
int endpos = pos;
doSearch=true;
DEBUGKPILOT<<"Found the ending >, now looking for the opening <"<<endl;
// Search for the opening <. There must not be a newline in the bookmark text.
while (doSearch && pos > 0) {
// DEBUGKPILOT<<"pos="<<pos<<", char="<<text[pos].latin1()<<endl;
pos--;
if (text[pos] == '\n') {
DEBUGKPILOT<<"Found carriage return at position "<<pos<<" inside the bookmark text, assuming this is not a bookmark, and the text ends in a >"<<endl;
doSearch = false;
pos = -1;
break;
}
if (text[pos] == '<') {
fBmks.append(new docMatchBookmark(text.mid(pos + 1, endpos - pos - 1)));
++nr;
DEBUGKPILOT<<"Found opening < at position "<<pos<<", bookmarktext ="<<text.mid(pos+1, endpos-pos-1)<<endl;
text.remove(pos, text.length());
pos--;
doSearch = false;
}
}
}
DEBUGKPILOT<<"Finished processing the next bookmark, current position: "<<pos<<endl;
}
return nr;
}
int DOCConverter::findBmkInline(TQString &text, bmkList &fBmks) {
FUNCTIONSETUP;
// bmkList res;
int nr=0;
TQRegExp rx(CSL1("<\\*(.*)\\*>"));
rx.setMinimal(TRUE);
int pos = 0;
while (pos >= 0) {
pos = rx.search(text, pos);
if (pos >= 0) {
fBmks.append(new docBookmark(rx.cap(1), pos+1));
++nr;
text = text.remove(pos, rx.matchedLength());
}
}
return nr;
}
int DOCConverter::findBmkFile(TQString &, bmkList &fBmks) {
FUNCTIONSETUP;
int nr=0;
TQString bmkfilename = txtfilename;
if (bmkfilename.endsWith(CSL1(".txt"))){
bmkfilename.remove(bmkfilename.length()-4, 4);
}
TQString oldbmkfilename=bmkfilename;
bmkfilename+=CSL1(BMK_SUFFIX);
TQFile bmkfile(bmkfilename);
if (!bmkfile.open(IO_ReadOnly)) {
bmkfilename=oldbmkfilename+CSL1(PDBBMK_SUFFIX);
bmkfile.setName(bmkfilename);
if (!bmkfile.open(IO_ReadOnly)) {
DEBUGKPILOT<<"Unable to open bookmarks file "<<bmkfilename<<" for reading the bookmarks of "<<docdb ->dbPathName()<<endl;
return 0;
}
}
DEBUGKPILOT<<"Bookmark file: "<<bmkfilename<<endl;
TQTextStream bmkstream(&bmkfile);
TQString line;
while ( !(line=bmkstream.readLine()).isEmpty() ) {
if (!line.isEmpty() && !line.startsWith(CSL1("#")) ) {
TQStringList bmkinfo=TQStringList::split(CSL1(","), line);
int fieldnr=bmkinfo.count();
// We use the same syntax for the entries as MakeDocJ bookmark files:
// <bookmark>,<string-to-search>,<bookmark-name-string>,<starting-bookmark>,<ending-bookmark>
// For an explanation see: http://home.kc.rr.com/krzysztow/PalmPilot/MakeDocJ/index.html
if (fieldnr>0){
DEBUGKPILOT<<"Working on bookmark \""<<line<<"\""<<endl;
docMatchBookmark*bmk=0L;
TQString bookmark=bmkinfo[0];
bool ok;
int pos=bookmark.toInt(&ok);
if (ok) {
if (fieldnr>1) {
TQString name(bmkinfo[1]);
DEBUGKPILOT<<"Bookmark \""<<name<<"\" set at position "<<pos<<endl;
fBmks.append(new docBookmark(name, pos));
}
} else if (bookmark==CSL1("-") || bookmark==CSL1("+")) {
if (fieldnr>1) {
TQString patt(bmkinfo[1]);
TQString name(patt);
if (fieldnr>2) {
int cap=bmkinfo[2].toInt(&ok);
if (ok) {
bmk=new docRegExpBookmark(patt, cap);
} else {
name=bmkinfo[2];
bmk=new docRegExpBookmark(patt, name);
}
} else{
bmk=new docRegExpBookmark(patt, name);
}
// The third entry in the line (optional) denotes the index of a capture subexpression (if an integer) or the bookmark text as regexp (if a string)
DEBUGKPILOT<<"RegExp Bookmark, pattern="<<patt<<", name="<<name<<endl;
if (bmk) {
if (bookmark==CSL1("-")) {
bmk->from=1;
bmk->to=1;
} else {
if (fieldnr>3) {
bool ok;
int tmp=bmkinfo[3].toInt(&ok);
if (ok) bmk->from=tmp;
if (fieldnr>4) {
tmp=bmkinfo[4].toInt(&ok);
if (ok) bmk->to=tmp;
}
}
}
fBmks.append(bmk);
bmk=0L;
} else {
DEBUGKPILOT<<"Could not allocate bookmark "<<name<<endl;
}
} else {
DEBUGKPILOT<<"RegExp bookmark found with no other information (no bookmark pattern nor name)"<<endl;
}
} else {
TQString pattern(bookmark);
if (fieldnr>1) pattern=bmkinfo[1];
if (fieldnr>2) bookmark=bmkinfo[2];
DEBUGKPILOT<<"RegExp Bookmark, pattern="<<pattern<<", name="<<bookmark<<endl;
bmk=new docRegExpBookmark(pattern, bookmark);
if (bmk) {
bmk->from=1;
bmk->to=1;
fBmks.append(bmk);
}
}
} // fieldnr>0
} // !line.isEmpty()
} // while
return nr;
}
bool DOCConverter::convertTXTtoPDB() {
FUNCTIONSETUP;
if (!docdb) {
emit logError(i18n("Unable to open Database for writing"));
return false;
}
TQString text = readText();
if (fBmkTypes & eBmkEndtags) {
findBmkEndtags(text, fBookmarks);
} // end: EndTag Bookmarks
// Search for all tags <* Bookmark text *> in the text. We have to delete them immediately, otherwise the later bookmarks will be off.
if (fBmkTypes & eBmkInline) {
findBmkInline(text, fBookmarks);
} // end: Inline Bookmarks
// Read in regular expressions and positions from an external file (doc-filename with extension .bmk)
if (fBmkTypes & eBmkFile)
{
findBmkFile(text, fBookmarks);
}
// Process the bookmarks: find the occurrences of the regexps, and sort them if requested:
bmkSortedList pdbBookmarks;
pdbBookmarks.setAutoDelete(TRUE);
docBookmark*bmk;
for (bmk = fBookmarks.first(); bmk; bmk = fBookmarks.next())
{
bmk->findMatches(text, pdbBookmarks);
}
switch (eSortBookmarks)
{
case eSortName:
docBookmark::compare_pos=false;
// qHeapSort(pdbBookmarks);
pdbBookmarks.sort();
break;
case eSortPos:
docBookmark::compare_pos=true;
pdbBookmarks.sort();
break;
case eSortNone:
default:
break;
}
#ifdef DEBUG
DEBUGKPILOT << "Bookmarks: "<<endl;
for (bmk = pdbBookmarks.first(); bmk; bmk = pdbBookmarks.next())
{
DEBUGKPILOT<<bmk->bmkName.left(20)<<" at position "<<bmk->position<<endl;
}
#endif
if (!docdb->isOpen()) {
emit logError(i18n("Unable to open palm doc database %1").tqarg(docdb->dbPathName()) );
return false;
}
// Clean the whole database, otherwise the records would be just appended!
docdb->deleteRecord(0, true);
// Header record for the doc file format
PilotDOCHead docHead;
docHead.position=0;
docHead.recordSize=4096;
docHead.spare=0;
docHead.storyLen=text.length();
docHead.version=compress?DOC_COMPRESSED:DOC_UNCOMPRESSED;
docHead.numRecords=(int)( (text.length()-1)/docHead.recordSize)+1;
PilotRecord*rec=docHead.pack();
docdb->writeRecord(rec);
KPILOT_DELETE(rec);
DEBUGKPILOT << "Write header record: length="<<text.length()<<", compress="<<compress<<endl;
// First compress the text, then write out the bookmarks and - if existing - also the annotations
int len=text.length();
int start=0,reclen=0;
int recnum=0;
while (start<len)
{
reclen=min(len-start, PilotDOCEntry::TEXT_SIZE);
DEBUGKPILOT << "Record #"<<recnum<<", reclen="<<reclen<<", compress="<<compress<<endl;
PilotDOCEntry recText;
// recText.setText(text.mid(start, reclen), reclen);
recText.setText(text.mid(start, reclen));
// if (compress)
recText.setCompress(compress);
PilotRecord*textRec=recText.pack();
docdb->writeRecord(textRec);
++recnum;
start+=reclen;
KPILOT_DELETE(textRec);
}
recnum=0;
// Finally, write out the bookmarks
for (bmk = pdbBookmarks.first(); bmk; bmk = pdbBookmarks.next())
// for (bmkList::const_iterator it=pdbBookmarks.begin(); it!=pdbBookmarks.end(); ++it)
{
++recnum;
DEBUGKPILOT << "Bookmark #"<<recnum<<", Name="<<bmk->bmkName.left(20)<<", Position="<<bmk->position<<endl;
PilotDOCBookmark bmkEntry;
bmkEntry.pos=bmk->position;
strncpy(&bmkEntry.bookmarkName[0], bmk->bmkName.latin1(), 16);
PilotRecord*bmkRecord=bmkEntry.pack();
docdb->writeRecord(bmkRecord);
KPILOT_DELETE(bmkRecord);
}
pdbBookmarks.clear();
fBookmarks.clear();
return true;
}
bool DOCConverter::convertPDBtoTXT()
{
FUNCTIONSETUP;
if (txtfilename.isEmpty()) {
emit logError(i18n("No filename set for the conversion"));
return false;
}
if (!docdb) {
emit logError(i18n("Unable to open Database for reading"));
return false;
}
// The first record of the db is the document header containing information about the doc db
PilotRecord*headerRec = docdb->readRecordByIndex(0);
if (!headerRec)
{
emit logError(i18n("Unable to read database header for database %1.").tqarg(docdb->dbPathName()));
KPILOT_DELETE(docdb);
return false;
}
PilotDOCHead header(headerRec);
KPILOT_DELETE(headerRec);
DEBUGKPILOT<<"Database "<<docdb->dbPathName()<<" has "<<header.numRecords<<" text records, "<<endl
<<" total number of records: "<<docdb->recordCount()<<endl
<<" position="<<header.position<<endl
<<" recordSize="<<header.recordSize<<endl
<<" spare="<<header.spare<<endl
<<" storyLen="<<header.storyLen<<endl
// <<" textRecordSize="<<header.textRecordSize<<endl
<<" version="<<header.version<<endl;
// next come the header.numRecords real document records (might be compressed, see the version flag in the header)
TQFile docfile(txtfilename);
if (!docfile.open(IO_WriteOnly))
{
emit logError(i18n("Unable to open output file %1.").tqarg(txtfilename));
KPILOT_DELETE(docdb);
return false;
}
TQString doctext;
for (int i=1; i<header.numRecords+1; ++i)
{
PilotRecord*rec=docdb->readRecordByIndex(i);
if (rec)
{
PilotDOCEntry recText(rec, header.version==DOC_COMPRESSED);
doctext.append(recText.getText());
DEBUGKPILOT<<"Record "<<i<<endl;
KPILOT_DELETE(rec);
} else {
emit logMessage(i18n("Could not read text record #%1 from Database %2").tqarg(i).tqarg(docdb->dbPathName()));
}
}
// After the document records possibly come a few bookmark records, so read them in and put them in a separate bookmark file.
// for the ztxt conduit there might be annotations after the bookmarks, so the upper bound needs to be adapted.
int upperBmkRec=docdb->recordCount();
bmkSortedList bmks;
bmks.setAutoDelete(TRUE);
for (int i=header.numRecords+1; i<upperBmkRec; ++i)
{
PilotRecord*rec=docdb->readRecordByIndex(i);
if (rec)
{
PilotDOCBookmark bookie(rec);
docBookmark*bmk=new docBookmark(TQString::tqfromLatin1(bookie.bookmarkName), bookie.pos);
bmks.append(bmk);
KPILOT_DELETE(rec);
} else {
emit logMessage(i18n("Could not read bookmark record #%1 from Database %2").tqarg(i).tqarg(docdb->dbPathName()));
}
}
// TODO: Sort the list of bookmarks according to their position
docBookmark::compare_pos=true;
bmks.sort();
if ((fBmkTypes & eBmkFile) && (bmks.count()>0))
{
TQString bmkfilename = docfile.name();
if (bmkfilename.endsWith(CSL1(".txt"))){
bmkfilename.remove(bmkfilename.length()-4, 4);
}
bmkfilename+=CSL1(PDBBMK_SUFFIX);
TQFile bmkfile(bmkfilename);
if (!bmkfile.open(IO_WriteOnly))
{
emit logError(i18n("Unable to open file %1 for the bookmarks of %2.")
.tqarg(bmkfilename).tqarg(docdb ->dbPathName()));
}
else
{
DEBUGKPILOT<<"Writing "<<upperBmkRec-header.numRecords<<
"("<<upperBmkRec<<") bookmarks to file "<<bmkfilename<<endl;
TQTextStream bmkstream(&bmkfile);
for (docBookmark*bmk=bmks.first(); bmk; bmk=bmks.next())
{
bmkstream<<bmk->position<<", "<<bmk->bmkName<<endl;
}
//bmkstream.close();
bmkfile.close();
}
}
if (fBmkTypes & eBmkInline)
{
for (docBookmark*bmk=bmks.last(); bmk; bmk=bmks.prev())
{
doctext.insert(bmk->position, TQString(CSL1("<*") +
bmk->bmkName +
CSL1("*>")));
}
}
// Finally, write the actual text out to the file.
TQTextStream docstream(&docfile);
docstream<<doctext;
//docstream.close();
docfile.close();
docdb->cleanup();
// reset all records to unchanged. I don't know if this is really such a wise idea?
docdb->resetSyncFlags();
return true;
}
|