summaryrefslogtreecommitdiffstats
path: root/kspread/kspread_numformat.cc
blob: e3a0bee7ab66429191f340fcdd78c0eedac075b5 (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
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
/* This file is part of the KDE project
   Copyright (C) 2003 Norbert Andres, nandres@web.de

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

/*
The only function visible from the outside is formatNumber, whose
implementation is at the very bottom of this file. Its prototype
is declared in kspread_util.h. However, it is not used anywhere.
TODO: Find out whether it is supposed to be used instead of
something else (locale()->formatNumber() maybe?) and either use it
or get rid of it.
Tomas
*/
#include <ctype.h>
#include <math.h>

#include <tqdatetime.h>
#include <tqmap.h>
#include <tqstring.h>

#include <kcalendarsystem.h>
#include <tdelocale.h>

#include "kspread_util.h"
#include "kspread_value.h"

namespace NumFormat_Local
{
  enum { Unknown, TimeDate, Number, Scientific, Fraction } Type;

  TQString g_Monday;
  TQString g_Tuesday;
  TQString g_Wednesday;
  TQString g_Thursday;
  TQString g_Friday;
  TQString g_Saturday;
  TQString g_Sunday;
  TQString g_Mon;
  TQString g_Tue;
  TQString g_Wed;
  TQString g_Thu;
  TQString g_Fri;
  TQString g_Sat;
  TQString g_Sun;
  TQString g_January;
  TQString g_February;
  TQString g_March;
  TQString g_April;
  TQString g_MayL;
  TQString g_June;
  TQString g_July;
  TQString g_August;
  TQString g_September;
  TQString g_October;
  TQString g_November;
  TQString g_December;
  TQString g_Jan;
  TQString g_Feb;
  TQString g_Mar;
  TQString g_Apr;
  TQString g_May;
  TQString g_Jun;
  TQString g_Jul;
  TQString g_Aug;
  TQString g_Sep;
  TQString g_Oct;
  TQString g_Nov;
  TQString g_Dec;

  struct DateTime
  {
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
  };

  struct ConvertionInfo
  {
    DateTime * dt;

    int        rightOpt;
    int        rightReq;
    int        leftReq;
    int        rightSpace;
    int        leftSpace;
    int        upReq;

    int        reqCounter;
    int        reqFirst;
    int        optFirst;

    bool       ampm;

    bool       thSet;
    bool       showMinus;
    bool       negRed;
    bool       negBr;
    TQString    postfix;
    TQString    prefix;
  };

  class BaseFormat
  {
   public: 
    int        type;

    TQString    postfix;
    TQString    prefix;
  };

  class NumberFormat : public BaseFormat
  {
   public:
    bool       thSet;
    bool       showMinus;
    bool       negRed;
    bool       negBr;
    int        rightOpt;
    int        rightReq;
    int        leftReq;
    int        rightSpace;
    int        leftSpace;
  };

  class FractionFormat : public BaseFormat
  {
   public:
    bool       thSet;
    bool       showMinus;
    bool       negRed;
    bool       negBr;
    int        optFirst;
    int        reqFirst;
    int        reqCounter;
    int        fraction;
    int        fractionDigists;
  };

  class DateTimeFormat : public BaseFormat
  {
   public:
    bool       ampm;    
    TQString    format;
  };

  class ScientificFormat : public BaseFormat
  {
   public:
    bool       thSet;
    int        leftReq;
    int        rightReq;
    int        rightOpt;
    int        upReq;
    bool       showMinus;
    bool       negRed;
    bool       negBr;
    int        rightSpace;
    int        leftSpace;
  };

  class FormatStore
  {
   public:

    int  getType( TQString const & format, BaseFormat * f ) const
    {
      FormatMap::const_iterator iter = m_formats.find( format );
      if ( iter == m_formats.end() )
      {
        f = 0;
        return -1;
      }

      f = iter.data();
      return f->type;
    }

    void addFraction( TQString const & format, FractionFormat * f )
    {
      m_formats.insert( format, f );
    }

    void addNumber( TQString const & format, NumberFormat * n )
    {
      m_formats.insert( format, n );
    }

    void addDateTime( TQString const & format, DateTimeFormat * d )
    {
      m_formats.insert( format, d );
    }

    void addScientific( TQString const & format, ScientificFormat * d )
    {
      m_formats.insert( format, d );
    }

   private:
    class FormatMap : public TQMap<TQString, BaseFormat *> {};
    FormatMap    m_formats;
  };

  TQChar          g_dcSymbol;
  TQChar          g_thSymbol;
  TQChar          g_posSymbol;
  TQChar          g_negSymbol;
  DateTime       g_dateTime;
  ConvertionInfo g_convertionInfo;
  bool           g_init = false;

  FormatStore    g_formatStore;
}

using namespace NumFormat_Local;
using namespace KSpread;

void resetGlobals()
{
  g_convertionInfo.dt = 0;
  g_convertionInfo.thSet = false;
  g_convertionInfo.showMinus = true;
  g_convertionInfo.negRed = false;
  g_convertionInfo.negBr = false;
  g_convertionInfo.reqCounter = 0;
  g_convertionInfo.reqFirst = 0;
  g_convertionInfo.prefix = "";
  g_convertionInfo.postfix = "";
  g_convertionInfo.rightOpt = 0;
  g_convertionInfo.rightReq = 0;
  g_convertionInfo.leftReq = 0;
  g_convertionInfo.rightSpace = 0;
  g_convertionInfo.leftSpace = 0;
  g_convertionInfo.optFirst = 0;
  g_convertionInfo.upReq = 0;
  g_convertionInfo.ampm = false;
}

void initGlobals( TDELocale const * const locale )
{
  g_Monday    = locale->calendar()->weekDayName( 1, false );
  g_Tuesday   = locale->calendar()->weekDayName( 2, false );
  g_Wednesday = locale->calendar()->weekDayName( 3, false );
  g_Thursday  = locale->calendar()->weekDayName( 4, false );
  g_Friday    = locale->calendar()->weekDayName( 5, false );
  g_Saturday  = locale->calendar()->weekDayName( 6, false );
  g_Sunday    = locale->calendar()->weekDayName( 7, false );
  g_Mon       = locale->calendar()->weekDayName( 1, true );
  g_Tue       = locale->calendar()->weekDayName( 2, true );
  g_Wed       = locale->calendar()->weekDayName( 3, true );
  g_Thu       = locale->calendar()->weekDayName( 4, true );
  g_Fri       = locale->calendar()->weekDayName( 5, true );
  g_Sat       = locale->calendar()->weekDayName( 6, true );
  g_Sun       = locale->calendar()->weekDayName( 7, true );
  g_January   = locale->calendar()->monthName(  1, 2005, false );
  g_February  = locale->calendar()->monthName(  2, 2005, false );
  g_March     = locale->calendar()->monthName(  3, 2005, false );
  g_April     = locale->calendar()->monthName(  4, 2005, false );
  g_MayL      = locale->calendar()->monthName(  5, 2005, false );
  g_June      = locale->calendar()->monthName(  6, 2005, false );
  g_July      = locale->calendar()->monthName(  7, 2005, false );
  g_August    = locale->calendar()->monthName(  8, 2005, false );
  g_September = locale->calendar()->monthName(  9, 2005, false );
  g_October   = locale->calendar()->monthName( 10, 2005, false );
  g_November  = locale->calendar()->monthName( 11, 2005, false );
  g_December  = locale->calendar()->monthName( 12, 2005, false );
  g_Jan       = locale->calendar()->monthName(  1, 2005, true );
  g_Feb       = locale->calendar()->monthName(  2, 2005, true );
  g_Mar       = locale->calendar()->monthName(  3, 2005, true );
  g_Apr       = locale->calendar()->monthName(  4, 2005, true );
  g_May       = locale->calendar()->monthName(  5, 2005, true );
  g_Jun       = locale->calendar()->monthName(  6, 2005, true );
  g_Jul       = locale->calendar()->monthName(  7, 2005, true );
  g_Aug       = locale->calendar()->monthName(  8, 2005, true );
  g_Sep       = locale->calendar()->monthName(  9, 2005, true );
  g_Oct       = locale->calendar()->monthName( 10, 2005, true );
  g_Nov       = locale->calendar()->monthName( 11, 2005, true );
  g_Dec       = locale->calendar()->monthName( 12, 2005, true );

  g_dcSymbol  = locale->decimalSymbol()[0];
  g_thSymbol  = locale->thousandsSeparator()[0];
  g_posSymbol = locale->positiveSign()[0];
  g_negSymbol = locale->negativeSign()[0];

  g_init = true;
}

void convertDateTime( Value const & value )
{
  TQDateTime dt( value.asDateTime() );
  TQDate d( dt.date() );
  TQTime t( dt.time() );

  g_dateTime.year   = d.year();
  g_dateTime.month  = d.month();
  g_dateTime.day    = d.day();
  g_dateTime.hour   = t.hour();
  g_dateTime.minute = t.minute();
  g_dateTime.second = t.second();

  g_convertionInfo.dt = &g_dateTime;
}

void parseNegativePart( TQString & format, int i, 
                        int l, bool acceptDigits )
{
  g_convertionInfo.showMinus = false;
  g_convertionInfo.negRed    = false;
  g_convertionInfo.negRed    = false;
  bool end = false;

  while ( i < l && !end)
  {
    TQChar c( format[i] );
    switch( c )
    {
     case '-':
      g_convertionInfo.showMinus = true;
      break;
     case '(':
      g_convertionInfo.negBr = true;
      break;
     case '[':
      if ( format.find( "[red]", i, false ) == i )
      {
        g_convertionInfo.negRed = true;
        i += 5;
      }
      break;
     default:
      end = true;
    }
    ++i;
  }

  // find postfix
  bool quote = false;
  for ( int j = l - 1; j > i; --j )
  {
    if ( format[j] == '"' )
    {
      quote = !quote;
      continue;
    }

    if ( !quote && ( format[j] == '0' || format[j] != '?' 
                     || format[j] != '#' 
                     || ( format[j].isDigit() && acceptDigits ) ) )
    {
      g_convertionInfo.postfix = format.mid( j + 1 );
      format.remove( (unsigned int) (j + 1), (unsigned int) (l - j) );
      break;
    }
  }

  int p = g_convertionInfo.postfix.find( '"' );
  while ( p != -1 )
  {
    g_convertionInfo.postfix.remove( p, 1 );

    p = g_convertionInfo.postfix.find( '"', p );
  }
}

void createNumberStruct( BaseFormat * data, TQString const & format, bool insert )
{
  NumberFormat * d = new NumberFormat();
  d->type       = Number;
  d->prefix     = g_convertionInfo.prefix;
  d->postfix    = g_convertionInfo.postfix;
  d->thSet      = g_convertionInfo.thSet;
  d->showMinus  = g_convertionInfo.showMinus;
  d->negRed     = g_convertionInfo.negRed;
  d->negBr      = g_convertionInfo.negBr;
  d->rightOpt   = g_convertionInfo.rightOpt;
  d->rightReq   = g_convertionInfo.rightReq;
  d->leftReq    = g_convertionInfo.leftReq;
  d->rightSpace = g_convertionInfo.rightSpace;
  d->leftSpace  = g_convertionInfo.leftSpace;

  if ( insert )
    g_formatStore.addNumber( format, d );
  data = d;
}

void createDateTimeStruct( BaseFormat * data, TQString const & format, 
                           TQString const & optFormat, bool insert )
{
  DateTimeFormat * d = new DateTimeFormat();
  d->type       = TimeDate;
  d->prefix     = g_convertionInfo.prefix;
  d->postfix    = g_convertionInfo.postfix;
  d->ampm       = g_convertionInfo.ampm;
  d->format     = optFormat;

  if ( insert )
    g_formatStore.addDateTime( format, d );
  data = d;
}

void createScientificStruct( BaseFormat * data, TQString const & format, bool insert )
{
  ScientificFormat * d = new ScientificFormat();
  d->type       = Scientific;
  d->prefix     = g_convertionInfo.prefix;
  d->postfix    = g_convertionInfo.postfix;
  d->thSet      = g_convertionInfo.thSet;
  d->showMinus  = g_convertionInfo.showMinus;
  d->negRed     = g_convertionInfo.negRed;
  d->negBr      = g_convertionInfo.negBr;
  d->rightOpt   = g_convertionInfo.rightOpt;
  d->rightReq   = g_convertionInfo.rightReq;
  d->leftReq    = g_convertionInfo.leftReq;
  d->rightSpace = g_convertionInfo.rightSpace;
  d->leftSpace  = g_convertionInfo.leftSpace;
  d->upReq      = g_convertionInfo.upReq;

  if ( insert )
    g_formatStore.addScientific( format, d );
  data = d;
}


int doPreScan( TQString & format, TQString const & formatBack, TDELocale const * const /* locale */,
               bool insert, BaseFormat * data )
{  
  int type = g_formatStore.getType( format, data );
  if ( data != 0 )
    return type;

  resetGlobals();

  int l = format.length();
  int i = 0;
  int thFound = false;
  int leftReq  = 0;
  int leftOpt  = 0;
  int rightOpt = 0;
  int spaceInNum = -1;
  bool dcSeen = false;
  bool endFixed = false;

  FractionFormat * df = 0;
  int f   = 0;
  int d   = 0;
  int len = 0;
  int n   = 0;
  bool ok = false;
  TQString frac;

  while ( i < l )
  {
    TQString s;
    if ( endFixed )
    {
      g_convertionInfo.postfix += format.mid( i );
      format.remove( i, l - i );
      break;
    }
    TQChar ch( format[i] );
    switch( ch )
    {
     case '[':
      if ( type == Number )
        endFixed = true;

      if ( format[ i + 1] == '$' )
      {
        i += 2;
        bool found = false;
        while ( i < l && format[i] != ']' )
        {
          if ( format[i] == '-' )
            found = true;
          if ( !found )
          {
            if ( !endFixed )
              g_convertionInfo.prefix += format[i];
            else
              g_convertionInfo.postfix += format[i];
            format.remove( i, 1 );
            --i; --l;
          }
          ++i;
        }
      }
      else
      {
        if ( i + 1 >= l )  
        {
          g_convertionInfo.postfix += '[';
          format.remove( i, 1 );
          --l; --i;
        }
        else
          if ( ( format[ i + 1].lower() != 's' )
               && ( format[ i + 1].lower() != 'm' )
               && ( format[ i + 1].lower() != 'h' ) )
          {
            // strange!

            if ( endFixed )
              g_convertionInfo.postfix += format[i];
            else
              g_convertionInfo.prefix  += format[i];
            format.remove( i, 1 );
            --l; --i;
          }
          else
          {
            type = TimeDate;
            ++i;
            TQChar c( format[i] );
            ++i;
            while ( i < l && format[i] != ']' )
            {
              if ( format[i] != c )
              {
                format.remove( i, 1 );
                --l; --i;
                break;
              }
              ++i;
            }
          }
      }
      break;

     case '¤':
     case '$':
     case '¥':
     case '£':
     case '%':
      if ( type == Number )
        endFixed = true;

      if ( endFixed )
        g_convertionInfo.postfix += format[i];
      else
        g_convertionInfo.prefix  += format[i];
      format.remove( i, 1 );
      --i; --l;
      break;

     case '#':
      type = Number;
      if ( !dcSeen && leftReq > 0 )
      { // 00##.00 <= remove the '#'
        format.remove( i, 1 );
        --l; --i;
      }
      if ( !dcSeen )
        ++leftOpt;
      else
        ++g_convertionInfo.rightOpt;
      break;

     case '0':
      if ( spaceInNum > 0 )
      { // for fractions
        ++g_convertionInfo.reqCounter;
        break;
      }
      type = Number;
      if ( !dcSeen && rightOpt > 0 )
      { // 00##.##00 <= remove the '0'
        format.remove( i, 1 );
        --l; --i;
      }
      if ( !dcSeen )
        ++g_convertionInfo.leftReq;
      else
        ++g_convertionInfo.rightReq;
      break;

     case '/':
      if ( ( i + 1 < l ) && ( format[i + 1] == ' ' ) )
        ++i;
      while ( i < l )
      {
        if ( format[i] != '?' && !format[i].isDigit() && format[i] != '#' )
        {
          g_convertionInfo.postfix = format.mid(i);
          format.remove( i, l - i );
          break;
        }
        else 
        {
          ++d;
          frac += format[i];
        }
        ++i;
      }
      if ( i < l )
      {
        if ( format[i] == ';' )
        {
          ++i;
          parseNegativePart( format, i, l, true );
        }
        else
          if ( i + 3 < l )
          {
            if ( ( format[i + 1] == ')' ) && ( format[i + 2] == ';' ) )
            {
              i += 3;
              parseNegativePart( format, i, l, true );
            }
          }
      }      

      ok = false;
      f = frac.toInt( &ok );

      df = new FractionFormat();
      if ( ok )
        df->fraction = f;
      else
        df->fraction = -1;
      df->type       = Fraction;
      df->thSet      = g_convertionInfo.thSet;
      df->showMinus  = g_convertionInfo.showMinus;
      df->negRed     = g_convertionInfo.negRed;
      df->negBr      = g_convertionInfo.negBr;
      df->fractionDigists = d;
      df->reqCounter = g_convertionInfo.reqCounter;
      df->reqFirst   = g_convertionInfo.reqFirst;
      df->prefix     = g_convertionInfo.prefix;
      df->postfix    = g_convertionInfo.postfix;

      if ( insert )
        g_formatStore.addFraction( formatBack, df );
      data = df;

      return Fraction;
      break;

     case ',':
      if ( type == Unknown )
      {
        g_convertionInfo.prefix += ',';
      }      
      else if ( type == Number )
      {
        if ( dcSeen )
        {
          g_convertionInfo.postfix += ',';
          format.remove( i, 1 );
          --i; --l;
        }
        else
        {
          if ( thFound )
          {
            format.remove( i, 1 );
            --l; --i;
          }
          else
            thFound = true;
        }
      }

     case '.': // decimal point
      if ( type == Unknown )
      {
        int j = i + 1;
        if ( ( j < l ) 
             && ( format[j] == '0' || format[j] == '#' ) )
        {
          type = Number;
          dcSeen = true;
        }
        else
        {
          if ( j == l )
            g_convertionInfo.postfix += '.';
          else
            g_convertionInfo.prefix += '.';
          format.remove( i, 1 );
          --i; --l;
        }
      }
      else if ( type == Number )
      {
        dcSeen = true;
      }
      break;

     case '*':
      break;
      
     case '"':
      n = i;
      ++i;
      while ( i < l && format[i] != '"' )
      {
        s += format[i];
        ++i;
      }
      if ( type == Unknown )
        g_convertionInfo.prefix += s;
      else 
      {
        g_convertionInfo.postfix += s;
      }
      len = s.length();
      format.remove( i, len );
      i -= len; l -= len;
      break;

     case '_':
      if ( type == Number )
      {
        bool pr = false;
        if ( i + 3 < l )
        {
          if ( ( format[i + 1] != ')' ) || ( format[i + 2] != ';' ) )
            pr = true;
          else
          {
            i += 3;
            parseNegativePart( format, i, l, false );

            createNumberStruct( data, formatBack, insert );

            return Number;
          }
        }

        if ( pr )
        {
          g_convertionInfo.postfix += format.mid( i );
          format.remove( i, l - i );

          createNumberStruct( data, formatBack, insert );

          return Number;
        }
      }    
      break;

     case ';':
      if ( type == Unknown )
      {
        g_convertionInfo.postfix += ';';
        format.remove( i, 1 );
        --i; --l;
      }
      else
      {
        if ( type == Number )
        {
          ++i;
          parseNegativePart( format, i, l, false );

          createNumberStruct( data, formatBack, insert );

          return Number;
        }
        else
          if ( type == Scientific )
          {
            ++i;
            parseNegativePart( format, i, l, false );

            createScientificStruct( data, formatBack, insert );

            return Scientific;
          }
      }

     case ' ':
      if ( type == Number )
      {
        g_convertionInfo.optFirst = (leftOpt > 0 ? leftOpt : 0);
        g_convertionInfo.reqFirst = (leftReq > 0 ? leftReq : 0);

        spaceInNum = i;
        g_convertionInfo.postfix += ' ';
      }
      else if ( type == Unknown )
      {
        g_convertionInfo.prefix += ' ';
        format.remove( i, 1 );
        --i; --l;
      }      
      break;

     case 'A':
     case 'a':
      if ( type == TimeDate || type == Unknown )
      {
        if ( ( i + 1 < l ) && ( format[i + 1].lower() == 'm' ) )
        {
          g_convertionInfo.ampm = true;
          ++i;
          if ( ( i + 3 < l ) && ( format[i + 1] == '/' )
               && ( format[i + 2].lower() == 'p' ) 
               && ( format[i + 3].lower() == 'm' ) )
          {
            i += 3;
          }
        }
        else if ( type == Unknown )
        {
          g_convertionInfo.prefix += format[i];
          format.remove( i, 1 );
          --i; --l;
        }
      }
      else 
      {
        if ( !endFixed )
          endFixed = true;
        g_convertionInfo.postfix += format[i];
        format.remove( i, 1 );
        --i; --l;
      }
      break;

     case 'P':
     case 'p':
      if ( type == TimeDate || type == Unknown )
      {
        if ( ( i + 1 < l ) && ( format[i + 1].lower() == 'm' ) )
        {
          g_convertionInfo.ampm = true;
          i += 1;
        }
        else if ( type == Unknown )
        {
          g_convertionInfo.prefix += format[i];
          format.remove( i, 1 );
          --i; --l;
        }
      }
      else
      {
        if ( !endFixed )
          endFixed = true;
        g_convertionInfo.postfix += format[i];
        format.remove( i, 1 );
        --i; --l;
      }
      break;

     case 'M':
     case 'm':
      if ( type == Unknown )
        type = TimeDate;
      else if ( type != TimeDate )
        endFixed = true;        
      break;

     case 'S':
     case 's':
     case 'H':
     case 'h':
      if ( type != Unknown && type != TimeDate )
        endFixed = true;        
      else
        type = TimeDate;
      break;

     case 'D':
     case 'd':
     case 'Y':
     case 'y':
      if ( type != Unknown && type != TimeDate )
        endFixed = true;        
      else
        type = TimeDate;
      break;

     default:
      if ( type == Unknown )
      {
        g_convertionInfo.prefix += format[i];
        format.remove( i, 1 );
        --i; --l;
      }
      else if ( type == Number || type == Scientific 
                || type == Fraction )
      {
        endFixed = true;
        g_convertionInfo.postfix += format[i];
        format.remove( i, 1 );
        --l; --i;      
      }
    }

    ++i;
  }

  if ( type == Number )
    createNumberStruct( data, formatBack, insert );
  else if ( type == TimeDate )
    createDateTimeStruct( data, formatBack, format, insert );
  else if ( type == Scientific )
    createScientificStruct( data, formatBack, insert );

  return type;
}

void createNumber( TQString & result, Value const & value, 
                   TQString const & /*format*/, bool & setRed,
                   NumberFormat const * const data )
{
  int    prec = data->rightReq + data->rightOpt;
  double num  = value.asFloat();
  double m[]  = { 1, 10, 100, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10 };
  double mm   = ( prec > 10 ) ? pow( 10.0, prec ) : m[prec];

  num = floor( fabs( num ) * mm + 0.5 ) / mm;

  bool negative = ( num < 0 ? true : false );
  double nnum = ( negative ? -num : num );

  result = TQString::number( nnum, 'f', prec );

  int pos = result.find( '.' );
  if ( pos >= 0 )
  {
    result = result.replace( pos, 1, g_dcSymbol );

    // remove '0' from the end if not required
    if ( data->rightOpt > 0 )
    {      
      int i = result.length() - 1; // index
      int n = result.length() - data->rightOpt;

      for ( ; i > n; --i )
      {
        if ( result[i] != '0' )
          break;
      }
      result = result.left( i + 1 ); //length

      if ( i == pos ) // just decimal point
        result = result.remove( i, 1 );
    }

    // prepend '0' if wanted
    while ( data->leftReq > pos )
    {
      result.prepend( '0' );
      ++pos;
    }

    // put in thousand symbol if wanted
    if ( data->thSet && pos > 3 )
    {
      int l = pos - 3;
      while ( 0 < l )
      {
        result.insert( l, g_thSymbol );
        l -= 3;
      }
    }
  }

  if ( data->leftSpace > 0 )
  {
    for ( int i = 0; i < data->leftSpace; ++i )
      result.prepend( ' ' );
  }
  if ( data->rightSpace > 0 )
  {
    for ( int i = 0; i < data->rightSpace; ++i )
      result.append( ' ' );
  }

  if ( negative )
  { 
    if ( data->showMinus )
      result.prepend( g_negSymbol );

    if ( data->negBr )
    {
      result.prepend( '(' );
      result.append( ')' );
    }

    if ( data->negRed )
      setRed = true;
  }

  result.prepend( data->prefix );
  result.append( data->postfix );
}

void createFraction( TQString & result, Value const & value, 
                     TQString const & /*format*/,  bool & setRed,
                     FractionFormat const * const data )
{
  double num = value.asFloat();

  bool negative = ( num < 0 ? true : false );

  double fnum = floor( negative ? -num : num );
  
  double dec = num - fnum;
  double fraction;
  int index = 0;

  if ( data->fraction <= 0 )
  {
    // #,### ?/???
    double nnum = ( negative ? -num : num );
    double precision, denominator, numerator;
    int index = 2 + data->fractionDigists;
    int limit = 9;
    if ( data->fractionDigists == 2 )
      limit += 90;
    if ( data->fractionDigists >= 3 )
      limit += 990;

    do 
    {
      double val1   = nnum;
      double val2   = rint( nnum );
      double inter2 = 1;
      double inter4, p,  q;
      inter4 = p = q = 0.0;

      precision   = pow( 10.0, - index );
      numerator   = val2;
      denominator = 1;

      while ( fabs( numerator / denominator - nnum ) > precision ) 
      {
        val1 = (1 / ( val1 - val2 ) );
        val2 = rint( val1 );
        p = val2 * numerator + inter2;
        q = val2 * denominator + inter4;
        inter2 = numerator;
        inter4 = denominator;

        numerator = p;
        denominator = q;
      }
      --index;
    } while ( fabs( denominator ) > limit );

    index    = (int) fabs( numerator );
    fraction = (int) fabs( denominator );
  }
  else
  {
    // # #/4
    fraction = data->fraction;

    double calc = 0.0;
    double diff = dec;
    double d;
    for ( int i = 1; i <= fraction; ++i ) 
    {
      calc = i * 1.0 / index;
      d = fabs( dec - calc );
      if ( d < diff ) 
      {
        index = i;
        diff = d;
      }
    }
  }
   
  // ? index/fraction

  // 2.25:  #/4 => 9/4
  if ( data->optFirst == 0 && data->reqFirst == 0 && fnum > 0 )
    index += (int) (fnum * fraction);
  
  TQString frac;
  TQString left;
  if ( index > 0 )
  {
    TQString numerator;
    TQString denominator;
    
    numerator = TQString::number( index );
    int n = numerator.length() - data->reqCounter;
    for ( int i = 0; i < n; ++i )
    {
      numerator.prepend( '0' );
    }
    
    denominator = TQString::number( fraction );
    frac = numerator + '/' + denominator;
  }
  
  if ( data->optFirst > 0 || data->reqFirst > 0 )
  {
    if ( fnum == 0 && data->reqFirst > 0 )
    {
      for ( int i = 0; i < data->reqFirst; ++i )
        left += '0';
    }
    else if ( fnum > 0 )
    {
      left = TQString::number( fnum );
      int n = data->reqFirst - left.length();
      if ( n > 0 )
      {
        for ( int i = 0; i < n; ++i )
        {
          left.prepend( '0' );
        }
      }
    }
  }
  
  if ( data->thSet )
  {
    int l = left.length() - 3;
    while ( 0 < l )
    {
      left.insert( l, g_thSymbol );
      l -= 3;
    }
  }

  left = left + ' ' + frac;

  if ( negative )
  {
    if ( data->showMinus )
      left.prepend( g_negSymbol );

    if ( data->negBr )
    {
      left.prepend( '(' );
      left.append( ')' );
    }

    if ( data->negRed )
      setRed = true;
  }

  result = left;
}

void createScientific( TQString & result, Value const & value, 
                       TQString const & /*format*/, bool & setRed,
                       ScientificFormat const * const data )
{
  double num = value.asFloat();

  bool negative   = ( num < 0 ? true : false );
  double nnum     = ( negative ? -num : num );

  result = TQString::number( nnum, 'E', data->rightReq + data->rightOpt );
  
  int pos = result.find( '.' );
  if ( pos >= 0 )
  {
    result = result.replace( pos, 1, g_dcSymbol );
    if ( data->rightOpt > 0 )
    {      
      int i   = result.find( 'E', pos, false ) - 1;
      int n   = result.length() - data->rightOpt;

      if ( i > 0 )
      {
        int rem = 0;
        for ( ; i > n; --i )
        {
          if ( result[i] != '0' )
            break;
          else
            ++rem;
        }
        result = result.remove( i + 1, rem ); 
      }
    }

    while ( data->leftReq > pos )
    {
      result.prepend( '0' );
      ++pos;
    }

    if ( data->thSet && pos > 3 )
    {
      int l = pos - 3;
      while ( 0 < l )
      {
        result.insert( l, g_thSymbol );
        l -= 3;
      }
    }
  }

  if ( negative )
  { 
    if ( data->showMinus )
      result.prepend( g_negSymbol );

    if ( data->negBr )
    {
      result.prepend( '(' );
      result.append( ')' );
    }

    if ( data->negRed )
      setRed = true;
  }

  result.prepend( data->prefix );
  result.append( data->postfix );
}

void appendAMPM( TQString & result, Value const & value )
{
  if ( !g_convertionInfo.dt )
    convertDateTime( value );

  int hour = g_convertionInfo.dt->hour;
  if ( hour > 12 )
    result.append( i18n("PM") );
  else
    result.append( i18n("AM") );
}

void appendHour( TQString & result, Value const & value, 
                 int digits, bool elapsed, bool ampm )
{
  if ( !g_convertionInfo.dt )
    convertDateTime( value );
    
  int hour = g_convertionInfo.dt->hour;
  if ( elapsed )
  {
    TQDate d1( g_convertionInfo.dt->year, g_convertionInfo.dt->month, g_convertionInfo.dt->day );
    TQDate d2( 1900, 1, 1 );
    hour += ( d2.daysTo( d1 ) * 24 );
  }
  if ( hour < 10 && digits == 2 )
    result += '0';
  else
    if ( hour > 12 && ampm )
    {
      hour -= 12;
      if ( digits == 2 && hour < 10 )
        result += '0';
    }

  result += TQString::number( hour );
}

void appendMinutes( TQString & result, Value const & value, 
                    int digits, bool elapsed )
{
  if ( !g_convertionInfo.dt )
    convertDateTime( value );
    
  int minute = g_convertionInfo.dt->minute;
  if ( elapsed )
  {
    TQDate d1( g_convertionInfo.dt->year, g_convertionInfo.dt->month, g_convertionInfo.dt->day );
    TQDate d2( 1900, 1, 1 );
    minute += ( d2.daysTo( d1 ) * 24 * 60 );
  }
  if ( minute < 10 && digits == 2 )
    result += '0';

  result += TQString::number( minute );
}

void appendSecond( TQString & result, Value const & value, 
                   int digits, bool elapsed )
{
  if ( !g_convertionInfo.dt )
    convertDateTime( value );
    
  int second = g_convertionInfo.dt->second;
  if ( elapsed )
  {
    TQDate d1( g_convertionInfo.dt->year, g_convertionInfo.dt->month, g_convertionInfo.dt->day );
    TQDate d2( 1900, 1, 1 );
    second += ( d2.daysTo( d1 ) * 24 * 60 * 60 );
  }
  if ( second < 10 && digits == 2 )
    result += '0';

  result += TQString::number( second );
}

void appendYear( TQString & result, Value const & value, 
                 int digits )
{
  if ( !g_convertionInfo.dt )
    convertDateTime( value );

  int year = g_convertionInfo.dt->year;
  if ( digits <= 2 )
    result += TQString::number( year ).right( 2 );
  else
    result += TQString::number( year );
}

void appendMonth( TQString & result, Value const & value, 
                  int digits )
{
  if ( !g_convertionInfo.dt )
    convertDateTime( value );

  int month = g_convertionInfo.dt->month;
  if ( digits == 1 )
    result += TQString::number( month );
  else
    if ( digits == 2 )
    {
      if ( month < 10 )
        result += '0';

      result += TQString::number( month );
    }
    else
    {
      switch ( month )
      {
       case 1:
        result += ( digits != 3 ? g_January : g_Jan );
        break;

       case 2:
        result += ( digits != 3 ? g_February : g_Feb );
        break;

       case 3:
        result += ( digits != 3 ? g_March : g_Mar );
        break;

       case 4:
        result += ( digits != 3 ? g_April : g_Apr );
        break;

       case 5:
        result += ( digits != 3 ? g_MayL : g_May );
        break;

       case 6:
        result += ( digits != 3 ? g_June : g_Jun );
        break;

       case 7:
        result += ( digits != 3 ? g_July : g_Jul );
        break;

       case 8:
        result += ( digits != 3 ? g_August : g_Aug );
        break;

       case 9:
        result += ( digits != 3 ? g_September : g_Sep );
        break;

       case 10:
        result += ( digits != 3 ? g_October : g_Oct );
        break;

       case 11:
        result += ( digits != 3 ? g_November : g_Nov );
        break;

       case 12:
        result += ( digits != 3 ? g_December : g_Dec );
        break;
      }    
    }
}

void appendDays( TQString & result, Value const & value, 
                 int digits )
{
  if ( !g_convertionInfo.dt )
    convertDateTime( value );

  int day = g_convertionInfo.dt->day;
  if ( digits == 1 )
    result += TQString::number( day );
  else
    if ( digits == 2 )
    {
      if ( day < 10 )
        result += '0';

      result += TQString::number( day );
    }    
    else
    {
      TQDate date( g_convertionInfo.dt->year, g_convertionInfo.dt->month, g_convertionInfo.dt->day );
      int weekDay = date.dayOfWeek();
      switch ( weekDay )
      {
       case 1:
        result += ( digits != 3 ? g_Monday : g_Mon );
        break;

       case 2:
        result += ( digits != 3 ? g_Tuesday : g_Tue );
        break;

       case 3:
        result += ( digits != 3 ? g_Wednesday : g_Wed );
        break;

       case 4:
        result += ( digits != 3 ? g_Thursday : g_Thu );
        break;

       case 5:
        result += ( digits != 3 ? g_Friday : g_Fri );
        break;

       case 6:
        result += ( digits != 3 ? g_Saturday : g_Sat );
        break;

       case 7:
        result += ( digits != 3 ? g_Sunday : g_Sun );
        break;
      }
    }
}

void createDateTime( TQString & result, Value const & value, 
                     TQString const & /*format*/, 
                     DateTimeFormat const * const data )
{
  result = data->prefix;
  bool elapsed = false;
  bool elapsedFound = false;
  bool minute = false; // how to interpret 'm'
  int digits = 1;
  int i = 0;
  int l = (int) data->format.length();
  while ( i < l )
  {
    switch( data->format[i].lower() )
    {
     case '"':
      ++i;
      while ( i < l )
      {
        if ( data->format[i] == '"' )
          break;
        else
          result += data->format[i];
      }
      break;

     case '[':
      if ( elapsedFound )
        result += '[';
      else
      {
        elapsed = true;
        elapsedFound = true;
      }
      break;

     case ']':
      if ( elapsed )
        elapsed = false;
      else
        result += ']';
      break;

     case 'h':
      minute = true;
      if ( data->format[i + 1] == 'h' )
      {
        appendHour( result, value, 2, elapsed, data->ampm );
        ++i;
      }
      else
        appendHour( result, value, 1, elapsed, data->ampm );
      break;

     case 'm':
      digits = 1;
      
      while ( data->format[i + 1] == 'm' )
      {
        ++i;
        ++digits;
      }

      if ( minute )
        appendMinutes( result, value, digits, elapsed );
      else
        appendMonth( result, value, digits );
      
      break;

     case 's':
      minute = true;
      if ( data->format[i + 1] == 's' )
      {
        appendSecond( result, value, 2, elapsed );
        ++i;
      }
      else
        appendSecond( result, value, 1, elapsed );
      break;

     case 'd':
      minute = false;
      digits = 1;
      
      while ( data->format[i + 1] == 'd' )
      {
        ++i;
        ++digits;
      }
      appendDays( result, value, digits );
      break;

     case 'y':
      minute = false;
      digits = 1;
      
      while ( data->format[i + 1] == 'y' )
      {
        ++i;
        ++digits;
      }
      appendYear( result, value, digits );
      break;

     case 'a':
     case 'p':
      if ( data->format[i + 1] == 'm' )
      {
        ++i;
        if ( data->format[i + 1] == '/' 
             && data->format[i + 2].lower() == 'p' 
             && data->format[i + 3].lower() == 'm' )
          i += 3;

        appendAMPM( result, value );
      }
      
     default:
      result += data->format[i];
    }

    ++i;
  }

  result += data->postfix;
}

TQString formatNumber( Value const & value, TQString format, bool & setRed,
                      TDELocale const * const locale, bool insert )
{
  // need delocalized strings: dcSymbol: '.', thSymbol = ','
  if ( !g_init )
    initGlobals( locale );

  TQString backup( format );
  TQString result;
  BaseFormat * data = 0;
  setRed = false;

  int t = doPreScan( format, backup, locale, insert, data );

  if ( t == Number )
  {
    createNumber( result, value, format, setRed, (NumberFormat *) data );

    if ( !insert )
      delete (NumberFormat *) data;

    return result;
  }
  else if ( t == Fraction )
  {
    createFraction( result, value, format, setRed, (FractionFormat *) data );

    if ( !insert )
      delete (FractionFormat *) data;

    return result;
  }
  else if ( t == Scientific )
  {
    createScientific( result, value, format, setRed, (ScientificFormat *) data );

    if ( !insert )
      delete (ScientificFormat *) data;

    return result;
  }
  else if ( t == TimeDate )
  {
    createDateTime( result, value, format, (DateTimeFormat *) data );

    if ( !insert )
      delete (DateTimeFormat *) data;

    return result;
  }
  else if ( data != 0 )
  {
    result = data->prefix + data->postfix;

    if ( !insert )
      delete data;

    return result;
  }

  return result;
}