Files
Work/worksht/WORKSHT.CPP
2024-08-07 09:16:27 -04:00

176 lines
4.6 KiB
C++

#include <worksht/worksht.hpp>
BOOL Worksheet::open(const String &strPathFileName)
{
Array<ColumnData> &matrix=mMatrix;
Record lotusRecord;
char buffer[1024];
char charByte;
FileIO inFile;
int maxRow(0);
int maxCol(0);
BOOL isFirstPass;
mRows=0;
mCols=0;
isFirstPass=TRUE;
if(!inFile.open(strPathFileName))return FALSE;
for(int pass=0;pass<2;pass++)
{
if(1==pass)
{
inFile.rewind();
matrix.size(maxRow+1);
for(int index=0;index<mMatrix.size();index++)matrix[index].size(maxCol+1);
}
while(TRUE)
{
processEvents();
if(!lotusRecord.read(inFile))break;
::OutputDebugString(lotusRecord.toString()+String("\n"));
if(isFirstPass)
{
if(Record::Bof!=lotusRecord.type())return FALSE;
isFirstPass=FALSE;
}
if(Record::Label==lotusRecord.type())
{
Label label(lotusRecord);
label.read(inFile);
if(label.row()>maxRow)maxRow=label.row();
if(label.column()>maxCol)maxCol=label.column();
if(!pass)continue;
(matrix[label.row()])[label.column()]=::new Label(label);
(matrix[label.row()])[label.column()].disposition(PointerDisposition::Delete);
continue;
}
if(Record::Number==lotusRecord.type())
{
Number number(lotusRecord);
number.read(inFile);
if(number.row()>maxRow)maxRow=number.row();
if(number.column()>maxCol)maxCol=number.column();
if(!pass)continue;
(matrix[number.row()])[number.column()]=::new Number(number);
(matrix[number.row()])[number.column()].disposition(PointerDisposition::Delete);
continue;
}
if(Record::Integer==lotusRecord.type())
{
Integer integer(lotusRecord);
integer.read(inFile);
if(integer.row()>maxRow)maxRow=integer.row();
if(integer.column()>maxCol)maxCol=integer.column();
if(!pass)continue;
(matrix[integer.row()])[integer.column()]=::new Integer(integer);
(matrix[integer.row()])[integer.column()].disposition(PointerDisposition::Delete);
continue;
}
if(Record::Formula==lotusRecord.type()){::OutputDebugString("Formula encountered\n");break;}
if(Record::Eof==lotusRecord.type())break;
if(lotusRecord.length()<sizeof(buffer))inFile.read(buffer,lotusRecord.length());
else for(int index=0;index<lotusRecord.length();index++)inFile.read(&charByte);
}
}
mRows=maxRow;
mCols=maxCol;
return (maxRow&&maxCol);
}
BOOL Worksheet::save(const String &strPathFileName)
{
Array<ColumnData> &matrix=mMatrix;
FileIO outFile;
BYTE header[]={0x06,0x04};
BYTE format[]={0x27};
if(!outFile.open(strPathFileName,FileIO::GenericWrite,FileIO::FileShareRead,FileIO::CreateAlways))return FALSE;
Record lotusRecord(Record::Bof,2);
lotusRecord.write(outFile);
outFile.write(header,sizeof(header));
lotusRecord.type(Record::LabelFmt);
lotusRecord.length(1);
lotusRecord.write(outFile);
outFile.write(format,sizeof(format));
for(int row=0;row<rows();row++)
{
processEvents();
for(int col=0;col<cols();col++)
{
if(!(matrix[row])[col].isOkay())continue;
Record &record=*(matrix[row])[col];
if(Record::Label==record.type())
{
Label &label=(Label&)record;
label.write(outFile);
}
else if(Record::Number==record.type())
{
Number &number=(Number&)record;
number.write(outFile);
}
else if(Record::Integer==record.type())
{
Integer &integer=(Integer&)record;
integer.write(outFile);
}
}
}
lotusRecord.type(Record::Eof);
lotusRecord.length(0);
lotusRecord.write(outFile);
outFile.flush();
outFile.close();
return true;
}
BOOL Worksheet::saveText(const String &strPathFileName)
{
Array<ColumnData> &matrix=mMatrix;
String strColumn;
FileIO outFile;
char *pCursor;
if(!outFile.open(strPathFileName,FileIO::GenericWrite,FileIO::FileShareRead,FileIO::CreateAlways))return FALSE;
strColumn.reserve(MaxColumn);
for(int row=0;row<rows();row++)
{
processEvents();
::memset((char*)strColumn,0,MaxColumn);
pCursor=strColumn;
for(int col=0;col<cols();col++)
{
if(!(matrix[row])[col].isOkay())continue;
Record &record=*(matrix[row])[col];
if(Record::Label==record.type())pCursor=((Label&)record).catstr(pCursor);
else if(Record::Number==record.type())pCursor=((Number&)record).catstr(pCursor);
else if(Record::Integer==record.type())pCursor=((Integer&)record).catstr(pCursor);
::strcat(pCursor," ");
while(*pCursor)pCursor++;
}
::strcat(pCursor,"\n");
outFile.write((char*)strColumn,(pCursor-(char*)strColumn)+1);
}
return TRUE;
}
void Worksheet::setDimensions(UINT rows,UINT cols)
{
mMatrix.size(rows+1);
for(int row=0;row<rows;row++)mMatrix[row].size(cols+1);
mRows=rows;
mCols=cols;
}
// virtuals
void Worksheet::processEvents(void)
{
}