~ubuntu-branches/ubuntu/precise/koffice/precise

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
/*
 *  Copyright (c) 2001 Graham Short.  <grahshrt@netscape.net>
 *
 *  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <qpro/common.h>

#include <string.h>

#include <iostream>

#include <qpro/record.h>
#include <qpro/formula.h>

// -----------------------------------------------------------------------

#include <iomanip>
#include <strstream>

void
Charout(ostream& pOut, unsigned char pChar)
{
    pOut << ((pChar < 32) || (pChar > 126) ? '.' : (char)pChar);
}

void
Hexout(ostream& pOut, unsigned char pChar)
{
    pOut << setiosflags(ios::uppercase)
    << setfill('0')
    << setw(2)
    << hex
    << (int)pChar
    << dec;
}

int
Hexout(char* pChar, int pLen)
{
    std::ostrstream*   lOStr = new std::ostrstream;

    while (pLen) {
        int lIdx = 0;

        for (lIdx = 0; lIdx < 16; ++lIdx) {
            if (pLen) {
                Hexout(cerr, *pChar);
                cerr << (lIdx == 8 ? "-" : " ");
                Charout(*lOStr, (unsigned char)*pChar);
                ++pChar;
                --pLen;
            } else {
                cerr << "   ";
            }
        }

        cerr << lOStr->rdbuf() << endl;

        delete lOStr;
        lOStr = new std::ostrstream;
    }

    delete lOStr;
    lOStr = 0;

    return 0;
}

// -----------------------------------------------------------------------

QpRec::QpRec(QpRecType pType)
        : cType(pType)
{
}

QpRec::~QpRec()
{
}

QP_INT16
QpRec::type()
{
    return cType;
}

// -----------------------------------------------------------------------
//
//
//QpCellRef::QpCellRef(QpIStream& pIn)
//{
//   pIn >> cNoteBook >> cColumn >> cPage >> cRow;
//
//QP_DEBUG("CellRef: NoteBook" << cNoteBook << ", col "
//         << cColumn << ", Page " << (int)cPage << ", Row "
//         << cRow << endl
//        );
//}
//
//QpCellRef::~QpCellRef()
//{
//}
//
//QP_UINT8
//QpCellRef::column()
//{
//   return cColumn;
//}
//
//QP_INT16
//QpCellRef::row()
//{
//   return cRow;
//}
//
// -----------------------------------------------------------------------

QpRecCell::QpRecCell(QpRecType pType)
        : QpRec(pType)
        , cAttributes(0)
        , cColumn(0)
        , cPage(0)
        , cRow(0)
        , cCellRef(0)
{
}

QpRecCell::~QpRecCell()
{
    delete [] cCellRef;
    cCellRef = 0;
}


void
QpRecCell::attributes(QP_INT16 pAttributes)
{
    cAttributes = pAttributes;
}

QP_INT16
QpRecCell::attributes()
{
    return cAttributes;
}

void
QpRecCell::column(QP_UINT8 pColumn)
{
    cColumn = pColumn;
}

QP_UINT8
QpRecCell::column()
{
    return cColumn;
}

void
QpRecCell::row(QP_INT16 pRow)
{
    cRow = pRow;
}

QP_INT16
QpRecCell::row()
{
    return cRow;
}

int
QpRecCell::loadCellInfo(QpIStream& pIn)
{
    pIn >> cColumn >> cPage >> cRow >> cAttributes;

    QP_DEBUG(" col " << (unsigned)cColumn << ", Page " << (unsigned)cPage
             << ", Row " << cRow << ", Ref "
             << /*???cellRef()
            <<*/ ", Attr " << cAttributes
            );

    return 6;  // number of bytes consumed
}

//const char*
//QpRecCell::cellRef()
//{
//  if( cCellRef == 0 )
//  {
//     cCellRef = new char[20]; // hardcoded len???
//
/// ??? what value should notebook param be?
//     cellRef( cCellRef, 0, 0, cColumn, cRow ); //hardcoded page no. ?????
//  }
//
//  return cCellRef;
//

void
QpRecCell::cellRef(char* pText, QpTableNames& pTable, QP_INT16 /*pNoteBook*/, QP_UINT8 pPage, QP_UINT8 pColumn, QP_INT16 pRow)
{
//??? cope with relative/absolute references

    std::strstream lOut(pText, 20, ios::out); // ??? ard coded len
    int       lPageRelative = pRow & 0x8000;
    int       lColRelative  = pRow & 0x4000;
    int       lRowRelative  = pRow & 0x2000;
    QP_UINT8  lCol          = (lColRelative ? cColumn + pColumn : pColumn);

    // Sign bit for row is in bit 0x1000, so either set all top bits or lose all top bits
    QP_INT16  lRow = (lRowRelative ? cRow + (pRow & 0x1000 ? pRow | 0xE000 : pRow & 0x1FFF)
                              : pRow & 0x1FFF
                             );

    // Are we referencing a different page ?

    if (lPageRelative && (pPage == 0)) {
        // no - page is zero relative to this one
    } else
        if (pPage != cPage) {
            // yes - not relative & page is a different one

            QP_UINT8 lPage = (lPageRelative ? pPage + cPage : pPage);

            QP_DEBUG("pTable.name((unsigned)lPage) = " <<  pTable.name((unsigned)lPage) << endl);

            lOut << pTable.name((unsigned)lPage) << '!'; // is '!' compat with QPRO???
        }

    if (!lColRelative) {
        lOut << '$';
    }
    if (lCol < 26) {
        lOut << (char)('A' + lCol);
    } else {
        lOut << (char)('A' -1 + lCol / 26)
        << (char)('A' + lCol % 26);
    }

    if (!lRowRelative) {
        lOut << '$';
    }

    lOut << (lRow & 0x1FFF) + 1 << ends;
}

void
QpRecCell::cellRef(char* pText, QpTableNames& pTable, QpIStream& pFormulaRef)
{
    QP_INT16 lNoteBook;
    pFormulaRef >> lNoteBook;

    // block references (eg. A1..A9) have bit 0x1000 set

    if (lNoteBook & 0x1000) {
        QP_UINT8 lFirstColumn;
        QP_UINT8 lFirstPage;
        QP_INT16 lFirstRow;
        QP_UINT8 lLastColumn;
        QP_UINT8 lLastPage;
        QP_INT16 lLastRow;

        pFormulaRef >> lFirstColumn
        >> lFirstPage
        >> lFirstRow
        >> lLastColumn
        >> lLastPage
        >> lLastRow;

        QP_DEBUG("BlockRef: NoteBook " << lNoteBook
                 << ", 1st col " << lFirstColumn
                 << ", 1st page " << (unsigned)lFirstPage
                 << ", 1st row " << lFirstRow
                 << ", last col " << lLastColumn
                 << ", last page " << (unsigned)lLastPage
                 << ", last row " << lLastRow
                 << endl
                );
// ??? next few lines shouldn't just add rows together
        cellRef(pText, pTable, lNoteBook, lFirstPage, lFirstColumn, lFirstRow);
// ?? temp next line      strcat( pText, ".." );
        strcat(pText, ":");
        cellRef(&pText[strlen(pText)], pTable, lNoteBook, lLastPage, lLastColumn, lLastRow);
    } else {
        QP_UINT8 lColumn;
        QP_UINT8 lPage;
        QP_INT16 lRow;

        pFormulaRef >> lColumn >> lPage >> lRow;

        QP_DEBUG("FormulaRef: NoteBook " << lNoteBook << ", Col " << (unsigned)lColumn
                 << ", Page " << (unsigned)lPage << ", Row " << lRow << endl
                );

// ??? sort out what to do about lNotebook
// ??? next few lines shouldn't just add rows together
        cellRef(pText, pTable, lNoteBook, lPage, lColumn, lRow);
    }
}

// -----------------------------------------------------------------------

QpRecBof::QpRecBof(QP_INT16, QpIStream& pIn)
        : QpRec(QpBof)
{
    pIn >> cFileFormat;

    QP_DEBUG("BOF fileformat=" << cFileFormat << endl);
}

QpRecBof::~QpRecBof()
{
}

// -----------------------------------------------------------------------

QpRecEof::QpRecEof(QP_INT16, QpIStream&)
        : QpRec(QpEof)
{
    QP_DEBUG("EOF" << endl);
}

QpRecEof::~QpRecEof()
{
}


// -----------------------------------------------------------------------

QpRecRecalcMode::QpRecRecalcMode(QP_INT16, QpIStream& pIn)
        : QpRec(QpRecalcMode)
{
    QP_INT8 lMode;

    pIn >> lMode;

    cMode = (MODE)(unsigned char) lMode;

    QP_DEBUG("Recalc Mode = "
             << (int)lMode << (cMode == Manual ? " (Manual)"
                               : cMode == Background ? " (Background)"
                               : cMode == Automatic  ? " (Automatic)"
                               : " (Unknown)"
                              )
             << endl
            );
}

QpRecRecalcMode::~QpRecRecalcMode()
{
}


void
QpRecRecalcMode::mode(MODE pMode)
{
    cMode = pMode;
}

QpRecRecalcMode::MODE
QpRecRecalcMode::mode()
{
    return cMode;
}


// -----------------------------------------------------------------------

QpRecRecalcOrder::QpRecRecalcOrder(QP_INT16, QpIStream& pIn)
        : QpRec(QpRecalcOrder)
{
    QP_INT8 lOrder;

    pIn >> lOrder;

    cOrder = (ORDER)(unsigned char) lOrder;

    QP_DEBUG("Recalc Order = "
             << (int)lOrder << (cOrder == Natural ? " (Natural)"
                                : cOrder == Column ? " (Column)"
                                : cOrder == Row  ? " (Row)"
                                : " (Unknown)"
                               )
             << endl
            );
}

QpRecRecalcOrder::~QpRecRecalcOrder()
{
}


void
QpRecRecalcOrder::order(ORDER pOrder)
{
    cOrder = pOrder;
}

QpRecRecalcOrder::ORDER
QpRecRecalcOrder::order()
{
    return cOrder;
}


// -----------------------------------------------------------------------

QpRecEmptyCell::QpRecEmptyCell(QP_INT16, QpIStream& pIn)
        : QpRecCell(QpEmptyCell)
{
    QP_DEBUG("Empty Cell - ");

    loadCellInfo(pIn);

    QP_DEBUG(endl);
}

QpRecEmptyCell::~QpRecEmptyCell()
{
}


// -----------------------------------------------------------------------

QpRecIntegerCell::QpRecIntegerCell(QP_INT16, QpIStream& pIn)
        : QpRecCell(QpIntegerCell)
{
    QP_DEBUG("Integer Cell - ");

    loadCellInfo(pIn);

    pIn >> cInt;

    QP_DEBUG(", Int " << cInt << endl);
}

QpRecIntegerCell::~QpRecIntegerCell()
{
}

QP_INT16
QpRecIntegerCell::integer()
{
    return cInt;
}

// -----------------------------------------------------------------------

QpRecFloatingPointCell::QpRecFloatingPointCell(QP_INT16, QpIStream& pIn)
        : QpRecCell(QpFloatingPointCell)
{
    QP_DEBUG("Float Cell - ");

    loadCellInfo(pIn);

    pIn >> cValue;

    QP_DEBUG(", Value " << cValue << endl);
}

QpRecFloatingPointCell::~QpRecFloatingPointCell()
{
}

QP_INT64
QpRecFloatingPointCell::value()
{
    return cValue;
}

// -----------------------------------------------------------------------

QpRecLabelCell::QpRecLabelCell(QP_INT16 pLen, QpIStream& pIn)
        : QpRecCell(QpLabelCell)
{
    QP_DEBUG("Label Cell - ");
    int lLabelLen = pLen - loadCellInfo(pIn) - 1;

    pIn >> cLabelPrefix;

    cLabel = new char[lLabelLen];

    pIn.read(cLabel, lLabelLen);

    QP_DEBUG(", Prefix " << cLabelPrefix << ", Label " << cLabel << endl);
}

QpRecLabelCell::~QpRecLabelCell()
{
    delete [] cLabel;
    cLabel =  0;
}

char
QpRecLabelCell::labelPrefix()
{
    return cLabelPrefix;
}

const char*
QpRecLabelCell::label()
{
    return cLabel;
}

// -----------------------------------------------------------------------

QpRecFormulaCell::QpRecFormulaCell(QP_INT16 pLen, QpIStream& pIn)
        : QpRecCell(QpFormulaCell)
        , cFormula(0)
{
    QP_DEBUG("Formula Cell - ");

    int lFormulaLen = pLen - loadCellInfo(pIn);

    pIn >> cLastValue;
    lFormulaLen -= 8;

    pIn >> cState;
    lFormulaLen -= 2;

    pIn >> cLen;
    lFormulaLen -= 2;

    pIn >> cCellRef;
    lFormulaLen -= 2;

    cFormula = new char[lFormulaLen];

    pIn.read(cFormula, lFormulaLen);

    QP_DEBUG(", LastValue " << cLastValue << ", State " << cState << endl);
    QP_DEBUG("   FormulaLen " << cLen << ", CellRef " << cCellRef << ", Formula" << endl);
#ifdef QP_TRACE
    Hexout(cFormula, lFormulaLen);
#endif
    QP_DEBUG(endl);
}

QpRecFormulaCell::~QpRecFormulaCell()
{
    delete [] cFormula;
    cFormula = 0;
}

const char*
QpRecFormulaCell::formula()
{
    return cFormula;
}

QP_INT16
QpRecFormulaCell::formulaLen()
{
    return cLen;
}

QP_INT16
QpRecFormulaCell::formulaReferences()
{
    return cCellRef;
}

// -----------------------------------------------------------------------

QpRecUnknown::QpRecUnknown(QP_INT16 /*pType*/, QP_INT16 pLen, QpIStream& pIn)
        : QpRec(QpUnknown)
{
    QP_DEBUG("Unknown Type " << pType << ", len " << pLen << endl);

    if (pLen > 0) {
        char* lBuf = new char[pLen];

        pIn.read(lBuf, pLen);

        delete [] lBuf;
        lBuf = 0;
    }
}

QpRecUnknown::~QpRecUnknown()
{
}

// -----------------------------------------------------------------------

QpRecBop::QpRecBop(QP_INT16, QpIStream& pIn)
        : QpRec(QpBop)
{
    pIn >> cPageIndex;
    QP_DEBUG("BOP: " << (unsigned)cPageIndex << endl);
}

QpRecBop::~QpRecBop()
{
}


QP_UINT8
QpRecBop::pageIndex()
{
    return cPageIndex;
}


// -----------------------------------------------------------------------

QpRecPageName::QpRecPageName(QP_INT16, QpIStream& pIn)
        : QpRec(QpPageName)
{
    pIn >> cPageName;

    QP_DEBUG("Page Name: " << cPageName << endl);
}

QpRecPageName::~QpRecPageName()
{
    delete [] cPageName;
}

const char*
QpRecPageName::pageName()
{
    return cPageName;
}
// -----------------------------------------------------------------------

QpRecPassword::QpRecPassword(QP_INT16 pLen, QpIStream& pIn)
        : QpRec(QpPassword)
{
    QP_DEBUG("Password len = " << pLen << endl);

    cPassword = new QP_UINT8[pLen];

    pIn.read((char*)cPassword, pLen);

    QP_DEBUG("Password(Hex) = ");
#ifdef QP_TRACE
    Hexout((char*)cPassword, pLen);
#endif
    QP_DEBUG(endl);
}

QpRecPassword::~QpRecPassword()
{
    delete [] cPassword;
    cPassword = 0;
}

const QP_UINT8*
QpRecPassword::password()
{
    return cPassword;
}