/structural/supporting_matl_misc_and_old/support/matrixTypeGenerator/ver_2/BLAS_LAPACK/generator/src/laFortRead.cpp
C++ | 6229 lines | 4928 code | 739 blank | 562 comment | 858 complexity | c5e787dc7056794a46af21b0c488ee4a MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause
- #include <fstream>
- #include <ostream>
- #include <iostream>
- #include <vector>
- #include <list>
- #include <string>
- #include <cctype>
- #include <cstdlib>
- #include <sstream>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include "eMsg.h"
- using namespace std;
- #define LINE_SIZE 1024
- // this is a list of all the globals
- // objList<fortranType> fTypes;
- // objList<cppType_basic> bTypes;
- // objList<cppType_template> tTypes;
- // list<fortranFunction> fortranFunctionList;
- // objList<mapText> textMaps;
- // objList<cppFunctionName> cppFuncs;
- // cppType* voidType;
- // sourceFile fortranSource;
- // sourceFile cppPrototype;
- // sourceFile cppDefinition;
- // commandSource_recording recorder;
- char* copyLine(const char* ln)
- {
- unsigned long L=0;
- if(ln==NULL)
- throw eMsg("fortranFunction: the input array is NULL");
- while(ln[L]!=0)
- L++;
- L++;
- char* retVal=new char[L];
- if(retVal==NULL)
- throw eMsg("copyLine(): Failed to allocate memory");
- for(unsigned long i=0;i<L;i++)
- retVal[i]=ln[i];
- return retVal;
- }
- bool strEqual(const char* lhs, const char* rhs)
- {
- if(lhs==NULL || rhs==NULL)
- {
- if(lhs==rhs)
- return true;
- return false;
- }
- unsigned long i=0;
- while(lhs[i]!=0 && rhs!=0)
- {
- if(lhs[i]!=rhs[i])
- return false;
- i++;
- }
- if(rhs[i]!=lhs[i])
- return false;
- return true;
- }
- bool strFilterEqual(const char* fltr, const char* test)
- {
- unsigned long at=0;
- unsigned long atFlt=0;
- // Null filter means everything matches automatically
- if(fltr[atFlt]==0)
- return true;
- while(fltr[atFlt]!=0 && test[at]!=0)
- {
- // Are we matching tokens?
- if(fltr[atFlt]=='*')
- {
- atFlt++;
- // test to see if there is a trailing token, otherwise we return true
- if(fltr[atFlt]==0)
- return true;
- // The function of a '**' is undefined
- if(fltr[atFlt]=='*')
- throw eMsg("strFilterEqual(): '**' type filter not allowed");
- // There is a token and thus we need to find a match
- bool seekMatch=true;
- while(seekMatch)
- {
- // seek forward until a possible match
- while(test[at]!=0 && test[at]!=fltr[atFlt])
- at++;
- if(test[at]==0)
- return false;
- unsigned long cnt=0;
- while(test[at+cnt]!=0 && fltr[atFlt+cnt]!=0 && fltr[atFlt+cnt]!='*' && test[at+cnt]==fltr[atFlt+cnt])
- cnt++;
- // test to see if the test string ran out
- if( test[at+cnt]==0 )
- // this is ok only if we are at the end of the filter
- return fltr[atFlt+cnt]==0 || (fltr[atFlt+cnt]=='*' && fltr[atFlt+cnt+1]==0);
- // string didn;t run out test to see if the token was matched
- if( fltr[atFlt+cnt]=='*' || fltr[atFlt+cnt]==0 )
- {
- at+=cnt;
- atFlt+=cnt;
- // Test if the filter has ended, if so then we can complete the test
- if( fltr[atFlt]==0 )
- return test[at]==0;
- // everything matched and nothing has ended so we keep searching
- seekMatch=false;
- }
- else
- {
- // could not find token match, try again starting from the next character
- at++;
- }
- }
- }
- else
- {
- // no '*' has been found so a simple search
- if(test[at]!=fltr[atFlt])
- return false;
- at++;
- atFlt++;
- }
- }
- // reached the end of test or filter thus only successful if both at the end
- return test[at]==fltr[atFlt];
- }
- class wordList
- {
- private:
- unsigned long sz;
- vector<unsigned long> start;
- vector<unsigned long> end;
- char* line;
- public:
- wordList& setLine(const char* line_in)
- {
- if(line_in==NULL)
- {
- sz=0;
- return *this;
- }
- unsigned long at=0;
- sz=0;
- while(line_in[at]!=0)
- {
- while(line_in[at]!=0 && isspace(line_in[at]))
- at++;
- if(line_in[at]!=0)
- sz++;
- while(line_in[at]!=0 && !isspace(line_in[at]))
- at++;
- }
- start.resize(sz);
- end.resize(sz);
- if(line!=NULL)
- delete[] line;
- line=new char[at+1];
- at=0;
- unsigned long atWrd=0;
- while(line_in[at]!=0)
- {
- while(line_in[at]!=0 && isspace(line_in[at]))
- {
- line[at]=line_in[at];
- at++;
- }
- if(line_in[at]!=0)
- start[atWrd]=at;
- while(line_in[at]!=0 && !isspace(line_in[at]))
- {
- end[atWrd]=at;
- line[at]=line_in[at];
- at++;
- }
- atWrd++;
- }
- line[at]=0;
- return *this;
- }
- wordList(const char* line_in=NULL) : sz(0) , start(0) , end(0) , line(NULL)
- {
- setLine(line_in);
- }
- ~wordList()
- {
- if(line!=NULL)
- delete[] line;
- }
- wordList& getWord(unsigned long at, char* word, unsigned long bfSz)
- {
- if(at>=sz)
- throw eMsg("wordList: The argument does not exist");
- unsigned long wSz=end[at]-start[at]+1;
- if(wSz>bfSz+1)
- throw eMsg("wordList: the buffer provided is not big enough");
- unsigned long i;
- for(i=0;i<wSz;i++)
- word[i]=line[start[at]+i];
- word[i]=0;
- return *this;
- }
- unsigned long size()
- {
- return sz;
- }
- unsigned long getStart(unsigned long at)
- {
- if(at>=sz)
- throw eMsg("wordList: the requested start does not exist");
- return start[at];
- }
- unsigned long getEnd(unsigned long at)
- {
- if(at>=sz)
- throw eMsg("wordList: the requested end does not exist");
- return end[at];
- }
- };
- class cppType
- {
- protected:
- static int typeID;
- int myID;
- friend class cppType_template;
- virtual void addMyID(list<int>& idList) const
- {
- idList.push_back(myID);
- }
- public:
- cppType()
- {
- myID=typeID++;
- }
- cppType(const cppType& rhs)
- {
- this->myID=rhs.myID;
- }
- virtual ~cppType()
- {}
- // virtual const char* getName()=0;
- virtual void printType(ostream& out)=0;
- virtual void printDescription(ostream& out)=0;
- virtual cppType* copyType()=0;
- bool operator==(const cppType& rhs)
- {
- list<int> lhsType;
- list<int> rhsType;
- this->addMyID(lhsType);
- rhs.addMyID(rhsType);
- unsigned long tpCount=lhsType.size();
- if(tpCount!=rhsType.size())
- return false;
- list<int>::iterator itLhs=lhsType.begin();
- list<int>::iterator itRhs=rhsType.begin();
- for(unsigned long i=0;i<tpCount;i++)
- {
- if(*itLhs!=*itRhs)
- return false;
- itLhs++;
- itRhs++;
- }
- return true;
- }
- bool operator!=(const cppType& rhs)
- {
- return !(*this==rhs);
- }
- };
- int cppType::typeID=0;
- class cppType_basic : public cppType
- {
- private:
- char* name;
- char* description;
- public:
- cppType_basic& setName(const char* name_in)
- {
- unsigned long i=0;
- while(name_in[i]!=0)
- i++;
- i++;
- if(name!=NULL)
- delete[] name;
- name=new char[i];
- if(name==NULL)
- throw eMsg("cppType_basic::setName(): Failed to allocate memory");
- for(unsigned long j=0;j<i;j++)
- name[j]=name_in[j];
- return *this;
- }
- cppType_basic& setDescription(const char* desc_in)
- {
- unsigned long i=0;
- while(desc_in[i]!=0)
- i++;
- i++;
- if(description!=NULL)
- delete[] description;
- description=new char[i];
- if(description==NULL)
- throw eMsg("cppType_basic::setDescription(): Failed to allocate memory");
- for(unsigned long j=0;j<i;j++)
- description[j]=desc_in[j];
- return *this;
- }
- cppType_basic() : name(NULL) , description(NULL)
- {}
- cppType_basic(const char* name_in, const char* desc_in) : name(NULL) , description(NULL)
- {
- setName(name_in);
- setDescription(desc_in);
- }
- cppType_basic(const cppType_basic& rhs) : cppType(rhs) , name(NULL) , description(NULL)
- {
- if(rhs.name!=NULL)
- this->setName(rhs.name);
- if(rhs.description)
- this->setDescription(rhs.description);
- }
- virtual ~cppType_basic()
- {
- if(name!=NULL)
- delete[] name;
- if(description!=NULL)
- delete[] description;
- }
- virtual void printType(ostream& out)
- {
- if(name==NULL)
- return;
- out << name;
- }
- virtual void printDescription(ostream& out)
- {
- if(description==NULL)
- return;
- out << name << ": " << description;
- }
- virtual cppType* copyType()
- {
- return new cppType_basic(*this);
- }
- const char* getName()
- {
- return name;
- }
- };
- class cppType_template : public cppType_basic
- {
- private:
- vector<bool> isType;
- vector<cppType*> args;
- vector<int> vals;
- vector<bool> canDelete;
- void addMyID(list<int>& idList) const
- {
- this->cppType::addMyID(idList);
- for(unsigned long i=0;i<args.size();i++)
- {
- if(isType[i])
- {
- if(args[i]==NULL)
- throw eMsg("cppType_template: template parameter has not been set");
- args[i]->addMyID(idList);
- }
- else
- {
- idList.push_back(vals[i]);
- }
- }
- }
- public:
- cppType_template(const char* name_in,const char* desc_in,unsigned long size_in) : cppType_basic(name_in,desc_in) , isType(size_in,true) , args(size_in,NULL) , vals(size_in,0) , canDelete(size_in,false)
- {}
- cppType_template(unsigned long size_in=0) : isType(size_in,true) , args(size_in,NULL) , vals(size_in,0) , canDelete(size_in,false)
- {}
- cppType_template(const cppType_template& rhs) : cppType_basic(rhs) , isType(rhs.isType) , args(rhs.args) , vals(rhs.vals) , canDelete(rhs.isType.size(),false)
- {}
- virtual ~cppType_template()
- {
- for(unsigned long i=0;i<args.size();i++)
- {
- if(canDelete[i] && args[i]!=NULL)
- delete args[i];
- }
- }
- cppType_template& resize(unsigned long size_in=0)
- {
- isType.resize(size_in,true);
- args.resize(size_in,NULL);
- vals.resize(size_in,0);
- canDelete.resize(size_in,false);
- return *this;
- }
- unsigned long size()
- {
- return isType.size();
- }
- cppType_template& clearTypes()
- {
- for(unsigned long i=0;i<isType.size();i++)
- {
- if(isType[i])
- args[i]=NULL;
- }
- return *this;
- }
- cppType_template& setArgType(unsigned long at, cppType* arg, bool canDelete_in)
- {
- if(at>=isType.size())
- throw eMsg("cppType_template: The argument does not exist");
- if(args[at]!=NULL && args[at]!=arg && canDelete[at])
- delete args[at];
- isType[at]=true;
- args[at]=arg;
- canDelete[at]=canDelete_in;
- return *this;
- }
- cppType_template& setArgType(unsigned long at, int val)
- {
- if(at>=isType.size())
- throw eMsg("cppType_template: The argument does not exist");
- isType[at]=true;
- vals[at]=val;
- return *this;
- }
- virtual void printType(ostream& out)
- {
- this->cppType_basic::printType(out);
- out << '<';
- for(unsigned long i=0;i<isType.size();i++)
- {
- if(i>0)
- out << ',';
- out << ' ';
- if(isType[i] && args[i]!=NULL)
- args[i]->printType(out);
- else if(!isType[i])
- out << vals[i];
- else
- throw eMsg("cppType_template: the template type has not been set");
- out << ' ';
- }
- out << '>';
- }
- virtual void printDescription(ostream& out)
- {
- this->cppType_basic::printDescription(out);
- }
- virtual cppType* copyType()
- {
- return new cppType_template(*this);
- }
- };
- class fortranType
- {
- private:
- vector<vector<const char*> > fTypeName;
- cppType* cType;
- public:
- fortranType(unsigned long sz_in) : fTypeName(sz_in,vector<const char*>(0,NULL))
- {}
- fortranType& setSize(unsigned long sz_in)
- {
- fTypeName.resize(sz_in);
- return *this;
- }
- fortranType& setTypeSize(unsigned long typeAt, unsigned long wordCnt)
- {
- if(typeAt>=fTypeName.size())
- throw eMsg("fortranType: the requested type does not exist");
- fTypeName[typeAt].resize(wordCnt);
- return *this;
- }
- fortranType& setText(unsigned long typeAt,unsigned long at, const char* wrd)
- {
- if(typeAt>=fTypeName.size())
- throw eMsg("fortranType: the requested type does not exist");
- if(at>=fTypeName[typeAt].size())
- throw eMsg("fortranType: the word in the type does not exist");
- fTypeName[typeAt][at]=wrd;
- return *this;
- }
- fortranType& setType(cppType* cType_in)
- {
- if(cType_in==NULL)
- throw eMsg("fortranType: the type is NULL");
- cType=cType_in;
- return *this;
- }
- bool isType(wordList& line,unsigned long& end)
- {
- char word[LINE_SIZE];
- for(unsigned long i=0;i<fTypeName.size();i++)
- {
- bool fail=false;
- unsigned long j;
- for(j=0; j<fTypeName[i].size();j++)
- {
- line.getWord(j,word,LINE_SIZE);
- if(!strEqual(word,fTypeName[i][j]))
- fail=true;
- else
- end=line.getEnd(j);
- }
- if(!fail)
- {
- return true;
- }
- }
- return false;
- }
- bool isType(wordList& line)
- {
- unsigned long end;
- return this->isType(line,end);
- }
- cppType* getType()
- {
- return cType;
- }
- };
- template<class oType>
- class objList
- {
- private:
- list<oType*> typeObjs;
- bool own;
- vector<oType*> vct;
- bool vctGood;
- public:
- objList(bool own_in=true) : own(own_in) , vctGood(false)
- {}
- ~objList()
- {
- if(own)
- {
- for(typename list<oType*>::iterator it=typeObjs.begin();it!=typeObjs.end();it++)
- delete *it;
- }
- }
- unsigned long size()
- {
- return typeObjs.size();
- }
- objList& addObj(oType* newType)
- {
- if(newType==NULL)
- throw eMsg("objList: the new type is NULL");
- typeObjs.push_back(newType);
- vctGood=false;
- return *this;
- }
- oType* getObj(unsigned long at)
- {
- if(at>=typeObjs.size())
- throw eMsg("objList: The requested type does not exist");
- if(!vctGood)
- {
- vct.resize(typeObjs.size(),NULL);
- typename list<oType*>::iterator it=typeObjs.begin();
- for(unsigned long i=0;i<typeObjs.size();i++)
- {
- vct[i]=*it;
- it++;
- }
- vctGood=true;
- }
- return vct[at];
- }
- objList& deleteBack(unsigned long cnt=1)
- {
- if(cnt>typeObjs.size())
- cnt=typeObjs.size();
- for(unsigned long i=0;i<cnt;i++)
- {
- if(own)
- delete typeObjs.back();
- typeObjs.pop_back();
- }
- return *this;
- }
- };
- objList<fortranType> fTypes;
- objList<cppType_basic> bTypes;
- objList<cppType_template> tTypes;
- cppType* voidType;
- cppType* readType(wordList& line, unsigned long& end)
- {
- for(unsigned long i=0;i<fTypes.size();i++)
- {
- fortranType* fTp=fTypes.getObj(i);
- if(fTp->isType(line,end))
- {
- end++;
- return fTp->getType();
- }
- }
- return NULL;
- }
- cppType* readType(wordList& line)
- {
- unsigned long end;
- return readType(line,end);
- }
- class variable
- {
- private:
- char* name;
- cppType* type;
- bool deleteType;
- unsigned long pointerLevel;
- bool isReference;
- bool isConst;
- public:
- variable& setName(const char* name_in)
- {
- if(name!=NULL)
- delete[] name;
- if(name_in==NULL)
- {
- name=NULL;
- return *this;
- }
- unsigned long L=0;
- while(name_in[L]!=0)
- L++;
- L++;
- name=new char[L];
- if(name==NULL)
- throw eMsg("variable: Failed to allocate memory");
- for(unsigned long i=0;i<L;i++)
- name[i]=name_in[i];
- return *this;
- }
- variable& setType(cppType* type_in,bool deleteType_in=false)
- {
- if(type!=NULL && type!=type_in && deleteType)
- delete type;
- type=type_in;
- deleteType=deleteType_in;
- return *this;
- }
- variable& setPointerLevel(unsigned long lvl_in)
- {
- pointerLevel=lvl_in;
- return *this;
- }
- variable& setReference(bool isRef_in)
- {
- isReference=isRef_in;
- return *this;
- }
- variable& setConst(bool isConst_in)
- {
- isConst=isConst_in;
- return *this;
- }
- variable(cppType* type_in=NULL,const char* name_in=NULL,long pntrLevel=0,bool isReference_in=false,bool isConst_in=false,bool deleteType_in=false) : name(NULL) , type(type_in) , deleteType(false) , pointerLevel(pntrLevel) , isReference(isReference_in) , isConst(isConst_in)
- {
- setType(type_in,deleteType_in);
- setName(name_in);
- }
- variable(const variable& asg) : name(NULL) , type(NULL) , deleteType(false) , pointerLevel(0) , isReference(false) , isConst(false)
- {
- *this=asg;
- }
- ~variable()
- {
- if(name!=NULL)
- delete[] name;
- if(deleteType && type!=NULL)
- delete type;
- }
- variable& operator=(const variable& asg)
- {
- if(name!=NULL)
- delete[] name;
- if(asg.name==NULL)
- this->name=NULL;
- else
- {
- this->name=copyLine(asg.name);
- if(this->name==NULL)
- throw eMsg("variable::operator=(): Failed to allocate memory");
- }
- if(this->deleteType && this->type!=NULL)
- delete this->type;
- if(asg.deleteType)
- {
- if(asg.type==NULL)
- {
- this->type=NULL;
- this->deleteType=false;
- }
- else
- {
- this->type=asg.type->copyType();
- if(this->type==NULL)
- throw eMsg("variable::operator=(): Failed to allocate memory");
- this->deleteType=asg.deleteType;
- }
- }
- else
- {
- this->deleteType=false;
- this->type=asg.type;
- }
- this->pointerLevel=asg.pointerLevel;
- this->isReference=asg.isReference;
- this->isConst=asg.isConst;
- return *this;
- }
- variable& printName(ostream& out)
- {
- if(name==NULL)
- throw eMsg("variable: Name has not been set");
- out << name;
- return *this;
- }
- variable& printDecl(ostream& out)
- {
- if(type==NULL)
- throw eMsg("variable: type has not been set");
- if(isConst)
- out << "const ";
- type->printType(out);
- for(unsigned long i=0;i<pointerLevel;i++)
- out << '*';
- if(isReference)
- out << '&';
- out << ' ';
- this->printName(out);
- return *this;
- }
- cppType* getType()
- {
- return type;
- }
- const char* getName()
- {
- return name;
- }
- };
- struct comment
- {
- char* line;
- bool tagged;
-
- comment() : line(NULL)
- {}
-
- ~comment()
- {
- if(line!=NULL)
- delete[] line;
- }
-
- comment(const comment& asg)
- {
- if(asg.line!=NULL)
- {
- unsigned long i=0;
- while(asg.line[i]!=0)
- i++;
- this->line=new char[i+1];
- if(this->line==NULL)
- throw eMsg("comment: Failed to allocate memory");
- i=0;
- while(asg.line[i]!=0)
- {
- this->line[i]=asg.line[i];
- i++;
- }
- this->line[i]=0;
- }
- else
- {
- this->line=NULL;
- }
- this->tagged=asg.tagged;
- }
- comment& operator=(const comment& asg)
- {
- if(this->line!=NULL)
- delete[] this->line;
- if(asg.line!=NULL)
- {
- unsigned long i=0;
- while(asg.line[i]!=0)
- i++;
- this->line=new char[i+1];
- if(this->line==NULL)
- throw eMsg("comment: Failed to allocate memory");
- i=0;
- while(asg.line[i]!=0)
- {
- this->line[i]=asg.line[i];
- i++;
- }
- }
- else
- {
- this->line=NULL;
- }
- this->tagged=asg.tagged;
- }
- };
- class nameSource
- {
- protected:
- vector<bool> sourceIsSunk;
- void setSourceSize(unsigned long sz_in)
- {
- sourceIsSunk.resize(sz_in,false);
- }
- public:
- nameSource() : sourceIsSunk(0,false)
- {}
- virtual ~nameSource()
- {}
- virtual unsigned long sourceSize()
- {
- return sourceIsSunk.size();
- }
- virtual const char* getSourceName(unsigned long at)=0;
- virtual const char* getSourceDescription()=0;
- virtual const char* getSourceTermDescription(unsigned long at)=0;
- virtual bool isSunk(unsigned long at)
- {
- if(at>=sourceIsSunk.size())
- throw eMsg("nameSource::isSunk(): The source does not exist");
- return sourceIsSunk[at];
- }
- virtual void setSunk(unsigned long at,bool isSunk=true)
- {
- if(at>=sourceIsSunk.size())
- throw eMsg("nameSource::setSunk(): The source does not exist");
- sourceIsSunk[at]=isSunk;
- }
- };
- class nameSink
- {
- protected:
- vector<nameSource*> sources;
- vector<unsigned long> atSources;
- void setSinkSize(unsigned long sz)
- {
- sources.resize(sz);
- atSources.resize(sz);
- }
- public:
- nameSink()
- {}
- virtual ~nameSink()
- {}
- unsigned long sinkSize()
- {
- return sources.size();
- }
- void setSource(unsigned long atSnk,nameSource* src,unsigned long atSrc)
- {
- if(atSnk>=sources.size())
- throw eMsg("nameSink: The sink does not exist");
- sources[atSnk]=src;
- atSources[atSnk]=atSrc;
- }
- bool sinkSet(unsigned long at)
- {
- if(at>=sources.size())
- throw eMsg("nameSink: The sink does not exist");
- if(sources[at]!=NULL)
- return true;
- return false;
- }
- virtual const char* getSinkDescription()=0;
- virtual const char* getSinkTermDescription(unsigned long at)=0;
- };
- class declaration
- {
- public:
- virtual void printDecl(ostream& out)=0;
- };
- class code
- {
- public:
- code()
- {}
- virtual ~code()
- {}
- virtual void writeCode(ostream& out)=0;
- };
- class function
- {
- public:
- virtual const char* getFunctionName()=0;
- virtual unsigned long getFunctionArgumentCount()=0;
- // virtual const char* getArgName(unsigned long at)=0;
- virtual const char* getArgumentDescription(unsigned long at)=0;
- };
- class nameMap : virtual public nameSource , virtual public nameSink
- {
- public:
- nameMap()
- {}
- virtual ~nameMap()
- {}
- };
- class mappingCode : public nameMap , public code
- {
- public:
- mappingCode()
- {}
- virtual ~mappingCode()
- {}
- };
- enum txtTYPE
- {
- ttINPUT,
- ttOUTPUT,
- ttTYPE,
- ttCUSTOM
- };
- class mapText
- {
- private:
- char* txt;
- unsigned long inCnt;
- unsigned long outCnt;
- unsigned long typeCnt;
- unsigned long custCnt;
- char* desc;
- char** inDesc;
- char** outDesc;
- char** tpDesc;
- char** cstDesc;
- void clearText()
- {
- if(txt!=NULL)
- delete[] txt;
- txt=NULL;
- if(desc!=NULL)
- delete[] desc;
- desc=NULL;
- if(inDesc!=NULL)
- {
- for(unsigned long i=0;i<inCnt;i++)
- {
- if(inDesc[i])
- delete[] inDesc[i];
- }
- delete[] inDesc;
- inDesc=NULL;
- }
- inCnt=0;
- if(outDesc!=NULL)
- {
- for(unsigned long i=0;i<outCnt;i++)
- {
- if(outDesc[i])
- delete[] outDesc[i];
- }
- delete[] outDesc;
- outDesc=NULL;
- }
- outCnt=0;
- if(tpDesc!=NULL)
- {
- for(unsigned long i=0;i<typeCnt;i++)
- {
- if(tpDesc[i])
- delete[] tpDesc[i];
- }
- delete[] tpDesc;
- tpDesc=NULL;
- }
- typeCnt=0;
- if(cstDesc!=NULL)
- {
- for(unsigned long i=0;i<custCnt;i++)
- {
- if(cstDesc[i])
- delete[] cstDesc[i];
- }
- delete[] cstDesc;
- cstDesc=NULL;
- }
- custCnt=0;
- }
- public:
- bool setText(const char* text)
- {
- clearText();
- if(text==NULL)
- {
- txt=NULL;
- return false;
- }
- else
- {
- txt=copyLine(text);
- if(txt==NULL)
- throw eMsg("mapText: Failed to allocate text");
- }
- unsigned long at=0;
- inCnt=0;
- outCnt=0;
- typeCnt=0;
- custCnt=0;
- while(txt[at]!=0)
- {
- if(txt[at]=='@')
- {
- char number[24];
- at++;
- if(txt[at]!='(')
- return false;
- at++;
- txtTYPE tt=ttINPUT;
- if(txt[at]=='o')
- tt=ttOUTPUT;
- else if(txt[at]=='i')
- tt=ttINPUT;
- else if(txt[at]=='t')
- tt=ttTYPE;
- else if(txt[at]=='c')
- tt=ttCUSTOM;
- else
- return false;
- at++;
- unsigned long i=0;
- while(txt[at]!=')' && txt[at]!=0)
- {
- if(!isdigit(txt[at]))
- return false;
- number[i++]=txt[at++];
- }
- number[i]=0;
- if(txt[at]==0)
- return false;
- if(txt[at]!=')')
- return false;
- unsigned long atTerm=atoi(number);
- switch(tt)
- {
- case ttOUTPUT:
- if(outCnt<(atTerm+1))
- outCnt=atTerm+1;
- break;
- case ttINPUT:
- if(inCnt<(atTerm+1))
- inCnt=atTerm+1;
- break;
- case ttTYPE:
- if(typeCnt<(atTerm+1))
- typeCnt=atTerm+1;
- break;
- case ttCUSTOM:
- if(custCnt<(atTerm+1))
- custCnt=atTerm+1;
- break;
- default:
- throw eMsg("mapText: An unexpected error occured");
- }
- }
- at++;
- }
- if(inCnt>0)
- {
- inDesc=new char*[inCnt];
- if(inDesc==NULL)
- throw eMsg("mapText::setText(): Failed to allocate memory");
- for(unsigned long i=0;i<inCnt;i++)
- inDesc[i]=NULL;
- }
- if(outCnt>0)
- {
- outDesc=new char*[outCnt];
- if(outDesc==NULL)
- throw eMsg("mapText::setText(): Failed to allocate memory");
- for(unsigned long i=0;i<outCnt;i++)
- outDesc[i]=NULL;
- }
- if(typeCnt>0)
- {
- tpDesc=new char*[typeCnt];
- if(tpDesc==NULL)
- throw eMsg("mapText::setText(): Failed to allocate memory");
- for(unsigned long i=0;i<typeCnt;i++)
- tpDesc[i]=NULL;
- }
- if(custCnt>0)
- {
- cstDesc=new char*[custCnt];
- if(cstDesc==NULL)
- throw eMsg("mapText::setText(): Failed to allocate memory");
- for(unsigned long i=0;i<custCnt;i++)
- cstDesc[i]=NULL;
- }
- return true;
- };
- mapText(const char* txt_in=NULL) : txt(NULL) , inCnt(0) , outCnt(0) , typeCnt(0) , custCnt(0) , desc(NULL) , inDesc(NULL) , outDesc(NULL) , tpDesc(NULL) , cstDesc(NULL)
- {
- if(txt_in!=NULL)
- {
- if(!setText(txt_in))
- throw eMsg("mapText::mapText(): Cannot create mapText with malformed text");
- }
- }
- ~mapText()
- {
- clearText();
- }
- mapText& setDescription(const char* dsc_in)
- {
- if(desc!=NULL)
- delete[] desc;
- if(dsc_in==NULL)
- desc=NULL;
- else
- {
- desc=copyLine(dsc_in);
- if(desc==NULL)
- throw eMsg("mapText::setDescription(): Failed to allocate memory");
- }
- return *this;
- }
- mapText& setInputDescription(unsigned long at, const char* dsc_in)
- {
- if(at>=inCnt)
- throw eMsg("mapText::setInputDescription(): The input description does not exist");
- if(inDesc==NULL)
- throw eMsg("mapText::setInputDescription(): The container for the input description has not been set");
- if(inDesc[at]!=NULL)
- delete[] inDesc[at];
- if(dsc_in==NULL)
- inDesc[at]=NULL;
- else
- {
- inDesc[at]=copyLine(dsc_in);
- if(inDesc[at]==NULL)
- throw eMsg("mapText::setInputDescription(): Failed to allocate memory");
- }
- return *this;
- }
- mapText& setOutputDescription(unsigned long at, const char* dsc_in)
- {
- if(at>=outCnt)
- throw eMsg("mapText::setOutputDescription(): The input description does not exist");
- if(outDesc==NULL)
- throw eMsg("mapText::setOutputDescription(): The container for the input description has not been set");
- if(outDesc[at]!=NULL)
- delete[] outDesc[at];
- if(dsc_in==NULL)
- outDesc[at]=NULL;
- else
- {
- outDesc[at]=copyLine(dsc_in);
- if(outDesc[at]==NULL)
- throw eMsg("mapText::setOutputDescription(): Failed to allocate memory");
- }
- return *this;
- }
- mapText& setTypeDescription(unsigned long at, const char* dsc_in)
- {
- if(at>=typeCnt)
- throw eMsg("mapText::setTypeDescription(): The input description does not exist");
- if(tpDesc==NULL)
- throw eMsg("mapText::setTypeDescription(): The container for the input description has not been set");
- if(tpDesc[at]!=NULL)
- delete[] tpDesc[at];
- if(dsc_in==NULL)
- tpDesc[at]=NULL;
- else
- {
- tpDesc[at]=copyLine(dsc_in);
- if(tpDesc[at]==NULL)
- throw eMsg("mapText::setTypeDescription(): Failed to allocate memory");
- }
- return *this;
- }
- mapText& setCustomDescription(unsigned long at, const char* dsc_in)
- {
- if(at>=custCnt)
- throw eMsg("mapText::setCustomDescription(): The input description does not exist");
- if(cstDesc==NULL)
- throw eMsg("mapText::setCustomDescription(): The container for the input description has not been set");
- if(cstDesc[at]!=NULL)
- delete[] cstDesc[at];
- if(dsc_in==NULL)
- cstDesc[at]=NULL;
- else
- {
- cstDesc[at]=copyLine(dsc_in);
- if(cstDesc[at]==NULL)
- throw eMsg("mapText::setCustomDescription(): Failed to allocate memory");
- }
- return *this;
- }
- const char* getTxt() const
- {
- return txt;
- }
- unsigned long getInCnt() const
- {
- return inCnt;
- }
- unsigned long getOutCnt() const
- {
- return outCnt;
- }
- unsigned long getTypeCnt() const
- {
- return typeCnt;
- }
- unsigned long getCustCnt() const
- {
- return custCnt;
- }
- const char* getDescription() const
- {
- return desc;
- }
- const char* getInputDescription(unsigned long at) const
- {
- if(at>=inCnt)
- throw eMsg("mapText::getInputDescription(): The requested description does not exist");
- return inDesc[at];
- }
- const char* getOutputDescription(unsigned long at) const
- {
- if(at>=outCnt)
- throw eMsg("mapText::getOutputDescription(): The description does not exist");
- return outDesc[at];
- }
- const char* getTypeDescription(unsigned long at) const
- {
- if(at>=typeCnt)
- throw eMsg("mapText::getTypeDescription(): The description does not exist");
- return tpDesc[at];
- }
- const char* getCustomDescription(unsigned long at) const
- {
- if(at>=custCnt)
- throw eMsg("mapText::getCustomDescription(): The description does not exist");
- return cstDesc[at];
- }
- };
- objList<mapText> textMaps;
- class nameMap_text : public mappingCode
- {
- private:
- const mapText* mpTxt;
- const char* txt;
- unsigned long outCnt;
- unsigned long typeCnt;
- unsigned long custCnt;
- vector<cppType*> types;
- vector<bool> canDelete;
- vector<char*> outNames;
- vector<char*> custNames;
- public:
- nameMap_text& setText(const mapText& mpTxt_in)
- {
- mpTxt=&mpTxt_in;
- this->txt=mpTxt->getTxt();
- this->outCnt=mpTxt->getOutCnt();
- this->typeCnt=mpTxt->getTypeCnt();
- this->custCnt=mpTxt->getCustCnt();
- types.resize(typeCnt,NULL);
- canDelete.resize(typeCnt,false);
- outNames.resize(outCnt,NULL);
- custNames.resize(custCnt,NULL);
- this->setSinkSize(mpTxt->getInCnt());
- this->setSourceSize(outCnt);
- return *this;
- }
- nameMap_text() : mpTxt(NULL) , txt(NULL) , outCnt(0) , typeCnt(0) , custCnt(0) , types(0,NULL) , canDelete(0,false) , outNames(0,NULL) , custNames(0,NULL)
- {}
- nameMap_text(const mapText& mpTxt_in) : mpTxt(NULL) , txt(NULL) , outCnt(0) , typeCnt(0) , custCnt(0) , types(0,NULL) , canDelete(0,false) , outNames(0,NULL) , custNames(0,NULL)
- {
- this->setText(mpTxt_in);
- }
- ~nameMap_text()
- {
- for(unsigned long i=0;i<types.size();i++)
- {
- if(types[i]!=NULL && canDelete[i])
- delete types[i];
- }
- for(unsigned long i=0;i<outNames.size();i++)
- {
- if(outNames[i]!=NULL)
- delete[] outNames[i];
- }
- for(unsigned long i=0;i<custNames.size();i++)
- {
- if(custNames[i]!=NULL)
- delete[] custNames[i];
- }
- }
- nameMap_text& setOutName(unsigned long at,const char* name)
- {
- if(at>=outNames.size())
- throw eMsg("nameMap_text: The nameMap does not have those outputs");
- if(outNames[at]!=NULL)
- delete[] outNames[at];
- outNames[at]=NULL;
- if(name==NULL)
- return *this;
- outNames[at]=copyLine(name);
- if(outNames[at]==NULL)
- throw eMsg("nameMap_text: failed to allocate memory");
- return *this;
- }
- const char* getOutName(unsigned long at)
- {
- if(at>=outNames.size())
- throw eMsg("nameMap_text: The output does not exist");
- return outNames[at];
- }
- unsigned long outNameCount()
- {
- return outNames.size();
- }
- nameMap_text& setCustName(unsigned long at,const char* name)
- {
- if(at>=custNames.size())
- throw eMsg("nameMap_text: The nameMap does not have those customs");
- if(custNames[at]!=NULL)
- delete[] custNames[at];
- custNames[at]=NULL;
- if(name==NULL)
- return *this;
- custNames[at]=copyLine(name);
- if(custNames[at]==NULL)
- throw eMsg("nameMap_text: failed to allocate memory");
- return *this;
- }
- const char* getCustName(unsigned long at)
- {
- if(at>=custNames.size())
- throw eMsg("nameMap_text: The custom does not exist");
- return custNames[at];
- }
- unsigned long custNameCount()
- {
- return custNames.size();
- }
- nameMap_text& setCppType(unsigned long at, cppType* type_in,bool canDelete_in=false)
- {
- if(at>=types.size())
- throw eMsg("nameMap_text: The type does not exist");
- if(types[at]!=NULL && types[at]!=type_in && canDelete[at])
- delete types[at];
- types[at]=type_in;
- canDelete[at]=canDelete_in;
- return *this;
- }
- cppType* getCppType(unsigned long at)
- {
- if(at>=types.size())
- throw eMsg("nameMap_text: The type does not exist");
- return types[at];
- }
- unsigned long getTypeCount()
- {
- return types.size();
- }
- void writeCode(ostream& out)
- {
- if(txt==NULL)
- return;
- unsigned long at=0;
- while(txt[at]!=0)
- {
- if(txt[at]=='@')
- {
- char number[24];
- at++;
- if(txt[at]!='(')
- throw eMsg("nameMap_text: malformed text");
- at++;
- txtTYPE tt=ttINPUT;
- if(txt[at]=='o')
- tt=ttOUTPUT;
- else if(txt[at]=='i')
- tt=ttINPUT;
- else if(txt[at]=='t')
- tt=ttTYPE;
- else if(txt[at]=='c')
- tt=ttCUSTOM;
- else
- throw eMsg("nameMap_text: The text is malformed");
- at++;
- unsigned long i=0;
- while(txt[at]!=')' && txt[at]!=0)
- {
- if(!isdigit(txt[at]))
- throw eMsg("nameMap_text: malformed text");
- number[i++]=txt[at++];
- }
- if(txt[at]==0)
- throw eMsg("nameMap_text: malformed text");
- if(txt[at]!=')')
- throw eMsg("nameMap_text: malformed text");
- number[i]=0;
- unsigned long atTerm=atoi(number);
- switch(tt)
- {
- case ttOUTPUT:
- if(atTerm>=outCnt)
- throw eMsg("nameMap_text: specified an output that does not exist");
- if(outNames[atTerm]==NULL)
- throw eMsg("nameMap_text: not all the outputs have been specified");
- out << outNames[atTerm];
- break;
- case ttINPUT:
- if(atTerm>=this->sinkSize())
- throw eMsg("nameMap_text: specified and input that does not exist");
- if(this->sources[atTerm]==NULL)
- throw eMsg("nameMap_text: the input data has not been specified");
- out << this->sources[atTerm]->getSourceName(this->atSources[atTerm]);
- break;
- case ttTYPE:
- if(atTerm>=typeCnt)
- throw eMsg("nameMap_text: The requested type does not exist");
- if(types[atTerm]==NULL)
- throw eMsg("nameMap_text: The requested type has not been specified");
- types[atTerm]->printType(out);
- break;
- case ttCUSTOM:
- if(atTerm>=custCnt)
- throw eMsg("nameMap_text: The requested custom does not exist");
- if(custNames[atTerm]==NULL)
- throw eMsg("nameMap_text: The requested custom has not been specified");
- out << custNames[atTerm];
- break;
- default:
- throw eMsg("nameMap_text: There was an error trying to fill in a custom item");
- }
- }
- else
- {
- out << txt[at];
- }
- at++;
- }
- out << endl;
- }
- const char* getSinkDescription()
- {
- if(mpTxt==NULL)
- throw eMsg("nameMap_text::getSinkDescription(): The text does not exist");
- return mpTxt->getDescription();
- }
- const char* getSinkTermDescription(unsigned long at)
- {
- if(mpTxt==NULL)
- throw eMsg("nameMap_text::getSinkTermDescription(): The text does not exist");
- return mpTxt->getInputDescription(at);
- }
- const char* getSourceName(unsigned long at)
- {
- if(at>=outCnt)
- throw eMsg("nameMap_text::getSourceName(): The source name does not exist");
- return outNames[at];
- }
- const char* getSourceDescription()
- {
- if(mpTxt==NULL)
- throw eMsg("nameMap_text::getSourceDescription(): The text has not been set");
- return mpTxt->getDescription();
- }
- const char* getSourceTermDescription(unsigned long at)
- {
- if(mpTxt==NULL)
- throw eMsg("nameMap_text::getSourceTermDescription(): The text source has not been set");
- return mpTxt->getOutputDescription(at);
- }
- };
- class returnSink : public mappingCode
- {
- public:
- returnSink()
- {
- this->setSourceSize(0);
- this->setSinkSize(1);
- }
- const char* getSinkDescription()
- {
- return "Returns a varible";
- }
- const char* getSinkTermDescription(unsigned long at)
- {
- return "The variable to be sunk";
- }
- const char* getSourceName(unsigned long at)
- {
- throw eMsg("returnSink::getSourceName(): This object is not meant to return any source information");
- }
- const char* getSourceDescription()
- {
- return "This is an empty source that merely returns a variable";
- }
- const char* getSourceTermDescription(unsigned long at)
- {
- throw eMsg("returnSink::getSourceTermDescription(): This object has not sources");
- }
- void writeCode(ostream& out)
- {
- out << "\treturn " << this->sources[0]->getSourceName(this->atSources[0]) << ';' << endl;
- }
- };
- class returnValue : public mappingCode
- {
- private:
- char* value;
- public:
- returnValue(const char* val=NULL) : value(NULL)
- {
- this->setSourceSize(0);
- this->setSinkSize(0);
- if(val!=NULL)
- {
- value=copyLine(val);
- if(value==NULL)
- throw eMsg("returnValue::returnValue(): Failed to allocate memory");
- }
- }
- ~returnValue()
- {
- if(value!=NULL)
- delete[] value;
- }
- returnValue& setValue(const char* val)
- {
- if(value!=NULL)
- delete[] value;
- if(val==NULL)
- {
- value=NULL;
- return *this;
- }
- value=copyLine(val);
- if(value==NULL)
- throw eMsg("returnValue::setValue(): Failed to allocate memory");
- return *this;
- }
- const char* getSinkDescription()
- {
- return "This is an empty sink that returns a value";
- }
- const char* getSinkTermDescription(unsigned long at)
- {
- throw eMsg("returnValue::getSinkTermDescription(): This is an empty sink");
- }
- const char* getSourceName(unsigned long at)
- {
- throw eMsg("returnValue::getSourceName(): This is an empty source");
- }
- const char* getSourceDescription()
- {
- return "This is an empty source that return a value";
- }
- const char* getSourceTermDescription(unsigned long at)
- {
- throw eMsg("returnValue::getSourceTermDescription(): This is an empty source");
- }
- void writeCode(ostream& out)
- {
- if(value==NULL)
- throw eMsg("returnValue::writeCode(): The value has not been set");
- out << "\treturn " << value << ';' << endl;
- }
- };
- class callingCode : public mappingCode
- {
- protected:
- function* func;
- bool canDelete;
- unsigned long inExtra;
- void printCall(ostream& out)
- {
- if(func==NULL)
- throw eMsg("callingCode::printCall(): The calling code does not have a function to call");
- out << func->getFunctionName() << "( ";
- bool isFirst=true;
- for(unsigned long i=0;i<func->getFunctionArgumentCount();i++)
- {
- if(!isFirst)
- out << " , ";
- out << this->sources[i]->getSourceName(this->atSources[i]);
- isFirst=false;
- }
- out << " )";
- }
- callingCode(unsigned long outCnt,function* func_in=NULL,bool canDelete_in=false, unsigned long inExtra_in=0) : func(func_in) , canDelete(canDelete_in) , inExtra(inExtra_in)
- {
- if(func_in!=NULL)
- this->setSinkSize(func_in->getFunctionArgumentCount());
- this->setSourceSize(outCnt);
- }
- public:
- callingCode(function* func_in=NULL,bool canDelete_in=false) : func(func_in) , canDelete(canDelete_in) , inExtra(0)
- {
- if(func_in!=NULL)
- this->setSinkSize(func_in->getFunctionArgumentCount());
- this->setSourceSize(0);
- }
- virtual ~callingCode()
- {
- if(func!=NULL && canDelete)
- delete func;
- }
- virtual void setFunction(function* func_in,bool canDelete_in=false)
- {
- if(func!=NULL && func!=func_in && canDelete)
- delete func;
- func=func_in;
- canDelete=canDelete_in;
- if(func!=NULL)
- this->setSinkSize(func->getFunctionArgumentCount()+inExtra);
- }
- virtual void writeCode(ostream& out)
- {
- out << '\t';
- this->printCall(out);
- out << ';' << endl;
- }
- virtual const char* getSourceName(unsigned long at)
- {
- throw eMsg("callingCode::getSourceName(): This code provides no sources");
- }
- virtual const char* getSourceDescription()
- {
- return "This calls a function, no new variables are created";
- }
- virtual const char* getSourceTermDescription(unsigned long at)
- {
- throw eMsg("callingCode::getSourceDescription(): This code provides no sources");
- }
- virtual const char* getSinkDescription()
- {
- return "This calls a function where each argument is an input to this code segment";
- }
- virtual const char* getSinkTermDescription(unsigned long at)
- {
- if(func==NULL)
- throw eMsg("callingCode::getSinkTermDescription(): The function has not been set");
- if(at>=this->sinkSize())
- throw eMsg("callingCode::getSinkTermDescription(): The sink does not exist");
- return func->getArgumentDescription(at);
- }
- };
- class callingCode_return : public callingCode
- {
- public:
- callingCode_return(function* func_in=NULL,bool canDelete_in=false) : callingCode(func_in,canDelete_in)
- {}
- void writeCode(ostream& out)
- {
- out << "\treturn ";
- this->printCall(out);
- out << ';' << endl;
- }
- const char* getSinkDescription()
- {
- return "return a function call where each argument is an input to this code segment";
- }
- const char* getSourceDescription()
- {
- return "This calls a function, no new variables are created because it returns";
- }
- };
- class callingCode_assignNew : public callingCode
- {
- private:
- cppType* retType;
- char* retVar;
- bool deleteRet;
- public:
- callingCode_assignNew(function* func_in=NULL,bool canDelete_in=false, cppType* retType_in=NULL, const char* retVar_in=NULL, bool deleteRet_in=false) : callingCode(1,func_in,canDelete_in) , retType(retType_in) , retVar(NULL) , deleteRet(deleteRet_in)
- {
- if(retVar_in!=NULL)
- {
- retVar=copyLine(retVar_in);
- if(retVar==NULL)
- throw eMsg("callingCode_assignNew::callingCode_assignNew(): Failed to allocate memory");
- }
- }
- ~callingCode_assignNew()
- {
- if(retType!=NULL && deleteRet)
- delete retType;
- if(retVar!=NULL)
- delete[] retVar;
- }
- callingCode_assignNew& setVariable(cppType* retType_in,const char* retVar_in,bool deleteRet=false)
- {
- if(retType!=NULL && deleteRet)
- delete retType;
- if(retVar!=NULL)
- delete[] retVar;
- retType=retType_in;
- if(retVar_in==NULL)
- {
- retVar=NULL;
- }
- else
- {
- retVar=copyLine(retVar_in);
- if(retVar==NULL)
- throw eMsg("callingCode_assignNew::setVariable(): Failed to allocate memory");
- }
- return *this;
- }
- void writeCode(ostream& out)
- {
- if(retType==NULL || retVar==NULL)
- throw eMsg("callingCode_assignNew::writeCode(): Not all the inputs have been specified");
- out << '\t';
- retType->printType(out);
- out << ' ' << retVar << '=';
- this->printCall(out);
- out << ';' << endl;
- }
- const char* getSinkDescription()
- {
- return "return a function call where each argument is an input to this code segment";
- }
- const char* getSourceName(unsigned long at)
- {
- if(at!=0)
- throw eMsg("callingCode_assignNew::getSourceName(): The output does not exist");
- if(retVar==NULL)
- throw eMsg("callingCode_assignNew::getSourceName(): The output has not been specified");
- return retVar;
- }
- const char* getSourceDescription()
- {
- return "The calls a function, the return value is assigned to this code segments output variable";
- }
- const char* getSourceTermDescription(unsigned long at)
- {
- if(at!=0)
- throw eMsg("callingCode_assignNew::getSourceTermDescription(): The output does not exist");
- return "The variable return value is assigned to";
- }
- };
- class callingCode_assignOld : public callingCode
- {
- public:
- callingCode_assignOld(function* func_in=NULL,bool canDelete_in=false) : callingCode(0,func_in,canDelete_in,1)
- {}
- void writeCode(ostream& out)
- {
- unsigned long atSink=this->sinkSize()-1;
- out << '\t' << this->sources[atSink]->getSourceName(this->atSources[atSink]) << '=';
- this->printCall(out);
- out << ';' << endl;
- }
- const char* getSinkDescription()
- {
- return "return a function call where each argument is an input to this code segment, the return value is assigned to an old variable";
- }
- const char* getSourceDescription()
- {
- return "This calls a function, no new variables are created because it returns";
- }
- const char* getSinkTermDescription(unsigned long at)
- {
- if(at>=this->sinkSize())
- throw eMsg("callingCode_assignOld::getSinkTermDescription(): THe sink does not exist");
- if(at<(this->sinkSize()-1))
- return callingCode::getSinkTermDescription(at);
- return "The variable where the return value is assigned";
- }
- };
- class cppFunctionCode : public declaration
- {
- public:
- virtual void writeDefinition(ostream& out)=0;
- };
- class cppFunctionName;
- class cppFunctionInstance;
- class cppFunctionName
- {
- private:
- char* name;
- list<cppFunctionInstance*> funcList;
- bool isEqual(cppFunctionInstance* rhs,cppFunctionInstance* lhs);
- public:
- cppFunctionName(const char* fName=NULL);
- virtual ~cppFunctionName();
- const char* getName();
- bool isUnique(cppFunctionInstance* fnc_test);
- unsigned long getFunctionCount();
- cppFunctionInstance* getFunctionInstance(unsigned long at);
- cppFunctionInstance* createFunctionInstance();
- void deleteLastInstance();
- };
- class cppFunctionInstance : public cppFunctionCode , public nameSource
- {
- private:
- cppFunctionName* funcNameSource;
- list<mappingCode*> mapCodePointers;
- list<bool> delMapCode;
- callingCode* myCall;
- bool deleteCall;
- list<code*> defnCode;
- list<nameSource*> sources;
- list<nameSink*> sinks;
- vector<variable*> args;
- vector<bool> deleteVar;
- vector<char*> argDesc;
- vector<bool> sunk;
- cppType* retType;
- bool callAdded;
- bool retCalled;
- bool areSinksClosed();
- void printFunctionDecl(ostream& out);
- public:
- cppFunctionInstance(cppFunctionName* funcNameSource_in);
- ~cppFunctionInstance();
- unsigned long getArgumentCount();
- cppFunctionInstance& setArgumentCount(unsigned long argCnt);
- cppFunctionInstance& addArgument(unsigned long at, variable* arg,const char* desc,bool canDelete_in=false);
- cppFunctionInstance& defineReturn(cppType* retType_in);
- variable* getVariable(unsigned long at);
- bool checkSinks();
- bool canWrite();
- bool canAdd();
- bool canAddCall();
- cppFunctionInstance& addMap(mappingCode* mpCd_in,bool retCall=false,bool canDelete_in=false);
- cppFunctionInstance& addCall(callingCode* clCd_in,bool retCall=false,bool canDelete_in=false);
- unsigned long sourceObjectCount();
- unsigned long sinkObjectCount();
- nameSource* getSource(unsigned long at);
- nameSink* getSink(); // returns the most recently added sink
- nameSink* getSink(unsigned long at);
- // this is inherited from nameSource
- unsigned long sourceSize();
- const char* getSourceName(unsigned long at);
- const char* getSourceDescription();
- const char* getSourceTermDescription(unsigned long at);
- bool isSunk(unsigned long at);
- void setSunk(unsigned long at,bool isSunk=true);
- void writeDefinition(ostream& out);
- void printDecl(ostream& out);
- };
- bool cppFunctionName::isEqual(cppFunctionInstance* rhs,cppFunctionInstance* lhs)
- {
- if(rhs->getArgumentCount()!=lhs->getArgumentCount())
- return false;
- for(unsigned long i=0;i<rhs->getArgumentCount();i++)
- {
- if((*(rhs->getVariable(i)->getType()))!=(*(lhs->getVariable(i)->getType())))
- return false;
- }
- return true;
- }
- cppFunctionName::cppFunctionName(const char* fName) : name(NULL)
- {
- if(fName!=NULL)
- {
- name=copyLine(fName);
- if(name==NULL)
- throw eMsg("cppFunctionName: Failed to allocate memory");
- }
- }
- cppFunctionName::~cppFunctionName()
- {
- if(name!=NULL)
- delete[] name;
- for(list<cppFunctionInstance*>::iterator it=funcList.begin();it!=funcList.end();it++)
- {
- if((*it)!=NULL)
- delete *it;
- }
- }
- const char* cppFunctionName::getName()
- {
- if(name==NULL)
- throw eMsg("cppFunctionName: The function name has not been specified");
- return name;
- }
- bool cppFunctionName::isUnique(cppFunctionInstance* fnc_test)
- {
- for(list<cppFunctionInstance*>::iterator it=funcList.begin();it!=funcList.end();it++)
- {
- if(fnc_test!=*it)
- {
- if(isEqual(fnc_test,*it))
- return false;
- }
- }
- return true;
- }
- unsigned long cppFunctionName::getFunctionCount()
- {
- return funcList.size();
- }
- cppFunctionInstance* cppFunctionName::getFunctionInstance(unsigned long at)
- {
- if(at>=funcList.size())
- throw eMsg("cppFunctionName::getFunctionInstance(): The function instance does not exist");
- list<cppFunctionInstance*>::iterator it=funcList.begin();
- for(unsigned long i=0;i<at;i++)
- it++;
- return *it;
- }
- cppFunctionInstance* cppFunctionName::createFunctionInstance()
- {
- funcList.push_back(new cppFunctionInstance(this));
- if(funcList.back()==NULL)
- throw eMsg("cppFunctionName::createFunctionInstance(): Failed to allocate memory");
- return funcList.back();
- }
- void cppFunctionName::deleteLastInstance()
- {
- if(funcList.size()==0)
- return;
- delete funcList.back();
- funcList.pop_back();
- }
- bool cppFunctionInstance::areSinksClosed()
- {
- if(sinks.size()>0)
- {
- nameSink* lstSnk=sinks.back();
- for(unsigned long i=0;i<lstSnk->sinkSize();i++)
- {
- if(!lstSnk->sinkSet(i))
- return false;
- }
- }
- return true;
- }
- void cppFunctionInstance::printFunctionDecl(ostream& out)
- {
- if(retType==NULL || funcNameSource==NULL)
- throw eMsg("cppFunctionInstance::printFunctionDecl(): Not all the inputs have been set");
- if(!funcNameSource->isUnique(this))
- throw eMsg("cppFunctionInstance::printFunctionDecl(): This function instance is not unique");
- for(unsigned long i=0;i<args.size();i++)
- {
- if(args[i]==NULL)
- throw eMsg("cppFunctionInstance::printFunctionDecl(): Not all the inputs have been set");
- }
- retType->printType(out);
- out << ' ';
- out << funcNameSource->getName();
- out << "( ";
- bool isFirst=true;
- for(unsigned long i=0;i<args.size();i++)
- {
- if(!isFirst)
- out << " , ";
- args[i]->printDecl(out);
- isFirst=false;
- }
- out << " )";
- }
- cppFunctionInstance::cppFunctionInstance(cppFunctionName* funcNameSource_in) : funcNameSource(funcNameSource_in) , callAdded(false) , retCalled(false)
- {
- sources.push_back(this);
- }
- cppFunctionInstance::~cppFunctionInstance()
- {
- for(unsigned long i=0;i<argDesc.size();i++)
- {
- if(argDesc[i]!=NULL)
- delete[] argDesc[i];
- }
- for(unsigned long i=0;i<args.size();i++)
- {
- if(args[i]!=NULL && deleteVar[i])
- delete args[i];
- }
- typename list<mappingCode*>::iterator itPntr=mapCodePointers.begin();
- for(typename list<bool>::iterator itDel=delMapCode.begin();itDel!=delMapCode.end();itDel++)
- {
- if((*itPntr)!=NULL && (*itDel))
- delete (*itPntr);
- itPntr++;
- }
- }
- unsigned long cppFunctionInstance::getArgumentCount()
- {
- return args.size();
- }
- cppFunctionInstance& cppFunctionInstance::setArgumentCount(unsigned long argCnt)
- {
- for(unsigned long i=argCnt;i<argDesc.size();i++)
- {
- if(deleteVar[i])
- delete args[i];
- delete[] argDesc[i];
- argDesc[i]=NULL;
- }
- args.resize(argCnt,NULL);
- argDesc.resize(argCnt,NULL);
- deleteVar.resize(argCnt,false);
- sunk.resize(argCnt,false);
- return *this;
- }
- cppFunctionInstance& cppFunctionInstance::addArgument(unsigned long at, variable* arg,const char* desc,bool canDelete_in)
- {
- if(at>=args.size())
- throw eMsg("cppFunctionInstance::addArgument(): The argument does not exist");
- if(args[at]!=NULL && args[at]!=arg && deleteVar[at])
- delete args[at];
- args[at]=arg;
- deleteVar[at]=canDelete_in;
- if(argDesc[at]!=NULL)
- delete[] argDesc[at];
- argDesc[at]=copyLine(desc);
- if(argDesc[at]==NULL)
- throw eMsg("cppFunctionInstance::addArgument(): Failed to allocate memory");
- return *this;
- }
- cppFunctionInstance& cppFunctionInstance::defineReturn(cppType* retType_in)
- {
- if(retType_in==NULL)
- throw eMsg("The return type is not valid");
- retType=retType_in;
- return *this;
- }
- variable* cppFunctionInstance::getVariable(unsigned long at)
- {
- if(at>=args.size())
- throw eMsg("The requested variable does not exist");
- return args[at];
- }
- bool cppFunctionInstance::checkSinks()
- {
- return areSinksClosed();
- }
- bool cppFunctionInstance::canWrite()
- {
- if(retType==NULL)
- return false;
- if(retCalled)
- return funcNameSource->isUnique(this);
- if((*retType)!=(*voidType))
- return false;
- if(!checkSinks())
- return false;
- if(!callAdded)
- return false;
- return funcNameSource->isUnique(this);
- }
- bool cppFunctionInstance::canAdd()
- {
- return (retType==voidType || !retCalled);
- }
- bool cppFunctionInstance::canAddCall()
- {
- return !callAdded;
- }
- cppFunctionInstance& cppFunctionInstance::addMap(mappingCode* mpCd_in,bool retCall,bool canDelete_in)
- {
- if(retCalled)
- throw eMsg("cppFunctionInstance: Cannot add more code to a function that is complete");
- if(!areSinksClosed())
- throw eMsg("cppFunctionInstance: Cannot add another sink until all the sinks are closed");
- if(retCall)
- {
- if(retType==voidType || retType==NULL)
- throw eMsg("cppFunctionInstance: Cannot have a return statement in a function returning void");
- if(!callAdded)
- throw eMsg("cppFunctionInstance: Cannot call return without the fortran function being called");
- retCalled=true;
- }
- mapCodePointers.push_back(mpCd_in);
- delMapCode.push_back(canDelete_in);
- defnCode.push_back(mpCd_in);
- sources.push_back(mpCd_in);
- sinks.push_back(mpCd_in);
- return *this;
- }
- cppFunctionInstance& cppFunctionInstance::addCall(callingCode* clCd_in,bool retCall,bool canDelete_in)
- {
- if(retCalled)
- throw eMsg("cppFunctionInstance: The function has returned cannot add more code");
- if(callAdded)
- throw eMsg("cppFunctionInstance: Cannot have more than one instance of fortran function calls");
- callAdded=true;
- this->addMap(clCd_in,retCall,canDelete_in);
- return *this;
- }
- unsigned long cppFunctionInstance::sourceObjectCount()
- {
- return sources.size();
- }
- unsigned long cppFunctionInstance::sinkObjectCount()
- {
- return sinks.size();
- }
- nameSource* cppFunctionInstance::getSource(unsigned long at)
- {
- if(at>=sources.size())
- throw eMsg("cppFunctionInstance: The source does not exist");
- list<nameSource*>::iterator it=sources.begin();
- for(unsigned long i=0;i<at;i++)
- it++;
- return *it;
- }
- nameSink* cppFunctionInstance::getSink()
- {
- if(sinks.size()==0)
- return NULL;
- return sinks.back();
- }
- nameSink* cppFunctionInstance::getSink(unsigned long at)
- {
- if(at>=sinks.size())
- throw eMsg("cppFunctionInstance: The sink does not exist");
- list<nameSink*>::iterator it=sinks.begin();
- for(unsigned long i=0;i<at;i++)
- it++;
- return *it;
- }
- // this is inherited from nameSource
- unsigned long cppFunctionInstance::sourceSize()
- {
- return args.size();
- }
- const char* cppFunctionInstance::getSourceName(unsigned long at)
- {
- if(at>=args.size())
- throw eMsg("cppFunctionInstance: The argument (aka source) does not exist");
- return args[at]->getName();
- }
- const char* cppFunctionInstance::getSourceDescription()
- {
- return "The cpp function arguments";
- }
- const char* cppFunctionInstance::getSourceTermDescription(unsigned long at)
- {
- if(at>=args.size())
- throw eMsg("cppFunctionInstance: The argument (aka source) does not exist");
- return argDesc[at];
- }
- bool cppFunctionInstance::isSunk(unsigned long at)
- {
- if(at>=sunk.size())
- throw eMsg("cppFunctionInstance: The argument (aka source) does not exist");
- return sunk[at];
- }
- void cppFunctionInstance::setSunk(unsigned long at,bool isSunk)
- {
- if(at>=sunk.size())
- throw eMsg("cppFunctionInstance: The argument (aka source) does not exist");
- sunk[at]=isSunk;
- }
- void cppFunctionInstance::writeDefinition(ostream& out)
- {
- if(!canWrite())
- throw eMsg("cppFunctionInstance: The function is not ready to be written yet");
- this->printFunctionDecl(out);
- out << endl << '{' << endl;
- bool isFirst=true;
- for(list<code*>::iterator it=defnCode.begin();it!=defnCode.end();it++)
- {
- if(!isFirst)
- out << endl;
- (*it)->writeCode(out);
- isFirst=false;
- }
- out << '}' << endl << endl;
- }
- void cppFunctionInstance::printDecl(ostream& out)
- {
- this->printFunctionDecl(out);
- out << ';' << endl;
- }
- objList<cppFunctionName> cppFuncs;
- enum argIO
- {
- argUNKNOWN,
- argINPUT,
- argOUTPUT
- };
- // class fileReader
- // {
- // protected:
- //
- // list<comment> comments;
- // bool funcSet;
- // cppType* retType;
- // char* name;
- // list<variable*> args;
- // vector<argIO> ioStat;
- //
- // virtual bool read(const char* file)=0;
- //
- // public:
- //
- // fileReader() : funcSet(false) , retType(NULL) , name(NULL) , ioStat(0,argUNKNOWN)
- // {}
- //
- // ~fileReader()
- // {}
- // };
- class fortranFunction : public declaration , public function
- {
- private:
- list<comment> comments;
- bool funcSet;
- cppType* retType;
- bool deleteRet;
- char* name;
- list<variable*> args;
- vector<argIO> ioStat;
- vector<const char*> argDesc;
- bool read(const char* file)
- {
- fstream fin;
- fin.open(file,fstream::in);
- if(!fin.is_open())
- throw eMsg("fortranFunction: failed to read file");
- char line[LINE_SIZE];
- vector<bool> isFnd;
- bool argsRead=false;
- cppType* varType;
- bool inDecl=false;
- while(!fin.eof())
- {
- fin.getline(line,LINE_SIZE);
- char* ln=copyLine(line);
- if(ln[0]=='*')
- {
- comment newComm;
- newComm.line=ln;
- if(ln[1]=='>')
- newComm.tagged=true;
- else
- newComm.tagged=false;
- comments.push_back(newComm);
- }
- else
- {
- wordList words(ln);
- char word[LINE_SIZE];
- if(!funcSet)
- {
- unsigned long strt;
- for(unsigned long i=0;i<words.size();i++)
- {
- words.getWord(i,word,LINE_SIZE);
- if(strEqual(word,"SUBROUTINE"))
- {
- funcSet=true;
- retType=voidType;
- strt=words.getStart(i+1);
- break;
- }
- if(strEqual(word,"FUNCTION"))
- {
- funcSet=true;
- retType=readType(words);
- if(retType==NULL)
- {
- cout << "Failed to read the type in " << ln << endl;
- throw eMsg("fortranFunction: Failed to read type");
- }
- strt=words.getStart(i+1);
- break;
- }
- }
- if(funcSet)
- {
- unsigned long j=strt;
- char var[LINE_SIZE];
- while(ln[j]!='(' && !isspace(ln[j]) && ln[j]!=0)
- j++;
- if(ln[j]==0)
- throw eMsg("fortranFunction: Premature end to the line");
- j+=2;
- name=new char[j-strt];
- if(name==NULL)
- throw eMsg("fortranFunction: failed to allcate memory");
- j=strt;
- while(ln[j]!='(' && !isspace(ln[j]))
- {
- name[j-strt]=tolower(ln[j]);
- j++;
- }
- name[j-strt]='_';
- name[j+1-strt]=0;
- bool isVars=true;
- while(ln[j]!='(' && ln[j]!=0)
- j++;
- j++;
- while(isspace(ln[j]) && ln[j]!=')' && ln[j]!=0)
- j++;
- if(ln[j]==0)
- throw eMsg("fortranFunction: unexpected end to the line");
- if(ln[j]==')')
- isVars=false;
- while(isVars)
- {
- if(ln[j]==',')
- throw eMsg("fortranFunction: the argument list is malformed");
- unsigned long k=j;
- while(!isspace(ln[k]) && ln[k]!=0 && ln[k]!=')' && ln[k]!=',')
- k++;
- if(ln[k]==')')
- isVars=false;
- if(ln[k]==0)
- throw eMsg("fortranFunction: the declaration is malformed");
- unsigned long l=0;
- for(j=j;j<k;j++)
- var[l++]=ln[j];
- var[l]=0;
- variable* pVar=new variable(NULL,var,0,true);
- if(pVar==NULL)
- throw eMsg("fortranFunction: Failed to allocate memory");
- args.push_back(pVar);
- if(isVars)
- {
- while(ln[j]!=',' && ln[j]!=')' && ln[j]!=0)
- j++;
- if(ln[j]==0)
- throw eMsg("fortranFunction: The declaration seems malformed");
- if(ln[j]==')')
- {
- isVars=false;
- }
- else
- {
- j++;
- while(isspace(ln[j]) && ln[j]!=0 && ln[j]!=')' && ln[j]!=',')
- j++;
- if(ln[j]==0 || ln[j]==')' || ln[j]==',')
- throw eMsg("fortranFunction: The declration is messed up");
- }
- }
- }
- if(args.size()==0)
- argsRead=true;
- isFnd.resize(args.size(),false);
- ioStat.resize(args.size(),argUNKNOWN);
- argDesc.resize(args.size(),NULL);
- }
- }
- else if(!argsRead)
- {
- unsigned long i=0;
- if(inDecl)
- {
- while(isspace(ln[i]) && ln[i]!=0)
- i++;
- if(ln[i]!='&')
- inDecl=false;
- else
- i++;
- }
- if(!inDecl)
- varType=readType(words,i);
- if(varType!=NULL)
- {
- char tmpVar[LINE_SIZE];
- unsigned long j;
- while(ln[i]!=0)
- {
- bool isArray=false;
- while(isspace(ln[i]))
- i++;
- if(ln[i]!=0)
- {
- j=0;
- while(ln[i]!='(' && !isspace(ln[i]) && ln[i]!=',' && ln[i]!=0)
- tmpVar[j++]=ln[i++];
- tmpVar[j]=0;
- if(ln[i]!=0)
- {
- while(isspace(ln[i]) && ln[i]!=0)
- i++;
- if(ln[i]!=0)
- {
- if(ln[i]=='(')
- {
- isArray=true;
- while(ln[i]!=')' && ln[i]!=0)
- i++;
- if(ln[i]==0)
- throw eMsg("fortranFunction: the fortran type declaration is malformed");
- i++;
- while(isspace(ln[i]) && ln[i]!=0)
- i++;
- }
- if(ln[i]==',')
- i++;
- }
- }
- }
- j=0;
- for(list<variable*>::iterator it=args.begin();it!=args.end();it++)
- {
- if(!isFnd[j])
- {
- if(strEqual(tmpVar,(*it)->getName()))
- {
- (*it)->setType(varType);
- if(isArray)
- (*it)->setPointerLevel(1);
- isFnd[j]=true;
- argsRead=true;
- for(unsigned long k=0;k<isFnd.size();k++)
- {
- if(!isFnd[k])
- {
- argsRead=false;
- break;
- }
- }
- break;
- }
- }
- j++;
- }
- }
- }
- else
- {
- inDecl=false;
- }
- }
- delete[] ln;
- }
- }
- for(list<comment>::iterator it=comments.begin();it!=comments.end();it++)
- {
- if(it->tagged)
- {
- unsigned long i=0;
- const char* cLn=it->line;
- while(cLn[i]!='\\' && cLn[i]!=0)
- i++;
- if(cLn[i]!=0)
- {
- i++;
- char param[6];
- unsigned long j=0;
- while(j<5 && cLn[i]!=0)
- param[j++]=cLn[i++];
- if(cLn[i]!=0)
- {
- param[j]=0;
- if(strEqual("param",param))
- {
- while(cLn[i]!='[' && cLn[i]!=0)
- i++;
- if(cLn[i]!=0)
- {
- argIO thisArg=argUNKNOWN;
- while(cLn[i]!=']' && cLn[i]!=0)
- {
- if(cLn[i]=='i' && thisArg==argUNKNOWN)
- thisArg=argINPUT;
- if(cLn[i]=='o')
- thisArg=argOUTPUT;
- i++;
- }
- if(cLn[i]!=0)
- {
- i++;
- char tmpVar[LINE_SIZE];
- while(isspace(cLn[i]) && cLn[i]!=0)
- i++;
- if(cLn[i]!=0)
- {
- unsigned long j=0;
- while(cLn[i]!=0 && !isspace(cLn[i]))
- tmpVar[j++]=cLn[i++];
- tmpVar[j]=0;
- j=0;
- for(list<variable*>::iterator itArg=args.begin();itArg!=args.end();itArg++)
- {
- if(strEqual((*itArg)->getName(),tmpVar))
- {
- ioStat[j]=thisArg;
- argDesc[j]=copyLine(cLn);
- if(argDesc[j]==NULL)
- throw eMsg("fortranFunction::read(): Failed to allocate memory");
- }
- j++;
- }
- }
- }
- }
- }
- }
- }
- }
- }
- fin.close();
- return true;
- }
- public:
- fortranFunction(const char* fn=NULL) : funcSet(false) , retType(NULL) , deleteRet(false) , name(NULL) , ioStat(0,argUNKNOWN)
- {
- if(fn!=NULL)
- read(fn);
- }
- fortranFunction(const fortranFunction& asg)
- {
- for(typename list<comment>::const_iterator it=asg.comments.begin();it!=asg.comments.end();it++)
- this->comments.push_back(*it);
- this->funcSet=asg.funcSet;
- retType=asg.retType;
- if(asg.name!=NULL)
- {
- this->name=copyLine(asg.name);
- if(this->name==NULL)
- throw eMsg("fortranFunction::fortranFunction(): Failed to allocate memory");
- }
- else
- {
- this->name=NULL;
- }
- for(typename list<variable*>::const_iterator it=asg.args.begin();it!=asg.args.end();it++)
- {
- variable* tmpVar=new variable(*(*it));
- if(tmpVar==NULL)
- throw eMsg("fortranFunction::fortranFunction(): Failed to allocate memory");
- this->args.push_back(tmpVar);
- }
- this->ioStat.resize(asg.ioStat.size(),argUNKNOWN);
- for(unsigned long i=0;i<this->ioStat.size();i++)
- this->ioStat[i]=asg.ioStat[i];
- this->argDesc.resize(asg.argDesc.size(),NULL);
- for(unsigned long i=0;i<this->argDesc.size();i++)
- {
- if(asg.argDesc[i]!=NULL)
- this->argDesc[i]=copyLine(asg.argDesc[i]);
- }
- }
- ~fortranFunction()
- {
- if(name!=NULL)
- delete[] name;
- for(typename list<variable*>::iterator it=args.begin();it!=args.end();it++)
- delete *it;
- for(unsigned long i=0;i<argDesc.size();i++)
- {
- if(argDesc[i]!=NULL)
- delete[] argDesc[i];
- }
- }
- fortranFunction& operator=(const fortranFunction& asg)
- {
- // delete this data
- if(name!=NULL)
- delete[] name;
- for(typename list<variable*>::iterator it=args.begin();it!=args.end();it++)
- delete *it;
- for(unsigned long i=0;i<argDesc.size();i++)
- {
- if(argDesc[i]!=NULL)
- delete[] argDesc[i];
- }
- // copy over the other data
- for(typename list<comment>::const_iterator it=asg.comments.begin();it!=asg.comments.end();it++)
- this->comments.push_back(*it);
- this->funcSet=asg.funcSet;
- retType=asg.retType;
- if(asg.name!=NULL)
- {
- this->name=copyLine(asg.name);
- if(this->name==NULL)
- throw eMsg("fortranFunction::fortranFunction(): Failed to allocate memory");
- }
- else
- this->name=NULL;
- for(typename list<variable*>::const_iterator it=asg.args.begin();it!=asg.args.end();it++)
- {
- variable* tmpVar=new variable(*(*it));
- if(tmpVar==NULL)
- throw eMsg("fortranFunction::fortranFunction(): Failed to allocate memory");
- this->args.push_back(tmpVar);
- }
- this->ioStat.resize(asg.ioStat.size(),argUNKNOWN);
- for(unsigned long i=0;i<this->ioStat.size();i++)
- this->ioStat[i]=asg.ioStat[i];
- this->argDesc.resize(asg.argDesc.size(),NULL);
- for(unsigned long i=0;i<this->argDesc.size();i++)
- {
- if(asg.argDesc[i]!=NULL)
- this->argDesc[i]=copyLine(asg.argDesc[i]);
- }
- return *this;
- }
- unsigned long argCount()
- {
- return args.size();
- }
- argIO getArgStat(unsigned long at)
- {
- if(at>=ioStat.size())
- throw eMsg("fortranFunction: The argument does not exist");
- return ioStat[at];
- }
- void setArgStat(unsigned long at, argIO argStat_in, const char* description=NULL)
- {
- if(at>=ioStat.size())
- throw eMsg("fortranFunction: The argument does not exist");
- ioStat[at]=argStat_in;
- if(argDesc[at]!=NULL && argDesc[at]!=description)
- delete[] argDesc[at];
- if(argDesc[at]!=description)
- {
- if(description!=NULL)
- {
- argDesc[at]=copyLine(description);
- if(argDesc[at]==NULL)
- throw eMsg("fortranFunction::setArgStat(): Failed to allocate memory");
- }
- else
- {
- argDesc[at]=NULL;
- }
- }
- }
- void printDecl(ostream& out)
- {
- if(retType!=NULL)
- retType->printType(out);
- out << ' ' << name << "( ";
- unsigned long at=0;
- bool first=true;
- for(list<variable*>::iterator it=args.begin();it!=args.end();it++)
- {
- if((*it)!=NULL)
- {
- if(!first)
- out << " , ";
- if(ioStat[at++]==argINPUT)
- out << "const ";
- (*it)->printDecl(out);
- first=false;
- }
- }
- out << " );" << endl;
- }
- const char* getFunctionName()
- {
- return this->name;
- }
- unsigned long getFunctionArgumentCount()
- {
- return args.size();
- }
- const char* getArgumentDescription(unsigned long at)
- {
- static stringstream tmpDesc;
- if(at>=args.size())
- throw eMsg("fortranFunction::getArgumentDescription(): The description does not exist");
- if(argDesc[at]==NULL)
- {
- tmpDesc.str("");
- tmpDesc << "Function argument " << at;
- return tmpDesc.str().c_str();
- }
- return argDesc[at];
- }
- const char* getArgumentName(unsigned long at)
- {
- if(at>=args.size())
- throw eMsg("fortranFunction::getArgumentName(): The argument does not exist");
- typename list<variable*>::iterator it=args.begin();
- for(unsigned long i=0;i<at;i++)
- it++;
- if((*it)==NULL)
- throw eMsg("fortranFunction::getArgumentName(): The argument is NULL");
- return (*it)->getName();
- }
- };
- // read the fortran file
- // get the input output if needed
- // write the prototype
- // select/define cpp function name
- // define the arguments and return value for the name
- // check that the arguments are unique
- // each argument is a source
- // the return value is a sink
- // write the cpp function declaration
- // introduce the fortran function as a code segment
- // each argument is a sink
- // the return value is a source
- // OPTIONAL define/select maps for the code
- // the maps are really blocks of text with special formating to insert custom keywords
- // Link the sources with sinks until all sinks are full (outstanding sources are ignored)
- // When everything is linked the cpp definition can be written
- // void setCppFuncCnt(unsigned long funcCnt)
- // {
- // }
- //
- // unsigned long getCppFuncCnt()
- // {
- // }
- //
- // void addCppFunc(unsigned long at, cppFunctionInstance* func)
- // {
- // }
- // class commandSource
- // {
- // public:
- //
- // virtual ~commandSource()
- // {}
- //
- // virtual char getCharacter()=0;
- // virtual void getLine(string& str)=0;
- // virtual long getInteger()=0;
- // virtual double getFloat()=0;
- // virtual bool isOver()=0;
- // };
- // class function
- // {
- // public:
- //
- // virtual void printDecl(ostream& out)=0;
- // virtual void printCall(ostream& out)=0;
- // virtual bool overloaded()=0;
- // virtual unsigned long size()=0;
- // virtual variable* getVariable(unsigned long at)=0;
- // };
- //
- // class function_fortran
- // {
- // private:
- //
- // unsigned long sz;
- //
- //
- // public:
- //
- // bool overloaded()
- // {
- // return false;
- // }
- // }
- //
- //
- //
- // class program
- // {
- // private:
- //
- //
- // fstream* pOut;
- // fstream* dOut;
- // ostream* scrn;
- //
- // public:
- //
- // virtual bool runComms(commandSource* comms_in,ostream* scrn_in=NULL)=NULL;
- //
- // };
- class commentSink
- {
- public:
- commentSink()
- {}
- ~commentSink()
- {}
- virtual void sinkComment(const char* comment)
- {}
- };
- class commandSource
- {
- public:
- commandSource()
- {}
- virtual ~commandSource()
- {}
- virtual char getChar()=0;
- virtual long getInt()=0;
- virtual const char* getToken()=0; // a word
- virtual const char* getLine()=0;
- virtual const char* getText()=0;
- virtual unsigned long getLineCount()=0;
- // if recording and an error is made this is called so the erroneous command is not recorded
- virtual void clearRecord()
- {}
- // check to see if there is any more input or tells the program that a new batch is coming and to flush the buffer
- virtual bool startRecord()
- {
- return true;
- }
- virtual void flushRecord()
- {}
- virtual void flushErrorToStream(ostream& out)
- {}
- virtual void flushErrorToFile(ostream& out, const char* fn)
- {}
- // tells the console that it is all over
- virtual void exitRecord()
- {}
- virtual void setCommentSink(commentSink* cSnk)
- {}
- };
- class commandSource_basic : public commandSource
- {
- private:
- commentSink* cmntSink;
- protected:
- istream& in;
- unsigned long cnt;
- char line[LINE_SIZE*10];
- void getRealLine(char* gtLine=NULL,unsigned long lnSize=0)
- {
- bool needLine=true;
- if(gtLine==NULL)
- {
- gtLine=line;
- lnSize=LINE_SIZE*10;
- }
- else
- {
- if(lnSize==0)
- throw eMsg("commandSource_basic::getRealLine(): Invalid Line Size");
- }
- while(needLine && !in.eof())
- {
- in.getline(gtLine,lnSize);
- if(line[0]!='%')
- {
- needLine=false;
- }
- else
- {
- if(cmntSink!=NULL)
- cmntSink->sinkComment(line);
- }
- }
- if(needLine)
- throw eMsg("commandSource_basic(): Pre-mature eof, failed to retrieve a real line");
- }
- public:
- commandSource_basic(istream& input) : cmntSink(NULL) , in(input) , cnt(0)
- {}
- ~commandSource_basic()
- {}
- char getChar()
- {
- char retVal;
- this->getRealLine();
- retVal=line[0];
- return retVal;
- }
- long getInt()
- {
- long value;
- stringstream ss;
- this->getRealLine();
- ss << line;
- ss >> value;
- return value;
- }
- const char* getToken() // a word
- {
- char* retVal=line;
- unsigned long at=0;
- this->getRealLine();
- while(isspace(retVal[0]) && retVal[0]!=0)
- retVal++;
- while(!isspace(retVal[at]) && retVal[at]!=0)
- at++;
- retVal[at]=0;
- return retVal;
- }
- const char* getLine()
- {
- this->getRealLine();
- return line;
- }
- virtual const char* getText()
- {
- char* atLine=line;
- unsigned long read=0;
- cnt=0;
- while(!in.eof() && ((10*LINE_SIZE-read)>0))
- {
- this->getRealLine(atLine,10*LINE_SIZE-read);
- // in.getline(atLine,10*LINE_SIZE-read);
- if(!in.eof())
- {
- read+=in.gcount();
- atLine=&(line[read]);
- if(read>0)
- line[read-1]='\n';
- cnt++;
- }
- }
- line[read-1]=0;
- in.clear();
- return line;
- }
- unsigned long getLineCount()
- {
- return cnt;
- }
- void setCommentSink(commentSink* cSnk)
- {
- cmntSink=cSnk;
- }
- };
- class commandSource_file : public commandSource_basic
- {
- private:
- ostream* out;
- public:
- commandSource_file(fstream& fin, ostream* out_in=NULL) : commandSource_basic(fin) , out(out_in)
- {
- }
- ~commandSource_file()
- {
- }
- char getChar()
- {
- char c=this->commandSource_basic::getChar();
- if(out!=NULL)
- *out << c << endl;
- return c;
- }
- long getInt()
- {
- long val=this->commandSource_basic::getInt();
- if(out!=NULL)
- *out << val << endl;
- return val;
- }
- const char* getToken()
- {
- const char* val=this->commandSource_basic::getToken();
- if(out!=NULL)
- *out << val << endl;
- return val;
- }
- const char* getLine()
- {
- const char* val=this->commandSource_basic::getLine();
- if(out!=NULL)
- *out << val << endl;
- return val;
- }
- const char* getText()
- {
- char* atLine=line;
- unsigned long read=0;
- cnt=this->commandSource_basic::getInt();
- for(unsigned long i=0;i<cnt;i++)
- {
- this->getRealLine(atLine,10*LINE_SIZE-read);
- // in.getline(atLine,10*LINE_SIZE-read);
- read+=in.gcount();
- atLine=&(line[read]);
- if(read>0)
- line[read-1]='\n';
- }
- line[read-1]=0;
- in.clear();
- if(out!=NULL)
- *out << line << endl;
- return line;
- }
- bool startRecord()
- {
- in.peek();
- if(in.eof())
- return false;
- else
- return true;
- }
- };
- class commandSource_recording : public commandSource , private commentSink
- {
- private:
- commandSource* comm;
- bool deleteComm;
- char* recFile;
- bool append;
- bool rec;
- fstream fout;
- stringstream sout;
- commentSink* cmntSink;
- void sinkComment(const char* comment)
- {
- sout << comment << '\n';
- if(cmntSink!=NULL)
- cmntSink->sinkComment(comment);
- }
- public:
- commandSource_recording(commandSource* comm_in=NULL, bool canDelete_in=false, const char* recFile_in=NULL, bool append_in=false) : comm(comm_in) , deleteComm(canDelete_in) , recFile(NULL) , append(append_in) , rec(false) , cmntSink(NULL)
- {
- if(comm!=NULL)
- comm->setCommentSink(this);
- if(recFile_in!=NULL)
- {
- recFile=copyLine(recFile_in);
- if(recFile==NULL)
- throw eMsg("commandSource_recording::commandSource_recording(): Failed to allocate memory");
- }
- }
- ~commandSource_recording()
- {
- if(comm!=NULL && deleteComm)
- delete comm;
- if(recFile!=NULL)
- delete[] recFile;
- }
- commandSource_recording& setCommandSource(commandSource* comm_in=NULL, bool canDelete_in=false)
- {
- if(comm!=NULL && comm!=comm_in && deleteComm)
- delete comm;
- comm=comm_in;
- if(comm!=NULL)
- comm->setCommentSink(this);
- deleteComm=canDelete_in;
- return *this;
- }
- const char* getRecordFile()
- {
- return recFile;
- }
- bool recording()
- {
- return rec;
- }
- commandSource_recording& start()
- {
- if(rec)
- return *this;
- if(recFile==NULL)
- throw eMsg("commandSource_recording::start(): The recording file has not been set");
- if(append)
- fout.open(recFile,fstream::out|fstream::app);
- else
- fout.open(recFile,fstream::out);
- if(!fout.is_open())
- throw eMsg("commandSource_recording::start(): Filed to open the file");
- rec=true;
- return *this;
- }
- commandSource_recording& stop()
- {
- if(!rec)
- return *this;
- fout.close();
- fout.clear();
- rec=false;
- return *this;
- }
- bool getAppendState()
- {
- return append;
- }
- commandSource_recording& setAppendState(bool append_in)
- {
- append=append_in;
- return *this;
- }
- commandSource_recording& setRecordFile(const char* recFile_in,bool append_in=false)
- {
- bool wasRec=rec;
- if(rec)
- this->stop();
- if(recFile!=NULL)
- {
- delete[] recFile;
- recFile=NULL;
- }
- if(recFile_in!=NULL)
- {
- recFile=copyLine(recFile_in);
- if(recFile==NULL)
- throw eMsg("commandSource_recording::commandSource_recording(): Failed to allocate memory");
- }
- append=append_in;
- if(wasRec && recFile!=NULL)
- this->start();
- return *this;
- }
- char getChar()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::getChar(): The command source has not been set");
- char c=comm->getChar();
- if(rec)
- {
- if(c==0)
- sout << '\n';
- else
- sout << c << '\n';
- }
- return c;
- }
- long getInt()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::getInt(): The command source has not been set");
- long val=comm->getInt();
- if(rec)
- {
- sout << val << '\n';
- }
- return val;
- }
- const char* getToken()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::getToken(): The command source has not been set");
- const char* tok=comm->getToken();
- if(rec)
- {
- sout << tok << '\n';
- }
- return tok;
- }
- const char* getLine()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::getLine(): The command source has not been set");
- const char* ln=comm->getLine();
- if(rec)
- {
- sout << ln << '\n';
- }
- return ln;
- }
- const char* getText()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::getText(): The command source has not been set");
- const char* txt=comm->getText();
- sout << comm->getLineCount() << '\n';
- if(rec)
- {
- sout << txt << '\n';
- }
- return txt;
- }
- unsigned long getLineCount()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::getLineCount(): The command source has not been set");
- return comm->getLineCount();
- }
- // if recording and an error is made this is called so the erroneous command is not recorded
- void clearRecord()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::clearRecord(): The command source has not been set");
- sout.str("");
- comm->clearRecord();
- }
- // check to see if there is any more input or tells the program that a new batch is coming and to flush the buffer
- bool startRecord()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::startRecord(): The command source has not been set");
- if(rec)
- this->clearRecord();
- else
- this->start();
- return comm->startRecord();
- }
- void flushRecord()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::exitRecord(): The command source has not been set");
- if(rec)
- fout << sout.str();
- this->clearRecord();
- comm->flushRecord();
- }
- void flushErrorToStream(ostream& out)
- {
- out << "Error producing command sequence recorded by commandSource_recording:" << endl;
- out << sout.str();
- this->clearRecord();
- if(comm!=NULL)
- comm->flushErrorToStream(out);
- }
- void flushErrorToFile(ostream& out,const char* fn)
- {
- fstream fout;
- fout.open(fn,fstream::out);
- if(!fout.is_open())
- {
- out << "Failed to open file " << fn << endl;
- }
- else
- {
- out << "Printing erroneous command sequence to " << fn << endl;
- this->flushErrorToStream(fout);
- fout.flush();
- fout.close();
- fout.clear();
- }
- }
- // tells the console that it is all over
- void exitRecord()
- {
- if(comm==NULL)
- throw eMsg("commandSource_recording::exitRecord(): The command source has not been set");
- this->clearRecord();
- if(rec)
- {
- this->stop();
- this->setAppendState(true);
- }
- comm->exitRecord();
- }
- void setCommentSink(commentSink* cSnk)
- {
- cmntSink=cSnk;
- }
- };
- bool basicMenu(const char* title, vector<const char*>& options, vector<bool>& canDelete, ostream& out, commandSource& in, unsigned long& select, unsigned long permCnt=0)
- {
- unsigned long at=permCnt;
- unsigned long sz=options.size();
- if(permCnt>9 && options.size()>10)
- throw eMsg("basicMenu(): Cannot display menu too many permanent options");
- bool retVal=true;
- while(true)
- {
- cout << "\n\n" << endl;
- cout << title << endl;
- for(unsigned long i=0;i<permCnt;i++)
- out << i << '\t' << options[i] << endl;
- for(unsigned long i=0;(i<(10-permCnt) && (i+at)<sz);i++)
- out << i+permCnt << '\t' << options[i+at] << endl;
- if(sz<10)
- cout << "Make selection or [q]uit: ";
- else if(at==permCnt)
- cout << "Make selection, [n]ext or [q]uit: ";
- else if(sz+permCnt-at<=10)
- cout << "Make selection, [b]ack or [q]uit: ";
- else
- cout << "Make selection, [n]ext, [b]ack or [q]uit: ";
- cout.flush();
- char answer=tolower(in.getChar());
- select=10;
- switch(answer)
- {
- case 'n':
- at+=10-permCnt;
- if(at>=sz)
- at-=10-permCnt;
- break;
- case 'b':
- if(at>permCnt)
- at-=10-permCnt;
- break;
- case '0':
- select=0;
- break;
- case '1':
- select=1;
- break;
- case '2':
- select=2;
- break;
- case '3':
- select=3;
- break;
- case '4':
- select=4;
- break;
- case '5':
- select=5;
- break;
- case '6':
- select=6;
- break;
- case '7':
- select=7;
- break;
- case '8':
- select=8;
- break;
- case '9':
- select=9;
- break;
- case 'q':
- select=11;
- break;
- }
- if(select<10)
- {
- if(select<permCnt)
- break;
- select+=at-permCnt;
- if(select<sz)
- break;
- }
- else if(select>10)
- {
- retVal=false;
- break;
- }
- }
- for(unsigned long i=0;i<sz;i++)
- {
- if(options[i]!=NULL && canDelete[i])
- delete[] options[i];
- }
- return retVal;
- }
- bool nestedMenu(const char* title, vector<const char*>& pageHeader, vector< vector<const char*> >& options, vector<bool>& delHead, vector< vector<bool> >& canDelete, ostream& out, commandSource& in, unsigned long& major, unsigned long& minor, vector<unsigned long>& permCnt)
- {
- if(pageHeader.size()!=options.size() || delHead.size()!=pageHeader.size())
- throw eMsg("nestedMenu(): The input data is inconsistent");
- for(unsigned long i=0;i<options.size();i++)
- {
- if(options[i].size()!=canDelete[i].size())
- throw eMsg("nestedMenu(): The input data is inconsistent");
- }
- bool majorRun=true;
- bool retVal=true;
- unsigned long maj=0;
- unsigned long pgCnt=0;
- unsigned long atPage=0;
- for(unsigned long i=0;i<options.size();i++)
- {
- if(options[i].size()>0)
- pgCnt++;
- }
- if(pgCnt==0)
- {
- retVal=false;
- majorRun=false;
- }
- while(majorRun)
- {
- bool minorRun=true;
- unsigned long at=permCnt[maj];
- unsigned long sz=options[maj].size();
- if(permCnt[maj]>9 && options[maj].size()>10)
- throw eMsg("basicMenu(): Cannot display menu too many permanent options");
- while(minorRun)
- {
- vector<const char*>& opt=options[maj];
- unsigned long pmCnt=permCnt[maj];
- cout << "\n\n" << endl;
- cout << title << endl;
- cout << pageHeader[maj] << endl;
- for(unsigned long i=0;i<pmCnt;i++)
- out << i << '\t' << opt[i] << endl;
- for(unsigned long i=0;(i<(10-pmCnt) && (i+at)<sz);i++)
- out << i+pmCnt << '\t' << opt[i+pmCnt] << endl;
- if(atPage==0 && pgCnt==1)
- {
- if(sz<10)
- cout << "Make selection or [q]uit: ";
- else if(at==pmCnt)
- cout << "Make selection, [n]ext or [q]uit: ";
- else if(sz+pmCnt-at<=10)
- cout << "Make selection, [b]ack or [q]uit: ";
- else
- cout << "Make selection, [n]ext, [b]ack or [q]uit: ";
- }
- else if(atPage==0)
- {
- // if(sz<10)
- // cout << "Make selection, [N]ext page or [q]uit: ";
- // else if(at==pmCnt)
- // cout << "Make selection, [n]ext, [N]ext page or [q]uit: ";
- // else if(sz+pmCnt-at<=10)
- // cout << "Make selection, [b]ack, [N]ext page, or [q]uit: ";
- // else
- // cout << "Make selection, [n]ext, [b]ack, [N]ext page, or [q]uit: ";
- if(at==pmCnt)
- cout << "Make selection, [n]ext, [N]ext page or [q]uit: ";
- else
- cout << "Make selection, [n]ext, [b]ack, [N]ext page, or [q]uit: ";
- }
- else if(atPage==(pgCnt-1))
- {
- // if(sz<10)
- // cout << "Make selection, [B]ack page or [q]uit: ";
- // else if(at==pmCnt)
- // cout << "Make selection, [n]ext, [B]ack page or [q]uit: ";
- // else if(sz+pmCnt-at<=10)
- // cout << "Make selection, [b]ack, [B]ack page or [q]uit: ";
- // else
- // cout << "Make selection, [n]ext, [b]ack, [B]ack page or [q]uit: ";
- if(sz+pmCnt-at<=10)
- cout << "Make selection, [b]ack, [B]ack page or [q]uit: ";
- else
- cout << "Make selection, [n]ext, [b]ack, [B]ack page or [q]uit: ";
- }
- else
- {
- // if(sz<10)
- // cout << "Make selection, [N]ext page, [B]ack page or [q]uit: ";
- // else if(at==pmCnt)
- // cout << "Make selection, [n]ext, [N]ext page, [B]ack page or [q]uit: ";
- // else if(sz+pmCnt-at<=10)
- // cout << "Make selection, [b]ack, [N]ext page, [B]ack page or [q]uit: ";
- // else
- cout << "Make selection, [n]ext, [b]ack, [N]ext page, [B]ack page or [q]uit: ";
- }
- cout.flush();
- char answer=in.getChar();
- minor=10;
- switch(answer)
- {
- case 'n':
- at+=10-pmCnt;
- if(at<sz)
- break;
- at-=10-pmCnt;
- case 'N':
- if(atPage<(pgCnt-1))
- {
- atPage++;
- maj++;
- while(options[maj].size()==0)
- maj++;
- minorRun=false;
- }
- break;
- case 'b':
- if(at>pmCnt)
- {
- at-=10-pmCnt;
- break;
- }
- case 'B':
- if(atPage>0)
- {
- atPage--;
- maj--;
- while(options[maj].size()==0)
- maj--;
- minorRun=false;
- }
- break;
- case '0':
- minor=0;
- break;
- case '1':
- minor=1;
- break;
- case '2':
- minor=2;
- break;
- case '3':
- minor=3;
- break;
- case '4':
- minor=4;
- break;
- case '5':
- minor=5;
- break;
- case '6':
- minor=6;
- break;
- case '7':
- minor=7;
- break;
- case '8':
- minor=8;
- break;
- case '9':
- minor=9;
- break;
- case 'q':
- minor=11;
- break;
- case 'Q':
- minor=11;
- break;
- }
- if(minor<10)
- {
- major=maj;
- if(minor<pmCnt)
- break;
- minor+=at-pmCnt;
- if(minor<sz)
- {
- majorRun=false;
- minorRun=false;
- }
- }
- else if(minor>10)
- {
- majorRun=false;
- minorRun=false;
- retVal=false;
- }
- }
- }
- for(unsigned long i=0;i<pageHeader.size();i++)
- {
- if(pageHeader[i]!=NULL && delHead[i])
- delete[] pageHeader[i];
- }
- for(unsigned long i=0;i<options.size();i++)
- {
- for(unsigned long j=0;j<options[i].size();j++)
- {
- if(options[i][j]!=NULL && canDelete[i][j])
- delete[] options[i][j];
- }
- }
- return retVal;
- }
- class sourceFile
- {
- private:
- list<declaration*> decls;
- list<cppFunctionCode*> defns;
- char* fn;
- sourceFile& writeFile(ostream& out)
- {
- out << endl;
- for(typename list<declaration*>::iterator it=decls.begin();it!=decls.end();it++)
- (*it)->printDecl(out);
- out << endl;
- for(typename list<cppFunctionCode*>::iterator it=defns.begin();it!=defns.end();it++)
- (*it)->writeDefinition(out);
- out << endl;
- return *this;
- }
- public:
- sourceFile(const char* file_in=NULL) : fn(NULL)
- {
- if(file_in!=NULL)
- {
- fn=copyLine(file_in);
- if(fn==NULL)
- throw eMsg("sourceFile::sourceFile(): Failed to allocate memory");
- }
- }
- ~sourceFile()
- {
- if(fn!=NULL)
- delete[] fn;
- }
- const char* getFileName()
- {
- return fn;
- }
- sourceFile& setFileName(const char* file_in)
- {
- if(fn!=NULL)
- {
- delete[] fn;
- fn=NULL;
- }
- if(file_in==NULL)
- {
- fn=NULL;
- return *this;
- }
- fn=copyLine(file_in);
- if(fn==NULL)
- throw eMsg("sourceFile::setFileName(): Failed to allocate memory");
- return *this;
- }
- sourceFile& addDeclaration(declaration* dec)
- {
- if(dec==NULL)
- throw eMsg("sourceFile::addDeclaration(): The declaration is NULL");
- decls.push_back(dec);
- return *this;
- }
- sourceFile& addDefinition(cppFunctionCode* def)
- {
- if(def==NULL)
- throw eMsg("sourceFile::addDefinition(): Cannot accept a NULL argument");
- defns.push_back(def);
- return *this;
- }
- sourceFile& writeFile(bool append=true)
- {
- if(fn==NULL)
- return writeFile(cout);
- fstream fout;
- if(append)
- fout.open(fn,fstream::out | fstream::app);
- else
- fout.open(fn,fstream::out);
- if(!fout.is_open())
- throw eMsg("sourceFile::writeFile(): Failed to open file");
- return writeFile(fout);
- }
- };
- sourceFile fortranSource;
- sourceFile cppPrototype;
- sourceFile cppDefinition;
- commandSource_recording recorder;
- vector<const char*> fileArguments;
- vector<bool> fileArgumentProcessed;
- list<fortranFunction> fortranFunctionList;
- // DEFINE CPP TYPE
- bool createCPPType(commandSource& comm, ostream& out, cppType*& newType,bool makeCopy=true)
- {
- // ask if it is a basic type or template
- bool typeCreated=false;
- if(makeCopy)
- newType=NULL;
- vector<const char*> opt(2,NULL);
- vector<bool> del(2,false);
- opt[0]="basic type";
- opt[1]="template type";
- unsigned long ans;
- if(basicMenu("What kind of C++ type?",opt,del,out,comm,ans,0))
- {
- cppType_basic* bTp;
- cppType_template* tTp;
- const char* name;
- const char* desc;
- long argCnt=0;
- unsigned long uCnt=0;
- char ansYN;
- bool getCnt=true;
- bool failed=false;
- switch(ans)
- {
- case 0:
- out << "Specify the name: ";
- out.flush();
- name=copyLine(comm.getToken());
- if(name==NULL)
- throw eMsg("createCPPType(): Failed to allocate memory");
- out << "Give a description for this type: ";
- out.flush();
- desc=copyLine(comm.getLine());
- if(desc==NULL)
- {
- delete[] name;
- throw eMsg("createCPPType(): Failed to allocate memory");
- }
- out << "About to create a basic type with name " << name << " continue? [y]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN!='n' && ansYN!='N')
- {
- bTp=new cppType_basic(name,desc);
- if(bTp==NULL)
- {
- delete[] name;
- delete[] desc;
- throw eMsg("createCPPType(): Failed to allocate memory");
- throw eMsg("createCPPType(): Failed to allocate memory");
- }
- bTypes.addObj(bTp);
- if(makeCopy)
- {
- newType=new cppType_basic(*bTp);
- if(newType==NULL)
- {
- delete[] name;
- delete[] desc;
- throw eMsg("createCPPType(): Failed to allocate memory");
- }
- }
- }
- delete[] name;
- delete[] desc;
- break;
- case 1:
- out << "Specify the name: ";
- out.flush();
- name=copyLine(comm.getToken());
- if(name==NULL)
- throw eMsg("createCPPType(): Failed to allocate memory");
- out << "Give a description for this type: ";
- out.flush();
- desc=copyLine(comm.getLine());
- if(desc==NULL)
- {
- delete[] name;
- throw eMsg("createCPPType(): Failed to allocate memory");
- }
- getCnt=true;
- failed=false;
- while(getCnt)
- {
- out << "How many template arguments? ";
- out.flush();
- argCnt=comm.getInt();
- if(argCnt<0)
- {
- out << "Cannot accept a negative, do you want to try again [y]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- {
- getCnt=false;
- failed=true;
- }
- }
- else
- {
- getCnt=false;
- }
- }
- if(!failed)
- {
- uCnt=argCnt;
- out << "About to create a template type with name " << name << " with " << uCnt << " arguments" << endl;
- out << "Described as " << desc << endl;
- out << "Continue? [y]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN!='n' && ansYN!='N')
- {
- tTp=new cppType_template(name,desc,uCnt);
- if(tTp==NULL)
- {
- delete[] name;
- delete[] desc;
- throw eMsg("createCPPType(): Failed to allocate memory");
- }
- tTypes.addObj(tTp);
- typeCreated=true;
- if(makeCopy)
- {
- newType=new cppType_template(*tTp);
- if(newType==NULL)
- {
- delete[] name;
- delete[] desc;
- throw eMsg("createCPPType(): Failed to allocate memory");
- }
- }
- }
- }
- delete[] name;
- delete[] desc;
- break;
- }
- }
- return typeCreated;
- }
- bool createCPPType(commandSource& comm, ostream& out)
- {
- cppType* dummy=NULL;
- return createCPPType(comm, out, dummy, false);
- }
- bool createTextMap(commandSource& comm, ostream& out, mapText*& txtMp)
- {
- bool getText=true;
- bool exit=false;
- char ansYN;
- txtMp=new mapText();
- const char* txt=NULL;
- // get text
- while(getText)
- {
- out << "Give the code for the new map:" << endl;
- txt=copyLine(comm.getText());
- if(txt==NULL)
- {
- delete txtMp;
- throw eMsg("createTextMap(): Failed to allocate memory");
- }
- if(txtMp->setText(txt))
- {
- getText=false;
- }
- else
- {
- out << "The text given is malformed, try again? [y]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- {
- exit=true;
- getText=false;
- }
- }
- delete[] txt;
- }
- if(exit)
- {
- delete txtMp;
- txtMp=NULL;
- return false;
- }
- // get description
- out << "Give a brief description: ";
- out.flush();
- txt=copyLine(comm.getLine());
- if(txt==NULL)
- {
- delete txtMp;
- throw eMsg("createTextMap(): Failed to allocate memory");
- }
- txtMp->setDescription(txt);
- delete[] txt;
- // get inputs
- for(unsigned long i=0;i<txtMp->getInCnt();i++)
- {
- out << "Give a brief description of input " << i+1 << ": ";
- out.flush();
- txt=copyLine(comm.getLine());
- if(txt==NULL)
- {
- delete txtMp;
- throw eMsg("createTextMap(): Failed to allocate memory");
- }
- txtMp->setInputDescription(i,txt);
- delete[] txt;
- }
- // get outputs
- for(unsigned long i=0;i<txtMp->getOutCnt();i++)
- {
- out << "Give a brief description of output " << i+1 << ": ";
- out.flush();
- txt=copyLine(comm.getLine());
- if(txt==NULL)
- {
- delete txtMp;
- throw eMsg("createTextMap(): Failed to allocate memory");
- }
- txtMp->setOutputDescription(i,txt);
- delete[] txt;
- }
- // get types
- for(unsigned long i=0;i<txtMp->getTypeCnt();i++)
- {
- out << "Give a brief description of type " << i+1 << ": ";
- out.flush();
- txt=copyLine(comm.getLine());
- if(txt==NULL)
- {
- delete txtMp;
- throw eMsg("createTextMap(): Failed to allocate memory");
- }
- txtMp->setTypeDescription(i,txt);
- delete[] txt;
- }
- // get customs
- for(unsigned long i=0;i<txtMp->getCustCnt();i++)
- {
- out << "Give a brief description of custom field " << i+1 << ": ";
- out.flush();
- txt=copyLine(comm.getLine());
- if(txt==NULL)
- {
- delete txtMp;
- throw eMsg("createTextMap(): Failed to allocate memory");
- }
- txtMp->setCustomDescription(i,txt);
- delete[] txt;
- }
- // ask for confirmation
- out << "About to submit mapText:" << endl;
- out << txtMp->getTxt() << endl;
- out << "Described as " << txtMp->getDescription() << endl;
- bool prntSpace=false;
- for(unsigned long i=0;i<txtMp->getInCnt();i++)
- {
- if(prntSpace)
- out << ", ";
- out << "Input " << i+1 << " is '" << txtMp->getInputDescription(i) << '\'';
- prntSpace=true;
- }
- for(unsigned long i=0;i<txtMp->getOutCnt();i++)
- {
- if(prntSpace)
- out << ", ";
- out << "Output " << i+1 << " is '" << txtMp->getOutputDescription(i) << '\'';
- prntSpace=true;
- }
- for(unsigned long i=0;i<txtMp->getTypeCnt();i++)
- {
- if(prntSpace)
- out << ", ";
- out << "Type " << i+1 << " is '" << txtMp->getTypeDescription(i) << '\'';
- prntSpace=true;
- }
- for(unsigned long i=0;i<txtMp->getCustCnt();i++)
- {
- if(prntSpace)
- out << ", ";
- out << "Custom field " << i+1 << " is '" << txtMp->getCustomDescription(i) << '\'';
- prntSpace=true;
- }
- out << endl << "Do you want to submit this? [y]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- {
- delete txtMp;
- txtMp=NULL;
- return false;
- }
- textMaps.addObj(txtMp);
- return true;
- }
- bool createTextMap(commandSource& comm, ostream& out)
- {
- mapText* tmp;
- return createTextMap(comm,out,tmp);
- }
- bool createCppFunctionName(commandSource& comm, ostream& out, cppFunctionName*& func)
- {
- out << "What name do you want for the function name: ";
- out.flush();
- const char* name=copyLine(comm.getToken());
- if(name==NULL)
- throw eMsg("createCppFunctionName(): Failed to allocate memory");
- out << "Are you sure you want a function called " << name << "? [y] ";
- out.flush();
- char ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- {
- delete[] name;
- return false;
- }
- func=new cppFunctionName(name);
- delete[] name;
- cppFuncs.addObj(func);
- return true;
- }
- bool createCppFunctionName(commandSource& comm, ostream& out)
- {
- cppFunctionName* func;
- return createCppFunctionName(comm, out, func);
- }
- bool readFortranFunction(commandSource& comm, ostream& out)
- {
- bool getList=true;
- bool retry=true;
- unsigned long fileCnt=0;
- unsigned long openFile=0;
- const char* fltr=NULL;
- char ansYN;
- unsigned long ans;
- unsigned long atFile;
- while(retry)
- {
- retry=false;
- while(getList)
- {
- if(fltr!=NULL)
- delete[] fltr;
- out << "Please specify a search filter: ";
- out.flush();
- fltr=copyLine(comm.getLine());
- for(unsigned long i=0;i<fileArgumentProcessed.size();i++)
- {
- if(!fileArgumentProcessed[i])
- {
- openFile++;
- if(strFilterEqual(fltr,fileArguments[i]))
- fileCnt++;
- }
- }
- if(openFile==0)
- {
- out << "There are no more fortran files to process" << endl;
- delete[] fltr;
- return false;
- }
- if(fileCnt==0)
- {
- out << "The filter provided does not match any of the open files, try another? [y]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- getList=false;
- }
- else
- {
- getList=false;
- }
- }
- if(fileCnt==0)
- {
- delete[] fltr;
- return false;
- }
- if(fileCnt>1)
- {
- vector<const char*> opt(fileCnt,NULL);
- vector<unsigned long> fileIndex(fileCnt,0);
- vector<bool> del(fileCnt,false);
- atFile=0;
- for(unsigned long i=0;i<fileArgumentProcessed.size();i++)
- {
- if(!fileArgumentProcessed[i] && strFilterEqual(fltr,fileArguments[i]))
- {
- opt[atFile]=fileArguments[i];
- fileIndex[atFile]=i;
- atFile++;
- }
- }
- if(!basicMenu("What file would you like to process?",opt,del,out,comm,ans,0))
- {
- delete[] fltr;
- return false;
- }
- atFile=fileIndex[ans];
- }
- else
- {
- for(unsigned long i=0;i<fileArgumentProcessed.size();i++)
- {
- if(!fileArgumentProcessed[i] && strFilterEqual(fltr,fileArguments[i]))
- {
- atFile=i;
- break;
- }
- }
- out << "Going to process " << fileArguments[atFile] << " is this correct [y]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- retry=true;
- }
- }
- delete[] fltr;
- // read a fortran function
- fortranFunction test(fileArguments[atFile]);
- out << "The prototype for the function is ";
- test.printDecl(out);
- out << endl;
- // now lets make sure all the arguments are set
- for(unsigned long i=0;i<test.argCount();i++)
- {
- bool isOut;
- const char* argDesc;
- bool overRide=false;
- if(test.getArgStat(i)==argUNKNOWN)
- {
- out << "Is argument " << test.getArgumentName(i) << " in position " << i+1 << " of the argument list an output? [n]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- isOut=true;
- else
- isOut=false;
- }
- else
- {
- if(test.getArgStat(i)==argOUTPUT)
- isOut=true;
- else
- isOut=false;
- out << "Argument " << test.getArgumentName(i) << " in position " << i+1 << " of the argument list is an ";
- if(isOut)
- out << " output ";
- else
- out << " input ";
- out << "is this correct? [y]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- {
- isOut=!isOut;
- overRide=true;
- }
- }
- if(test.getArgStat(i)==argUNKNOWN)
- {
- out << "Give a brief description of the argument: ";
- out.flush();
- argDesc=comm.getLine();
- if(isOut)
- test.setArgStat(i,argOUTPUT,argDesc);
- else
- test.setArgStat(i,argINPUT,argDesc);
- }
- else
- {
- bool newDesc=false;
- out << "Give a brief description of the argument: [" << test.getArgumentDescription(i) << "] ";
- out.flush();
- argDesc=comm.getLine();
- if(argDesc[0]!=0)
- newDesc=true;
- else
- argDesc=test.getArgumentDescription(i);
- if(newDesc || overRide)
- {
- if(isOut)
- test.setArgStat(i,argOUTPUT,argDesc);
- else
- test.setArgStat(i,argINPUT,argDesc);
- }
- }
- }
- out << "\nAbout to add the fortran function defined in file:" << endl;
- out << '\t' << fileArguments[atFile] << endl;
- out << "That defines the function" << endl;
- out << '\t';
- test.printDecl(out);
- out << endl << endl << "With the following argument descriptions:" << endl;
- for(unsigned long i=0;i<test.argCount();i++)
- {
- out << '\t' << test.getArgumentName(i) << '\t';
- if(test.getArgStat(i)==argOUTPUT)
- out << "out\t";
- else
- out << "in\t";
- out << test.getArgumentDescription(i) << endl;
- }
- out << "Do you want to submit this? [y]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- return false;
- fortranFunctionList.push_back(test);
- fileArgumentProcessed[atFile]=true;
- fortranSource.addDeclaration(&(fortranFunctionList.back()));
- return true;
- }
- // Retrieve cpp function name
- bool getCppFunctionName(commandSource& comm, ostream& out, cppFunctionName*& func)
- {
- // populate menu with all the available cpp function names
- unsigned long cnt=cppFuncs.size();
- unsigned long ans;
- out << "Creating function name for ";
- fortranFunctionList.back().printDecl(out);
- out << endl;
- if(cnt==0)
- return createCppFunctionName(comm,out,func);
- vector<const char*> opt(cnt+1,NULL);
- vector<bool> del(cnt+1,false);
- opt[0]="Create new function name";
- for(unsigned long i=0;i<cnt;i++)
- opt[i+1]=cppFuncs.getObj(i)->getName();
- // note that an option to define new type will always be present
- // wait for selection
- stringstream ss;
- ss.str("");
- ss << "Retrieve function name for ";
- fortranFunctionList.back().printDecl(ss);
- if(basicMenu(ss.str().c_str(),opt,del,out,comm,ans,1))
- {
- if(ans==0)
- {
- out << "Creating function name for ";
- fortranFunctionList.back().printDecl(out);
- out << endl;
- return createCppFunctionName(comm,out,func);
- }
- func=cppFuncs.getObj(ans-1);
- return true;
- }
- return false;
- }
- // RETRIEVE CPP TYPE
- bool getCppType(commandSource& comm,ostream& out,cppType*& type)
- {
- unsigned long bCnt=bTypes.size();
- unsigned long tCnt=tTypes.size();
- unsigned long cnt=bCnt+tCnt+1;
- char ansYN;
- vector<const char*> opt(cnt,NULL);
- vector<bool> del(cnt,NULL);
- opt[0]="Define new C++ Type";
- stringstream ss;
- for(unsigned long i=0;i<bCnt;i++)
- {
- ss.str("");
- ss << bTypes.getObj(i)->getName() << " \t";
- bTypes.getObj(i)->printDescription(ss);
- opt[i+1]=copyLine(ss.str().c_str());
- if(opt[i+1]==NULL)
- {
- for(unsigned long j=1;j<(i+1);j++)
- delete[] opt[j];
- throw eMsg("getCppType(): Failed to allocate memory");
- }
- del[i+1]=true;
- }
- for(unsigned long i=0;i<tCnt;i++)
- {
- ss.str("");
- ss << tTypes.getObj(i)->getName() << " \t";
- tTypes.getObj(i)->printDescription(ss);
- opt[i+bCnt+1]=copyLine(ss.str().c_str());
- if(opt[i+bCnt+1]==NULL)
- {
- for(unsigned long j=1;j<(i+bCnt+1);j++)
- delete[] opt[j];
- throw eMsg("getCppType(): Failed to allocate memory");
- }
- del[i+bCnt+1]=true;
- }
- unsigned long ans;
- if(!basicMenu("Select an option for a C++ type:",opt,del,out,comm,ans,1))
- return false;
- if(ans==0)
- return createCPPType(comm,out,type,true);
- if(ans<=bCnt)
- {
- type=new cppType_basic(*(bTypes.getObj(ans-1)));
- if(type==NULL)
- throw eMsg("getCppType(): Failed to allocate memory");
- return true;
- }
- cppType_template* tmpType=new cppType_template(*(tTypes.getObj(ans-1-bCnt)));
- if(tmpType==NULL)
- throw eMsg("getCppType(): Failed to allocate memory");
- for(unsigned long i=0;i<tmpType->size();i++)
- {
- bool getType=true;
- while(getType)
- {
- out << "Need to retrieve information for template argument " << i+1 << endl;
- out << "Is the argument a type [y]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- {
- out << "Please specify the value for the argument: ";
- out.flush();
- long val=comm.getInt();
- tmpType->setArgType(i,val);
- getType=false;
- }
- else
- {
- out << "About to retrieve a type for the template argument " << i+1 << "..." << endl;
- cppType* arg;
- if(!getCppType(comm,out,arg))
- {
- out << "Failed to retrieve a type, do you want to exit to the previous menu [n]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- {
- delete tmpType;
- return false;
- }
- }
- else
- {
- getType=false;
- tmpType->setArgType(i,arg,true);
- }
- }
- }
- }
- type=tmpType;
- return true;
- }
- // GET THE MAP TEXT
- bool getCodeSegment(commandSource& comm, ostream& out, bool funcCalled, bool needReturn, mappingCode*& mapCode, callingCode*& callCode, bool& isReturn)
- {
- mapCode=NULL;
- callCode=NULL;
- isReturn=false;
- // callingCode
- // callingCode_assignNew
- // callingCode_assignOld
- // callingCode_return
- // returnValue
- // returnSink
- // nameMap_text...
- // objList<mapText> textMaps;
- unsigned long mpCnt=textMaps.size();
- unsigned long cnt;
- bool getCode=true;
- char ansYN;
- if(!funcCalled)
- {
- if(needReturn)
- cnt=mpCnt+5;
- else
- cnt=mpCnt+4;
- }
- else if(needReturn)
- {
- cnt=mpCnt+3;
- }
- else
- {
- cnt=mpCnt+1;
- }
- mapText* txtMp=NULL;
- while(getCode)
- {
- vector<const char*> opt(cnt,NULL);
- vector<bool> del(cnt,false);
- unsigned long atIndex=0;
- opt[atIndex++]="Define a new text map";
- if(!funcCalled)
- {
- opt[atIndex++]="Basic function call";
- opt[atIndex++]="Function call that assigns the return value to a new variable";
- opt[atIndex++]="Function call that assigns the return value to an existing variable";
- if(needReturn)
- opt[atIndex++]="Function call that return the return value of the function";
- }
- else if(needReturn)
- {
- opt[atIndex++]="Return a value";
- opt[atIndex++]="Return a variable";
- }
- for(unsigned long i=0;i<mpCnt;i++)
- opt[atIndex++]=textMaps.getObj(i)->getDescription();
- unsigned long ans;
- if(!basicMenu("What code segment do you want to add?",opt,del,out,comm,ans,1))
- return false;
- if(ans!=0)
- {
- if(!funcCalled)
- {
- if(needReturn)
- {
- if(ans>4)
- ans+=2;
- }
- else
- {
- if(ans>3)
- ans+=3;
- }
- }
- else
- {
- if(needReturn)
- {
- ans+=4;
- }
- else
- {
- ans+=6;
- }
- }
- }
- cppType* tp=NULL;
- const char* txt;
- switch(ans)
- {
- case 0:
- if(!createTextMap(comm,out,txtMp))
- {
- out << "Do you want to return to the main menu [n]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- return false;
- else
- break;
- }
- getCode=false;
- break;
- case 1:
- out << "Are you sure you want to submit a simple function call [y]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN!='n' && ansYN!='N')
- {
- callCode=new callingCode(&(fortranFunctionList.back()),false);
- if(callCode==NULL)
- throw eMsg("getCodeSegment(): Failed to allocate memory");
- return true;
- }
- out << "Do you want to return to the main menu [n]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- return false;
- break;
- case 2:
- out << "Need the type for the new variable" << endl;
- if(!getCppType(comm,out,tp))
- {
- out << "Do you want to return to the main menu [n]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- return false;
- break;
- }
- out << "What name do you want for this variable: ";
- out.flush();
- txt=copyLine(comm.getToken());
- out << "Are you sure you want to submit a function call that assigns to a variable of type ";
- tp->printType(out);
- out << " with the name " << txt << " [y] ? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN!='n' && ansYN!='N')
- {
- callCode=new callingCode_assignNew(&(fortranFunctionList.back()),false,tp,txt,true);
- delete[] txt;
- if(callCode==NULL)
- throw eMsg("getCodeSegment(): Failed to allocate memory");
- return true;
- }
- delete[] txt;
- out << "Do you want to return to the main menu [n]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- return false;
- break;
- case 3:
- out << "Are you sure you want to submit a function call that assigns to an existing variable [y]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN!='n' && ansYN!='N')
- {
- callCode=new callingCode_assignOld(&(fortranFunctionList.back()),false);
- if(callCode==NULL)
- throw eMsg("getCodeSegment(): Failed to allocate memory");
- return true;
- }
- out << "Do you want to return to the main menu [n]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- return false;
- break;
- case 4:
- out << "Are you sure you want to submit a function call where the result is the return value [y]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN!='n' && ansYN!='N')
- {
- callCode=new callingCode_return(&(fortranFunctionList.back()),false);
- if(callCode==NULL)
- throw eMsg("getCodeSegment(): Failed to allocate memory");
- isReturn=true;
- return true;
- }
- out << "Do you want to return to the main menu [n]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- return false;
- break;
- case 5:
- out << "What value to you want to return: ";
- out.flush();
- txt=copyLine(comm.getToken());
- out << "Do you want to submit a return the value " << txt << " statement [y]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN!='n' && ansYN!='N')
- {
- mapCode=new returnValue(txt);
- delete[] txt;
- isReturn=true;
- return true;
- }
- delete[] txt;
- out << "Do you want to return to the main menu [n]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- return false;
- break;
- case 6:
- out << "Do you want to submit a return a variable statement [y]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN!='n' && ansYN!='N')
- {
- mapCode=new returnSink();
- isReturn=true;
- return true;
- }
- out << "Do you want to return to the main menu [n]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- return false;
- break;
- default:
- txtMp=textMaps.getObj(ans-7);
- getCode=false;
- break;
- }
- }
- nameMap_text* map=new nameMap_text(*txtMp);
- if(map==NULL)
- throw eMsg("getCodeSegment(): Failed to allocate memory");
- const char* tok;
- cppType* tmpType;
- for(unsigned long i=0;i<txtMp->getOutCnt();i++)
- {
- out << "Specify the name for output " << i+1 << " described by " << txtMp->getOutputDescription(i) << ": ";
- out.flush();
- tok=comm.getToken();
- map->setOutName(i,tok);
- }
- for(unsigned long i=0;i<txtMp->getCustCnt();i++)
- {
- out << "Specify the token for the custom field " << i+1 << " described by " << txtMp->getCustomDescription(i) << ": ";
- out.flush();
- tok=comm.getToken();
- map->setCustName(i,tok);
- }
- for(unsigned long i=0;i<txtMp->getTypeCnt();i++)
- {
- bool getType=true;
- while(getType)
- {
- out << "Specify the type for type " << i+1 << " described as " << txtMp->getTypeDescription(i) << endl;
- if(!getCppType(comm,out,tmpType))
- {
- out << "Do you want to return to the main menu [n] ? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- {
- delete map;
- return false;
- }
- }
- else
- {
- getType=false;
- }
- }
- map->setCppType(i,tmpType,true);
- }
- out << "About to submit the text map described as " << txtMp->getDescription() << " and written as " << endl;
- out << txtMp->getTxt() << endl;
- for(unsigned long i=0;i<txtMp->getOutCnt();i++)
- out << "With output " << i+1 << ": " << map->getOutName(i) << endl;
- for(unsigned long i=0;i<txtMp->getCustCnt();i++)
- out << "With custom field " << i+1 << ": " << map->getCustName(i) << endl;
- for(unsigned long i=0;i<txtMp->getTypeCnt();i++)
- {
- out << "With type " << i+1 << ": ";
- map->getCppType(i)->printType(out);
- out << endl;
- }
- out << "Are you sure everything is correct [y]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- {
- delete map;
- return false;
- }
- mapCode=map;
- return true;
- }
- // CPP FUNCTION INSTANCE
- bool createCppFunctionInstance(commandSource& comm, ostream& out, cppFunctionInstance*& fncInst)
- {
- fncInst=NULL;
- char ansYN;
- if(fortranFunctionList.size()==0)
- {
- out << "Cannot define a C++ function until a fortran function has been defined" << endl;
- return false;
- }
- cppFunctionName* funcName;
- // Call Retrieve Cpp function Name
- if(!getCppFunctionName(comm,out,funcName))
- return false;
- cppFunctionInstance* fnc=funcName->createFunctionInstance();
- if(fnc==NULL)
- throw eMsg("createCppFunctionInstance(): Failed to allocate memory");
- unsigned long argCnt;
- out << "Defining function for ";
- fortranFunctionList.back().printDecl(out);
- out << endl;
- out << "How many arguments for this function? ";
- out.flush();
- argCnt=comm.getInt();
- fnc->setArgumentCount(argCnt);
- for(unsigned long i=0;i<argCnt;i++)
- {
- cppType* argType;
- // get the cpp type
- if(!getCppType(comm,out,argType))
- {
- funcName->deleteLastInstance();
- return false;
- }
- // ask if it is a reference
- out << "Is this const? [n] ";
- out.flush();
- ansYN=comm.getChar();
- bool isCnst=false;
- if(ansYN=='y' || ansYN=='Y')
- isCnst=true;
- // get the pointer level
- bool getLevel=true;
- bool fail=false;
- long pntrCnt=0;
- while(getLevel && !fail)
- {
- out << "How many pointer levels is this argument? ";
- out.flush();
- pntrCnt=comm.getInt();
- if(pntrCnt<0)
- {
- out << "Cannot accept a negative value, try again? [y]:";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- {
- fail=true;
- getLevel=false;
- }
- }
- else
- {
- getLevel=false;
- }
- }
- if(fail)
- {
- funcName->deleteLastInstance();
- delete argType;
- return false;
- }
- // ask if it is a reference
- out << "Is this a reference? [n] ";
- out.flush();
- ansYN=comm.getChar();
- bool isRef=false;
- if(ansYN=='y' || ansYN=='Y')
- isRef=true;
- // ask for the name of the variable
- out << "What name do you want to give to this variable? ";
- out.flush();
- const char* name=comm.getToken();
- variable* var=new variable(argType,name,pntrCnt,isRef,isCnst,true);
- if(var==NULL)
- {
- delete argType;
- throw eMsg("createCppFunctionInstance(): Failed to allocate memory");
- }
- out << "Describe the variable: ";
- out.flush();
- const char* desc=comm.getLine();
- fnc->addArgument(i,var,desc,true);
- }
- // Retrieve the return tyoe
- out << "\n\n\nRetrieve the return type for this function" << endl;
- cppType* retType;
- if(!getCppType(comm,out,retType))
- {
- funcName->deleteLastInstance();
- return false;
- }
- fnc->defineReturn(retType);
- bool addingMaps=true;
- bool funcCalled=false;
- mappingCode* mapCode;
- callingCode* callCode;
- bool isReturn;
- bool needReturn=((*retType)!=(*voidType));
- while(addingMaps)
- {
- if(funcCalled && !needReturn)
- {
- out << "Note the function call has occured and no return statement is needed, do you want to add any more code segments [n]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN!='y' && ansYN!='Y')
- addingMaps=false;
- }
- if(addingMaps)
- {
- if(!getCodeSegment(comm, out, funcCalled, needReturn, mapCode, callCode, isReturn))
- {
- funcName->deleteLastInstance();
- return false;
- }
- if(mapCode==NULL && callCode==NULL)
- {
- out << "An error occurred trying to get the map" << endl;
- funcName->deleteLastInstance();
- return false;
- }
- if(mapCode!=NULL)
- fnc->addMap(mapCode,isReturn,true);
- if(callCode!=NULL)
- {
- fnc->addCall(callCode,isReturn,true);
- funcCalled=true;
- }
- // connect all the sinks with a source
- nameSink* snk=fnc->getSink();
- if(snk==NULL)
- throw eMsg("createCppFunctionInstance(): Unexplained error");
- for(unsigned long i=0;i<snk->sinkSize();i++)
- {
- stringstream ss;
- ss.str("");
- ss << "Select the source for sink term " << i+1 << " of the last code segment described as " << snk->getSinkTermDescription(i);
- unsigned long srcCnt=fnc->sourceObjectCount()-1;
- vector<bool> delHead(srcCnt,false);
- unsigned long major=0;
- unsigned long minor=0;
- vector<unsigned long> permCnt(srcCnt,0);
- vector<const char*> pageHeader(srcCnt,NULL);
- vector< vector<const char*> > options(srcCnt);
- vector< vector<bool> > canDelete(srcCnt);
- unsigned long srcTrmCnt=0;
- for(unsigned long j=0;j<srcCnt;j++)
- {
- nameSource* src=fnc->getSource(j);
- pageHeader[j]=src->getSourceDescription();
- options[j].resize(src->sourceSize(),NULL);
- canDelete[j].resize(src->sourceSize(),true);
- for(unsigned long k=0;k<src->sourceSize();k++)
- {
- srcTrmCnt++;
- stringstream srcTrm;
- srcTrm.str("");
- srcTrm << "Variable '" << src->getSourceName(k) << "' as described as '" << src->getSourceTermDescription(k) << "'";
- options[j][k]=copyLine(srcTrm.str().c_str());
- }
- }
- if(srcTrmCnt==0)
- {
- out << "\n\nFailed to add code segment because there are no sources for the sink\n\n" << endl;
- funcName->deleteLastInstance();
- return false;
- }
- if(!nestedMenu(ss.str().c_str(), pageHeader, options, delHead, canDelete, out, comm, major, minor, permCnt))
- {
- funcName->deleteLastInstance();
- return false;
- }
- fnc->getSink()->setSource(i,fnc->getSource(major),minor); // INFO
- }
- // adjust to any changes in state
- if(isReturn)
- {
- if(!funcCalled || !fnc->canWrite())
- {
- out << "The sequence of code is invalid" << endl;
- funcName->deleteLastInstance();
- }
- else
- {
- addingMaps=false;
- }
- }
- }
- }
- out << "The C++ function is " << endl;
- fnc->writeDefinition(out);
- out << '\n' << endl;
- out << "Do you want to submit this [y]? ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='n' || ansYN=='N')
- {
- funcName->deleteLastInstance();
- return false;
- }
- fncInst=fnc;
- cppPrototype.addDeclaration(fnc);
- cppDefinition.addDefinition(fnc);
- return true;
- // while(addingArgs)
- // {
- // Ask "Do you want to add an argument?"
- // if( no )
- // break;
- // Call retrieve cpp function type
- // Get var name
- // Get the variable description
- // add variable to the instance
- // }
- // Ask "Is there a return value"?
- // If yes
- // Retrieve cpp Type
- // set retType accordingly
- // else
- // set return type to void
- // while ( adding maps )
- // {
- // permanent entry is new map selection
- // populate menu entries with map descriptions
- // // Note that if call has been added do not list calling maps
- // wait for selection
- // if( new map selected )
- // define new map
- // else
- // with response retrieve entry
- // for all outputs
- // display description
- // get token
- // for all types
- // display description
- // retrieve cpp type
- // for all customs
- // display description
- // get token
- // for all sink terms
- // set major title to "retrieving source for <sink description>: <insert sink term description>"
- // for each non-empty source
- // set minor title to "<insert source description>"
- // populate menue with each source term description
- // wait for selection
- // set sink term
- // if !canAdd
- // break;
- // if canWrite
- // Ask "do you want to add another map?"
- // if no
- // break
- // }
- // return instance
- }
- bool createCppFunctionInstance(commandSource& comm, ostream& out)
- {
- cppFunctionInstance* fncInst=NULL;
- return createCppFunctionInstance(comm,out,fncInst);
- }
- // RUN COMMANDS
- void runCommands(commandSource& comm, ostream& out)
- {
- bool canExit=false;
- while(!canExit && comm.startRecord())
- {
- vector<const char*> opt;
- vector<bool> del;
- if(fortranFunctionList.size()>0)
- {
- opt.resize(6);
- del.resize(6,false);
- }
- else
- {
- opt.resize(5);
- del.resize(5,false);
- }
- opt[0]="Define a new C++ type";
- opt[1]="Define a new code map";
- opt[2]="Define a new C++ function name";
- opt[3]="Define a fortran function";
- if(fortranFunctionList.size()>0)
- {
- opt[4]="Define a new C++ function instance";
- opt[5]="Return to the main menu";
- }
- else
- {
- opt[4]="Return to the main menu";
- }
- stringstream ss;
- ss << "What object do you want to define?";
- if(fortranFunctionList.size()>0)
- {
- ss << " [";
- fortranFunctionList.back().printDecl(ss);
- ss << "]";
- }
- unsigned long ans;
- if(basicMenu(ss.str().c_str(), opt, del, out, comm, ans, 0))
- {
- if(fortranFunctionList.size()==0 && ans==4)
- ans=5;
- switch(ans)
- {
- case 0:
- out << "About to define a new C++ type" << endl;
- try
- {
- if(createCPPType(comm,out))
- comm.flushRecord();
- else
- comm.clearRecord();
- }
- catch(eMsg e)
- {
- comm.flushErrorToFile(out,"error.err");
- throw e;
- }
- break;
- case 1:
- out << "About to define a new code map" << endl;
- try
- {
- if(createTextMap(comm, out))
- comm.flushRecord();
- else
- comm.clearRecord();
- }
- catch(eMsg e)
- {
- comm.flushErrorToFile(out,"error.err");
- throw e;
- }
- break;
- case 2:
- out << "About to define a new C++ function name" << endl;
- try
- {
- if(createCppFunctionName(comm,out))
- comm.flushRecord();
- else
- comm.clearRecord();
- }
- catch(eMsg e)
- {
- comm.flushErrorToFile(out,"error.err");
- throw e;
- }
- break;
- case 3:
- out << "About to read a new fortran function" << endl;
- try
- {
- if(readFortranFunction(comm,out))
- comm.flushRecord();
- else
- comm.clearRecord();
- }
- catch(eMsg e)
- {
- comm.flushErrorToFile(out,"error.err");
- throw e;
- }
- break;
- case 4:
- out << "About to define a new instance of a C++ function" << endl;
- try
- {
- if(createCppFunctionInstance(comm,out))
- comm.flushRecord();
- else
- comm.clearRecord();
- }
- catch(eMsg e)
- {
- comm.flushErrorToFile(out,"error.err");
- throw e;
- }
- break;
- case 5:
- out << "Returning to the main menu..." << endl;
- canExit=true;
- break;
- }
- }
- else
- {
- canExit=true;
- }
- }
- comm.exitRecord();
- // while ( not exit )
- // populate entries (with if statement to check if fortran file is loaded)
- // wait for selection
- // switch selection
- // case define new type
- // define new type
- // break
- // case define new map
- // define new map
- // break;
- // case define new cpp function name
- // cpp function name
- // break;
- // case select fortran function
- // populate menue entries for fortran functions (from argument list)
- // wait for selection
- // set fortran function type
- // add fortran type to the fortran code file
- // note that there is no break here
- // case retrieve C++ function instance
- // retrieve C++ function instance
- // add C++ function instance to both the definition and declaration C++ source files
- // }
- }
- // MAIN
- int mainMenu(commandSource& comm, ostream& out)
- {
- // while ( not exit )
- bool exit=false;
- while(!exit)
- {
- //
- // populate menu entries
- unsigned long ans;
- vector<const char*> opt(8,NULL);
- stringstream ss;
- ss.str("");
- ss << "Define FORTRAN prototype file [" << fortranSource.getFileName() << ']';
- opt[0]=copyLine(ss.str().c_str());
- if(opt[0]==NULL)
- throw eMsg("mainMenu(): Failed to allocate memory");
- ss.str("");
- ss << "Define C++ prototype file [" << cppPrototype.getFileName() << ']';
- opt[1]=copyLine(ss.str().c_str());
- if(opt[1]==NULL)
- throw eMsg("mainMenu(): Failed to allocate memory");
- ss.str("");
- ss << "Define C++ definition file [" << cppDefinition.getFileName() << ']';
- opt[2]=copyLine(ss.str().c_str());
- if(opt[2]==NULL)
- throw eMsg("mainMenu(): Failed to allocate memory");
- ss.str("");
- ss << "Define command history file [" << recorder.getRecordFile() << ']';
- if(recorder.getAppendState())
- ss << " appending output";
- else
- ss << " writing new output";
- opt[3]=copyLine(ss.str().c_str());
- if(opt[3]==NULL)
- throw eMsg("mainMenu(): Failed to allocate memory");
- opt[4]="Run recorded commands";
- opt[5]="Execute new commands";
- opt[6]="Write Code";
- opt[7]="Quit";
- vector<bool> del(8,false);
- del[0]=true;
- del[1]=true;
- del[2]=true;
- del[3]=true;
- if(basicMenu("Main Menu: What would you like to do?", opt, del, out, comm, ans, 0))
- {
- if(ans==7)
- exit=true;
- }
- else
- {
- exit=true;
- }
- if(!exit)
- {
- const char* fn;
- char ansYN;
- bool appendFile;
- fstream fin;
- commandSource_file commFile(fin,&cout);
- commandSource* commAuto;
- bool switchAppState=false;
- switch(ans)
- {
- case 0:
- out << "Defining the FORTRAN prototype file..." << endl;
- out << "Specify the output file: ";
- out.flush();
- fortranSource.setFileName(comm.getLine());
- out << endl;
- break;
- case 1:
- out << "Defining the C++ prototype file..." << endl;
- out << "Specify the output file: ";
- out.flush();
- cppPrototype.setFileName(comm.getLine());
- out << endl;
- break;
- case 2:
- out << "Defining the C++ definition file..." << endl;
- out << "Specify the output file: ";
- out.flush();
- cppDefinition.setFileName(comm.getLine());
- out << endl;
- break;
- case 3:
- out << "Defining the command history file" << endl;
- out << "Specify the output file [" << recorder.getRecordFile() << "]: ";
- out.flush();
- fn=copyLine(comm.getLine());
- out << "Do you want to overwrite the file y/n [n]: ";
- out.flush();
- ansYN=comm.getChar();
- appendFile=true;
- if(ansYN=='y' || ansYN=='Y')
- appendFile=false;
- if(fn[0]==0)
- recorder.setAppendState(appendFile);
- else
- recorder.setRecordFile(fn,appendFile);
- delete[] fn;
- break;
- case 4:
- out << "Do you want to run the commands already recorded in " << recorder.getRecordFile() << "? [y]: ";
- out.flush();
- ansYN=comm.getChar();
- commAuto=&commFile;
- if(ansYN=='n' || ansYN=='N')
- {
- out << "Specify what command file: ";
- out.flush();
- fn=copyLine(comm.getLine());
- if(!strEqual(fn,recorder.getRecordFile()))
- {
- out << "Do you want to append these recorded commands to the current command record? [n]: ";
- out.flush();
- ansYN=comm.getChar();
- if(ansYN=='y' || ansYN=='Y')
- {
- commAuto=&recorder;
- recorder.setCommandSource(&commFile);
- }
- }
- }
- else
- {
- fn=copyLine(recorder.getRecordFile());
- switchAppState=true;
- }
- fin.open(fn,fstream::in);
- delete[] fn;
- if(!fin.is_open())
- throw eMsg("mainMenu(): Failed to open file");
- out << "running recorded input now..." << endl;
- runCommands(*commAuto,out);
- fin.close();
- fin.clear();
- if(switchAppState)
- recorder.setAppendState(true);
- out << endl << endl << "We have completed running the recorded commands" << endl;
- break;
- case 5:
- recorder.setCommandSource(&comm);
- out << "executing commands now..." << endl;
- runCommands(recorder,out);
- break;
- case 6:
- out << "Will write code now..." << endl;
- fortranSource.writeFile(false);
- if(strEqual(cppPrototype.getFileName(),fortranSource.getFileName()))
- cppPrototype.writeFile(true);
- else
- cppPrototype.writeFile(false);
- if(strEqual(cppDefinition.getFileName(),fortranSource.getFileName()) || strEqual(cppDefinition.getFileName(),cppPrototype.getFileName()))
- cppDefinition.writeFile(true);
- else
- cppDefinition.writeFile(false);
- break;
- }
- }
- // wait for selection(terminal command sourc)
- // switch selection
- // case get file X
- // get file X
- // case exit
- // set exit
- // break
- // case write code
- // write code
- // break
- // case run recorded commands
- // create new file based command source
- // runCommands ( file command source )
- // ask if new commands should be appended to this file
- // break
- // case run terminal commands
- // runCommands ( terminal cammand source )
- // break
- }
- return 0;
- }
- /*
- RETRIEVE CPP FUNCTION NAME
- populate menu with all the available cpp function names
- // note that an option to define new type will always be present
- wait for selection
- switch selection
- case define new cpp name
- define new cpp name
- return result
- case old name
- return the selection
- RETRIEVE CPP TYPE
- populate menu with all the available cpp types (with description)
- // note and option to define a new type will always be present
- if define a new type
- define new cpp type
- return result
- else
- {
- with selection retrieve the cpp type
- if template type
- {
- for each template argument
- {
- ask "is type?"
- if yes
- Retrieve CPP Type
- else
- command getLong
- }
- }
- }
- */
- int main(int argc,char** argv)
- {
- if(argc<6)
- {
- cout << "laFortRead fortran_prototype c++_prototype c++_definition command_history input_1 input_2 ... input_n" << endl;
- return 1;
- }
- fortranSource.setFileName(argv[1]);
- cppPrototype.setFileName(argv[2]);
- cppDefinition.setFileName(argv[3]);
- // define some basic C++ types
- cppType_basic* bTp;
- bTp=new cppType_basic("void","This is nothing");
- bTypes.addObj(bTp);
- voidType=bTp;
- bTp=new cppType_basic("bool","This is a boolean");
- bTypes.addObj(bTp);
- bTp=new cppType_basic("char","This is a simple character");
- bTypes.addObj(bTp);
- bTp=new cppType_basic("long","This is an integer");
- bTypes.addObj(bTp);
- bTp=new cppType_basic("float","This is a single precision float");
- bTypes.addObj(bTp);
- bTp=new cppType_basic("double","This is a double precision float");
- bTypes.addObj(bTp);
- bTp=new cppType_basic("complex<float>","This is the single precision complex number");
- bTypes.addObj(bTp);
- bTp=new cppType_basic("complex<double>","This is the double precision complex number");
- bTypes.addObj(bTp);
- // define the analogous fortran types
- fortranType* fTp;
- fTp=new fortranType(1);
- fTp->setTypeSize(0,1).setText(0,0,"LOGICAL").setType(bTypes.getObj(1));
- fTypes.addObj(fTp);
- fTp=new fortranType(2);
- fTp->setTypeSize(0,1).setText(0,0,"BYTE").setTypeSize(1,1).setText(1,0,"CHARACTER").setType(bTypes.getObj(2));
- fTypes.addObj(fTp);
- fTp=new fortranType(1);
- fTp->setTypeSize(0,1).setText(0,0,"INTEGER").setType(bTypes.getObj(3));
- fTypes.addObj(fTp);
- fTp=new fortranType(1);
- fTp->setTypeSize(0,1).setText(0,0,"REAL").setType(bTypes.getObj(4));
- fTypes.addObj(fTp);
- fTp=new fortranType(2);
- fTp->setTypeSize(0,1).setText(0,0,"REAL*8").setTypeSize(1,2).setText(1,0,"DOUBLE").setText(1,1,"PRECISION").setType(bTypes.getObj(5));
- fTypes.addObj(fTp);
- fTp=new fortranType(1);
- fTp->setTypeSize(0,1).setText(0,0,"COMPLEX").setType(bTypes.getObj(6));
- fTypes.addObj(fTp);
- fTp=new fortranType(2);
- fTp->setTypeSize(0,1).setText(0,0,"COMPLEX*16").setTypeSize(1,2).setText(1,0,"DOUBLE").setText(1,1,"COMPLEX").setType(bTypes.getObj(7));
- fTypes.addObj(fTp);
- fortranSource.setFileName(argv[1]);
- cppPrototype.setFileName(argv[2]);
- cppDefinition.setFileName(argv[3]);
- recorder.setRecordFile(argv[4],false);
- fileArguments.resize(argc-5,NULL);
- fileArgumentProcessed.resize(argc-5,NULL);
- for(unsigned long i=0;i<fileArguments.size();i++)
- {
- fileArguments[i]=argv[i+5];
- fileArgumentProcessed[i]=false;
- }
- commandSource_basic comm(cin);
- return mainMenu(comm, cout);
- /*
- fstream pOut;
- fstream dOut;
- fstream cOut;
- fstream fin;
- // temporary variables for creating variables
- cppType_template* tmpTType;
- cppType_basic* tmpBType;
- // read a fortran function
- fortranFunction test(argv[1]);
- // now lets make sure all the arguments are set
- for(unsigned long i=0;i<test.argCount();i++)
- {
- if(test.getArgStat(i)==argUNKNOWN)
- test.setArgStat(i,argOUTPUT);
- }
- cout << endl << "About to print the prototype for the fortran function" << endl;
- test.printDecl(cout);
- cout << ';' << endl;
- // define a new template type
- cppType_template* tTp;
- tTp=new cppType_template(1);
- if(tTp==NULL)
- throw eMsg("main(): Failed to allocate memory");
- tTp->setName("matrixRdWt_GenCol");
- tTp->setDescription("General Matrix");
- tTypes.addObj(tTp);
- tTp=new cppType_template(1);
- if(tTp==NULL)
- throw eMsg("main(): Failed to allocate memory");
- tTp->setName("matrixRdWtSqr_GenCol");
- tTp->setDescription("General Square Matrix");
- tTypes.addObj(tTp);
- // define/select a function name
- cppFunctionName* fncName;
- fncName=new cppFunctionName("solveMatrix");
- cppFuncs.addObj(fncName);
- // create instance
- cppFunctionInstance* fnc=fncName->createFunctionInstance();
- // retrieve the types
- tmpBType=bTypes.getObj(5);
- tmpTType=tTypes.getObj(0);
- cppType_template* bMtxType;
- bMtxType=new cppType_template(*tmpTType);
- bMtxType->setArgType(0,tmpBType,false);
- tmpTType=tTypes.getObj(1);
- cppType_template* aMtxType;
- aMtxType=new cppType_template(*tmpTType);
- aMtxType->setArgType(0,tmpBType,false);
- // add the arguments
- fnc->setArgumentCount(3);
- variable* varA=new variable(aMtxType,"A",0,true,true);
- variable* varB=new variable(bMtxType,"B",0,true,true);
- tmpBType=bTypes.getObj(3);
- variable* varPivot=new variable(tmpBType,"pivot",1,true);
- fnc->defineReturn(tmpBType);
- fnc->addArgument(0,varA,"The A Matrix", true);
- fnc->addArgument(1,varB,"The B Matrix", true);
- fnc->addArgument(2,varPivot,"The pivot array", true);
- // print the C++ declaration
- cout << endl << "About to print the prototype for the cpp function instance" << endl;
- fnc->printDecl(cout);
- // =====================
- // this section is devoted to writing code
- // =====================
- mapText* tmpMpTxt;
- nameMap_text* tmpNameMap;
- // create the simple variable declaration statement
- tmpMpTxt=new mapText("\t@(t0) @(o0)=@(c0);");
- if(tmpMpTxt==NULL)
- throw eMsg("main(): Failed to allocate memory");
- tmpMpTxt->setDescription("Declares a new variable with an initialized value");
- tmpMpTxt->setOutputDescription(0,"The variable name");
- tmpMpTxt->setTypeDescription(0,"The variable type");
- tmpMpTxt->setCustomDescription(0,"The initialized value");
- textMaps.addObj(tmpMpTxt);
- // add the variable declaration to the code
- tmpNameMap=new nameMap_text(*tmpMpTxt);
- if(tmpNameMap==NULL)
- throw eMsg("main(): Failed to allocate memory");
- tmpNameMap->setOutName(0,"info");
- tmpNameMap->setCustName(0,"0");
- tmpNameMap->setCppType(0,new cppType_basic(*(bTypes.getObj(3))),true);
- fnc->addMap(tmpNameMap,false,true);
- // create the matrix info extraction statements
- tmpMpTxt=new mapText("\tunsigned long @(o1), @(o2);\n\t@(o0)=@(i0).array();\n\t@(i0).size(@(o1),@(o2));\n\t@(o3)=@(i0).leadDim();");
- if(tmpMpTxt==NULL)
- throw eMsg("main(): Failed to allocate memory");
- tmpMpTxt->setDescription("Extracts all the information from a matrix to make a call to LAPACK");
- tmpMpTxt->setInputDescription(0,"The matrix that will be used in a future LAPACK call");
- tmpMpTxt->setOutputDescription(0, "The matrix array");
- tmpMpTxt->setOutputDescription(1, "Number of rows in the matrix");
- tmpMpTxt->setOutputDescription(2, "Number of columns in the matrix");
- tmpMpTxt->setOutputDescription(3, "The leading dimension in the matrix");
- textMaps.addObj(tmpMpTxt);
- // add the array extraction for the A matrix
- tmpNameMap=new nameMap_text(*tmpMpTxt);
- if(tmpNameMap==NULL)
- throw eMsg("main(): Failed to allocate memory");
- tmpNameMap->setOutName(0,"arrayA");
- tmpNameMap->setOutName(1,"rowsA");
- tmpNameMap->setOutName(2,"colsA");
- tmpNameMap->setOutName(3,"ldA");
- fnc->addMap(tmpNameMap,false,true);
- // set the argument to this segments input
- fnc->getSink()->setSource(0,fnc->getSource(0),0);
- // the the array extraction for the B matrix
- tmpNameMap=new nameMap_text(*tmpMpTxt);
- if(tmpNameMap==NULL)
- throw eMsg("main(): Failed to allocate memory");
- tmpNameMap->setOutName(0,"arrayB");
- tmpNameMap->setOutName(1,"rowsB");
- tmpNameMap->setOutName(2,"colsB");
- tmpNameMap->setOutName(3,"ldB");
- fnc->addMap(tmpNameMap,false,true);
- // link the function argument to this statement
- fnc->getSink()->setSource(0,fnc->getSource(0),1);
- // add the fortran function call to the code
- callingCode* clCd=new callingCode(&test);
- if(clCd==NULL)
- throw eMsg("main(): failed to allocate memory");
- fnc->addCall(clCd,false,true);
- // link all the inputs to the arguments
- fnc->getSink()->setSource(0,fnc->getSource(2),1); // N
- fnc->getSink()->setSource(1,fnc->getSource(3),2); // NRHS
- fnc->getSink()->setSource(2,fnc->getSource(2),0); // A
- fnc->getSink()->setSource(3,fnc->getSource(2),3); // LDA
- fnc->getSink()->setSource(4,fnc->getSource(0),2); // IPIV
- fnc->getSink()->setSource(5,fnc->getSource(3),0); // B
- fnc->getSink()->setSource(6,fnc->getSource(3),3); // LDB
- fnc->getSink()->setSource(7,fnc->getSource(1),0); // INFO
- // create and add the return sink
- returnSink* retSnk;
- retSnk=new returnSink();
- if(retSnk==NULL)
- throw eMsg("main(): failed to allocate memory");
- fnc->addMap(retSnk,true,true);
- fnc->getSink()->setSource(0,fnc->getSource(1),0);
- cout << endl;
- cout << "About to write the cpp function definition" << endl;
- fnc->writeDefinition(cout);
- cout << endl;
- commandSource_basic comm(cin);
-
- vector<const char*> opts(15,"option");
- opts[0]="option 0";
- opts[1]="option 1";
- opts[2]="option 2";
- opts[3]="option 3";
- opts[4]="option 4";
- opts[5]="option 5";
- opts[6]="option 6";
- opts[7]="option 7";
- opts[8]="option 8";
- opts[9]="option 9";
- opts[10]="option 10";
- opts[11]="option 11";
- opts[12]="option 12";
- opts[13]="option 13";
- opts[14]="option 14";
- vector<bool> del(15,false);
- unsigned long ans;
- if(basicMenu("Pick and option", opts, del, cout, comm, ans, 7))
- cout << "You selected " << ans << endl;
- else
- cout << "You exited" << endl;
- return mainMenu(comm, cout);
- */
- // delete data
- // delete bMtxType;
- // delete aMtxType;
- // delete varA;
- // delete varB;
- // delete varPivot;
- // open the prototype file
- // pOut.open(argv[1],fstream::out);
- // if(!pOut.is_open())
- // throw eMsg("Failed to open the prototype");
- //
- // // open the derived file
- // dOut.open(argv[2],fstream::out);
- // if(!dOut.is_open())
- // {
- // pOut.close();
- // throw eMsg("Failed to open the derived");
- // }
- //
- // // test to see if there are some existing commands
- // {
- // stat sb;
- // if(stat(argv[3],&sb)==0)
- // {
- // cOut.open(argv[3],fstream::in);
- // if(!dOut.is_open())
- // {
- // pOut.close();
- // throw eMsg("Failed to open the derived");
- // }
- //
- // }
- // }
- return 0;
- }