Commit 38cc8d20 authored by David C. Lonie's avatar David C. Lonie

Style fixup for information keys in legacy IO.

Removed *_END tags.
parent 3e6288f6
......@@ -12,7 +12,7 @@ vector-valued. Only the information objects attached to arrays are encoded.
A block of metadata may immediately follow the specification of an array.
Whitespace is permitted between the array data and the opening `METADATA` tag.
The metadata block is terminated by the `METADATA_END` tag.
The metadata block is terminated by an empty line..
```
# vtk DataFile Version 4.1
......@@ -28,7 +28,7 @@ COMPONENT_NAMES
X%20coordinates
Y%20coordinates
Z%20coordinates
INFORMATION
INFORMATION 8
NAME Double LOCATION TestKey
DATA 1
NAME DoubleVector LOCATION TestKey
......@@ -48,8 +48,7 @@ Second%20(with%20whitespace!)
Third%20(with%0Anewline!)
NAME UnsignedLong LOCATION TestKey
DATA 9
INFORMATION_END
METADATA_END
CELLS 3 15
4 0 1 2 3
4 0 4 1 5
......@@ -67,15 +66,15 @@ vtkGhostType 1 3 unsigned_char
METADATA
COMPONENT_NAMES
Ghost%20level%20information
INFORMATION
INFORMATION 1
NAME UNITS_LABEL LOCATION vtkDataArray
DATA radians
INFORMATION_END
METADATA_END
```
As shown, a metadata block can have two sections, `COMPONENT_NAMES` and
`INFORMATION`.
`INFORMATION`. The `INFORMATION` tag is followed by the number of information
keys that follow.
## COMPONENT_NAMES ##
......@@ -85,9 +84,9 @@ must be one line per component.
## INFORMATION ##
If the `METADATA` block contains the line `INFORMATION`, every line until the
terminating `INFORMATION_END` tag is interpreted as `vtkInformation` data. The
general form of a single valued information entry is:
If the `METADATA` block contains the line `INFORMATION`, the number of information
keys is read from the INFORMATION line and `vtkInformation` data that follows is
parsed. The general form of a single valued information entry is:
```
NAME [key name] LOCATION [key location (e.g. class name)]
......
......@@ -1957,7 +1957,7 @@ vtkAbstractArray *vtkDataReader::ReadArray(const char *dataType, int numTuples,
this->LowerCase(line, 256);
// Blank line indicates end of metadata:
if (strncmp(line, "metadata_end", 12) == 0)
if (strlen(line) == 0)
{
break;
}
......@@ -1982,8 +1982,15 @@ vtkAbstractArray *vtkDataReader::ReadArray(const char *dataType, int numTuples,
if (strncmp(line, "information", 11) == 0)
{
int numKeys;
if (sscanf(line, "information %d", &numKeys) != 1)
{
vtkWarningMacro("Invalid information header: " << line);
continue;
}
vtkInformation *info = array->GetInformation();
this->ReadInformation(info);
this->ReadInformation(info, numKeys);
continue;
}
}
......@@ -2628,29 +2635,25 @@ int vtkDataReader::ReadEdgeFlags(vtkDataSetAttributes *a, int numPts)
return 1;
}
int vtkDataReader::ReadInformation(vtkInformation *info)
int vtkDataReader::ReadInformation(vtkInformation *info, int numKeys)
{
// Assuming that the opening INFORMATION line has been read.
char line[256];
char name[256];
char location[256];
for (;;)
for (int keyIdx = 0; keyIdx < numKeys; ++keyIdx)
{
if (!this->ReadLine(line))
do
{
vtkErrorMacro("Unexpected EOF while parsing INFORMATION section.");
return 0;
if (!this->ReadLine(line))
{
vtkErrorMacro("Unexpected EOF while parsing INFORMATION section.");
return 0;
}
}
while (strlen(line) == 0); // Skip empty lines
if (strlen(line) == 0)
{ // Skip empty lines
continue;
}
if (strncmp("INFORMATION_END", line, 15) == 0)
{ // End of information
break;
}
else if (strncmp("NAME ", line, 5) == 0)
if (strncmp("NAME ", line, 5) == 0)
{ // New key
if (sscanf(line, "NAME %s LOCATION %s", name, location) != 2)
{
......
......@@ -387,7 +387,7 @@ protected:
// Description:
// Format is detailed \ref IOLegacyInformationFormat "here".
int ReadInformation(vtkInformation *info);
int ReadInformation(vtkInformation *info, int numKeys);
int ReadDataSetData(vtkDataSet *ds);
......
......@@ -1357,7 +1357,7 @@ int vtkDataWriter::WriteArray(ostream *fp, int dataType, vtkAbstractArray *data,
this->WriteInformation(fp, info);
}
*fp << "METADATA_END" << endl;
*fp << endl;
}
fp->flush();
......@@ -1786,6 +1786,56 @@ int vtkDataWriter::WriteEdgeFlagsData(ostream *fp, vtkDataArray *edgeFlags, int
return this->WriteArray(fp, edgeFlags->GetDataType(), edgeFlags, format, num, 1);
}
bool vtkDataWriter::CanWriteInformationKey(vtkInformation *info,
vtkInformationKey *key)
{
vtkInformationDoubleKey *dKey = NULL;
vtkInformationDoubleVectorKey *dvKey = NULL;
if ((dKey = vtkInformationDoubleKey::SafeDownCast(key)))
{
// Skip keys with NaNs/infs
double value = info->Get(dKey);
if (!vtkMath::IsFinite(value))
{
vtkWarningMacro("Skipping key '" << key->GetLocation() << "::"
<< key->GetName() << "': bad value: " << value);
return false;
}
return true;
}
else if ((dvKey = vtkInformationDoubleVectorKey::SafeDownCast(key)))
{
// Skip keys with NaNs/infs
int length = dvKey->Length(info);
bool valid = true;
for (int i = 0; i < length; ++i)
{
double value = info->Get(dvKey, i);
if (!vtkMath::IsFinite(value))
{
vtkWarningMacro("Skipping key '" << key->GetLocation() << "::"
<< key->GetName() << "': bad value: " << value);
valid = false;
break;
}
}
return valid;
}
else if (vtkInformationIdTypeKey::SafeDownCast(key) ||
vtkInformationIntegerKey::SafeDownCast(key) ||
vtkInformationIntegerVectorKey::SafeDownCast(key) ||
vtkInformationStringKey::SafeDownCast(key) ||
vtkInformationStringVectorKey::SafeDownCast(key) ||
vtkInformationUnsignedLongKey::SafeDownCast(key))
{
return true;
}
vtkDebugMacro("Could not serialize information with key "
<< key->GetLocation() << "::" << key->GetName() << ": "
"Unsupported data type '" << key->GetClassName() << "'.");
return false;
}
namespace {
void writeInfoHeader(std::ostream *fp, vtkInformationKey *key)
{
......@@ -1796,11 +1846,24 @@ void writeInfoHeader(std::ostream *fp, vtkInformationKey *key)
int vtkDataWriter::WriteInformation(std::ostream *fp, vtkInformation *info)
{
*fp << "INFORMATION\n";
// This will contain the serializable keys:
vtkNew<vtkInformation> keys;
vtkInformationKey *key = NULL;
vtkNew<vtkInformationIterator> iter;
char buffer[1024];
iter->SetInformationWeak(info);
vtkInformationKey *key = NULL;
for (iter->InitTraversal(); (key = iter->GetCurrentKey());
iter->GoToNextItem())
{
if (this->CanWriteInformationKey(info, key))
{
keys->CopyEntry(info, key);
}
}
*fp << "INFORMATION " << keys->GetNumberOfKeys() << "\n";
iter->SetInformationWeak(keys.Get());
char buffer[1024];
for (iter->InitTraversal(); (key = iter->GetCurrentKey());
iter->GoToNextItem())
{
......@@ -1814,15 +1877,6 @@ int vtkDataWriter::WriteInformation(std::ostream *fp, vtkInformation *info)
vtkInformationUnsignedLongKey *ulKey = NULL;
if ((dKey = vtkInformationDoubleKey::SafeDownCast(key)))
{
// Skip keys with NaNs/infs
double value = info->Get(dKey);
if (!vtkMath::IsFinite(value))
{
vtkWarningMacro("Skipping key '" << key->GetLocation() << "::"
<< key->GetName() << "': bad value: " << value);
continue;
}
writeInfoHeader(fp, key);
// "%lg" is used to write double array data in ascii, using the same
// precision here.
......@@ -1831,28 +1885,10 @@ int vtkDataWriter::WriteInformation(std::ostream *fp, vtkInformation *info)
}
else if ((dvKey = vtkInformationDoubleVectorKey::SafeDownCast(key)))
{
// Skip keys with NaNs/infs
int length = dvKey->Length(info);
bool valid = true;
for (int i = 0; i < length; ++i)
{
double value = info->Get(dvKey, i);
if (!vtkMath::IsFinite(value))
{
vtkWarningMacro("Skipping key '" << key->GetLocation() << "::"
<< key->GetName() << "': bad value: " << value);
valid = false;
break;
}
}
if (!valid)
{
continue;
}
writeInfoHeader(fp, key);
// Size first:
int length = dvKey->Length(info);
snprintf(buffer, 1024, "%d", length);
*fp << buffer << " ";
......@@ -1932,7 +1968,6 @@ int vtkDataWriter::WriteInformation(std::ostream *fp, vtkInformation *info)
"Unsupported data type '" << key->GetClassName() << "'.");
}
}
*fp << "INFORMATION_END\n";
return 1;
}
......
......@@ -35,6 +35,7 @@ class vtkDataSet;
class vtkFieldData;
class vtkGraph;
class vtkInformation;
class vtkInformationKey;
class vtkPoints;
class vtkTable;
......@@ -249,6 +250,8 @@ protected:
int WritePedigreeIdData(ostream *fp, vtkAbstractArray *p, int num);
int WriteEdgeFlagsData(ostream *fp, vtkDataArray *edgeFlags, int num);
bool CanWriteInformationKey(vtkInformation *info, vtkInformationKey *key);
// Description:
// Format is detailed \ref IOLegacyInformationFormat "here".
int WriteInformation(ostream *fp, vtkInformation *info);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment