summaryrefslogtreecommitdiffstats
path: root/filters/kword/ascii/asciiimport.cpp
blob: b7c4c0f5ef33555901e5b09606ed56cc578823b9 (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
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
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
/* This file is part of the KDE project
   Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org>
   Copyright (C) 2000 Michael Johnson <mikej@xnet.com>
   Copyright (C) 2001, 2002, 2004 Nicolas GOUTTE <goutte@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <config.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <tqobject.h>
#include <tqstring.h>
// #include <tqregexp.h> // Currently not needed (due to disabled code)
#include <tqtextcodec.h>
#include <tqfile.h>
#include <tqtextstream.h>
#include <tqdom.h>
#include <tqfontinfo.h>

#include <kdebug.h>
#include <tdeglobal.h>
#include <kgenericfactory.h>

#include <KoGlobal.h>
#include <KoPageLayout.h>
#include <KoStore.h>
#include <KoFilterChain.h>
#include <KoFilterManager.h>

#include <asciiimport.h>
#include <asciiimport.moc>
#include "ImportDialog.h"

class ASCIIImportFactory : KGenericFactory<ASCIIImport, KoFilter>
{
public:
    ASCIIImportFactory(void) : KGenericFactory<ASCIIImport, KoFilter> ("kwordasciiimport")
    {}
protected:
    virtual void setupTranslations( void )
    {
        TDEGlobal::locale()->insertCatalogue( "kofficefilters" );
    }
};

K_EXPORT_COMPONENT_FACTORY( libasciiimport, ASCIIImportFactory() )

ASCIIImport::ASCIIImport(KoFilter *, const char *, const TQStringList &) :
                     KoFilter() {
}

void ASCIIImport::prepareDocument(TQDomDocument& mainDocument, TQDomElement& mainFramesetElement)
{
    // TODO: other paper formats
    KoFormat paperFormat=PG_DIN_A4; // ISO A4
    KoOrientation paperOrientation=PG_PORTRAIT;

    mainDocument.appendChild(
        mainDocument.createProcessingInstruction(
        "xml","version=\"1.0\" encoding=\"UTF-8\""));

    TQDomElement elementDoc;
    elementDoc=mainDocument.createElement("DOC");
    elementDoc.setAttribute("editor","KWord's Plain Text Import Filter");
    elementDoc.setAttribute("mime","application/x-kword");
    // TODO: We claim to be syntax version 2, but we should verify that it is also true.
    elementDoc.setAttribute("syntaxVersion",2);
    mainDocument.appendChild(elementDoc);

    TQDomElement element;
    element=mainDocument.createElement("ATTRIBUTES");
    element.setAttribute("processing",0);
    element.setAttribute("standardpage",1);
    element.setAttribute("hasHeader",0);
    element.setAttribute("hasFooter",0);
    //element.setAttribute("unit","mm"); // use KWord default instead
    elementDoc.appendChild(element);

    TQDomElement elementPaper=mainDocument.createElement("PAPER");
    elementPaper.setAttribute("format",paperFormat);
    elementPaper.setAttribute("width" ,KoPageFormat::width (paperFormat,paperOrientation) * 72.0 / 25.4);
    elementPaper.setAttribute("height",KoPageFormat::height(paperFormat,paperOrientation) * 72.0 / 25.4);
    elementPaper.setAttribute("orientation",PG_PORTRAIT);
    elementPaper.setAttribute("columns",1);
    elementPaper.setAttribute("columnspacing",2);
    elementPaper.setAttribute("hType",0);
    elementPaper.setAttribute("fType",0);
    elementPaper.setAttribute("spHeadBody",9);
    elementPaper.setAttribute("spFootBody",9);
    elementPaper.setAttribute("zoom",100);
    elementDoc.appendChild(elementPaper);

    element=mainDocument.createElement("PAPERBORDERS");
    element.setAttribute("left",28);
    element.setAttribute("top",42);
    element.setAttribute("right",28);
    element.setAttribute("bottom",42);
    elementPaper.appendChild(element);

    TQDomElement framesetsPluralElementOut=mainDocument.createElement("FRAMESETS");
    mainDocument.documentElement().appendChild(framesetsPluralElementOut);

    mainFramesetElement=mainDocument.createElement("FRAMESET");
    mainFramesetElement.setAttribute("frameType",1);
    mainFramesetElement.setAttribute("frameInfo",0);
    mainFramesetElement.setAttribute("autoCreateNewFrame",1);
    mainFramesetElement.setAttribute("removable",0);
    // TODO: "name" attribute (needs I18N)
    framesetsPluralElementOut.appendChild(mainFramesetElement);

    TQDomElement frameElementOut=mainDocument.createElement("FRAME");
    frameElementOut.setAttribute("left",28);
    frameElementOut.setAttribute("top",42);
    frameElementOut.setAttribute("bottom",566);
    frameElementOut.setAttribute("right",798);
    frameElementOut.setAttribute("runaround",1);
    mainFramesetElement.appendChild(frameElementOut);

    TQDomElement elementStylesPlural=mainDocument.createElement("STYLES");
    elementDoc.appendChild(elementStylesPlural);

    TQDomElement elementStyleStandard=mainDocument.createElement("STYLE");
    elementStylesPlural.appendChild(elementStyleStandard);

    element=mainDocument.createElement("NAME");
    element.setAttribute("value","Standard");
    elementStyleStandard.appendChild(element);

    element=mainDocument.createElement("FOLLOWING");
    element.setAttribute("name","Standard");
    elementStyleStandard.appendChild(element);

    TQDomElement elementFormat=mainDocument.createElement("FORMAT");
    elementStyleStandard.appendChild(elementFormat);

    // Use TQFontInfo, as it does not give back -1 as point size.
    TQFontInfo defaultFontInfo(KoGlobal::defaultFont());

    element=mainDocument.createElement("FONT");
    element.setAttribute("name",defaultFontInfo.family());
    elementFormat.appendChild(element);

    element=mainDocument.createElement("SIZE");
    element.setAttribute("value",defaultFontInfo.pointSize());
    elementFormat.appendChild(element);

    element=mainDocument.createElement("ITALIC");
    element.setAttribute("value",0);
    elementFormat.appendChild(element);

    element=mainDocument.createElement("WEIGHT");
    element.setAttribute("value",50);
    elementFormat.appendChild(element);

    element=mainDocument.createElement("UNDERLINE");
    element.setAttribute("value",0);
    elementFormat.appendChild(element);

    element=mainDocument.createElement("STRIKEOUT");
    element.setAttribute("value",0);
    elementFormat.appendChild(element);

    element=mainDocument.createElement("VERTALIGN");
    element.setAttribute("value",0);
    elementFormat.appendChild(element);

    element=mainDocument.createElement("COLOR");
    element.setAttribute("red",  -1);
    element.setAttribute("green",1);
    element.setAttribute("blue", -1);
    elementFormat.appendChild(element);

    element=mainDocument.createElement("TEXTBACKGROUNDCOLOR");
    element.setAttribute("red",  -1);
    element.setAttribute("green",-1);
    element.setAttribute("blue", -1);
    elementFormat.appendChild(element);

}

KoFilter::ConversionStatus ASCIIImport::convert( const TQCString& from, const TQCString& to )
{
    if (to!="application/x-kword" || from!="text/plain")
        return KoFilter::NotImplemented;

    AsciiImportDialog* dialog = 0;
    if (!m_chain->manager()->getBatchMode())
    {
    	dialog = new AsciiImportDialog();
	if (!dialog)
	{
	  kdError(30502) << "Dialog has not been created! Aborting!" << endl;
	  return KoFilter::StupidError;
	}
	if (!dialog->exec())
	{
	  kdDebug(30502) << "Dialog was aborted! Aborting filter!" << endl; // this isn't an error!
	  return KoFilter::UserCancelled;
	}
    }

    TQTextCodec* codec;
    int paragraphStrategy;
    if (dialog)
    {
      	codec = dialog->getCodec();
	paragraphStrategy=dialog->getParagraphStrategy();
	delete dialog;
    }
    else
    {
      codec = TQTextCodec::codecForName("UTF-8");
      paragraphStrategy=0;
    }

    TQFile in(m_chain->inputFile());
    if(!in.open(IO_ReadOnly)) {
        kdError(30502) << "Unable to open input file!" << endl;
        in.close();
        return KoFilter::FileNotFound;
    }

    TQString tbl;  // string for table XML

    TQDomDocument mainDocument;
    TQDomElement mainFramesetElement;

    prepareDocument(mainDocument,mainFramesetElement);

    TQTextStream stream(&in);

    if (!codec)
    {
        kdError(30502) << "Could not create TQTextCodec! Aborting" << endl;
        in.close();
        return KoFilter::StupidError;
    }

    kdDebug(30502) << "Charset used: " << codec->name() << endl;

    stream.setCodec(codec);

    if (1==paragraphStrategy)
        sentenceConvert(stream, mainDocument, mainFramesetElement);
    else if (999==paragraphStrategy)
        oldWayConvert(stream, mainDocument, mainFramesetElement);
    else
        asIsConvert(stream, mainDocument, mainFramesetElement);


#if 0
    kdDebug(30502) << mainDocument.toString() << endl;
#endif

    KoStoreDevice* out=m_chain->storageFile( "root", KoStore::Write );
    if(!out) {
        kdError(30502) << "Unable to open output file!" << endl;
        in.close();
        return KoFilter::StorageCreationError;
    }
    TQCString cstr=mainDocument.toCString();
    // WARNING: we cannot use KoStore::write(const TQByteArray&) because it gives an extra NULL character at the end.
    out->writeBlock(cstr,cstr.length());
    in.close();
    return KoFilter::OK;
}

void ASCIIImport::oldWayConvert(TQTextStream& stream, TQDomDocument& mainDocument,
    TQDomElement& mainFramesetElement)
{
    kdDebug(30502) << "Entering: ASCIIImport::oldWayConvert" << endl;
    TQStringList paragraph;  // lines of the paragraph
    int linecount = 0;  // line counter used to position tables
    //int table_no = 0;  // used for table identifiers
    int numLines; // Number of lines of the paragraph

    bool lastCharWasCr=false; // Was the previous character a Carriage Return?
    TQString strLine;
    while(!stream.atEnd())
    {
        paragraph.clear();
        // Read in paragraph
        for(int line_no = numLines = 0; line_no < MAXLINES; line_no++, numLines++)
        {
            // We need to read a line
            // NOTE: we cannot use TQStreamText::readLine,
            //   as it does not know anything about Carriage Returns
            strLine=readLine(stream,lastCharWasCr);
            if (strLine.isEmpty())
            {
                paragraph.append(TQString());
                break;
            }

            const int length = strLine.length();
            if (strLine.at(length-1) == '-')
                // replace the hard hyphen - at line end by a soft hyphen
                strLine.at(length-1)=TQChar(173);
            else
                strLine += ' '; // add space to end of line

            paragraph.append(strLine);
        } // for(line_no = 0;

         //   process tables
#if 0
        if ( Table( &Line[0], &linecount, numLines, table_no, tbl, str))
            table_no++;
        else
#else
        if (true)
#endif
        {
        // Process bullet and dash lists
#if 0
        if(ListItem( &Line[0], numLines, str))
            linecount += (numLines + 1);
        else
           {
#else
            if (true)
            {
#endif
                processParagraph(mainDocument,mainFramesetElement,paragraph);
                linecount += ( numLines + 1);  // increment the line count
            }  // else
        }  // else
     }  // while(!eof)

#if 0
    // Add table info
    if( table_no > 0) str += tbl;
#endif
}

void ASCIIImport::asIsConvert(TQTextStream& stream, TQDomDocument& mainDocument,
    TQDomElement& mainFramesetElement)
// Paragraph strategy: one line, one paragraph
{
    kdDebug(30502) << "Entering: ASCIIImport::asIsConvert" << endl;
    bool lastCharWasCr=false; // Was the previous character a Carriage Return?
    TQString strLine;
    while(!stream.atEnd())
    {
        // Read one line and consider it being a paragraph
        strLine=readLine(stream,lastCharWasCr);
        writeOutParagraph(mainDocument,mainFramesetElement, "Standard", strLine,
            0, 0);
    }
}

void ASCIIImport::sentenceConvert(TQTextStream& stream, TQDomDocument& mainDocument,
    TQDomElement& mainFramesetElement)
// Strategy:
// - end a paragraph when a line ends with a point or similar punctuation.
// - search the punctuation at the end of the line, even if the sentence is quoted or in parentheses.
// - an empty line also ends the paragraph
// TODO/FIXME: we have a little problem with empty lines. Perhaps we should not allow empty paragraphs!
{
    kdDebug(30502) << "Entering: ASCIIImport::sentenceConvert" << endl;
    TQStringList paragraph;  // lines of the paragraph
    bool lastCharWasCr=false; // Was the previous character a Carriage Return?
    TQString strLine;
    TQString stoppingPunctuation(".!?");
    TQString skippingQuotes("\"')");
    while (!stream.atEnd())
    {
        paragraph.clear();
        for(;;)
        {
            // We need to read a line
            // NOTE: we cannot use TQStreamText::readLine,
            //   as it does not know anything about Carriage Returns
            strLine=readLine(stream,lastCharWasCr);
            if (strLine.isEmpty())
            {
                break;
            }

            paragraph.append(strLine);

            uint lastPos=strLine.length()-1;
            TQChar lastChar;
            // Skip a maximum of 10 quotes (or similar) at the end of the line
            for (int i=0;i<10;i++)
            {
                lastChar=strLine[lastPos];
                if (lastChar.isNull())
                    break;
                else if (skippingQuotes.find(lastChar)==-1)
                    break;
                else
                    lastPos--;
            }

            lastChar=strLine[lastPos];
            if (lastChar.isNull())
                continue;
            else if (stoppingPunctuation.find(lastChar)!=-1)
                break;
        }
#if 1
        writeOutParagraph(mainDocument,mainFramesetElement, "Standard",
            paragraph.join(" ").simplifyWhiteSpace(), 0, 0);
#else
        // FIXME/TODO: why is this not working?
        //processParagraph(mainDocument,mainFramesetElement,paragraph);
#endif
     }  // while(!eof)
}

void ASCIIImport::processParagraph(TQDomDocument& mainDocument,
    TQDomElement& mainFramesetElement, const TQStringList& paragraph)
{
    // Paragraph with no tables or lists
    TQString text;
    TQStringList::ConstIterator it=paragraph.begin(); // Current line (at start, the first one)
    TQStringList::ConstIterator previousLine=it; // The previous one (at start, also the first one)
    int firstindent=Indent(*it);

    // We work with one line in advance (therefore the two it++)
    for( it++; it!=paragraph.end(); it++)
    {
        text += *previousLine; // add previous line to paragraph
        // check for a short line - if short make it a paragraph
        if( (*previousLine).length() <= shortline)
        {
            if((*it).length() > shortline)
            // skip if short last line of normal paragraph
            {
                const int secondindent = Indent(*previousLine);
                writeOutParagraph(mainDocument,mainFramesetElement,
                    "Standard", text.simplifyWhiteSpace(), firstindent, secondindent);

                firstindent = Indent(*it);
                text = TQString();  // reinitialize paragraph text
            }
        }
        previousLine=it;
    }
    // write out paragraph begin to end
    const int secondindent = Indent(*previousLine);
    writeOutParagraph(mainDocument,mainFramesetElement,
        "Standard", text.simplifyWhiteSpace(), firstindent, secondindent);
}

void ASCIIImport::writeOutParagraph(TQDomDocument& mainDocument,
    TQDomElement& mainFramesetElement, const TQString& name,
    const TQString& text, const int firstindent, const int secondindent)
{
    TQDomElement paragraphElementOut=mainDocument.createElement("PARAGRAPH");
    mainFramesetElement.appendChild(paragraphElementOut);
    TQDomElement textElement=mainDocument.createElement("TEXT");
    paragraphElementOut.appendChild(textElement);
    //TQDomElement formatsPluralElementOut=mainDocument.createElement("FORMATS");
    //paragraphElementOut.appendChild(formatsPluralElementOut);
    TQDomElement layoutElement=mainDocument.createElement("LAYOUT");
    paragraphElementOut.appendChild(layoutElement);

    TQDomElement element;
    element=mainDocument.createElement("NAME");
    element.setAttribute("value",name);
    layoutElement.appendChild(element);

    double size;
    element=mainDocument.createElement("INDENTS");
    size = firstindent-secondindent;
    size *= ptsperchar;  // convert indent spaces to points
    element.setAttribute("first",TQString::number(size));
    size = secondindent;
    size *= ptsperchar;  // convert indent spaces to points
    element.setAttribute("left",TQString::number(size));
    element.setAttribute("right",0);

    textElement.appendChild(mainDocument.createTextNode(text));
    textElement.normalize(); // Put text together (not sure if needed)

#if 0
    // If the paragraph is indented, write out indentation elements.
    // TODO: why not always write identation?
    if (firstindent > 0 || secondindent > 0)         \
             WriteOutIndents( firstindent, secondindent, str);

    // If this is a bullet or a dash list, write out a COUNTER element
    if(type.isEmpty())
       {
       str += "<COUNTER type=\"";
       str += type;  // "6" = bullet "7" = dash
       str += "\" depth=\"0\" start=\"1\" numberingtype=\"0\" lefttext=\"\" righttext=\".\" />\n";
       }

    str += "<FORMAT>\n";
    // for now we try with style (TODO: verify that KWord 1.2 works correctly)
    //str += "<FONT name=\"times\"/>\n";
    str += "</FORMAT>\n";
    str += "</LAYOUT>\n";
    str += "</PARAGRAPH>\n";
#endif
}  // WriteOutParagraph

   /* The Indent method determines the equivalent number of spaces
      at the beginning of a line   */

int ASCIIImport::Indent(const TQString& line) const
{

    TQChar c;  // for reading string a character at a time
    int count=0;  // amount of white space at the begining of the line

    for( uint i = 0; i < line.length(); i++ )
    {
         c = line.at(i);
         if( c == TQChar(' '))
            count++;
         else if( c == TQChar('\t'))
            count += spacespertab;
         else
            break;
    }

   return count;

}  // Indent

    /* The WriteOutTableCell method writes out a single table cell
        in Kword XML to the output string str. The table is sized according
       to the spacing in the ascii document.
       Arguments
         int table_no - used to keep the group number count
         int row - row number
         int col - column number
         Position *pos - pointer to the table position
         TQString str - the output string.

       Returns - void
        */

#if 0
   void ASCIIImport::WriteOutTableCell( int table_no, int row,
                         int col, Position *pos, TQString &str)
   {

   str += "<FRAMESET frameType=\"1\" frameInfo=\"0\" grpMgr=\"grpmgr_";

   TQString buf = TQString::number(table_no); // convert int to string
   str += buf;

   str += "\" row=\"";
   buf = TQString::number(row); // convert row to string
   str += buf;

   str += "\" col=\"";
   buf = TQString::number(col); // convert col to string
   str += buf;

   str += "\" rows=\"1\" cols=\"1\" removable=\"0\" visible=\"1\" name=\"Cell ";
   buf = TQString::number(row); // convert row to string
   str += buf;
   str += ",";
   buf = TQString::number(col); // convert col to string
   str += buf;

   str += "\">\n";

   str += "<FRAME left=\"";
   buf = TQString::number(pos->left); // convert left coordinate to string
   str += buf;

   str += "\" top=\"";
   buf = TQString::number(pos->top); // convert top coordinate to string
   str += buf;

   str += "\" right=\"";
   buf = TQString::number(pos->right); // convert right coordinate to string
   str += buf;

   str += "\" bottom=\"";
   buf = TQString::number(pos->bottom); // convert bottom coordinate to string
   str += buf;

   str += "\" runaround=\"0\" autoCreateNewFrame=\"0\" newFrameBehavior=\"1\" />\n";

   return;

   }  // WriteOutTableCell




bool ASCIIImport::Table( TQString *Line, int *linecount, int no_lines,
                         int table_no, TQString &tbl, TQString &str )
{
    return false; // this method is disabled

   /* This method examines a paragraph for embedded tables.
      If none are found, it returns. If tables are found then it
      writes out the paragraph including the tables. It does not
      check for embedded bullet or dash lists which is a seperate
      method.
      Arguments
         TQString *Line - pointer to the array of text lines in the paragraph.
         int *linecount - pointer to the line counter used to position tables
         int no_lines - the number of lines in the paragraph
         int *table_no - pointer to the table number counter
         TQString str  - the output string
      Returns - enum value indicating wheterer a table was processed. */

      enum LiType{paragraph, table} linetype[MAXLINES];
      struct Tabs tabs[MAXLINES];  // array of structures for tables
      struct Position pos;  // struct to pass cell position
      int width[MAXCOLUMNS]; // array of column widths
      TQString *currentline;  // pointer to the current line
      TQString *beginline;  // pointer to the beginning line
      TQString text;  // text buffer
      TQString text1;  // another text buffer
      TQString No_indents;  // Line with leading white space removed
      int index, index1, index2, index3; // used to define column positions in a line

      int no_cols;  // column counter
      int firstindent;
      int secondindent;
      int i;  // counter
      int j;  // counter
      int k;  // counter
      TQString *firstline;
      firstline = Line;
      int begin = 0;  // beginning line number
      int tabcount = 0;   // counts tab characters

      // check all lines for tabs
      for(i = 0; i < no_lines; i++)
         {
         no_cols = 0;  // inilialize the number of columns
         index = 0;  // initialize the line index

         // check the line for leading tabs and indentation
         if( (tabs[i].indent = Indent( *Line)) > 0)
             {
             // save and remove tabs at beginning of the line
             text = *Line;  // copy
             Line->remove(TQRegExp("^[\\ \t]*"));
             }

         // find column positions and record text fields
         while((index2 = Line->find( TQRegExp("\t"),index)) > index
         || (index3 = MultSpaces( *Line, index)) > index )
           {
           index1 = kMax(index2, index3);
           if( index2 > index3)
           index1 = Line->find( TQRegExp("[^\t]"), index1);
           tabcount++;
           tabs[i].field[no_cols] = Line->mid(index, (index1 - index -1));
           tabs[i].width[no_cols] = index1 - index + spacespertab - 1;

           no_cols++;
           index = index1;
           }  // while( (index1 = ...

         // record last column
         if( no_cols > 0)
            {
            index1 = Line->length() - (index + 1);  // +1 for space at line end
            tabs[i].field[no_cols] = Line->mid(index,index1);
            tabs[i].width[no_cols] = index1;
            }

         if(tabs[i].indent > 0) *Line = text;  // restore original line
         Line++;  // increment the line pointer
         tabs[i].columns = no_cols;
         }  // for(i = 0; ...
         // All lines of the paragraph have been checked for tabs

     // check for a tab in at least one line
     if( tabcount == 0) return false;
     tabcount = 0;

     Line = firstline;  // reset the line pointer

     for(j = 0; j < MAXCOLUMNS; j++) width[j] = 0;
     for( i = 0; i <= no_lines; i++)
        {
        if(tabs[i].columns == 0 )  // indicates no tabs in line
           {
           text += *Line;  // add line of text to the paragraph
           linetype[i] = paragraph;
           }

        else  // this means a tabulated line
           {
           linetype[i] = table;

           // calculate the column widths
           for(j = 0; j <= tabs[i].columns; j++)
              width[j] = kMax(tabs[i].width[j], width[j] );
           }  // else

        if(i > 0)
           {
           if( linetype[i] != linetype[(i - 1)] || i == no_lines) // change of type
              {
              if( linetype[(begin)] == paragraph )  // write out a paragraph
                 {
                 text = "";  // initialize a null string for paragraph text
                 currentline = firstline;
                 currentline += begin;  //starting line
                 beginline = currentline;
                 firstindent = Indent(*currentline);
                 currentline += 1;  // second line
                 if( (i - begin) > 1)
                        secondindent = Indent( *currentline );
                 else secondindent = 0;

                 for( j= begin; j < i; j++)
                    {
                    text += *beginline;  // form text entry
                    beginline++;   // increment pointer
                    }
                 // process the white space to eliminate unwanted spaces
                 TQString text1 = text.simplifyWhiteSpace();
                 WriteOutParagraph( "Standard", "", text1 , firstindent, secondindent, str);
                 *linecount += (i - begin);

                 }  // if( linetype[(i - 1)]

              else       // this implies a table
                 {
                 // Write out a blank line to align text and table
                 WriteOutParagraph( "Standard", "", "" , 0, 0, str);
                 for( j = begin; j < i; j++ )
                    {
                    int row = j - begin;
                    pos.top = (double)((*linecount) + 1) * ptsperline;
                    pos.bottom = pos.top + ptsperline;
                    // initialize position of first col
                    pos.left = leftmargin + (double)tabs[i].indent * ptsperchar;

                    // write out a cell for each column
                    for( k = 0; k <= tabs[j].columns; k++)
                       {

                       text = tabs[j].field[k];
                       // process the white space to eliminate unwanted spaces
                       text1 = text.simplifyWhiteSpace();

                       // calculate position of table cell
                       pos.right = pos.left + (double)width[k] * ptsperchar;

                       WriteOutTableCell( table_no, row, k, &pos, tbl);
                       WriteOutParagraph( "Standard", "", text1 , 0, 0, tbl);
                       tbl += "</FRAMESET>\n";
                       pos.left += ptsperchar * (double)width[k];
                       }  // for( k = 0; ...
                    (*linecount)  += 1;
                    }   // for( j = begin; ...


                 }  // else...
              begin = i;
              }  // if( linetype[i]...

           }  // if(i > 0)
        Line++;
        }  // for( i = 0; ...

   (*linecount) += 1;

   return true;
   } // end of Table()
#endif

   // the following method finds the location of multiple spaces in a string
int ASCIIImport::MultSpaces(const TQString& text, const int index) const
{

    TQChar c;
    TQChar lastchar = 'c'; // previous character - initialize non blank
    bool found = false;
    // kdDebug(30502) << "length = "  << text.length() << endl;
    for (uint i = index; i < text.length(); i++)
    {
        c = text.at(i);
    // kdDebug(30502) << "i = " << i << " found = " << found << " c = " << c << " lastchar = " << lastchar << endl;
        if ( (c != ' ') && found)
            return i;
        else if (c == ' ' && lastchar == ' ')
            found = true;
        lastchar = c;
    }
    return -1;
} // MultSpaces

#if 0
   bool ASCIIImport::ListItem( TQString *Line, int no_lines,
             TQString &str )
      {

   /* This method examines a paragraph for embedded lists.
      If none are found, it returns. If lists are found then it
      writes out the paragraph including the lists.
      Arguments
         TQString *Line - pointer to the array of text lines in the paragraph.
         int *linecount - pointer to the line counter used to position tables
         int no_lines - the number of lines in the paragraph
         TQString &str  - the output string
      Returns - bool value indicating whether a list item was processed. */


      TQString *currentline;  // pointer to the current line
      enum Listtype{none, bullet, dash} listtype[MAXLINES];
      TQString type;  // type of list - bullet or dash
      TQString *firstline;
      int i;  // counter
      int firstindent;
      int secondindent;
      int begin = 0;  // beginning line number
      int listcount = 0;   // counts lines with list items
      firstline = Line;

      // check all lines for list item indicators - or *
      for(i = 0; i < no_lines; i++)
         {
         listtype[i] = none;  // initialize

         // check for a bullet list item
         if( IsListItem( *Line, TQChar('*')))
            {
            listtype[i] = bullet;
            listcount++;
            }
         // check for a dash list item
         if( IsListItem( *Line, TQChar('-')))
            {
            listtype[i] = dash;
            listcount++;
            }

         Line++;  // increment the line pointer
         }  // for(i = 0; ...
         // All lines of the paragraph have been checked for list items

         // check for at least one list
         if( listcount == 0) return false;

         TQString text = "";  // initialize a null string for paragraph text
         Line = firstline;  // reset the line pointer

         for( i = 0; i < no_lines; i++)
            {

            if(i > 0)
               {
               if( listtype[i] != none || Line->isEmpty()) // new list item
                  {
                  if( listtype[begin] == bullet)  // write out a bullet list
                     {
                     type = "6";
                     // delete * at beginning of line
                     text.remove( TQRegExp("^[ \t]*\\* ") );
                     }
                  else if( listtype[begin] == dash) // write out a dash list
                     {
                     type = "7";
                     // delete - at beginning of line
                     text.remove( TQRegExp("^[ \t]*\\- ") );
                     }
                  else if( listtype[begin] == none) // write out a paragraph
                     type = "";

                     currentline = firstline;
                     currentline += begin;  //starting line
                     firstindent = Indent(*currentline);
                     if( (i - begin) > 1)
                        {
                        currentline += 1;  // second line
                        secondindent = Indent( *currentline );
                        }
                     else secondindent = 0;

                     // process the white space to eliminate unwanted spaces
                     TQString text1 = text.simplifyWhiteSpace();
                     WriteOutParagraph( "Standard", type, text1 , firstindent, secondindent, str);

                     begin = i;
                     text = "";  // reset text


                  }  // if( listtype[i] != none


               }  // if(i > 0)

            text += *Line;  // add text to paragraph
            Line++;

            }  // for( i = 0; ...

         // write out the last paragraph
         if( listtype[begin] == bullet)  // write out a bullet list
            {
            type = "6";
            // delete * at beginning of line
            text.replace( TQRegExp("^[ \t]*\\* "), "");
            }
         else if( listtype[begin] == dash) // write out a dash list
            {
            type = "7";
            // delete - at beginning of line
            text.replace( TQRegExp("^[ \t]*\\- "), "");
            }
         else if( listtype[begin] == none) // write out a paragraph
            type = "";

         currentline = firstline;
         currentline += begin;  //starting line
         firstindent = Indent(*currentline);
         if( (i - begin) > 1)
            {
            currentline += 1;  // second line
            secondindent = Indent( *currentline );
            }
         else secondindent = 0;

         // process the white space to eliminate unwanted spaces
         TQString text1 = text.simplifyWhiteSpace();
         WriteOutParagraph( "Standard", type, text1 , firstindent, secondindent, str);



   return true;
   } // end of ListItem()
#endif

/* The IsListItem method checks a paragraph's first line and determines if
   the text appears to be a list item (bullet or dash).

   Arguments
     TQString FirstLine  the first line of text in a paragraph
     TQChar mark         the mark - or * that indicates a list item

   Return
     true if the line appears as a list item, false if not. */

bool ASCIIImport::IsListItem( TQString FirstLine, TQChar mark )
  {

     int i;  // counter
     TQChar c; // to stream into one character at a time



     int k = FirstLine.find(mark);

     if( k < 0) return false;  // list item mark not on line

     // find first non white character on the line
     for( i=0; IsWhiteSpace( c = FirstLine.at((uint)i) ); i++);

     if(i != k ) return false;  // mark is in wrong position

     c = FirstLine.at((uint)++i);

     return IsWhiteSpace(c);  // character after the mark must be a white space

  }  // IsListItem


bool ASCIIImport::IsWhiteSpace(const TQChar& c) const
{
     if ( c == TQChar(' ') || c == TQChar('\t') )
        return true;
     else
        return false;
} // IsWhiteSpace

TQString ASCIIImport::readLine(TQTextStream& textstream, bool& lastCharWasCr)
{
    // We need to read a line, character by character
    // NOTE: we cannot use TQStreamText::readLine,
    //   as it does not know anything about Carriage Returns
    TQString strLine;
    TQChar ch;
    while (!textstream.atEnd())
    {
        textstream >> ch; // Read one character
        if (ch=="\n")
        {
            if (lastCharWasCr)
            {
                // We have a line feed following a Carriage Return
                // As the Carriage Return has already ended the previous line,
                // discard this Line Feed.
                lastCharWasCr=false;
            }
            else
            {
                // We have a normal Line Feed, therefore we end the line
                break;
            }
        }
        else if (ch=="\r")
        {
            // We have a Carriage Return, therefore we end the line
            lastCharWasCr=true;
            break;
        }
        else if (ch==char(12)) // Form Feed
        {
            // Ignore the form feed
            continue;
        }
        else
        {
            strLine+=ch;
            lastCharWasCr=false;
        }
    } // while
    return strLine;
}