Changeset 1268 in Sophya for trunk/SophyaPI/PIext/cxxexecutor.cc
- Timestamp:
- Nov 1, 2000, 1:01:28 AM (25 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaPI/PIext/cxxexecutor.cc
r1265 r1268 12 12 /* --Methode-- */ 13 13 CxxExecutor::CxxExecutor(PIACmd *mpiac, PIStdImgApp* /* app */) 14 : mUserCodeFn(""), mCompOpt(""), mLinkOpt(""), mMyLibs("") 14 : mUserCodeFn(""), mUserFctFn(""), mCompOpt(""), mLinkOpt(""), mMyLibs("") 15 , mDefTmp(""), mDefRoot("cxx_spiapp"), mDefFunc("usercxx") 15 16 { 16 17 mIncList.resize(0); 17 18 mCallArgs.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; 18 25 19 26 // On enregistre les nouvelles commandes … … 24 31 usage = "c++exec: Execute the following c++ user code\n"; 25 32 usage+= "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\"";33 usage+= "Warning: c++ user code can be found in \"TmpDir/"+mDefRoot+".h\"\n"; 34 usage+= " total generated code can be found in \"TmpDir/"+mDefRoot+".cc\""; 28 35 mpiac->RegisterCommand(kw, usage, this, hgrp); 29 36 30 37 kw = "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\"";38 usage = "c++execfrf: Execute c++ user_code [user_function_code]\n"; 39 usage+= "Usage: c++execfrf fileuser.cc [fileuserfct.cc]\n"; 40 usage+= "Warning: total generated code can be found in \"TmpDir/"+mDefRoot+".cc\""; 34 41 mpiac->RegisterCommand(kw, usage, this, hgrp); 35 42 … … 48 55 kw = "c++createfrf"; 49 56 usage = "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"; 57 usage+= " with a user file code \"fileuser.cc\"\n"; 58 usage+= " and an optional user function code \"fileuserfct.cc\"\n"; 59 usage+= "Usage: c++createfrf file.cc func fileuser.cc [fileuserfct.cc]\n"; 52 60 mpiac->RegisterCommand(kw, usage, this, hgrp); 53 61 … … 105 113 106 114 /* --Methode-- */ 107 int CxxExecutor::Execute(string& kw, vector<string>& tokens )115 int CxxExecutor::Execute(string& kw, vector<string>& tokens, string& toks) 108 116 { 109 117 int rc=0; 110 if(kw == "c++exec" || kw == "c++execfrf") {118 if(kw == "c++exec") { 111 119 if(tokens.size()<1) { 112 120 cout<<"Usage: c++exec c++ user code"<<endl; 113 cout<<"Usage: c++execfrf fileuser.cc"<<endl;114 121 return(1); 115 122 } 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]); 118 136 if(rc) return(1); 119 137 rc = CrFile(); if(rc) return(1); … … 132 150 return(1); 133 151 } 134 rc = FillUserCode(tok ens,2); if(rc) return(1);152 rc = FillUserCode(toks,2); if(rc) return(1); 135 153 rc = CrFile(tokens[0],tokens[1]); if(rc) return(1); 136 154 137 155 } else if(kw == "c++createfrf") { 138 156 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; 140 158 return(1); 141 159 } 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); 143 163 rc = CrFile(tokens[0],tokens[1]); if(rc) return(1); 144 164 … … 173 193 {cout<<"c++mylibs "<<GetLinkLibs()<<endl; return(0);} 174 194 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 } 201 196 202 197 return(0); … … 204 199 205 200 /* --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-- */230 201 int CxxExecutor::CrFile(string cfilename,string func) 231 202 { 203 if(cfilename.size()<1) cfilename = mDefTmp + mDefRoot + ".cc"; 204 if(func.size()<1) func = mDefFunc; 205 232 206 ofstream os(cfilename.c_str(),ios::out); 233 207 if(!os) … … 240 214 PutIncludeUser(os); 241 215 os<<endl; 216 217 os<<"//-------------------------------------------------//"<<endl; 218 os<<"//------------- Fonctions utilisateur -------------//"<<endl; 219 os<<"//-------------------------------------------------//"<<endl; 220 if(mUserFctFn.size()>0) os<<"#include \""<<mUserFctFn<<"\""<<endl; 221 os<<endl; 222 242 223 243 224 os<<"extern \"C\" {"<<endl; … … 378 359 379 360 /* --Methode-- */ 380 int CxxExecutor::FillUserCode( vector<string>& usercode,uint_4 first)381 // - first is the first position in the vector<>where the code starts382 // User code is read from input. It is put into file " cxx_spiapp.h".361 int 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". 383 364 { 384 365 mUserCodeFn = ""; 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"; 366 mUserFctFn = ""; 367 368 // get the string part which is after word "first" 369 string code = usercode; 370 if(code.size()<=0) {cout<<"CxxExecutor::FillUserCode: no user code"<<endl; 371 return 1;} 372 size_t q; 373 for(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 } 382 if(code.size()<=0) 383 {cout<<"CxxExecutor::FillUserCode: no user code after "<<first<<endl; 384 return 1;} 385 386 // Fill the file with user code 387 mUserCodeFn = mDefTmp + mDefRoot + ".h"; 389 388 ofstream os(mUserCodeFn.c_str(),ios::out); 390 389 if(!os) {cout<<"CxxExecutor::FillUserCode: unable to open " 391 390 <<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; 391 os<<" "<<code; 395 392 cout<<"User code filled from standard input into "<<mUserCodeFn<<endl; 396 393 return 0; … … 398 395 399 396 /* --Methode-- */ 400 int CxxExecutor::FillUserCode(string filename )401 // User code is read from "filename" .397 int CxxExecutor::FillUserCode(string filename,string filefctname) 398 // User code is read from "filename" an optionally from filefctname. 402 399 { 403 400 mUserCodeFn = filename; 401 mUserFctFn = filefctname; 404 402 cout<<"User code filled from file "<<filename<<endl; 405 403 return 0; … … 409 407 int CxxExecutor::Compile(string rootfilename) 410 408 { 411 string fc = rootfilename + ".cc"; 412 string fl = rootfilename + ".so"; 409 if(rootfilename.size()<1) rootfilename = mDefRoot; 413 410 cout<<"Compile: "<<rootfilename<<endl; 414 411 int rc = 0; 415 412 rc = CrMakefile(); 416 413 if(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; 414 string make = ""; 415 make += "make -f " + mDefRoot + "_Makefile"; 416 make += " CXXFLAGS=\"" + mCompOpt + "\""; 417 make += " LDFLAGS=\"" + mLinkOpt + "\""; 418 make += " MYLIBS=\"" + mMyLibs + "\""; 419 make += " " + rootfilename; 422 420 rc = system(make.c_str()); 423 421 if(rc) … … 430 428 int CxxExecutor::CrMakefile(void) 431 429 { 432 ofstream os("cxx_spiapp_Makefile",ios::out); 430 string makename = mDefTmp + mDefRoot + "_Makefile"; 431 ofstream os(makename.c_str(),ios::out); 433 432 if(!os) 434 433 {cout<<"CxxExecutor::CrMakefile: unable to open file for Makefile"<<endl; … … 459 458 os<<"\t"<<"$(LINK.cc) -shared -o $@ $< $(LIBS) $(MYLIBS)"<<endl; 460 459 os<<"%.o:%.cc"<<endl; 461 os<<"\t"<<"$(COMPILE.cc) 460 os<<"\t"<<"$(COMPILE.cc) -o $@ $<"<<endl; 462 461 os<<"%.o:%.c"<<endl; 463 os<<"\t"<<"$(COMPILE.c) -c $(CFLAGS) 462 os<<"\t"<<"$(COMPILE.c) -c $(CFLAGS) $(USERFLAGS) -o $@ $<"<<endl; 464 463 //--------------------------------------------------------------------- 465 464 return 0; … … 469 468 int CxxExecutor::Link(string libname,string func) 470 469 { 470 if(libname.size()<1) libname = mDefTmp + mDefRoot + ".so"; 471 if(func.size()<1) func = mDefFunc; 472 471 473 NamedObjMgr omg; 472 474 PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter(); 473 475 string key("link"); 474 476 vector<string> arg; arg.push_back(libname); arg.push_back(func); 475 int rc = mpiac->ExecuteCommand(key,arg); 477 string toks = libname + " " + func; 478 int rc = mpiac->ExecuteCommand(key,arg,toks); 476 479 cout<<"Link from "<<libname<<" for function "<<func 477 480 <<" (rc="<<rc<<")"<<endl; … … 482 485 int CxxExecutor::Call(string func) 483 486 { 487 if(func.size()<1) func = mDefFunc; 488 484 489 NamedObjMgr omg; 485 490 PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter(); 486 491 string key("call"); 487 492 vector<string> arg; arg.push_back(func); 493 string toks = func; 488 494 if(mCallArgs.size()>0) 489 495 for(uint_4 i=0;i<mCallArgs.size();i++) arg.push_back(mCallArgs[i]); 490 mpiac->ExecuteCommand(key,arg );496 mpiac->ExecuteCommand(key,arg,toks); 491 497 return 0; 492 498 } … … 589 595 return mMyLibs; 590 596 } 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 caracteres611 // contenant des blancs, on ne casse les lignes qu'apres un ";" a condition612 // qu'il ne soit pas dans une chaine de caracteres c'est a dire613 // entoure de "....;....".614 // - Attention aux " dans les chaines de caracteres: "...\"..."615 // - Bugs non-gere pour les tortures: ".....\\" suivi par une autre616 // 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 gerer623 // si il y a plusieurs blancs!): "salut ca va" -> "salut ca va"624 // **** Conclusion ****625 // Pour ecrire du code sophistique, le faire dans un fichier626 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 blanc634 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 charactere644 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.