#include <string>
#include <stack>
#include <fstream>
#include <sstream>
#include <assert.h>
#include <stdlib.h>
#include <maya/MCacheFormatDescription.h>
#include <maya/MFnPlugin.h>
#include <maya/MGlobal.h>
#include <maya/MString.h>
#include <maya/MPxCacheFormat.h>
#include <maya/MTime.h>
#include <maya/MFloatVectorArray.h>
#include <maya/MDoubleArray.h>
#include <maya/MFloatArray.h>
#include <maya/MIntArray.h>
#include <maya/MVectorArray.h>
using namespace std;
{
public:
XmlCacheFormat();
~XmlCacheFormat();
static void* creator();
static void setPluginName(
const MString& name );
void close();
void beginWriteChunk();
void endWriteChunk();
void endReadChunk();
unsigned readArraySize();
int readInt32();
bool handlesDescription();
const MString& descriptionFileLocation,
const MString& descriptionFileLocation,
protected:
private:
void startXmlBlock( string& t );
void endXmlBlock();
void writeXmlTagValue( string& tag, string value );
void writeXmlTagValue( string& tag, int value );
bool readXmlTagValueInChunk(
string tag,
MStringArray& values );
void readXmlTag( string& value );
bool findXmlStartTag( string& tag );
bool findXmlStartTagInChunk( string& tag );
bool findXmlEndTag( string& tag );
void writeXmlValue( string& value );
void writeXmlValue( double value );
void writeXmlValue( float value );
void writeXmlValue( int value );
fstream fFile;
stack<string> fXmlStack;
FileAccessMode fMode;
};
MString XmlCacheFormat::fExtension =
"mc";
MString XmlCacheFormat::fCacheFormatName =
"xml_no_desc";
inline MString XmlCacheFormat::translatorName()
{
return fCacheFormatName;
}
void* XmlCacheFormat::creator()
{
return new XmlCacheFormat();
}
string XMLSTARTTAG( string x )
{
return ( "<" + x + ">" );
}
string XMLENDTAG( string x )
{
return ( "</" + x + ">" );
}
string cacheTag("awGeoCache");
string startTimeTag("startTime");
string endTimeTag("endTime");
string versionTag("version");
string timeTag("time");
string sizeTag("size");
string intTag("integer32");
string doubleArrayTag("doubleArray");
string floatArrayTag("floatArray");
string doubleVectorArrayTag("doubleVectorArray");
string floatVectorArrayTag("floatVectorArray");
string channelTag("channel");
string chunkTag("chunk");
string Autodesk_Cache_File("Autodesk_Cache_File");
string cacheType_Type("cacheType-Type");
string OneFilePerFrame("OneFilePerFrame");
string OneFile("OneFile");
string cacheType_Format("cacheType-Format");
string time_Range("time-Range");
string cacheTimePerFrame_TimePerFrame("cacheTimePerFrame-TimePerFrame");
string cacheVersion_Version("cacheVersion-Version");
string extra("extra");
string Channels("Channels");
string ChannelName("ChannelName");
string ChannelType("ChannelType");
string ChannelInterpretation("ChannelInterpretation");
string SamplingType("SamplingType");
string Double("Double");
string DoubleArray("DoubleArray");
string DoubleVectorArray("DoubleVectorArray");
string Int32Array("Int32Array");
string FloatArray("FloatArray");
string FloatVectorArray("FloatVectorArray");
string Regular("Regular");
string Irregular("Irregular");
string SamplingRate("SamplingRate");
string StartTime("StartTime");
string EndTime("EndTime");
XmlCacheFormat::XmlCacheFormat()
{
}
XmlCacheFormat::~XmlCacheFormat()
{
close();
}
XmlCacheFormat::open(
const MString& fileName, FileAccessMode mode )
{
bool rtn = true;
assert((fileName.
length() > 0));
fFileName = fileName;
if( mode == kWrite ) {
fFile.open(fFileName.asChar(), ios::out);
}
else if (mode == kReadWrite) {
fFile.open(fFileName.asChar(), ios::app);
} else {
fFile.open(fFileName.asChar(), ios::in);
}
if (!fFile.is_open()) {
rtn = false;
} else {
if (mode == kRead) {
rtn = readHeader();
}
}
}
XmlCacheFormat::isValid()
{
bool rtn = fFile.is_open();
}
XmlCacheFormat::readHeader()
{
bool rtn = false;
if (kWrite != fMode) {
if( fFile.is_open() ) {
string tag;
readXmlTag( tag );
if( tag == XMLSTARTTAG(cacheTag) )
{
readXmlTagValue( versionTag, value );
readXmlTagValue( startTimeTag, value );
readXmlTagValue( endTimeTag, value );
readXmlTag( tag );
if( tag != XMLENDTAG(cacheTag) )
{
}
rtn = true;
}
}
}
}
XmlCacheFormat::rewind()
{
if( fFile.is_open() )
{
close();
if(!open(fFileName, kRead ))
{
}
}
else
{
}
}
void XmlCacheFormat::close()
{
if( fFile.is_open() ) {
fFile.close();
}
}
MStatus XmlCacheFormat::writeInt32(
int i )
{
writeXmlTagValue(intTag, i);
}
int
XmlCacheFormat::readInt32()
{
readXmlTagValue(intTag, value);
return atoi( value[0].asChar() );
}
XmlCacheFormat::writeHeader(
const MString& version,
MTime& startTime,
MTime& endTime )
{
stringstream oss;
startXmlBlock(cacheTag);
writeXmlTagValue(versionTag, v);
oss << startTime;
writeXmlTagValue(startTimeTag, oss.str() );
oss.str("");
oss.clear();
oss << endTime;
writeXmlTagValue(endTimeTag, oss.str() );
endXmlBlock();
}
XmlCacheFormat::readTime(
MTime& time )
{
readXmlTagValue(timeTag, timeValue);
time.
setValue( strtod( timeValue[0].asChar(), NULL ) );
}
XmlCacheFormat::writeTime(
MTime& time )
{
stringstream oss;
oss << time;
writeXmlTagValue(timeTag, oss.str() );
}
XmlCacheFormat::findChannelName(
const MString& name )
{
while (readXmlTagValueInChunk(channelTag, value))
{
if( value.
length() == 1 && value[0] == name )
{
}
}
}
XmlCacheFormat::readChannelName(
MString& name )
{
readXmlTagValueInChunk(channelTag, value);
name = value[0];
}
XmlCacheFormat::readNextTime(
MTime& foundTime )
{
bool ret = readTime(readAwTime);
foundTime = readAwTime;
}
XmlCacheFormat::findTime(
MTime& time,
MTime& foundTime )
{
MTime preTime( seekTime - timeTolerance );
MTime postTime( seekTime + timeTolerance );
bool fileRewound = false;
while (1)
{
bool timeTagFound = beginReadChunk();
if ( ! timeTagFound && !fileRewound )
{
if(!rewind())
{
}
fileRewound = true;
timeTagFound = beginReadChunk();
}
if ( timeTagFound )
{
readTime(rTime);
if(rTime >= preTime && rTime <= postTime)
{
foundTime = rTime;
}
if(rTime > postTime )
{
if (!fileRewound)
{
if(!rewind())
{
}
fileRewound = true;
}
else
{
}
}
else
{
fileRewound = true;
}
endReadChunk();
}
else
{
break;
}
}
}
XmlCacheFormat::writeChannelName(
const MString& name )
{
writeXmlTagValue( channelTag, chan );
}
void XmlCacheFormat::beginWriteChunk()
{
startXmlBlock(chunkTag);
}
void XmlCacheFormat::endWriteChunk()
{
endXmlBlock();
}
MStatus XmlCacheFormat::beginReadChunk()
{
}
void XmlCacheFormat::endReadChunk()
{
findXmlEndTag(chunkTag);
}
XmlCacheFormat::writeDoubleArray(
const MDoubleArray& array )
{
assert(size != 0);
writeXmlTagValue(sizeTag,size);
startXmlBlock( doubleArrayTag );
for(int i = 0; i < size; i++)
{
writeXmlValue(array[i]);
}
endXmlBlock();
}
XmlCacheFormat::writeFloatArray(
const MFloatArray& array )
{
assert(size != 0);
writeXmlTagValue(sizeTag,size);
startXmlBlock( floatArrayTag );
for(int i = 0; i < size; i++)
{
writeXmlValue(array[i]);
}
endXmlBlock();
}
XmlCacheFormat::writeDoubleVectorArray(
const MVectorArray& array )
{
assert(size != 0);
writeXmlTagValue(sizeTag,size);
startXmlBlock( doubleVectorArrayTag );
for(int i = 0; i < size; i++)
{
writeXmlValue(array[i][0]);
writeXmlValue(array[i][1]);
writeXmlValue(array[i][2]);
string endl("\n");
writeXmlValue(endl);
}
endXmlBlock();
}
{
assert(size != 0);
writeXmlTagValue(sizeTag,size);
startXmlBlock( floatVectorArrayTag );
for(int i = 0; i < size; i++)
{
writeXmlValue(array[i][0]);
writeXmlValue(array[i][1]);
writeXmlValue(array[i][2]);
string endl("\n");
writeXmlValue(endl);
}
endXmlBlock();
}
unsigned
XmlCacheFormat::readArraySize()
{
if (readXmlTagValue(sizeTag, value))
{
unsigned readSize = (unsigned) atoi( value[0].asChar() );
return readSize;
}
return 0;
}
XmlCacheFormat::readDoubleArray(
MDoubleArray& array,
unsigned arraySize )
{
readXmlTagValue(doubleArrayTag, value);
assert( value.
length() == arraySize );
for (
unsigned int i = 0; i < value.
length(); i++ )
{
array[i] = strtod( value[i].asChar(), NULL );
}
}
XmlCacheFormat::readFloatArray(
MFloatArray& array,
unsigned arraySize )
{
readXmlTagValue(floatArrayTag, value);
assert( value.
length() == arraySize );
for (
unsigned int i = 0; i < value.
length(); i++ )
{
array[i] = (float)strtod( value[i].asChar(), NULL );
}
}
XmlCacheFormat::readDoubleVectorArray(
MVectorArray& array,
unsigned arraySize )
{
if( !readXmlTagValue(doubleVectorArrayTag, value) )
{
}
assert( value.
length() == arraySize * 3 );
for (unsigned i = 0; i < arraySize; i++ )
{
double v[3];
v[0] = strtod( value[i*3].asChar(), NULL );
v[1] = strtod( value[i*3+1].asChar(), NULL );
v[2] = strtod( value[i*3+2].asChar(), NULL );
}
}
{
readXmlTagValue(floatVectorArrayTag, value);
assert( value.
length() == arraySize * 3 );
for (unsigned i = 0; i < arraySize; i++ )
{
float v[3];
v[0] = (float)strtod( value[i*3].asChar(), NULL );
v[1] = (float)strtod( value[i*3+1].asChar(), NULL );
v[2] = (float)strtod( value[i*3+2].asChar(), NULL );
}
}
XmlCacheFormat::extension()
{
return fExtension;
}
bool
XmlCacheFormat::handlesDescription()
{
return true;
}
static bool readKeyAndValue( fstream& file, std::string& key, std::string& value )
{
char line[256];
for ( ;; )
{
if ( file.eof() )
{
value.clear();
return true;
}
if ( !file.good() )
return false;
line[0] = 0;
file.getline( line, sizeof line );
if ( line[0] != 0 )
break;
}
char* delimiter = strchr( line, '=' );
if ( delimiter == NULL )
return false;
*delimiter = 0;
key = line;
value = delimiter+1;
return true;
}
static bool readValue( fstream& file, const std::string& key, std::string& value )
{
std::string readKey;
if ( !readKeyAndValue( file, readKey, value ) )
return false;
if ( readKey != key )
return false;
return true;
}
static bool readValue( fstream& file,
const std::string& key,
MTime& time )
{
std::string value;
if ( !readValue( file, key, value ) )
return false;
double d;
if ( 1 != sscanf( value.c_str(), "%lf", &d ) )
return false;
return true;
}
const MString& descriptionFileLocation,
{
const MString filename = descriptionFileLocation + baseFileName +
".txt";
fstream file;
file.open( filename.
asChar(), ios::in );
if ( !file.is_open() )
char line[256] = "";
file.getline( line, sizeof line );
if ( line != Autodesk_Cache_File )
int channels = 0;
while ( file.good() )
{
std::string key, value;
if ( !readKeyAndValue( file, key, value ) ) { status =
MS::kFailure;
break; }
if ( key == cacheType_Type )
{
if ( value == OneFilePerFrame )
else if ( value == OneFile )
else
{
break;
}
}
else if ( key == cacheType_Format )
{
if ( value != fCacheFormatName.asChar() )
{
break;
}
}
else if ( key == time_Range )
{
}
else if ( key == cacheTimePerFrame_TimePerFrame )
{
double time = 0.0;
sscanf( value.c_str(), "%lf", &time );
if ( time == 0.0 )
{
break;
}
}
else if ( key == cacheVersion_Version )
{
}
else if ( key == extra )
{
}
else if ( key == Channels )
{
sscanf( value.c_str(), "%d", &channels );
break;
}
}
if ( status && !channels )
{
}
if ( status )
{
while ( file.good() )
{
std::string key, channelName;
if ( !readKeyAndValue( file, key, channelName ) ) { status =
MS::kFailure;
break; }
if ( key != ChannelName )
continue;
std::string interpretation, dataTypeString, samplingTypeString;
if ( !readValue( file, ChannelType, dataTypeString ) ) { status =
MS::kFailure;
break; }
if ( !readValue( file, ChannelInterpretation, interpretation ) ) { status =
MS::kFailure;
break; }
if ( !readValue( file, SamplingType, samplingTypeString ) ) { status =
MS::kFailure;
break; }
if ( dataTypeString == Double )
{
}
else if ( dataTypeString == DoubleArray )
{
}
else if ( dataTypeString == DoubleVectorArray )
{
}
else if ( dataTypeString == Int32Array )
{
}
else if ( dataTypeString == FloatArray )
{
}
else if ( dataTypeString == FloatVectorArray )
{
}
else
{
break;
}
if ( samplingTypeString == Regular )
{
}
else if ( samplingTypeString == Irregular )
{
}
else
{
break;
}
MTime samplingRate, startTime, endTime;
if ( !readValue( file, SamplingRate, samplingRate ) ) { status =
MS::kFailure;
break; }
if ( !readValue( file, StartTime, startTime ) ) { status =
MS::kFailure;
break; }
if ( !readValue( file, EndTime, endTime ) ) { status =
MS::kFailure;
break; }
dataType,
samplingType,
samplingRate,
startTime,
endTime,
&status );
if ( !status )
break;
}
}
file.close();
return status;
}
const MString& descriptionFileLocation,
{
const MString filename = descriptionFileLocation + baseFileName +
".txt";
fstream file;
file.open( filename.
asChar(), ios::out );
if ( !file.is_open() )
file << Autodesk_Cache_File << "\n";
file << cacheType_Type << "=";
switch ( distribution )
{
default:
break;
}
file << "\n";
file << cacheType_Format << "=" << fCacheFormatName << "\n";
MTime startTime, endTime;
file << cacheTimePerFrame_TimePerFrame <<
"=" << (int)timePerFrame.
as(
MTime::k6000FPS ) <<
"\n";
file << cacheVersion_Version << "=" << "2.0" << "\n";
for (
unsigned i=0; i<info.
length(); ++i )
file << extra << "=" << info[i] << "\n";
file << Channels << "=" << channels << "\n";
for ( unsigned i=0; i<channels; ++i )
{
file << ChannelName << "=" << channelName << "\n";
file << ChannelType << "=";
switch ( dataType )
{
default:
break;
}
file << "\n";
file << ChannelInterpretation << "=" << interpretation << "\n";
file << SamplingType << "=";
switch ( samplingType )
{
default:
break;
}
file << "\n";
}
file.close();
return status;
}
void XmlCacheFormat::startXmlBlock( string& t )
{
fXmlStack.push(t);
fFile << "<" << t << ">\n";
}
void XmlCacheFormat::endXmlBlock()
{
string block = fXmlStack.top();
fFile << "</" << block << ">\n";
fXmlStack.pop();
}
void XmlCacheFormat::writeXmlTagValue( string& tag, string value )
{
for (unsigned int i = 0; i < fXmlStack.size(); i++ )
fFile << "\t";
fFile << "<" << tag << "> ";
fFile << value;
fFile << " </" << tag << ">\n";
}
void XmlCacheFormat::writeXmlTagValue( string& tag, int value )
{
for (unsigned int i = 0; i < fXmlStack.size(); i++ )
fFile << "\t";
fFile << "<" << tag << "> ";
ostringstream oss;
oss << value;
fFile << oss.str();
fFile << " </" << tag << ">\n";
}
bool XmlCacheFormat::readXmlTagValue(
string tag,
MStringArray& values )
{
string endTag = XMLENDTAG(tag);
bool status = true;
if( findXmlStartTag(tag) )
{
string token;
fFile >> token;
while ( !fFile.eof() && token != endTag )
{
values.
append( token.data() );
fFile >> token;
}
}
else
{
status = false;
}
return status;
}
bool XmlCacheFormat::readXmlTagValueInChunk(
string tag,
MStringArray& values )
{
string endTag = XMLENDTAG(tag);
bool status = true;
if( findXmlStartTagInChunk(tag) )
{
string token;
fFile >> token;
while ( !fFile.eof() && token != endTag )
{
values.
append( token.data() );
fFile >> token;
}
}
else
{
status = false;
}
return status;
}
void XmlCacheFormat::readXmlTag( string& value )
{
fFile >> value;
}
bool XmlCacheFormat::findXmlStartTag( string& tag )
{
string tagRead;
string tagExpected = XMLSTARTTAG(tag);
fFile >> tagRead;
while(!fFile.eof() && tagRead != tagExpected)
{
fFile >> tagRead;
}
if( tagRead != tagExpected )
{
}
return ( tagRead == tagExpected );
}
bool XmlCacheFormat::findXmlStartTagInChunk( string& tag )
{
string tagRead;
string tagExpected = XMLSTARTTAG(tag);
string tagEndChunk("</"+chunkTag+">");
fFile >> tagRead;
while((!fFile.eof()) && (tagRead != tagExpected) && (tagRead != tagEndChunk))
{
fFile >> tagRead;
}
if( (tagRead != tagExpected) && (tagRead != tagEndChunk) )
{
}
return ( tagRead == tagExpected );
}
bool XmlCacheFormat::findXmlEndTag(string& tag)
{
string tagRead;
string tagExpected("</"+tag+">");
fFile >> tagRead;
if( tagRead != tagExpected )
{
}
return ( tagRead == tagExpected );
}
void XmlCacheFormat::writeXmlValue( string& value )
{
fFile << value << " ";
}
void XmlCacheFormat::writeXmlValue( double value )
{
fFile << value << " ";
}
void XmlCacheFormat::writeXmlValue( float value )
{
fFile << value << " ";
}
void XmlCacheFormat::writeXmlValue( int value )
{
fFile << value << " ";
}
{
MFnPlugin plugin( obj, PLUGIN_COMPANY,
"1.0",
"Any" );
plugin.registerCacheFormat(
XmlCacheFormat::translatorName(),
XmlCacheFormat::creator
);
}
{
plugin.deregisterCacheFormat( XmlCacheFormat::translatorName() );
}