#include <string>
#include <stack>
#include <fstream>
#include <sstream>
#include <assert.h>
#include <stdlib.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();
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";
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");
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;
}
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() );
}