| home
| contents
| previous
| next page
| send comment
| send link
| add bookmark |
Document.cpp
/*---------------------------------------------------------------------------------*
* "document.cpp"
*
* Definitions for the document class. These load, store, initialize,
* and provide access to the data in the spreadsheet document.
*
* Stephen R. Schmitt
*
* January 1999
*/
#include "document.h"
#include "scalc.h"
/*---------------------------------------------------------------------------------*
* "document" constructor allocates memory and initializes an
* empty spreadsheet.
*
* returns: nothing
*/
document::document()
{
int x, y; // cell coordinates
// allocate memory for each cell structure and set to EMPTY
for( x = 0; x < CXmax; x++ )
{
for( y = 0; y < CYmax; y++ )
{
sheet[x][y] = new CELL_TP;
if( sheet[x][y] )
{
sheet[x][y]->status = EMPTY;
sheet[x][y]->errloc = -1;
sheet[x][y]->val = 0.0;
memset( sheet[x][y]->contents, 0, MAX_CONTENTS );
}
else
{
printf( "Not enough memory!" );
assert( false );
}
}
}
strcpy( filename, "demo1.dat" );
}
/*---------------------------------------------------------------------------------*
* "~document" destructor frees memory allocated for spreadsheet
*
* returns: nothing
*/
document::~document()
{
int x, y;
// free memory for each cell
for( x = 0; x < CXmax; x++ )
{
for( y = 0; y < CYmax; y++ )
{
if( sheet[x][y] )
delete sheet[x][y];
}
}
}
/*---------------------------------------------------------------------------------*
* "clear_sheet" sets every cell to empty state
*
* returns: nothing
*/
void document::clear_sheet()
{
int x, y;
for( x = 0; x < CXmax; x++ )
{
for( y = 0; y < CYmax; y++ )
{
if( sheet[x][y] )
{
sheet[x][y]->status = EMPTY;
sheet[x][y]->errloc = -1;
sheet[x][y]->val = 0.0;
memset( sheet[x][y]->contents, 0, MAX_CONTENTS );
}
}
}
}
/*---------------------------------------------------------------------------------*
* "put_filename" provides write access to document file name
*
* returns: nothing
*/
void document::put_filename( char *buf ) // new file name
{
strcpy( filename, buf );
}
/*---------------------------------------------------------------------------------*
* "get_filename" provides read access to document file name
*
* returns: nothing
*/
void document::get_filename( char *buf ) // current file name
{
strcpy( buf, filename );
}
/*---------------------------------------------------------------------------------*
* "put_cell_contents" provides write access to cell contents string
*
* returns: nothing
*/
void document::put_cell_contents( int y, // row
int x, // col
char *buf ) // new cell contents
{
strcpy( sheet[x][y]->contents, buf );
}
/*---------------------------------------------------------------------------------*
* "get_cell_contents" provides read access to cell contents string
*
* returns: nothing
*/
void document::get_cell_contents( int y, // row
int x, // col
char *buf ) // current contents
{
strcpy( buf, sheet[x][y]->contents );
}
/*---------------------------------------------------------------------------------*
* "put_cell_status" provides write access to cell status code
*
* returns: nothing
*/
void document::put_cell_status( int y, // row
int x, // col
int status ) // new status code
{
sheet[x][y]->status = status;
}
/*---------------------------------------------------------------------------------*
* "get_cell_status" provides read access to cell status code
*
* returns: status code
*/
int document::get_cell_status( int y, // row
int x ) // col
{
return sheet[x][y]->status;
}
/*---------------------------------------------------------------------------------*
* "put_cell_errloc" provides write access to cell error location
*
* returns: nothing
*/
void document::put_cell_errloc( int y, // row
int x, // col
int errloc ) // new error location
{
sheet[x][y]->errloc = errloc;
}
/*---------------------------------------------------------------------------------*
* "get_cell_errloc" provides read access to cell error location
*
* returns: error location, -1 if empty sheet
*/
int document::get_cell_errloc( int y, // row
int x ) // col
{
return sheet[x][y]->errloc;
}
/*---------------------------------------------------------------------------------*
* "put_cell_value" provides write access to cell value
*
* returns: nothing
*/
void document::put_cell_value( int y, // row
int x, // col
double val ) // new cell value
{
sheet[x][y]->val = val;
}
/*---------------------------------------------------------------------------------*
* "get_cell_value" provides read access to cell value
*
* returns: value of cell
*/
double document::get_cell_value( int y, // row
int x ) // col
{
return sheet[x][y]->val;
}
/*---------------------------------------------------------------------------------*
* "save" sends the spread sheet to a disk file. Cell contents
* are separated by newline characters.
*
* note: There is an incompatibility between <iostream.h>
* and <curses.h>
*
* returns: true on success
*/
bool document::save()
{
int x, y;
FILE *out_stream;
if( !filename[0] )
return false;
out_stream = fopen( filename, "w" );
if( !out_stream )
return false;
for( x = 0; x < CXmax; x++ )
{
for( y = 0; y < CYmax; y++ )
{
if( sheet[x][y] )
fputs( sheet[x][y]->contents, out_stream );
fputs( "\n", out_stream );
}
}
fclose( out_stream );
return true;
}
/*---------------------------------------------------------------------------------*
* "load" gets a spread sheet from a disk file. Application needs to
* recalculate after entry of data.
*
* note: There is an incompatibility between <iostream.h>
* and <curses.h>
*
* returns: true on success
*/
bool document::load()
{
int x, y;
char buffer[MAX_CONTENTS], *pnewline;
FILE *in_stream;
if( !filename[0] )
return false;
in_stream = fopen( filename, "r" );
if( !in_stream )
return false;
clear_sheet(); // free any existing memory
for( x = 0; x < CXmax; x++ )
{
for( y = 0; y < CYmax; y++ )
{
if( feof( in_stream ) )
break;
// cell contents are separated by newline characters
fgets( buffer, MAX_CONTENTS, in_stream );
pnewline = strchr( buffer, '\n' );
if( pnewline )
*pnewline = 0;
if( buffer[0] )
put_cell_contents( y, x, buffer );
}
}
fclose( in_stream );
return true;
}
| home
| contents
| previous
| next page
| send comment
| send link
| add bookmark |
Copyright © 2004, Stephen R. Schmitt