#include <maya/MGlobal.h>
#include <maya/MCommandResult.h>
#include <maya/MIOStream.h>
#include <maya/MDagPath.h>
#include <maya/MItDag.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "atomNodeNameReplacer.h"
#include <vector>
#include <map>
const char kSemiColonChar = ';';
const char kSpaceChar = ' ';
const char kTabChar = '\t';
const char kHashChar = '#';
const char kNewLineChar = '\n';
const char kBraceLeftChar = '{';
const char kBraceRightChar = '}';
const char kDoubleQuoteChar = '"';
streamIO::streamIO()
{
}
double streamIO::asDouble (ifstream &clipFile)
{
advance(clipFile);
double value;
clipFile >> value;
return (value);
}
bool streamIO::isNextNumeric(ifstream &clipFile)
{
bool numeric = false;
advance(clipFile);
char next = clipFile.peek();
if ((next >= '0' && next <= '9')
||(next == '.') || (next == '-')) {
numeric = true;
}
return numeric;
}
void streamIO::advance (ifstream &clipFile)
{
while (clipFile) {
clipFile >> ws;
char next = clipFile.peek();
if (next == kSemiColonChar) {
clipFile.ignore(1, kSemiColonChar);
continue;
}
if (next == kHashChar) {
clipFile.ignore(INT_MAX, kNewLineChar);
continue;
}
break;
}
}
char* streamIO::asWord (ifstream &clipFile, bool includeWS )
{
static const int kBufLength = 1024;
static char string[kBufLength];
advance(clipFile);
char *c = string;
clipFile.read (c, 1);
if (*c == kDoubleQuoteChar) {
clipFile.read(c, 1);
while(!clipFile.eof() && (*c != kDoubleQuoteChar)) {
c++;
if (c-string >= kBufLength) {
break;
}
clipFile.read(c, 1);
}
} else {
if (*c == kBraceLeftChar || *c == kBraceRightChar) {
c++;
} else {
while(!clipFile.eof() && (*c != kSemiColonChar)) {
if (!includeWS && ((*c == kSpaceChar) || (*c == kTabChar))) {
break;
}
c++;
if (c-string >= kBufLength) {
break;
}
clipFile.read(c, 1);
}
}
}
*c = 0x00;
return (string);
}
char streamIO::asChar (ifstream &clipFile)
{
advance(clipFile);
return clipFile.get();
}
int streamIO::asInt (ifstream &clipFile)
{
advance(clipFile);
int value;
clipFile >> value;
return (value);
}
short streamIO::asShort (ifstream &clipFile)
{
advance(clipFile);
short value;
clipFile >> value;
return (value);
}
bool streamIO::doesFileExist(
const MString &fileName)
{
{
int isFile;
{
{
return (isFile > 0);
}
}
}
return false;
}
atomNodeNameReplacer::atomNodeNameReplacer(ReplaceType type,
MSelectionList &sList, std::vector<unsigned int> &depths,
:fReplaceType(type), fPrefix(prefix), fSuffix(suffix), fSearch(search), fReplace(replace), fMapFile(mapFile),
fSelectionList(sList),
fDepths(depths),
fAddMainPlaceholderNamespace(0)
{
if(fReplaceType == eMapFile && fMapFile.length() >0)
{
if(doesFileExist(fileName) == false)
{
fReplaceType = eHierarchy;
return;
}
ifstream mapAnim(fileName.
asChar());
if(mapAnim.is_open() == false)
{
fReplaceType = eHierarchy;
return;
}
else if(mapAnim.good() == false)
{
fReplaceType = eHierarchy;
mapAnim.close();
return;
}
char *dataType;
bool current = true;
while (!mapAnim.eof())
{
advance(mapAnim);
dataType = asWord(mapAnim);
if(dataType != NULL)
{
if(string.length() >0)
{
if(current)
fCurrentNames.append(string);
else fNewNames.append(string);
current = !current;
}
}
}
if(fNewNames.length() != fCurrentNames.length() && fCurrentNames.length() > 0)
fCurrentNames.remove(fCurrentNames.length() -1);
mapAnim.close();
}
}
void
atomNodeNameReplacer::setAddMainPlaceholderNamespace(bool val)
{
fAddMainPlaceholderNamespace = val;
}
bool atomNodeNameReplacer::matchByName()
{
return fReplaceType == eHierarchy ? false : true;
}
static MString stringSearchAndReplace(
{
MString inputString(originalString);
bool notDone = true;
while ( notDone )
{
int iw = inputString.indexW( searchString );
if ( iw < 0 )
{
newString += inputString;
notDone = false;
}
else
{
if ( iw > 0 )
int lastIndex = inputString.
numChars() - 1;
int firstIndex = iw + searchString.
numChars();
if ( firstIndex <= lastIndex )
secondPart = inputString.
substringW( firstIndex, lastIndex );
newString = newString + firstPart + replaceString;
inputString = secondPart;
}
}
return newString;
}
{
if(fReplaceType == eSearchReplace)
{
if(fSearch.length() > 0)
{
newString = stringSearchAndReplace( searchString, replaceString, inputString );
}
if(fPrefix.length() >0)
newString = fPrefix + newString;
if(fSuffix.length() > 0)
newString = newString + fSuffix;
return newString;
else
return name;
}
else if(fReplaceType == eMapFile)
{
return replacedNameFromMapFileStrings(name);
}
return name;
}
MString atomNodeNameReplacer::replacedNameFromMapFileStrings(
MString ¤t)
{
for(unsigned int i=0;i<fCurrentNames.length();++i)
{
if(fCurrentNames[i] == current && i < fNewNames.
length())
return fNewNames[i];
}
return current;
}
bool atomNodeNameReplacer::findNode(atomNodeNameReplacer::NodeType nodeType,
MString &nodeName,
unsigned int depth,
unsigned int childCount)
{
bool selected = false;
bool isDag = (nodeType == atomNodeNameReplacer::eDag);
bool isShape = (nodeType == atomNodeNameReplacer::eShape);
bool isAnimLayer = (nodeType == atomNodeNameReplacer::eAnimLayer);
if(matchByName() || nodeType == atomNodeNameReplacer::eDepend || isAnimLayer)
{
nodeName = replacedName(nodeName);
if (fAddMainPlaceholderNamespace) {
tmpName += nodeName;
nodeName = tmpName;
}
{
nodeName.
split(
':',nameSpaces);
unsigned int len = nameSpaces.
length();
if(len > 1)
{
nodeName = nameSpaces[len -1];
selected = true;
}
}
else
selected = true;
if(selected)
{
if(isAnimLayer)
return true;
if(nodeType != atomNodeNameReplacer::eDepend)
{
{
selected = fSelectionList.hasItem(dagPath);
}
}
else
{
{
selected = fSelectionList.hasItem(depNode);
}
}
return selected;
}
return false;
}
else if( isDag || isShape)
{
int i =0;
int lastOne = -1;
int length = (int)fSelectionList.length();
for(i = 0; i < (int)length;++i)
{
MStatus localStat = fSelectionList.getDagPath(i,dagPath);
{
unsigned int sceneDepth = fDepths[i];
if((isDag && selectedIsShape) || (isShape && (selectedIsShape ==false)))
{
selected = false;
lastOne = i;
continue;
}
else if(isShape && selectedIsShape == true)
{
selected = true;
lastOne =i;
break;
}
else if(sceneDepth < depth)
{
selected = false;
break;
}
else if(sceneDepth == depth)
{
selected = true;
lastOne =i;
break;
}
lastOne =i;
}
}
for(int k = lastOne; k >=0 ;--k)
{
fSelectionList.remove(k);
fDepths.erase( fDepths.begin( ) + k );
}
}
return selected;
}