Commit cc065636 authored by Ken Martin's avatar Ken Martin
Browse files

added reading from strings

parent 19d9db9c
......@@ -54,7 +54,6 @@ MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#define VTK_ASCII 1
#define VTK_BINARY 2
class vtkDataReader : public vtkObject
{
public:
......@@ -67,6 +66,17 @@ public:
vtkSetStringMacro(Filename);
vtkGetStringMacro(Filename);
// Description:
// Specify the InputString for use when reading from a character array.
vtkSetStringMacro(InputString);
vtkGetStringMacro(InputString);
// Description:
// Set/Get reading from an InputString instead of the default, a file.
vtkSetMacro(ReadFromInputString,int);
vtkGetMacro(ReadFromInputString,int);
vtkBooleanMacro(ReadFromInputString,int);
// Description:
// Get the type of file (ASCII or BINARY)
vtkGetMacro(FileType,int);
......@@ -109,16 +119,27 @@ public:
// Special methods
char *LowerCase(char *);
FILE *OpenVTKFile();
int ReadHeader(FILE *fp);
int ReadPointData(FILE *fp, vtkDataSet *ds, int numPts);
int ReadPoints(FILE *fp, vtkPointSet *ps, int numPts);
int ReadCells(FILE *fp, int size, int *data);
void CloseVTKFile(FILE *fp);
int OpenVTKFile();
int ReadHeader();
int ReadPointData(vtkDataSet *ds, int numPts);
int ReadPoints(vtkPointSet *ps, int numPts);
int ReadCells(int size, int *data);
void CloseVTKFile();
// some functions for reading in stuff
int ReadLine(char result[256]);
int ReadString(char result[256]);
int ReadInt(int *result);
int ReadUChar(unsigned char *result);
int ReadShort(short *result);
int ReadFloat(float *result);
void EatWhiteSpace();
FILE *GetFP() {return this->fp;};
protected:
char *Filename;
int FileType;
FILE *fp;
char *ScalarsName;
char *VectorsName;
......@@ -128,16 +149,20 @@ protected:
char *LookupTableName;
char *ScalarLut;
int ReadFromInputString;
char *InputString;
int InputStringPos;
vtkSetStringMacro(ScalarLut);
vtkGetStringMacro(ScalarLut);
int ReadScalarData(FILE *fp, vtkDataSet *ds, int numPts);
int ReadVectorData(FILE *fp, vtkDataSet *ds, int numPts);
int ReadNormalData(FILE *fp, vtkDataSet *ds, int numPts);
int ReadTensorData(FILE *fp, vtkDataSet *ds, int numPts);
int ReadCoScalarData(FILE *fp, vtkDataSet *ds, int numPts);
int ReadLutData(FILE *fp, vtkDataSet *ds);
int ReadTCoordsData(FILE *fp, vtkDataSet *ds, int numPts);
int ReadScalarData(vtkDataSet *ds, int numPts);
int ReadVectorData(vtkDataSet *ds, int numPts);
int ReadNormalData(vtkDataSet *ds, int numPts);
int ReadTensorData(vtkDataSet *ds, int numPts);
int ReadCoScalarData(vtkDataSet *ds, int numPts);
int ReadLutData(vtkDataSet *ds);
int ReadTCoordsData(vtkDataSet *ds, int numPts);
};
#endif
......
......@@ -64,6 +64,17 @@ public:
void SetFilename(char *name);
char *GetFilename();
// Description:
// Specify the InputString for use when reading from a character array.
SetInputString(char *in) {this->Reader.SetInputString(in);};
char *GetInputString() { return this->Reader.GetInputString();};
// Description:
// Set/Get reading from an InputString instead of the default, a file.
SetReadFromInputString(int in) {this->Reader.SetReadFromInputString(in);};
int GetReadFromInputString() {this->Reader.GetReadFromInputString();};
vtkBooleanMacro(ReadFromInputString,int);
int GetFileType();
void SetScalarsName(char *name);
......
......@@ -64,6 +64,17 @@ public:
void SetFilename(char *name);
char *GetFilename();
// Description:
// Specify the InputString for use when reading from a character array.
SetInputString(char *in) {this->Reader.SetInputString(in);};
char *GetInputString() { return this->Reader.GetInputString();};
// Description:
// Set/Get reading from an InputString instead of the default, a file.
SetReadFromInputString(int in) {this->Reader.SetReadFromInputString(in);};
int GetReadFromInputString() {this->Reader.GetReadFromInputString();};
vtkBooleanMacro(ReadFromInputString,int);
int GetFileType();
void SetScalarsName(char *name);
......
......@@ -64,6 +64,17 @@ public:
void SetFilename(char *name);
char *GetFilename();
// Description:
// Specify the InputString for use when reading from a character array.
SetInputString(char *in) {this->Reader.SetInputString(in);};
char *GetInputString() { return this->Reader.GetInputString();};
// Description:
// Set/Get reading from an InputString instead of the default, a file.
SetReadFromInputString(int in) {this->Reader.SetReadFromInputString(in);};
int GetReadFromInputString() {this->Reader.GetReadFromInputString();};
vtkBooleanMacro(ReadFromInputString,int);
int GetFileType();
void SetScalarsName(char *name);
......
......@@ -64,6 +64,17 @@ public:
void SetFilename(char *name);
char *GetFilename();
// Description:
// Specify the InputString for use when reading from a character array.
SetInputString(char *in) {this->Reader.SetInputString(in);};
char *GetInputString() { return this->Reader.GetInputString();};
// Description:
// Set/Get reading from an InputString instead of the default, a file.
SetReadFromInputString(int in) {this->Reader.SetReadFromInputString(in);};
int GetReadFromInputString() {this->Reader.GetReadFromInputString();};
vtkBooleanMacro(ReadFromInputString,int);
int GetFileType();
void SetScalarsName(char *name);
......
......@@ -64,6 +64,17 @@ public:
void SetFilename(char *name);
char *GetFilename();
// Description:
// Specify the InputString for use when reading from a character array.
SetInputString(char *in) {this->Reader.SetInputString(in);};
char *GetInputString() { return this->Reader.GetInputString();};
// Description:
// Set/Get reading from an InputString instead of the default, a file.
SetReadFromInputString(int in) {this->Reader.SetReadFromInputString(in);};
int GetReadFromInputString() {this->Reader.GetReadFromInputString();};
vtkBooleanMacro(ReadFromInputString,int);
int GetFileType();
void SetScalarsName(char *name);
......
This diff is collapsed.
......@@ -142,21 +142,19 @@ char *vtkDataSetReader::GetLookupTableName()
void vtkDataSetReader::Execute()
{
FILE *fp;
int retStat;
char line[257];
char line[256];
vtkDataSet *output;
vtkDebugMacro(<<"Reading vtk dataset...");
if ( this->Debug ) this->Reader.DebugOn();
else this->Reader.DebugOff();
if ( !(fp=this->Reader.OpenVTKFile()) || !this->Reader.ReadHeader(fp) )
if (!this->Reader.OpenVTKFile() || !this->Reader.ReadHeader())
return;
//
// Determine dataset type
//
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 )
if (!this->Reader.ReadString(line))
{
vtkErrorMacro(<< "Premature EOF reading dataset keyword");
return;
......@@ -167,17 +165,19 @@ void vtkDataSetReader::Execute()
//
// See if type is recognized.
//
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 )
if (!this->Reader.ReadString(line))
{
vtkErrorMacro(<< "Premature EOF reading type");
return;
}
rewind(fp);
this->Reader.CloseVTKFile();
if ( ! strncmp(this->Reader.LowerCase(line),"polydata",8) )
{
vtkPolyReader *preader = new vtkPolyReader;
preader->SetFilename(this->Reader.GetFilename());
preader->SetInputString(this->Reader.GetInputString());
preader->SetReadFromInputString(this->Reader.GetReadFromInputString());
preader->SetScalarsName(this->Reader.GetScalarsName());
preader->SetVectorsName(this->Reader.GetVectorsName());
preader->SetNormalsName(this->Reader.GetNormalsName());
......@@ -192,6 +192,8 @@ void vtkDataSetReader::Execute()
{
vtkStructuredPointsReader *preader = new vtkStructuredPointsReader;
preader->SetFilename(this->Reader.GetFilename());
preader->SetInputString(this->Reader.GetInputString());
preader->SetReadFromInputString(this->Reader.GetReadFromInputString());
preader->SetScalarsName(this->Reader.GetScalarsName());
preader->SetVectorsName(this->Reader.GetVectorsName());
preader->SetNormalsName(this->Reader.GetNormalsName());
......@@ -206,6 +208,8 @@ void vtkDataSetReader::Execute()
{
vtkStructuredGridReader *preader = new vtkStructuredGridReader;
preader->SetFilename(this->Reader.GetFilename());
preader->SetInputString(this->Reader.GetInputString());
preader->SetReadFromInputString(this->Reader.GetReadFromInputString());
preader->SetScalarsName(this->Reader.GetScalarsName());
preader->SetVectorsName(this->Reader.GetVectorsName());
preader->SetNormalsName(this->Reader.GetNormalsName());
......@@ -220,6 +224,8 @@ void vtkDataSetReader::Execute()
{
vtkUnstructuredGridReader *preader = new vtkUnstructuredGridReader;
preader->SetFilename(this->Reader.GetFilename());
preader->SetInputString(this->Reader.GetInputString());
preader->SetReadFromInputString(this->Reader.GetReadFromInputString());
preader->SetScalarsName(this->Reader.GetScalarsName());
preader->SetVectorsName(this->Reader.GetVectorsName());
preader->SetNormalsName(this->Reader.GetNormalsName());
......
......@@ -143,10 +143,8 @@ char *vtkPolyReader::GetLookupTableName()
void vtkPolyReader::Execute()
{
FILE *fp;
int numPts=0;
int retStat;
char line[257];
char line[256];
int npts, size, ncells;
vtkPolyData *output=(vtkPolyData *)this->Output;
......@@ -155,13 +153,12 @@ void vtkPolyReader::Execute()
if ( this->Debug ) this->Reader.DebugOn();
else this->Reader.DebugOff();
if ( !(fp=this->Reader.OpenVTKFile()) ||
! this->Reader.ReadHeader(fp) )
if ( !(this->Reader.OpenVTKFile()) || !this->Reader.ReadHeader())
return;
//
// Read polygonal data specific stuff
//
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 )
if (!this->Reader.ReadString(line))
{
vtkErrorMacro(<<"Data file ends prematurely!");
return;
......@@ -172,7 +169,7 @@ void vtkPolyReader::Execute()
//
// Make sure we're reading right type of geometry
//
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 )
if (!this->Reader.ReadString(line))
{
vtkErrorMacro(<<"Data file ends prematurely!");
return;
......@@ -188,29 +185,29 @@ void vtkPolyReader::Execute()
//
while (1)
{
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 ) break;
if (!this->Reader.ReadString(line)) break;
if ( ! strncmp(this->Reader.LowerCase(line),"points",6) )
{
if ( (retStat=fscanf(fp,"%d", &numPts)) == EOF || retStat < 1 )
if (!this->Reader.ReadInt(&numPts))
{
vtkErrorMacro(<<"Cannot read number of points!");
return;
}
this->Reader.ReadPoints(fp, output, numPts);
this->Reader.ReadPoints(output, numPts);
}
else if ( ! strncmp(line,"vertices",8) )
{
vtkCellArray *verts = new vtkCellArray;
if ( (retStat=fscanf(fp,"%d %d", &ncells, &size)) == EOF || retStat < 2 )
if (!(this->Reader.ReadInt(&ncells) && this->Reader.ReadInt(&size)))
{
vtkErrorMacro(<<"Cannot read vertices!");
return;
}
this->Reader.ReadCells(fp, size, verts->WritePtr(ncells,size));
this->Reader.ReadCells(size, verts->WritePtr(ncells,size));
verts->WrotePtr();
output->SetVerts(verts);
verts->Delete();
......@@ -220,13 +217,13 @@ void vtkPolyReader::Execute()
else if ( ! strncmp(line,"lines",5) )
{
vtkCellArray *lines = new vtkCellArray;
if ( (retStat=fscanf(fp,"%d %d", &ncells, &size)) == EOF || retStat < 2 )
if (!(this->Reader.ReadInt(&ncells) && this->Reader.ReadInt(&size)))
{
vtkErrorMacro(<<"Cannot read lines!");
return;
}
this->Reader.ReadCells(fp, size, lines->WritePtr(ncells,size));
this->Reader.ReadCells(size, lines->WritePtr(ncells,size));
lines->WrotePtr();
output->SetLines(lines);
lines->Delete();
......@@ -236,13 +233,13 @@ void vtkPolyReader::Execute()
else if ( ! strncmp(line,"polygons",8) )
{
vtkCellArray *polys = new vtkCellArray;
if ( (retStat=fscanf(fp,"%d %d", &ncells, &size)) == EOF || retStat < 2 )
if (!(this->Reader.ReadInt(&ncells) && this->Reader.ReadInt(&size)))
{
vtkErrorMacro(<<"Cannot read polygons!");
return;
}
this->Reader.ReadCells(fp, size, polys->WritePtr(ncells,size));
this->Reader.ReadCells(size, polys->WritePtr(ncells,size));
polys->WrotePtr();
output->SetPolys(polys);
polys->Delete();
......@@ -252,13 +249,13 @@ void vtkPolyReader::Execute()
else if ( ! strncmp(line,"triangle_strips",15) )
{
vtkCellArray *tris = new vtkCellArray;
if ( (retStat=fscanf(fp,"%d %d", &ncells, &size)) == EOF || retStat < 2 )
if (!(this->Reader.ReadInt(&ncells) && this->Reader.ReadInt(&size)))
{
vtkErrorMacro(<<"Cannot read triangle strips!");
return;
}
this->Reader.ReadCells(fp, size, tris->WritePtr(ncells,size));
this->Reader.ReadCells(size, tris->WritePtr(ncells,size));
tris->WrotePtr();
output->SetStrips(tris);
tris->Delete();
......@@ -267,7 +264,7 @@ void vtkPolyReader::Execute()
else if ( ! strncmp(line, "point_data", 10) )
{
if ( (retStat=fscanf(fp,"%d", &npts)) == EOF || retStat < 1 )
if (!this->Reader.ReadInt(&npts))
{
vtkErrorMacro(<<"Cannot read point data!");
return;
......@@ -279,7 +276,7 @@ void vtkPolyReader::Execute()
return;
}
this->Reader.ReadPointData(fp, output, npts);
this->Reader.ReadPointData(output, npts);
break; //out of this loop
}
......@@ -299,13 +296,13 @@ void vtkPolyReader::Execute()
else if ( !strncmp(line, "point_data", 10) )
{
vtkWarningMacro(<<"No geometry defined in data file!");
if ( (retStat=fscanf(fp,"%d", &numPts)) == EOF || retStat < 1 )
if (!this->Reader.ReadInt(&numPts))
{
vtkErrorMacro(<<"Cannot read point data!");
return;
}
this->Reader.ReadPointData(fp, output, numPts);
this->Reader.ReadPointData(output, numPts);
}
else
......
......@@ -143,10 +143,8 @@ char *vtkStructuredGridReader::GetLookupTableName()
void vtkStructuredGridReader::Execute()
{
FILE *fp;
int numPts=0, npts;
int retStat;
char line[257];
char line[256];
int dimsRead=0;
vtkStructuredGrid *output=(vtkStructuredGrid *)this->Output;
......@@ -154,12 +152,12 @@ void vtkStructuredGridReader::Execute()
if ( this->Debug ) this->Reader.DebugOn();
else this->Reader.DebugOff();
if ( !(fp=this->Reader.OpenVTKFile()) || !this->Reader.ReadHeader(fp) )
if (!this->Reader.OpenVTKFile() || !this->Reader.ReadHeader())
return;
//
// Read structured grid specific stuff
//
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 )
if (!this->Reader.ReadString(line))
{
vtkErrorMacro(<<"Data file ends prematurely!");
return;
......@@ -170,7 +168,7 @@ void vtkStructuredGridReader::Execute()
//
// Make sure we're reading right type of geometry
//
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 )
if (!this->Reader.ReadString(line))
{
vtkErrorMacro(<<"Data file ends prematurely!");
return;
......@@ -186,13 +184,14 @@ void vtkStructuredGridReader::Execute()
//
while (1)
{
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 ) break;
if (!this->Reader.ReadString(line)) break;
if ( ! strncmp(this->Reader.LowerCase(line),"dimensions",10) )
{
int dim[3];
if ( (retStat=fscanf(fp,"%d %d %d",dim, dim+1, dim+2)) == EOF
|| retStat < 3 )
if (!(this->Reader.ReadInt(dim) &&
this->Reader.ReadInt(dim+1) &&
this->Reader.ReadInt(dim+2)))
{
vtkErrorMacro(<<"Error reading dimensions!");
return;
......@@ -205,18 +204,18 @@ void vtkStructuredGridReader::Execute()
else if ( ! strncmp(line,"points",6) )
{
if ( (retStat=fscanf(fp,"%d", &npts)) == EOF || retStat < 1 )
if (!this->Reader.ReadInt(&npts))
{
vtkErrorMacro(<<"Error reading points!");
return;
}
this->Reader.ReadPoints(fp, output, npts);
this->Reader.ReadPoints(output, npts);
}
else if ( ! strncmp(line, "point_data", 10) )
{
if ( (retStat=fscanf(fp,"%d", &numPts)) == EOF || retStat < 1 )
if (!this->Reader.ReadInt(&numPts))
{
vtkErrorMacro(<<"Cannot read point data!");
return;
......@@ -228,7 +227,7 @@ void vtkStructuredGridReader::Execute()
return;
}
this->Reader.ReadPointData(fp, output, npts);
this->Reader.ReadPointData(output, npts);
break; //out of this loop
}
......@@ -246,7 +245,7 @@ void vtkStructuredGridReader::Execute()
else if ( !strncmp(line, "point_data", 10) )
{
vtkWarningMacro(<<"No geometry defined in data file!");
if ( (retStat=fscanf(fp,"%d", &npts)) == EOF || retStat < 1 )
if (!this->Reader.ReadInt(&npts))
{
vtkErrorMacro(<<"Cannot read point data!");
return;
......
......@@ -143,10 +143,8 @@ char *vtkStructuredPointsReader::GetLookupTableName()
void vtkStructuredPointsReader::Execute()
{
FILE *fp;
int numPts=0;
int retStat;
char line[257];
char line[256];
int npts;
int dimsRead=0, arRead=0, originRead=0;
vtkStructuredPoints *output=(vtkStructuredPoints *)this->Output;
......@@ -156,12 +154,12 @@ void vtkStructuredPointsReader::Execute()
if ( this->Debug ) this->Reader.DebugOn();
else this->Reader.DebugOff();
if ( !(fp=this->Reader.OpenVTKFile()) || !this->Reader.ReadHeader(fp) )
if (!this->Reader.OpenVTKFile() || !this->Reader.ReadHeader())
return;
//
// Read structured points specific stuff
//
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 )
if (!this->Reader.ReadString(line))
{
vtkErrorMacro(<<"Data file ends prematurely!");
return;
......@@ -172,7 +170,7 @@ void vtkStructuredPointsReader::Execute()
//
// Make sure we're reading right type of geometry
//
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 )
if (!this->Reader.ReadString(line))
{
vtkErrorMacro(<<"Data file ends prematurely!");
return;
......@@ -189,13 +187,14 @@ void vtkStructuredPointsReader::Execute()
numPts = output->GetNumberOfPoints(); // get default
while (1)
{
if ( (retStat=fscanf(fp,"%256s",line)) == EOF || retStat < 1 ) break;
if (!this->Reader.ReadString(line)) break;
if ( ! strncmp(this->Reader.LowerCase(line),"dimensions",10) )
{
int dim[3];
if ( (retStat=fscanf(fp,"%d %d %d",dim, dim+1, dim+2)) == EOF
|| retStat < 3 )
if (!(this->Reader.ReadInt(dim) &&
this->Reader.ReadInt(dim+1) &&
this->Reader.ReadInt(dim+2)))
{
vtkErrorMacro(<<"Error reading dimensions!");
return;
......@@ -209,8 +208,9 @@ void vtkStructuredPointsReader::Execute()
else if ( ! strncmp(line,"aspect_ratio",12) )
{
float ar[3];
if ( (retStat=fscanf(fp,"%f %f %f",ar, ar+1, ar+2)) == EOF
|| retStat < 3 )
if (!(this->Reader.ReadFloat(ar) &&
this->Reader.ReadFloat(ar+1) &&
this->Reader.ReadFloat(ar+2)))
{
vtkErrorMacro(<<"Error reading aspect ratio!");
return;
......@@ -223,8 +223,9 @@ void vtkStructuredPointsReader::Execute()
else if ( ! strncmp(line,"origin",6) )
{
float origin[3];
if ( (retStat=fscanf(fp,"%f %f %f",origin, origin+1, origin+2)) == EOF
|| retStat < 3 )
if (!(this->Reader.ReadFloat(origin) &&
this->Reader.ReadFloat(origin+1) &&
this->Reader.ReadFloat(origin+2)))
{
vtkErrorMacro(<<"Error reading origin!");
return;
......@@ -236,7 +237,7 @@ void vtkStructuredPointsReader::Execute()
else if ( ! strncmp(line, "point_data", 10) )
{
if ( (retStat=fscanf(fp,"%d", &npts)) == EOF || retStat < 1 )
if (!this->Reader.ReadInt(&npts))
{
vtkErrorMacro(<<"Cannot read point data!");
return;
......@@ -248,7 +249,7 @@ void vtkStructuredPointsReader::Execute()