summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-sheet/Excel.cpp
authorerik <erik>2007-01-24 19:46:19 (UTC)
committer erik <erik>2007-01-24 19:46:19 (UTC)
commita017bf21dd89159052f2f7a3fbc043a24956c08c (patch) (side-by-side diff)
tree008be2b62ee5487dc55b8a7c7f043c94268f8362 /noncore/apps/opie-sheet/Excel.cpp
parenta4a7bd22feb060a80e20c81cded43cc24f5cd423 (diff)
downloadopie-a017bf21dd89159052f2f7a3fbc043a24956c08c.zip
opie-a017bf21dd89159052f2f7a3fbc043a24956c08c.tar.gz
opie-a017bf21dd89159052f2f7a3fbc043a24956c08c.tar.bz2
Every file in this commit has a memory leak of some kind or another. I think
all of them are minor and should not effect properly running code. But if I were you I would give libstocks and the stockticker plugin in Today a wide berth. That library is atrocious.
Diffstat (limited to 'noncore/apps/opie-sheet/Excel.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/apps/opie-sheet/Excel.cpp4
1 files changed, 3 insertions, 1 deletions
diff --git a/noncore/apps/opie-sheet/Excel.cpp b/noncore/apps/opie-sheet/Excel.cpp
index 51fe707..27080e9 100644
--- a/noncore/apps/opie-sheet/Excel.cpp
+++ b/noncore/apps/opie-sheet/Excel.cpp
@@ -188,385 +188,387 @@ double ExcelBook::Double8Byte(int b1, int b2, int b3, int b4, int b5, int b6, in
ieee[4] = ((int)b4) & 0xff;ieee[5] = ((int)b3) & 0xff;
ieee[6] = ((int)b2) & 0xff;ieee[7] = ((int)b1) & 0xff;
}
else
{
ieee[0] = ((int)b1) & 0xff;ieee[1] = ((int)b2) & 0xff;
ieee[2] = ((int)b3) & 0xff;ieee[3] = ((int)b4) & 0xff;
ieee[4] = ((int)b5) & 0xff;ieee[5] = ((int)b6) & 0xff;
ieee[6] = ((int)b7) & 0xff;ieee[7] = ((int)b8) & 0xff;
}
return d;
};
bool ExcelBook::OpenFile(char *Filename)
{
printf("Opening excel file!\r\n");
File= fopen(Filename, "r");
Position=0; // first byte index in file
XFRecords.resize(0);
SharedStrings.resize(0);
Names.resize(0);
Sheets.resize(0);
if(File==NULL) return false;
printf("Opened excel file!\r\n");
return true;
};
bool ExcelBook::CloseFile(void)
{
int w1;
for(w1=0;w1<(int)XFRecords.count();w1++)
{
if(XFRecords[w1]!=NULL) {delete XFRecords[w1];XFRecords[w1]=NULL;};
};
for(w1=0;w1<(int)SharedStrings.count();w1++)
{
if(SharedStrings[w1]!=NULL) {delete SharedStrings[w1];SharedStrings[w1]=NULL;};
};
for(w1=0;w1<(int)Names.count();w1++)
{
if(Names[w1]!=NULL) {delete Names[w1];Names[w1]=NULL;};
};
for(w1=0;w1<(int)Sheets.count();w1++)
{
if(Sheets[w1]!=NULL) {delete Sheets[w1];Sheets[w1]=NULL;};
};
XFRecords.resize(0);
SharedStrings.resize(0);
Names.resize(0);
Sheets.resize(0);
fclose(File);
printf("closed excel file!\r\n");
if(File==NULL) return true;
return false;
};
void ExcelBook::SeekPosition(int pos)
{
if(!feof(File))
{
Position=pos;
//printf("SeekPosition:Pos:%d\r\n",Position);
fseek(File,pos,SEEK_SET);
};
};
void ExcelBook::SeekSkip(int pos)
{
if(!feof(File))
{
Position=Position+pos;
//printf("SeekSkip:Pos:%d\r\n",Position);
fseek(File, Position, SEEK_SET);
};
};
int ExcelBook::FileEOF(void)
{
if(File!=NULL) return(feof(File)); else return 0;
//EOF is defined in stdlib as -1
};
int ExcelBook::Get2Bytes(void)
{
int i1,i2;
i1=0; i2=0;
if (!feof(File))
{
i1=fgetc(File);
Position++;
};
if (!feof(File))
{
i2=fgetc(File);
Position++;
};
return Integer2Byte(i1,i2);
};
char* ExcelBook::Read(int pos, int length)
{
int i;
char *data;
data= new char[length];
SeekPosition(pos);
for(i=0; i<length; i++)
{
if(!feof(File)) data[i]=fgetc(File);
};
Position= Position+length;
return data;
};
QString ExcelBook::ReadUnicodeChar(int pos, int length)
{
int i;
QString data;
int i1=' ',i2=' ',ii;
SeekPosition(pos);
for(i=0; i<length; i++)
{
if(!feof(File)) i1=fgetc(File);
if(!feof(File)) i2=fgetc(File);
ii=Integer2Byte(i1,i2);
data.append(ii);
Position+=2;
};
return data;
};
QString* ExcelBook::GetString(int num)
{
if(num>=0 && num<(int)SharedStrings.count())
{
return SharedStrings[num];
};
return new QString("");
};
int ExcelBook::SeekBOF(void)
{
int opcode,version,streamtype,length,ret=0;
char *data;
while(!feof(File))
{
opcode=Get2Bytes();
if(opcode==XL_BOF)
{
length=Get2Bytes();
data=Read(Position,length);
version=Integer2Byte(data[0], data[1]);
streamtype=Integer2Byte(data[2], data[3]);
printf("SEEKBOF:opcode=XLBOF, %d ,version %d\r\n",Position,version);
delete [] data; data=NULL;
if (version==BIFF8) ret=8;
else if(version==BIFF7) ret=7;
printf("SEEKBOF:versionBIFF%d\r\n",ret);
if(streamtype==WBKGLOBAL) return ret *2;
else if(streamtype==WRKSHEET) return ret *1;
return 1;
};
};
return 0;
};
ExcelBREC* ExcelBook::GetBREC(void)
{
ExcelBREC* rec;
rec= new ExcelBREC;
if(FileEOF()) return NULL;
rec->data=NULL;
rec->code=Get2Bytes();
rec->length=Get2Bytes();
rec->position=Position;
SeekSkip(rec->length);
return rec;
};
ExcelBREC* ExcelBook::PeekBREC(void)
{
int oldpos;
ExcelBREC* NextRec;
oldpos=Position;
NextRec=GetBREC();
SeekPosition(oldpos);
return NextRec;
};
char* ExcelBook::GetDataOfBREC(ExcelBREC* record)
{
if(record->data==NULL)
{
- ConvertCharToArray(record,Read(record->position,record->length),record->length);
+ char* readData = Read(record->position,record->length);
+ ConvertCharToArray(record,readData,record->length);
+ delete [] readData;
};
return record->data;//new?
};
void ExcelBook::ConvertCharToArray(ExcelBREC* record, char* chars, int length)
{
record->data=new char[length];
for(int w1=0;w1<=length-1;w1++)
record->data[w1]=chars[w1];
};
bool ExcelSheet::InitCells()
{
int r;
Cells.resize(rows * cols + cols+1);
if(Cells.count()==0) return false;
for(r=0;r < Cells.count();r++)
{
Cells[r]=NULL;
};
return true;
};
void ExcelSheet::Set(int row, int col, ExcelCell* cell)
{
if(cell!=NULL&&(row*cols+col)<Cells.count())
{
Cells[row*cols+col]=cell;
};
};
ExcelCell* ExcelSheet::Get(int row, int col)
{
ExcelCell* cell;
cell=Cells[row*cols+col];
if(cell==NULL) return NULL;
return cell;
};
int ExcelBook::SheetHandleRecord(ExcelSheet* sheet, ExcelBREC* record)
{
char* data=NULL;
switch (record->code)
{
case XL_DIMENSION:
data = GetDataOfBREC(record);
if (record->length == 10)
{
sheet->rows = Integer2Byte(data[2], data[3]);
sheet->cols = Integer2Byte(data[6], data[7]);
}
else
{
sheet->rows = Integer4Byte(data[4], data[5], data[6], data[7]);
sheet->cols = Integer2Byte(data[10], data[11]);
}
sheet->InitCells();
break;
case XL_LABELSST:
HandleLabelSST(sheet, record);
break;
case XL_RK:
case XL_RK2:
HandleRK(sheet, record);
break;
case XL_MULRK:
HandleMulrk(sheet, record);
break;
case XL_ROW:
break;
case XL_NUMBER:
HandleNumber(sheet, record);
break;
case XL_BOOLERR:
break;
case XL_CONTINUE:
break;
case XL_FORMULA:
case XL_FORMULA2:
HandleFormula(sheet, record);
break;
case XL_LABEL:
break;
case XL_NAME:
HandleName(sheet, record);
break;
case XL_BOF:
break;
case XL_EOF:
return 0;
default:
break;
};
return 1;
};
int ExcelBook::ReadSheet(ExcelSheet* sheet)
{
ExcelBREC* record;
int oldpos;
oldpos = Position;
SeekPosition(sheet->position);
record = GetBREC();
while (record!=NULL)
{
if (!SheetHandleRecord(sheet, record)) break;
record=GetBREC();
};
SeekPosition(oldpos);
return 1;
};
ExcelSheet* ExcelBook::GetSheet(void)
{
ExcelSheet* sh=NULL;
int type;
type=SeekBOF();
Version=type;
sh=new ExcelSheet;
if(type)
{
sh->type=type;
sh->position=Position;
sh->name=QString("");
};
if(type==8||type==7)
{
ReadSheet(sh);
};
return sh;
};
void ExcelBook::ParseSheets(void)
{
int BOFs;
ExcelBREC* r;
BOFs=1;
r=GetBREC();
while(BOFs)
{
r=GetBREC();
switch(r->code)
{
case XL_SST:
HandleSST(r);
break;
case XL_TXO:
break;
case XL_NAME:
break;
case XL_ROW:
break;
case XL_FORMAT:
HandleFormat(r);
break;
case XL_XF:
HandleXF(r);
break;
case XL_BOUNDSHEET:
HandleBoundSheet(r);
break;
case XL_EXTSST:
break;
case XL_CONTINUE:
break;
case XL_EOF:
BOFs--;
break;
default:
break;
};
};
};