summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-sheet/Excel.cpp
Unidiff
Diffstat (limited to 'noncore/apps/opie-sheet/Excel.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/apps/opie-sheet/Excel.cpp83
1 files changed, 65 insertions, 18 deletions
diff --git a/noncore/apps/opie-sheet/Excel.cpp b/noncore/apps/opie-sheet/Excel.cpp
index fc49d56..57aef20 100644
--- a/noncore/apps/opie-sheet/Excel.cpp
+++ b/noncore/apps/opie-sheet/Excel.cpp
@@ -1,15 +1,43 @@
1/*
2 =. This file is part of the Opie Project
3 .=l. Copyright (C) 2004 Opie Developer Team <opie-devel@handhelds.org>
4 .>+-=
5 _;:, .> :=|. This program is free software; you can
6.> <`_, > . <= redistribute it and/or modify it under
7:`=1 )Y*s>-.-- : the terms of the GNU General Public
8.="- .-=="i, .._ License as published by the Free Software
9 - . .-<_> .<> Foundation; either version 2 of the License,
10 ._= =} : or (at your option) any later version.
11 .%`+i> _;_.
12 .i_,=:_. -<s. This program is distributed in the hope that
13 + . -:. = it will be useful, but WITHOUT ANY WARRANTY;
14 : .. .:, . . . without even the implied warranty of
15 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A
16 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU
17..}^=.= = ; Library General Public License for more
18++= -. .` .: details.
19 : = ...= . :.=-
20 -. .:....=;==+<; You should have received a copy of the GNU
21 -_. . . )=. = Library General Public License along with
22 -- :-=` this library; see the file COPYING.LIB.
23 If not, write to the Free Software Foundation,
24 Inc., 59 Temple Place - Suite 330,
25 Boston, MA 02111-1307, USA.
26
27*/
1 28
29#include "Excel.h"
2 30
31/* STD */
3#include <stdio.h> 32#include <stdio.h>
4#include <stdlib.h> 33#include <stdlib.h>
5#include <math.h> 34#include <math.h>
6#include <time.h> 35#include <time.h>
7#include <sys/types.h> 36#include <sys/types.h>
8#include <strings.h> 37#include <strings.h>
9#include "Excel.h"
10 38
11static xfrecord formatter[] = { 39static xfrecord formatter[] = {
12{ 0xe , DATEFORMAT, "%m/%d/%y"}, 40{ 0xe , DATEFORMAT, "%m/%d/%y"},
13{ 0xf , DATEFORMAT, "%d-%b-%y"}, 41{ 0xf , DATEFORMAT, "%d-%b-%y"},
14{ 0x10, DATEFORMAT, "%d-%b"}, 42{ 0x10, DATEFORMAT, "%d-%b"},
15{ 0x11, DATEFORMAT, "%b-%y"}, 43{ 0x11, DATEFORMAT, "%b-%y"},
@@ -60,13 +88,14 @@ int ExcelBook::Integer4Byte(int b1,int b2,int b3,int b4)
60 int i1 = Integer2Byte(b1, b2); 88 int i1 = Integer2Byte(b1, b2);
61 int i2 = Integer2Byte(b3, b4); 89 int i2 = Integer2Byte(b3, b4);
62 int val = i2 << 16 | i1; 90 int val = i2 << 16 | i1;
63 return val; 91 return val;
64}; 92};
65 93
66int ExcelBook::Integer2ByteFile(FILE *f) { 94int ExcelBook::Integer2ByteFile(FILE *f)
95{
67 int i1, i2; 96 int i1, i2;
68 i1 = fgetc(f); 97 i1 = fgetc(f);
69 i2 = fgetc(f); 98 i2 = fgetc(f);
70 return Integer2Byte(i1,i2); 99 return Integer2Byte(i1,i2);
71}; 100};
72 101
@@ -99,13 +128,14 @@ double ExcelBook::Double4Byte(int b1, int b2, int b3, int b4)
99 printf("Double4Byte: VALUEINT=%f\r\n",value); 128 printf("Double4Byte: VALUEINT=%f\r\n",value);
100 if ( (rk & 0x01) != 0) 129 if ( (rk & 0x01) != 0)
101 { 130 {
102 value /= 100.0; 131 value /= 100.0;
103 }; 132 };
104 return value; 133 return value;
105 }else 134 }
135 else
106 { 136 {
107 137
108 union { double d; unsigned long int b[2]; } dbl_byte; 138 union { double d; unsigned long int b[2]; } dbl_byte;
109 unsigned long int valbits = (rk & 0xfffffffc); 139 unsigned long int valbits = (rk & 0xfffffffc);
110 #if defined(__arm__) && !defined(__vfp__) 140 #if defined(__arm__) && !defined(__vfp__)
111 dbl_byte.b[0]=valbits; 141 dbl_byte.b[0]=valbits;
@@ -129,34 +159,40 @@ double ExcelBook::Double4Byte(int b1, int b2, int b3, int b4)
129void ExcelBook::DetectEndian(void) 159void ExcelBook::DetectEndian(void)
130{ 160{
131 int end; 161 int end;
132 long i = 0x44332211; 162 long i = 0x44332211;
133 unsigned char* a = (unsigned char*) &i; 163 unsigned char* a = (unsigned char*) &i;
134 end = (*a != 0x11); 164 end = (*a != 0x11);
135 if (end == 1) { 165 if (end == 1)
166 {
136 endian = BIG_ENDIAN; 167 endian = BIG_ENDIAN;
137 printf("BIGENDIAN!\r\n"); 168 printf("BIGENDIAN!\r\n");
138 } else { 169 }
170 else
171 {
139 endian = LITTLE_ENDIAN; 172 endian = LITTLE_ENDIAN;
140 printf("LITTLEENDIAN!\r\n"); 173 printf("LITTLEENDIAN!\r\n");
141 } 174 }
142}; 175};
143 176
144double ExcelBook::Double8Byte(int b1, int b2, int b3, int b4, int b5, int b6, int b7, int b8) 177double ExcelBook::Double8Byte(int b1, int b2, int b3, int b4, int b5, int b6, int b7, int b8)
145{ 178{
146 int i; 179 int i;
147 double d; 180 double d;
148 unsigned char *ieee; 181 unsigned char *ieee;
149 ieee = (unsigned char *)&d; 182 ieee = (unsigned char *)&d;
150 for (i = 0; i < 8; i++) ieee[i] = 0; 183 for (i = 0; i < 8; i++) ieee[i] = 0;
151 if (endian == BIG_ENDIAN) { 184 if (endian == BIG_ENDIAN)
185 {
152 ieee[0] = ((int)b8) & 0xff;ieee[1] = ((int)b7) & 0xff; 186 ieee[0] = ((int)b8) & 0xff;ieee[1] = ((int)b7) & 0xff;
153 ieee[2] = ((int)b6) & 0xff;ieee[3] = ((int)b5) & 0xff; 187 ieee[2] = ((int)b6) & 0xff;ieee[3] = ((int)b5) & 0xff;
154 ieee[4] = ((int)b4) & 0xff;ieee[5] = ((int)b3) & 0xff; 188 ieee[4] = ((int)b4) & 0xff;ieee[5] = ((int)b3) & 0xff;
155 ieee[6] = ((int)b2) & 0xff;ieee[7] = ((int)b1) & 0xff; 189 ieee[6] = ((int)b2) & 0xff;ieee[7] = ((int)b1) & 0xff;
156 } else { 190 }
191 else
192 {
157 ieee[0] = ((int)b1) & 0xff;ieee[1] = ((int)b2) & 0xff; 193 ieee[0] = ((int)b1) & 0xff;ieee[1] = ((int)b2) & 0xff;
158 ieee[2] = ((int)b3) & 0xff;ieee[3] = ((int)b4) & 0xff; 194 ieee[2] = ((int)b3) & 0xff;ieee[3] = ((int)b4) & 0xff;
159 ieee[4] = ((int)b5) & 0xff;ieee[5] = ((int)b6) & 0xff; 195 ieee[4] = ((int)b5) & 0xff;ieee[5] = ((int)b6) & 0xff;
160 ieee[6] = ((int)b7) & 0xff;ieee[7] = ((int)b8) & 0xff; 196 ieee[6] = ((int)b7) & 0xff;ieee[7] = ((int)b8) & 0xff;
161 } 197 }
162 return d; 198 return d;
@@ -390,13 +426,14 @@ int ExcelBook::SheetHandleRecord(ExcelSheet* sheet, ExcelBREC* record)
390 case XL_DIMENSION: 426 case XL_DIMENSION:
391 data = GetDataOfBREC(record); 427 data = GetDataOfBREC(record);
392 if (record->length == 10) 428 if (record->length == 10)
393 { 429 {
394 sheet->rows = Integer2Byte(data[2], data[3]); 430 sheet->rows = Integer2Byte(data[2], data[3]);
395 sheet->cols = Integer2Byte(data[6], data[7]); 431 sheet->cols = Integer2Byte(data[6], data[7]);
396 } else 432 }
433 else
397 { 434 {
398 sheet->rows = Integer4Byte(data[4], data[5], data[6], data[7]); 435 sheet->rows = Integer4Byte(data[4], data[5], data[6], data[7]);
399 sheet->cols = Integer2Byte(data[10], data[11]); 436 sheet->cols = Integer2Byte(data[10], data[11]);
400 } 437 }
401 sheet->InitCells(); 438 sheet->InitCells();
402 break; 439 break;
@@ -599,13 +636,14 @@ void ExcelBook::HandleBoundSheet(ExcelBREC* rec)
599 visibility = data[5]; 636 visibility = data[5];
600 length = data[6]; 637 length = data[6];
601 if(data[7]==0) 638 if(data[7]==0)
602 { 639 {
603 //ascii 640 //ascii
604 name=GetASCII(data,pos,length); 641 name=GetASCII(data,pos,length);
605 }else 642 }
643 else
606 { 644 {
607 name=GetUnicode(data,pos,length); 645 name=GetUnicode(data,pos,length);
608 }; 646 };
609 Names.resize(Names.count()+1); 647 Names.resize(Names.count()+1);
610 Names[Names.count()-1]=new QString(name); 648 Names[Names.count()-1]=new QString(name);
611}; 649};
@@ -805,26 +843,29 @@ QString* ExcelBook::CellDataString(ExcelSheet* sh, int row, int col)
805 format = XFRecords[c->xfindex]->format; 843 format = XFRecords[c->xfindex]->format;
806 date = (time_t) ((c->valued - utcOffsetDays) * sInADay); 844 date = (time_t) ((c->valued - utcOffsetDays) * sInADay);
807 tmptr = gmtime(&date); 845 tmptr = gmtime(&date);
808 if (dateformat) 846 if (dateformat)
809 { 847 {
810 strftime(str,1024,dateformat.ascii(),tmptr); 848 strftime(str,1024,dateformat.ascii(),tmptr);
811 } else 849 }
850 else
812 { 851 {
813 strftime(str,1024,format.ascii(),tmptr); 852 strftime(str,1024,format.ascii(),tmptr);
814 }; 853 };
815 } else 854 }
855 else
816 if (XFRecords[c->xfindex]->type == NUMBERFORMAT) 856 if (XFRecords[c->xfindex]->type == NUMBERFORMAT)
817 { 857 {
818 format = XFRecords[c->xfindex]->format; 858 format = XFRecords[c->xfindex]->format;
819 //sprintf(str,format.ascii(),c->valued); 859 //sprintf(str,format.ascii(),c->valued);
820 // the real format is ignored... 860 // the real format is ignored...
821 // because there is more work to be done in the field 861 // because there is more work to be done in the field
822 precision = CellGetPrecision(c->valued); 862 precision = CellGetPrecision(c->valued);
823 sprintf(str,"%.*f",precision,c->valued); 863 sprintf(str,"%.*f",precision,c->valued);
824 }else 864 }
865 else
825 { 866 {
826 precision = CellGetPrecision(c->valued); 867 precision = CellGetPrecision(c->valued);
827 sprintf(str,"%.*f",precision,c->valued); 868 sprintf(str,"%.*f",precision,c->valued);
828 }; 869 };
829 break; 870 break;
830 case CELL_DATE: 871 case CELL_DATE:
@@ -999,17 +1040,19 @@ void ExcelBook::HandleFormula(ExcelSheet* sheet, ExcelBREC* record)
999 data = GetDataOfBREC(record); 1040 data = GetDataOfBREC(record);
1000 row = Integer2Byte(data[0], data[1]); 1041 row = Integer2Byte(data[0], data[1]);
1001 col = Integer2Byte(data[2], data[3]); 1042 col = Integer2Byte(data[2], data[3]);
1002 if (data[6] == 0 && data[12] == -1 && data[13] == -1) 1043 if (data[6] == 0 && data[12] == -1 && data[13] == -1)
1003 { 1044 {
1004 // string 1045 // string
1005 } else 1046 }
1047 else
1006 if (data[6] == 1 && data[12] == -1 && data[13] == -1) 1048 if (data[6] == 1 && data[12] == -1 && data[13] == -1)
1007 { 1049 {
1008 // boolean 1050 // boolean
1009 } else 1051 }
1052 else
1010 if ( data[6] == 2 && data[12] == -1 && data[13] == -1) 1053 if ( data[6] == 2 && data[12] == -1 && data[13] == -1)
1011 { 1054 {
1012 // error 1055 // error
1013 } 1056 }
1014 else 1057 else
1015 { 1058 {
@@ -1120,13 +1163,14 @@ QString ExcelBook::GetFormula(int row, int col, ExcelSheet* sheet, char* data, i
1120 if (fareaststring) 1163 if (fareaststring)
1121 { 1164 {
1122 idx += fareastsize; 1165 idx += fareastsize;
1123 }; 1166 };
1124 s1=QString("""")+s1+QString(""""); 1167 s1=QString("""")+s1+QString("""");
1125 operands.prepend(new QString(s1)); 1168 operands.prepend(new QString(s1));
1126 }else 1169 }
1170 else
1127 { 1171 {
1128 w1=data[idx];idx++; 1172 w1=data[idx];idx++;
1129 s1=GetASCII(data,idx,w1); 1173 s1=GetASCII(data,idx,w1);
1130 s1=QString("""")+s1+QString(""""); 1174 s1=QString("""")+s1+QString("""");
1131 idx=idx+w1; 1175 idx=idx+w1;
1132 operands.prepend(new QString(s1)); 1176 operands.prepend(new QString(s1));
@@ -1139,13 +1183,14 @@ QString ExcelBook::GetFormula(int row, int col, ExcelSheet* sheet, char* data, i
1139 if(Version==8) 1183 if(Version==8)
1140 { 1184 {
1141 w1=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//row1 1185 w1=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//row1
1142 w2=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//row2 1186 w2=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//row2
1143 w3=Integer2Byte(data[idx],data[idx+1]) & 0x00FF;idx=idx+2;//col1 1187 w3=Integer2Byte(data[idx],data[idx+1]) & 0x00FF;idx=idx+2;//col1
1144 w4=Integer2Byte(data[idx],data[idx+1]) & 0x00FF;idx=idx+2;//col2 1188 w4=Integer2Byte(data[idx],data[idx+1]) & 0x00FF;idx=idx+2;//col2
1145 }else 1189 }
1190 else
1146 { 1191 {
1147 w1=Integer2Byte(data[idx],data[idx+1]) & 0x3FFF;idx=idx+2;//row1 1192 w1=Integer2Byte(data[idx],data[idx+1]) & 0x3FFF;idx=idx+2;//row1
1148 w2=Integer2Byte(data[idx],data[idx+1]) & 0x3FFF;idx=idx+2;//row2 1193 w2=Integer2Byte(data[idx],data[idx+1]) & 0x3FFF;idx=idx+2;//row2
1149 w3=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//col1 1194 w3=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//col1
1150 w4=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//col2 1195 w4=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//col2
1151 }; 1196 };
@@ -1158,13 +1203,14 @@ QString ExcelBook::GetFormula(int row, int col, ExcelSheet* sheet, char* data, i
1158 case 0x44: 1203 case 0x44:
1159 case 0x64://ptgRef 1204 case 0x64://ptgRef
1160 if(Version==8) 1205 if(Version==8)
1161 { 1206 {
1162 w1=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//row 1207 w1=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//row
1163 w2=Integer2Byte(data[idx],data[idx+1]) & 0x00FF;idx=idx+2;//col 1208 w2=Integer2Byte(data[idx],data[idx+1]) & 0x00FF;idx=idx+2;//col
1164 }else 1209 }
1210 else
1165 { 1211 {
1166 w1=Integer2Byte(data[idx],data[idx+1]) & 0x3FFF;idx=idx+2;//row 1212 w1=Integer2Byte(data[idx],data[idx+1]) & 0x3FFF;idx=idx+2;//row
1167 w2=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//col 1213 w2=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;//col
1168 }; 1214 };
1169 s1=FindCellName(w1,w2); 1215 s1=FindCellName(w1,w2);
1170 printf("token:ptgRef,ref=%s\r\n",s1.ascii()); 1216 printf("token:ptgRef,ref=%s\r\n",s1.ascii());
@@ -1329,13 +1375,14 @@ QString ExcelBook::GetFormula(int row, int col, ExcelSheet* sheet, char* data, i
1329 case 0x61://ptgFunction 1375 case 0x61://ptgFunction
1330 printf("token:ptgFunction\r\n"); 1376 printf("token:ptgFunction\r\n");
1331 if(token==0x22||token==0x42||token==0x62) 1377 if(token==0x22||token==0x42||token==0x62)
1332 { 1378 {
1333 w2=(int)data[idx];idx++; 1379 w2=(int)data[idx];idx++;
1334 w1=Integer2Byte(data[idx],data[idx+1]);idx=idx+2; 1380 w1=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;
1335 }else 1381 }
1382 else
1336 { 1383 {
1337 w1=Integer2Byte(data[idx],data[idx+1]);idx=idx+2; 1384 w1=Integer2Byte(data[idx],data[idx+1]);idx=idx+2;
1338 }; 1385 };
1339 switch(w1) 1386 switch(w1)
1340 { 1387 {
1341 case 0xf://SIN 1388 case 0xf://SIN