Changeset 1268 in Sophya for trunk/SophyaPI/PIext/cxxexecutor.cc


Ignore:
Timestamp:
Nov 1, 2000, 1:01:28 AM (25 years ago)
Author:
ercodmgr
Message:

3ieme argument string& toks pour Execute et ExecuteCommand
preparation gestion TmpDir dans cxxexecutor
suppression de ExecuteCXX ds cxxexecutor et appels
possibilite d'ajouter des userfct.cc ds cxxexecutor

cmv 1/11/00

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/SophyaPI/PIext/cxxexecutor.cc

    r1265 r1268  
    1212/* --Methode-- */
    1313CxxExecutor::CxxExecutor(PIACmd *mpiac, PIStdImgApp* /* app */)
    14   : mUserCodeFn(""), mCompOpt(""), mLinkOpt(""), mMyLibs("")
     14  : mUserCodeFn(""), mUserFctFn(""), mCompOpt(""), mLinkOpt(""), mMyLibs("")
     15  , mDefTmp(""), mDefRoot("cxx_spiapp"), mDefFunc("usercxx")
    1516{
    1617mIncList.resize(0);
    1718mCallArgs.resize(0);
     19
     20// Gestion des fichiers par default dans TmpDir
     21//.... CMV a gerer mais pas forcement OK ... voir avec Rz
     22//NamedObjMgr omg;
     23//string tmpdir = omg.GetTmpDir();
     24//if(tmpdir.size()>1) mDefTmp = tmpdir;
    1825
    1926// On enregistre les nouvelles commandes
     
    2431usage = "c++exec: Execute the following c++ user code\n";
    2532usage+= "Usage: c++exec c++ user code\n";
    26 usage+= "Warning: c++ user code can be found in \"cxx_spiapp.h\"\n";
    27 usage+= "         total generated code can be found in \"cxx_spiapp.cc\"";
     33usage+= "Warning: c++ user code can be found in \"TmpDir/"+mDefRoot+".h\"\n";
     34usage+= "         total generated code can be found in \"TmpDir/"+mDefRoot+".cc\"";
    2835mpiac->RegisterCommand(kw, usage, this, hgrp);
    2936
    3037kw = "c++execfrf";
    31 usage = "c++execfrf: Execute c++ user code contained in a file\n";
    32 usage+= "Usage: c++execfrf fileuser.cc\n";
    33 usage+= "Warning: total generated code can be found in \"cxx_spiapp.cc\"";
     38usage = "c++execfrf: Execute c++ user_code [user_function_code]\n";
     39usage+= "Usage: c++execfrf fileuser.cc [fileuserfct.cc]\n";
     40usage+= "Warning: total generated code can be found in \"TmpDir/"+mDefRoot+".cc\"";
    3441mpiac->RegisterCommand(kw, usage, this, hgrp);
    3542
     
    4855kw = "c++createfrf";
    4956usage = "c++createfrf: create a file \"file.cc\"to be used by spiapp\n";
    50 usage+= "              with a user file \"fileuser.cc\"\n";
    51 usage+= "Usage: c++createfrf file.cc func fileuser.cc\n";
     57usage+= "              with a user file code \"fileuser.cc\"\n";
     58usage+= "             and an optional user function code \"fileuserfct.cc\"\n";
     59usage+= "Usage: c++createfrf file.cc func fileuser.cc [fileuserfct.cc]\n";
    5260mpiac->RegisterCommand(kw, usage, this, hgrp);
    5361
     
    105113
    106114/* --Methode-- */
    107 int CxxExecutor::Execute(string& kw, vector<string>& tokens)
     115int CxxExecutor::Execute(string& kw, vector<string>& tokens, string& toks)
    108116{
    109117int rc=0;
    110 if(kw == "c++exec" || kw == "c++execfrf") {
     118if(kw == "c++exec") {
    111119  if(tokens.size()<1) {
    112120    cout<<"Usage: c++exec c++ user code"<<endl;
    113     cout<<"Usage: c++execfrf fileuser.cc"<<endl;
    114121    return(1);
    115122  }
    116   if(kw == "c++exec")    rc = FillUserCode(tokens,0);
    117   if(kw == "c++execfrf") rc = FillUserCode(tokens[0]);
     123  rc = FillUserCode(toks,0); if(rc) return(1);
     124  rc = CrFile();   if(rc) return(1);
     125  rc = Compile();  if(rc) return(1);
     126  rc = Link();     if(rc) return(1);
     127  rc = Call();     if(rc) return(1);
     128
     129} else if(kw == "c++execfrf") {
     130  if(tokens.size()<1) {
     131    cout<<"Usage: c++execfrf fileuser.cc [fileuserfct.cc]"<<endl;
     132    return(1);
     133  }
     134  if(tokens.size()>1) rc = FillUserCode(tokens[0],tokens[1]);
     135    else              rc = FillUserCode(tokens[0]);
    118136  if(rc) return(1);
    119137  rc = CrFile();   if(rc) return(1);
     
    132150    return(1);
    133151  }
    134   rc = FillUserCode(tokens,2);       if(rc) return(1);
     152  rc = FillUserCode(toks,2);       if(rc) return(1);
    135153  rc = CrFile(tokens[0],tokens[1]);  if(rc) return(1);
    136154
    137155} else if(kw == "c++createfrf") {
    138156  if(tokens.size()<3) {
    139     cout<<"Usage: c++createfrf file.cc func fileuser.cc"<<endl;
     157    cout<<"Usage: c++createfrf file.cc func fileuser.cc [fileuserfct.cc]"<<endl;
    140158    return(1);
    141159  }
    142   rc = FillUserCode(tokens[2]);     if(rc) return(1);
     160  if(tokens.size()>3) rc = FillUserCode(tokens[2],tokens[3]);
     161    else              rc = FillUserCode(tokens[2]);
     162  if(rc) return(1);
    143163  rc = CrFile(tokens[0],tokens[1]); if(rc) return(1);
    144164
     
    173193    {cout<<"c++mylibs "<<GetLinkLibs()<<endl; return(0);}
    174194  FillLinkLibs(tokens);
    175 
    176 
    177 //// A VIRER quand variable de objmanager OK  --> Pour Reza
    178 } else if(kw == "c++setvar") {
    179   if(tokens.size()<2) {
    180     cout<<" Usage: c++setvar varname varcontent "<<endl;
    181     return(1);
    182   }
    183   string varcont = tokens[1];
    184   if(tokens.size()>2)
    185     for(uint_4 i=2;i<tokens.size();i++) varcont += " " + tokens[i] ;
    186   NamedObjMgr omg;
    187   omg.SetVar(tokens[0], varcont);
    188 } else if(kw == "c++getvar") {
    189   if(tokens.size()<1) {
    190     cout<<" Usage: c++getvar varname "<<endl;
    191     return(1);
    192   }
    193   NamedObjMgr omg;
    194   cout<<"c++getvar("<<tokens[0]<<")="<<omg.GetVar(tokens[0])<<endl;
    195 }  else if(kw == "c++varlist") {
    196   NamedObjMgr omg;
    197   cout<<omg.GetVarList();
    198 }
    199 //// A VIRER quand variable de objmanager OK  --> Pour Reza
    200 
     195}
    201196
    202197return(0);
     
    204199
    205200/* --Methode-- */
    206 int CxxExecutor::ExecuteCxx(string const & code)
    207 {
    208   int rc;
    209 
    210   mUserCodeFn = "";
    211   mUserCodeFn = "cxx_spiapp.h";
    212   ofstream os(mUserCodeFn.c_str(),ios::out);
    213   if(!os) {cout<<"CxxExecutor::ExecuteCxx(): unable to open "
    214                <<mUserCodeFn<<endl;  mUserCodeFn = ""; return 1;}
    215  
    216   os << code ;
    217   os << endl ;
    218 
    219   cout<<"CxxExecutor: User code filled from standard input into "
    220       <<mUserCodeFn<<endl;
    221  
    222   rc = CrFile();   if(rc) return(1);
    223   rc = Compile();  if(rc) return(1);
    224   rc = Link();     if(rc) return(1);
    225   rc = Call();     if(rc) return(1);
    226   return(rc);
    227 }
    228 
    229 /* --Methode-- */
    230201int CxxExecutor::CrFile(string cfilename,string func)
    231202{
     203if(cfilename.size()<1) cfilename = mDefTmp + mDefRoot + ".cc";
     204if(func.size()<1) func = mDefFunc;
     205
    232206ofstream os(cfilename.c_str(),ios::out);
    233207if(!os)
     
    240214PutIncludeUser(os);
    241215os<<endl;
     216
     217os<<"//-------------------------------------------------//"<<endl;
     218os<<"//------------- Fonctions utilisateur -------------//"<<endl;
     219os<<"//-------------------------------------------------//"<<endl;
     220if(mUserFctFn.size()>0) os<<"#include \""<<mUserFctFn<<"\""<<endl;
     221os<<endl;
     222
    242223
    243224os<<"extern \"C\" {"<<endl;
     
    378359
    379360/* --Methode-- */
    380 int CxxExecutor::FillUserCode(vector<string>& usercode,uint_4 first)
    381 // - first is the first position in the vector<> where the code starts
    382 // User code is read from input. It is put into file "cxx_spiapp.h".
     361int CxxExecutor::FillUserCode(string& usercode,uint_4 first)
     362// - first is the first position in the "string" where the code starts
     363// User code is read from input. It is put into file "TmpDir/cxx_spiapp.h".
    383364{
    384365mUserCodeFn = "";
    385 uint_4 nus = usercode.size();
    386 if(nus<=first) {cout<<"CxxExecutor::FillUserCode: no user code"<<endl;
    387                 return 1;}
    388 mUserCodeFn = "cxx_spiapp.h";
     366mUserFctFn  = "";
     367
     368// get the string part which is after word "first"
     369string code = usercode;
     370if(code.size()<=0) {cout<<"CxxExecutor::FillUserCode: no user code"<<endl;
     371                    return 1;}
     372size_t q;
     373for(uint_4 i=0;i<=first;i++) {
     374  q = code.find_first_not_of(" \t");
     375  if(q>=code.size()) {code=""; break;}
     376  code = code.substr(q);
     377  if(i==first) break;
     378  q = code.find_first_of(" \t");
     379  if(q>=code.size()) {code=""; break;}
     380  code = code.substr(q);
     381}
     382if(code.size()<=0)
     383  {cout<<"CxxExecutor::FillUserCode: no user code after "<<first<<endl;
     384   return 1;}
     385
     386// Fill the file with user code
     387mUserCodeFn = mDefTmp + mDefRoot + ".h";
    389388ofstream os(mUserCodeFn.c_str(),ios::out);
    390389if(!os) {cout<<"CxxExecutor::FillUserCode: unable to open "
    391390             <<mUserCodeFn<<endl;  mUserCodeFn = ""; return 1;}
    392 // On ajoute un blanc pour les chaines de caracteres contenant des blancs
    393 for(uint_4 i=first;i<nus;i++) os<<" "<<usercode[i];
    394 os<<endl;
     391os<<" "<<code;
    395392cout<<"User code filled from standard input into "<<mUserCodeFn<<endl;
    396393return 0;
     
    398395
    399396/* --Methode-- */
    400 int CxxExecutor::FillUserCode(string filename)
    401 // User code is read from "filename".
     397int CxxExecutor::FillUserCode(string filename,string filefctname)
     398// User code is read from "filename" an optionally from filefctname.
    402399{
    403400mUserCodeFn = filename;
     401mUserFctFn  = filefctname;
    404402cout<<"User code filled from file "<<filename<<endl;
    405403return 0;
     
    409407int CxxExecutor::Compile(string rootfilename)
    410408{
    411 string fc = rootfilename + ".cc";
    412 string fl = rootfilename + ".so";
     409if(rootfilename.size()<1) rootfilename = mDefRoot;
    413410cout<<"Compile: "<<rootfilename<<endl;
    414411int rc = 0;
    415412rc = CrMakefile();
    416413if(rc) return(1);
    417 string make  = "make -f cxx_spiapp_Makefile";
    418        make += " CXXFLAGS=\"" + mCompOpt + "\"";
    419        make += " LDFLAGS=\""  + mLinkOpt + "\"";
    420        make += " MYLIBS=\""   + mMyLibs  + "\"";
    421        make += " " + rootfilename;
     414string make  = "";
     415make += "make -f " + mDefRoot + "_Makefile";
     416make += " CXXFLAGS=\"" + mCompOpt + "\"";
     417make += " LDFLAGS=\""  + mLinkOpt + "\"";
     418make += " MYLIBS=\""   + mMyLibs  + "\"";
     419make += " " + rootfilename;
    422420rc = system(make.c_str());
    423421if(rc)
     
    430428int CxxExecutor::CrMakefile(void)
    431429{
    432 ofstream os("cxx_spiapp_Makefile",ios::out);
     430string makename = mDefTmp + mDefRoot + "_Makefile";
     431ofstream os(makename.c_str(),ios::out);
    433432if(!os)
    434433  {cout<<"CxxExecutor::CrMakefile: unable to open file for Makefile"<<endl;
     
    459458os<<"\t"<<"$(LINK.cc) -shared -o $@ $< $(LIBS) $(MYLIBS)"<<endl;
    460459os<<"%.o:%.cc"<<endl;
    461 os<<"\t"<<"$(COMPILE.cc)  -o $@ $<"<<endl;
     460os<<"\t"<<"$(COMPILE.cc) -o $@ $<"<<endl;
    462461os<<"%.o:%.c"<<endl;
    463 os<<"\t"<<"$(COMPILE.c) -c $(CFLAGS)   $(USERFLAGS) -o $@ $<"<<endl;
     462os<<"\t"<<"$(COMPILE.c) -c $(CFLAGS) $(USERFLAGS) -o $@ $<"<<endl;
    464463//---------------------------------------------------------------------
    465464return 0;
     
    469468int CxxExecutor::Link(string libname,string func)
    470469{
     470if(libname.size()<1) libname = mDefTmp + mDefRoot + ".so";
     471if(func.size()<1) func = mDefFunc;
     472
    471473NamedObjMgr omg;
    472474PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter();
    473475string key("link");
    474476vector<string> arg; arg.push_back(libname); arg.push_back(func);
    475 int rc = mpiac->ExecuteCommand(key,arg);
     477string toks = libname + " " + func;
     478int rc = mpiac->ExecuteCommand(key,arg,toks);
    476479cout<<"Link from "<<libname<<" for function "<<func
    477480    <<" (rc="<<rc<<")"<<endl;
     
    482485int CxxExecutor::Call(string func)
    483486{
     487if(func.size()<1) func = mDefFunc;
     488
    484489NamedObjMgr omg;
    485490PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter();
    486491string key("call");
    487492vector<string> arg; arg.push_back(func);
     493string toks = func;
    488494if(mCallArgs.size()>0)
    489495  for(uint_4 i=0;i<mCallArgs.size();i++) arg.push_back(mCallArgs[i]);
    490 mpiac->ExecuteCommand(key,arg);
     496mpiac->ExecuteCommand(key,arg,toks);
    491497return 0;
    492498}
     
    589595return mMyLibs;
    590596}
    591 
    592 
    593 
    594 
    595 /* --Methode--  DO NOT DELETE.... cmv property !!!
    596 int CxxExecutor::FillUserCode(vector<string>& usercode,uint_4 first)
    597 {
    598 mUserCodeFn = "";
    599 uint_4 nus = usercode.size();
    600 if(nus<=first) {
    601   cout<<"CxxExecutor::FillUserCode: no user code"<<endl;
    602   return 1;
    603 }
    604 mUserCodeFn = "cxx_spiapp.h";
    605 ofstream os(mUserCodeFn.c_str(),ios::out);
    606 if(!os)
    607   {cout<<"CxxExecutor::FillUserCode: unable to open "<<mUserCodeFn<<endl;
    608    mUserCodeFn = ""; return 1;}
    609 // **** 1er probleme ****
    610 // - Pour la lisibilite et eviter les bugs avec les chaines de caracteres
    611 // contenant des blancs, on ne casse les lignes qu'apres un ";" a condition
    612 // qu'il ne soit pas dans une chaine de caracteres c'est a dire
    613 // entoure de "....;....".
    614 // - Attention aux " dans les chaines de caracteres:  "...\"..."
    615 // - Bugs non-gere pour les tortures: ".....\\" suivi par une autre
    616 //   chaine de caracteres qui contient un ';'
    617 //   ex: cout<<"aaa\\"; cout<<"blabla;blabla";
    618 // **** 2ieme probleme ****
    619 // - Le decodeur de ligne va couper les commentaires qui contiennent des blancs:
    620 // "salut ca va" --> tokens[0]="salut tokens[1]=ca tokens[2]=va" --> "salutcava"
    621 // - On contourne partiellement le pb en ajoutant un blanc en debut des tokens[]
    622 // identifies comme etant dans une chaine de caracteres (mais impossible de gerer
    623 // si il y a plusieurs blancs!): "salut     ca    va" -> "salut ca va"
    624 // **** Conclusion ****
    625 // Pour ecrire du code sophistique, le faire dans un fichier
    626 string dum = "";
    627 bool lastchar = false;
    628 bool comment = false;
    629 for(uint_4 i=first;i<nus;i++) {
    630   const char* str = usercode[i].c_str();
    631   size_t lstr= strlen(str);
    632   for(uint_4 j=0;j<lstr;j++) {
    633     // debut de mot dans un commentaire -> ajouter un blanc
    634     if(j==0 && comment) dum += " ";
    635     // on arrive sur un " : debut ou fin commentaire? inactif (\")?
    636     if(str[j]=='"') {
    637       if(j==0) comment = (comment) ? false: true;
    638       else {if(str[j-1]!='\\') comment = (comment) ? false: true;}
    639     }
    640     dum += str[j];
    641     if( i==nus-1 && j==lstr-1 ) lastchar = true;
    642     // On charge la string "dum" si : 1-/ on a un ";"
    643     //                                2-/ c'est le dernier charactere
    644     if((str[j]==';' && !comment) || lastchar)
    645       {os<<dum<<endl; dum = "";}
    646   }
    647 }
    648 cout<<"User code filled from standard input into "<<mUserCodeFn<<endl;
    649 return 0;
    650 }
    651 */
Note: See TracChangeset for help on using the changeset viewer.