| 1 | #include "cxxexecutor.h" | 
|---|
| 2 |  | 
|---|
| 3 | #include <typeinfo> | 
|---|
| 4 |  | 
|---|
| 5 | #include "dvlist.h" | 
|---|
| 6 |  | 
|---|
| 7 | #include "nomgadapter.h" | 
|---|
| 8 | #include "pistdimgapp.h" | 
|---|
| 9 |  | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 | /* --Methode-- */ | 
|---|
| 13 | CxxExecutor::CxxExecutor(PIACmd *mpiac, PIStdImgApp* /* app */) | 
|---|
| 14 | : mCompOpt(""), mLinkOpt(""), mMyLibs("") | 
|---|
| 15 | { | 
|---|
| 16 | mUserCode.resize(0); | 
|---|
| 17 | mIncList.resize(0); | 
|---|
| 18 | mCallArgs.resize(0); | 
|---|
| 19 |  | 
|---|
| 20 | NamedObjMgr omg; | 
|---|
| 21 |  | 
|---|
| 22 | // On enregistre les nouvelles commandes | 
|---|
| 23 | string hgrp = "CxxExecutorCmd"; | 
|---|
| 24 | string usage,kw; | 
|---|
| 25 |  | 
|---|
| 26 | kw = "c++exec"; | 
|---|
| 27 | usage = "c++exec: Execute the following c++ user code\n"; | 
|---|
| 28 | usage+= "Usage: c++exec c++ user code"; | 
|---|
| 29 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 30 |  | 
|---|
| 31 | kw = "c++execfrf"; | 
|---|
| 32 | usage = "c++execfrf: Execute c++ user code contained in a file\n"; | 
|---|
| 33 | usage+= "Usage: c++execfrf fileuser.cc"; | 
|---|
| 34 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 35 |  | 
|---|
| 36 | kw = "c++args"; | 
|---|
| 37 | usage = "c++args: Define user function arguments\n"; | 
|---|
| 38 | usage+= "Usage: c++args arg1 arg2 arg3 ..."; | 
|---|
| 39 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 40 |  | 
|---|
| 41 | kw = "c++create"; | 
|---|
| 42 | usage = "c++create: create a file to be used by spiapp\n"; | 
|---|
| 43 | usage+= "Usage: c++create file.cc func c++ user code...\n"; | 
|---|
| 44 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 45 |  | 
|---|
| 46 | kw = "c++createfrf"; | 
|---|
| 47 | usage = "c++createfrf: create a file \"file.cc\"to be used by spiapp\n"; | 
|---|
| 48 | usage+= "              with a user file \"fileuser.cc\"\n"; | 
|---|
| 49 | usage+= "Usage: c++createfrf file.cc func fileuser.cc\n"; | 
|---|
| 50 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 51 |  | 
|---|
| 52 | kw = "c++compile"; | 
|---|
| 53 | usage = "c++compile: compile a file (file.cc -> file.so)\n"; | 
|---|
| 54 | usage+= "Usage: c++compile file\n"; | 
|---|
| 55 | usage+= "Warning: give \"file\" or \"file.so\" to create \"file.so\" from \"file.cc\"\n"; | 
|---|
| 56 | usage+= "       : to be used before c++link"; | 
|---|
| 57 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 58 |  | 
|---|
| 59 | kw = "c++link"; | 
|---|
| 60 | usage = "c++link: link function \"func\" in file.so to spiapp\n"; | 
|---|
| 61 | usage+= "Usage: c++link file.so func"; | 
|---|
| 62 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 63 |  | 
|---|
| 64 | kw = "c++include"; | 
|---|
| 65 | usage = "c++include: give personnal includes to be used\n"; | 
|---|
| 66 | usage+= "Usage: c++include myinc1.h myinc2.h ...\n"; | 
|---|
| 67 | usage+= "Warning: to be used before c++create... c++exec..."; | 
|---|
| 68 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 69 |  | 
|---|
| 70 | kw = "c++compileopt"; | 
|---|
| 71 | usage = "c++compileopt: give additionnal compile options\n"; | 
|---|
| 72 | usage+= "Usage: c++compileopt -g -O5 -IMy_Inc_Dir ...\n"; | 
|---|
| 73 | usage+= "Warning: to be used before c++compile"; | 
|---|
| 74 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 75 |  | 
|---|
| 76 | kw = "c++linkopt"; | 
|---|
| 77 | usage = "c++linkopt: give additionnal link options\n"; | 
|---|
| 78 | usage+= "Usage: c++linkopt -g -O5 ..."; | 
|---|
| 79 | usage+= "Warning: to be used before c++compile"; | 
|---|
| 80 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 81 |  | 
|---|
| 82 | kw = "c++mylibs"; | 
|---|
| 83 | usage = "c++mylibs: give additionnal libraries\n"; | 
|---|
| 84 | usage+= "Usage: c++mylibs -LMy_Lib_Dir -lmylib1 -lmylib2 ..."; | 
|---|
| 85 | usage+= "Warning: to be used before c++compile"; | 
|---|
| 86 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 87 |  | 
|---|
| 88 | kw = "c++setvar"; | 
|---|
| 89 | usage = "c++setvar: Setting test variable \n"; | 
|---|
| 90 | usage+= "c++setvar varname varcontent "; | 
|---|
| 91 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 92 |  | 
|---|
| 93 | kw = "c++getvar"; | 
|---|
| 94 | usage = "c++getvar: Getting test variable content\n"; | 
|---|
| 95 | usage+= "c++getvar varname "; | 
|---|
| 96 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 97 |  | 
|---|
| 98 | kw = "c++varlist"; | 
|---|
| 99 | usage = "c++getvar: Printing test variable list\n"; | 
|---|
| 100 | usage+= "c++varlist  "; | 
|---|
| 101 | mpiac->RegisterCommand(kw, usage, this, hgrp); | 
|---|
| 102 |  | 
|---|
| 103 | } | 
|---|
| 104 |  | 
|---|
| 105 | /* --Methode-- */ | 
|---|
| 106 | CxxExecutor::~CxxExecutor() | 
|---|
| 107 | { | 
|---|
| 108 | } | 
|---|
| 109 |  | 
|---|
| 110 | /* --Methode-- */ | 
|---|
| 111 | int CxxExecutor::Execute(string& kw, vector<string>& tokens) | 
|---|
| 112 | { | 
|---|
| 113 | int rc=0; | 
|---|
| 114 | if(kw == "c++exec" || kw == "c++execfrf") { | 
|---|
| 115 | if(tokens.size()<1) { | 
|---|
| 116 | cout<<"Usage: c++exec c++ user code"<<endl; | 
|---|
| 117 | cout<<"Usage: c++execfrf fileuser.cc"<<endl; | 
|---|
| 118 | return(1); | 
|---|
| 119 | } | 
|---|
| 120 | if(kw == "c++exec")    rc = FillUserCode(tokens,0); | 
|---|
| 121 | if(kw == "c++execfrf") rc = FillUserCode(tokens[0]); | 
|---|
| 122 | if(rc) return(1); | 
|---|
| 123 | rc = CrFile();   if(rc) return(1); | 
|---|
| 124 | rc = Compile();  if(rc) return(1); | 
|---|
| 125 | rc = Link();     if(rc) return(1); | 
|---|
| 126 | rc = Call();     if(rc) return(1); | 
|---|
| 127 |  | 
|---|
| 128 | } else if(kw == "c++args") { | 
|---|
| 129 | mCallArgs.resize(0); | 
|---|
| 130 | if(tokens.size()>=1) | 
|---|
| 131 | for(uint_4 i=0;i<tokens.size();i++) mCallArgs.push_back(tokens[i]); | 
|---|
| 132 |  | 
|---|
| 133 | } else if(kw == "c++create") { | 
|---|
| 134 | if(tokens.size()<3) { | 
|---|
| 135 | cout<<"Usage: c++create file.cc func c++ user code ..."<<endl; | 
|---|
| 136 | return(1); | 
|---|
| 137 | } | 
|---|
| 138 | rc = FillUserCode(tokens,2);       if(rc) return(1); | 
|---|
| 139 | rc = CrFile(tokens[0],tokens[1]);  if(rc) return(1); | 
|---|
| 140 |  | 
|---|
| 141 | } else if(kw == "c++createfrf") { | 
|---|
| 142 | if(tokens.size()<3) { | 
|---|
| 143 | cout<<"Usage: c++createfrf file.cc func fileuser.cc"<<endl; | 
|---|
| 144 | return(1); | 
|---|
| 145 | } | 
|---|
| 146 | rc = FillUserCode(tokens[2]);     if(rc) return(1); | 
|---|
| 147 | rc = CrFile(tokens[0],tokens[1]); if(rc) return(1); | 
|---|
| 148 |  | 
|---|
| 149 | } else if(kw == "c++compile") { | 
|---|
| 150 | if(tokens.size()>=1) rc = Compile(tokens[0]); | 
|---|
| 151 | else                 rc = Compile(); | 
|---|
| 152 | if(rc) return(1); | 
|---|
| 153 |  | 
|---|
| 154 | } else if(kw == "c++link") { | 
|---|
| 155 | if(tokens.size()>=2)      rc = Link(tokens[0],tokens[1]); | 
|---|
| 156 | else if(tokens.size()>=1) rc = Link(tokens[0]); | 
|---|
| 157 | else                      rc = Link(); | 
|---|
| 158 | if(rc) return(1); | 
|---|
| 159 |  | 
|---|
| 160 | } else if(kw == "c++include") { | 
|---|
| 161 | mIncList.resize(0); | 
|---|
| 162 | if(tokens.size()>=1) | 
|---|
| 163 | for(uint_4 i=0;i<tokens.size();i++) mIncList.push_back(tokens[i]); | 
|---|
| 164 |  | 
|---|
| 165 | } else if(kw == "c++compileopt") { | 
|---|
| 166 | mCompOpt = ""; | 
|---|
| 167 | if(tokens.size()>=1) { | 
|---|
| 168 | for(uint_4 i=0;i<tokens.size();i++) mCompOpt += tokens[i] + " "; | 
|---|
| 169 | } | 
|---|
| 170 |  | 
|---|
| 171 | } else if(kw == "c++linkopt") { | 
|---|
| 172 | mLinkOpt = ""; | 
|---|
| 173 | if(tokens.size()>=1) { | 
|---|
| 174 | for(uint_4 i=0;i<tokens.size();i++) mLinkOpt += tokens[i] + " "; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | } else if(kw == "c++mylibs") { | 
|---|
| 178 | mMyLibs = ""; | 
|---|
| 179 | if(tokens.size()>=1) { | 
|---|
| 180 | for(uint_4 i=0;i<tokens.size();i++) mMyLibs += tokens[i] + " "; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | } else if(kw == "c++setvar") { | 
|---|
| 184 | if(tokens.size()<2) { | 
|---|
| 185 | cerr << " Usage: c++setvar varname varcontent " << endl; | 
|---|
| 186 | return(1); | 
|---|
| 187 | } | 
|---|
| 188 | string varcont = tokens[1]; | 
|---|
| 189 | if(tokens.size()>2) | 
|---|
| 190 | for(uint_4 i=2;i<tokens.size();i++) varcont += " " + tokens[i] ; | 
|---|
| 191 | NamedObjMgr omg; | 
|---|
| 192 | omg.SetVar(tokens[0], varcont); | 
|---|
| 193 | } else if(kw == "c++getvar") { | 
|---|
| 194 | if(tokens.size()<1) { | 
|---|
| 195 | cerr << " Usage: c++getvar varname " << endl; | 
|---|
| 196 | return(1); | 
|---|
| 197 | } | 
|---|
| 198 | NamedObjMgr omg; | 
|---|
| 199 | cout << "c++getvar(" << tokens[0] << ")=" << omg.GetVar(tokens[0]) << endl; | 
|---|
| 200 | }  else if(kw == "c++varlist") { | 
|---|
| 201 | NamedObjMgr omg; | 
|---|
| 202 | cout << omg.GetVarList(); | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 |  | 
|---|
| 206 | return(0); | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | /* --Methode-- */ | 
|---|
| 210 | int CxxExecutor::CrFile(string cfilename,string func) | 
|---|
| 211 | { | 
|---|
| 212 | ofstream os(cfilename.c_str(),ios::out); | 
|---|
| 213 | if(!os) { | 
|---|
| 214 | cout<<"CxxExecutor::CrFile: unable to open "<<cfilename<<endl; | 
|---|
| 215 | return 1; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | PutInclude(os); | 
|---|
| 219 | os<<endl; | 
|---|
| 220 |  | 
|---|
| 221 | PutIncludeUser(os); | 
|---|
| 222 | os<<endl; | 
|---|
| 223 |  | 
|---|
| 224 | os<<"extern \"C\" {"<<endl; | 
|---|
| 225 | os<<"  int "<<func<<"( vector<string>& args );"<<endl; | 
|---|
| 226 | os<<"}"<<endl<<endl; | 
|---|
| 227 | os<<"int "<<func<<"( vector<string>& args )"<<endl; | 
|---|
| 228 | os<<"{"<<endl; | 
|---|
| 229 | os<<"// Some definitions to help using spiapp;"<<endl; | 
|---|
| 230 | os<<"NamedObjMgr omg;"<<endl; | 
|---|
| 231 | os<<"Services2NObjMgr& srvo = *omg.GetServiceObj();"<<endl; | 
|---|
| 232 | os<<"//omg.AddObj(OBJECT* object,string nom_object);"<<endl; | 
|---|
| 233 | os<<endl; | 
|---|
| 234 |  | 
|---|
| 235 | PutObject(os); | 
|---|
| 236 | os<<endl; | 
|---|
| 237 |  | 
|---|
| 238 | PutVar(os); | 
|---|
| 239 | os<<endl; | 
|---|
| 240 |  | 
|---|
| 241 | PutUserCode(os); | 
|---|
| 242 | os<<endl; | 
|---|
| 243 |  | 
|---|
| 244 | os<<endl; | 
|---|
| 245 | os<<"return 0;"<<endl; | 
|---|
| 246 | os<<"}"<<endl; | 
|---|
| 247 |  | 
|---|
| 248 | cout<<"File "<<cfilename<<" for function "<<func<<" created"<<endl; | 
|---|
| 249 | return 0; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 | /* --Methode-- */ | 
|---|
| 253 | void CxxExecutor::PutInclude(ofstream& os) | 
|---|
| 254 | { | 
|---|
| 255 | os<<"#include \"machdefs.h\""<<endl | 
|---|
| 256 | <<endl | 
|---|
| 257 |  | 
|---|
| 258 | <<"//---- System et stdc++ include files"<<endl | 
|---|
| 259 | <<"#include <stdio.h>"<<endl | 
|---|
| 260 | <<"#include <stdlib.h>"<<endl | 
|---|
| 261 | <<"#include <math.h>"<<endl | 
|---|
| 262 | <<"#include <ctype.h>"<<endl | 
|---|
| 263 | <<"#include <string.h>"<<endl | 
|---|
| 264 | <<"#include <iostream.h>"<<endl | 
|---|
| 265 | <<"#include <fstream.h>"<<endl | 
|---|
| 266 | <<"#include <complex>"<<endl | 
|---|
| 267 | <<endl | 
|---|
| 268 |  | 
|---|
| 269 | <<"#include <typeinfo>"<<endl | 
|---|
| 270 | <<"#include <string>"<<endl | 
|---|
| 271 | <<"#include <vector>"<<endl | 
|---|
| 272 | <<"#include <map>"<<endl | 
|---|
| 273 | <<"#include <functional>"<<endl | 
|---|
| 274 | <<"#include <list>"<<endl | 
|---|
| 275 | <<endl | 
|---|
| 276 |  | 
|---|
| 277 | <<"//---- Sophya include files"<<endl | 
|---|
| 278 | <<"#include \"systools.h\""<<endl | 
|---|
| 279 | <<"#include \"ntools.h\""<<endl | 
|---|
| 280 | <<"#include \"array.h\""<<endl | 
|---|
| 281 | <<"#include \"histats.h\""<<endl | 
|---|
| 282 | <<endl | 
|---|
| 283 |  | 
|---|
| 284 | <<"//---- Spiapp include files"<<endl | 
|---|
| 285 | <<"#include \"nobjmgr.h\""<<endl | 
|---|
| 286 | <<"#include \"servnobjm.h\""<<endl | 
|---|
| 287 | <<endl | 
|---|
| 288 |  | 
|---|
| 289 | <<"#define KeepObj(obj) ___nomobj = #obj; omg.AddObj(obj,___nomobj);"<<endl | 
|---|
| 290 | <<"#define KeepVar(var) ___nomobj = #var; omg.GetVarList().Get(___nomobj) = var ;"<<endl | 
|---|
| 291 | <<endl; | 
|---|
| 292 |  | 
|---|
| 293 | return; | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | /* --Methode-- */ | 
|---|
| 297 | void CxxExecutor::PutIncludeUser(ofstream& os) | 
|---|
| 298 | { | 
|---|
| 299 | if(mIncList.size()<1) return; | 
|---|
| 300 | for(uint_4 i=0;i<mIncList.size();i++) | 
|---|
| 301 | os<<"#include \""<<mIncList[i]<<"\""<<endl; | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | /* --Methode-- */ | 
|---|
| 305 | void CxxExecutor::PutObject(ofstream& os) | 
|---|
| 306 | { | 
|---|
| 307 | NamedObjMgr omg; | 
|---|
| 308 | NObjMgrAdapter* objmgrad; | 
|---|
| 309 | vector<string> objlist; | 
|---|
| 310 | string patt = "*"; | 
|---|
| 311 | omg.GetObjList(patt,objlist); | 
|---|
| 312 | int nobjs = objlist.size(); | 
|---|
| 313 |  | 
|---|
| 314 | os<<"//-------------- Object List --------------"<<endl; | 
|---|
| 315 | os<<"//Number of objects = "<<nobjs<<endl; | 
|---|
| 316 | os<<"string ___nomobj;"<<endl<<endl; | 
|---|
| 317 | if(nobjs<=0) return; | 
|---|
| 318 |  | 
|---|
| 319 | string dir,nobj,stmp,obtype; | 
|---|
| 320 | for(int i=0;i<nobjs;i++) { | 
|---|
| 321 | objmgrad = omg.GetObjAdapter(objlist[i]); | 
|---|
| 322 | omg.ParseObjectName(objlist[i],dir,nobj); | 
|---|
| 323 | obtype = objmgrad->GetDataObjType(); | 
|---|
| 324 | stmp = "___" + nobj; | 
|---|
| 325 |  | 
|---|
| 326 | os<<"___nomobj = \""<<nobj<<"\";"<<endl; | 
|---|
| 327 | os<<obtype<<"* "<<stmp | 
|---|
| 328 | <<" = dynamic_cast< "<<obtype<<" * >(omg.GetObj(___nomobj));"<<endl; | 
|---|
| 329 | os<<"if("<<stmp<<"==NULL) throw NullPtrError" | 
|---|
| 330 | <<"(\"CxxExecutor::PutObject: Non existing object "<<nobj | 
|---|
| 331 | <<"... please update file\");"<<endl; | 
|---|
| 332 | os<<obtype<<"& "<<nobj<<" = (*"<<stmp<<");"<<endl<<endl; | 
|---|
| 333 | } | 
|---|
| 334 |  | 
|---|
| 335 | return; | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 | /* --Methode-- */ | 
|---|
| 339 | void CxxExecutor::PutVar(ofstream& os) | 
|---|
| 340 | { | 
|---|
| 341 | os<<"//-------------- Variable List --------------"<<endl; | 
|---|
| 342 | NamedObjMgr omg; | 
|---|
| 343 | DVList& varlist = omg.GetVarList(); | 
|---|
| 344 | // varlist.Show(); varlist.Print(); | 
|---|
| 345 | DVList::ValList::const_iterator it; | 
|---|
| 346 | for(it=varlist.Begin(); it!=varlist.End(); it++) { | 
|---|
| 347 | string key = (*it).first; | 
|---|
| 348 | os<<"___nomobj = \""<<key<<"\";"<<endl; | 
|---|
| 349 | os<<"MuTyV & "<<key<<" = omg.GetVarList().Get(___nomobj);"<<endl; | 
|---|
| 350 | } | 
|---|
| 351 |  | 
|---|
| 352 | } | 
|---|
| 353 |  | 
|---|
| 354 | /* --Methode-- */ | 
|---|
| 355 | void CxxExecutor::PutUserCode(ofstream& os) | 
|---|
| 356 | { | 
|---|
| 357 | os<<"//--------------------------------------------//"<<endl; | 
|---|
| 358 | os<<"//------------- Code utilisateur -------------//"<<endl; | 
|---|
| 359 | os<<"//--------------------------------------------//"<<endl; | 
|---|
| 360 | os<<endl; | 
|---|
| 361 | if(mUserCode.size()<1) return; | 
|---|
| 362 | for(uint_4 i=0;i<mUserCode.size();i++) os<<mUserCode[i]<<endl; | 
|---|
| 363 | } | 
|---|
| 364 |  | 
|---|
| 365 | /* --Methode-- */ | 
|---|
| 366 | int CxxExecutor::FillUserCode(vector<string>& usercode,uint_4 first) | 
|---|
| 367 | // - first is the first position in the vector<> where the code starts | 
|---|
| 368 | { | 
|---|
| 369 | mUserCode.resize(0); | 
|---|
| 370 | uint_4 nus = usercode.size(); | 
|---|
| 371 | if(nus<=first) { | 
|---|
| 372 | cout<<"CxxExecutor::FillUserCode: no user code"<<endl; | 
|---|
| 373 | return 1; | 
|---|
| 374 | } | 
|---|
| 375 | string dum = ""; | 
|---|
| 376 | // On ajoute un blanc pour les chaines de caracteres contenant des blancs | 
|---|
| 377 | for(uint_4 i=first;i<nus;i++) dum += " " + usercode[i]; | 
|---|
| 378 | // Tout dans une seule ligne pour eviter les pbs (cf au bas du fichier) | 
|---|
| 379 | mUserCode.push_back(dum); | 
|---|
| 380 | cout<<"User code filled from standard input"<<endl; | 
|---|
| 381 | return 0; | 
|---|
| 382 | } | 
|---|
| 383 |  | 
|---|
| 384 | /* --Methode-- */ | 
|---|
| 385 | int CxxExecutor::FillUserCode(string filename) | 
|---|
| 386 | { | 
|---|
| 387 | mUserCode.resize(0); | 
|---|
| 388 |  | 
|---|
| 389 | ifstream is(filename.c_str()); | 
|---|
| 390 | if(!is) { | 
|---|
| 391 | cout<<"CxxExecutor::FillUserCode: unable to open "<<filename<<endl; | 
|---|
| 392 | return 1; | 
|---|
| 393 | } | 
|---|
| 394 |  | 
|---|
| 395 | int nline = 4096; // Au plus 4096 characters par ligne! | 
|---|
| 396 | char *str = new char[nline+2]; | 
|---|
| 397 | while(is) | 
|---|
| 398 | {is.getline(str,nline); mUserCode.push_back((string) str);} | 
|---|
| 399 | delete [] str; | 
|---|
| 400 | if(mUserCode.size()<=0) { | 
|---|
| 401 | return 2; | 
|---|
| 402 | cout<<"CxxExecutor::FillUserCode: no user code"<<endl; | 
|---|
| 403 | } | 
|---|
| 404 |  | 
|---|
| 405 | cout<<"User code filled from file"<<filename<<endl; | 
|---|
| 406 | return 0; | 
|---|
| 407 | } | 
|---|
| 408 |  | 
|---|
| 409 | /* --Methode-- */ | 
|---|
| 410 | int CxxExecutor::Compile(string rootfilename) | 
|---|
| 411 | { | 
|---|
| 412 | string fc = rootfilename + ".cc"; | 
|---|
| 413 | string fl = rootfilename + ".so"; | 
|---|
| 414 | cout<<"Compile: "<<rootfilename<<endl; | 
|---|
| 415 | int rc = 0; | 
|---|
| 416 | rc = CrMakefile(); | 
|---|
| 417 | if(rc) return(1); | 
|---|
| 418 | string make  = "make -f cxx_spiapp_Makefile"; | 
|---|
| 419 | make += " CXXFLAGS=\"" + mCompOpt + "\""; | 
|---|
| 420 | make += " LDFLAGS=\""  + mLinkOpt + "\""; | 
|---|
| 421 | make += " MYLIBS=\""   + mMyLibs  + "\""; | 
|---|
| 422 | make += " " + rootfilename; | 
|---|
| 423 | rc = system(make.c_str()); | 
|---|
| 424 | if(rc) | 
|---|
| 425 | {cout<<"CxxExecutor::Compile : \n"<<make<<" Failed"<<endl; | 
|---|
| 426 | return 1000+rc;} | 
|---|
| 427 | return 0; | 
|---|
| 428 | } | 
|---|
| 429 |  | 
|---|
| 430 | /* --Methode-- */ | 
|---|
| 431 | int CxxExecutor::CrMakefile(void) | 
|---|
| 432 | { | 
|---|
| 433 | ofstream os("cxx_spiapp_Makefile",ios::out); | 
|---|
| 434 | if(!os) | 
|---|
| 435 | {cout<<"CxxExecutor::CrMakefile: unable to open file for Makefile"<<endl; | 
|---|
| 436 | return 1;} | 
|---|
| 437 | //--------------------------------------------------------------------- | 
|---|
| 438 | os<<"MODULEDECCXXFLAGS := -msg_quiet"<<endl; | 
|---|
| 439 | os<<"include $(DPCBASEREP)/Include/MakefileUser.h"<<endl; | 
|---|
| 440 | os<<"MYLIBS ="<<endl; | 
|---|
| 441 | os<<"LIBS = -L$(SLB) -lPI -lextsophya -lsophya -lm"<<endl; | 
|---|
| 442 | os<<"ifeq ($(MACHEROS),OSF1)"<<endl; | 
|---|
| 443 | os<<"LIBS := $(LIBS) -lfor"<<endl; | 
|---|
| 444 | os<<"endif"<<endl; | 
|---|
| 445 | os<<"ifeq ($(MACHEROS),Linux)"<<endl; | 
|---|
| 446 | os<<"LIBS := $(LIBS) -ldl -lf2c"<<endl; | 
|---|
| 447 | os<<"endif"<<endl; | 
|---|
| 448 | os<<"%.so:$(OBJ)%.o"<<endl; | 
|---|
| 449 | os<<"%:%.cc"<<endl; | 
|---|
| 450 | os<<"%:%.o"<<endl; | 
|---|
| 451 | os<<"%.o:%.cc"<<endl; | 
|---|
| 452 | os<<"%.o:%.c"<<endl; | 
|---|
| 453 | os<<"%:%.c"<<endl; | 
|---|
| 454 | os<<endl; | 
|---|
| 455 | os<<".PRECIOUS: %.so"<<endl; | 
|---|
| 456 | os<<endl; | 
|---|
| 457 | os<<"%:%.so"<<endl; | 
|---|
| 458 | os<<"\t"<<"echo $@ \" made (.so) \""<<endl; | 
|---|
| 459 | os<<"%.so:$(OBJ)%.o"<<endl; | 
|---|
| 460 | os<<"\t"<<"$(LINK.cc) -shared -o $@ $< $(LIBS) $(MYLIBS)"<<endl; | 
|---|
| 461 | os<<"$(OBJ)%.o:%.cc"<<endl; | 
|---|
| 462 | os<<"\t"<<"$(COMPILE.cc)  -o $@ $<"<<endl; | 
|---|
| 463 | os<<"$(OBJ)%.o:%.c"<<endl; | 
|---|
| 464 | os<<"\t"<<"$(COMPILE.c) -c $(CFLAGS)   $(USERFLAGS) -o $@ $<"<<endl; | 
|---|
| 465 | //--------------------------------------------------------------------- | 
|---|
| 466 | return 0; | 
|---|
| 467 | } | 
|---|
| 468 |  | 
|---|
| 469 | /* --Methode-- */ | 
|---|
| 470 | int CxxExecutor::Link(string libname,string func) | 
|---|
| 471 | { | 
|---|
| 472 | NamedObjMgr omg; | 
|---|
| 473 | PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter(); | 
|---|
| 474 | string key("link"); | 
|---|
| 475 | vector<string> arg; arg.push_back(libname); arg.push_back(func); | 
|---|
| 476 | int rc = mpiac->ExecuteCommand(key,arg); | 
|---|
| 477 | cout<<"Link from "<<libname<<" for function "<<func | 
|---|
| 478 | <<" (rc="<<rc<<")"<<endl; | 
|---|
| 479 | return 0; | 
|---|
| 480 | } | 
|---|
| 481 |  | 
|---|
| 482 | /* --Methode-- */ | 
|---|
| 483 | int CxxExecutor::Call(string func) | 
|---|
| 484 | { | 
|---|
| 485 | NamedObjMgr omg; | 
|---|
| 486 | PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter(); | 
|---|
| 487 | string key("call"); | 
|---|
| 488 | vector<string> arg; arg.push_back(func); | 
|---|
| 489 | if(mCallArgs.size()>0) | 
|---|
| 490 | for(int i=0;i<mCallArgs.size();i++) arg.push_back(mCallArgs[i]); | 
|---|
| 491 | mpiac->ExecuteCommand(key,arg); | 
|---|
| 492 | return 0; | 
|---|
| 493 | } | 
|---|
| 494 |  | 
|---|
| 495 | /* --Methode--  DO NOT DELETE.... cmv property !!! | 
|---|
| 496 | int CxxExecutor::FillUserCode(vector<string>& usercode,uint_4 first) | 
|---|
| 497 | // - first is the first position in the vector<> where the code starts | 
|---|
| 498 | { | 
|---|
| 499 | mUserCode.resize(0); | 
|---|
| 500 | uint_4 nus = usercode.size(); | 
|---|
| 501 | if(nus<=first) { | 
|---|
| 502 | cout<<"CxxExecutor::FillUserCode: no user code"<<endl; | 
|---|
| 503 | return 1; | 
|---|
| 504 | } | 
|---|
| 505 | // **** 1er probleme **** | 
|---|
| 506 | // - Pour la lisibilite et eviter les bugs avec les chaines de caracteres | 
|---|
| 507 | // contenant des blancs, on ne casse les lignes qu'apres un ";" a condition | 
|---|
| 508 | // qu'il ne soit pas dans une chaine de caracteres c'est a dire | 
|---|
| 509 | // entoure de "....;....". | 
|---|
| 510 | // - Attention aux " dans les chaines de caracteres:  "...\"..." | 
|---|
| 511 | // - Bugs non-gere pour les tortures: ".....\\" suivi par une autre | 
|---|
| 512 | //   chaine de caracteres qui contient un ';' | 
|---|
| 513 | //   ex: cout<<"aaa\\"; cout<<"blabla;blabla"; | 
|---|
| 514 | // **** 2ieme probleme **** | 
|---|
| 515 | // - Le decodeur de ligne va couper les commentaires qui contiennent des blancs: | 
|---|
| 516 | // "salut ca va" --> tokens[0]="salut tokens[1]=ca tokens[2]=va" --> "salutcava" | 
|---|
| 517 | // - On contourne partiellement le pb en ajoutant un blanc en debut des tokens[] | 
|---|
| 518 | // identifies comme etant dans une chaine de caracteres (mais impossible de gerer | 
|---|
| 519 | // si il y a plusieurs blancs!): "salut     ca    va" -> "salut ca va" | 
|---|
| 520 | // **** Conclusion **** | 
|---|
| 521 | // Pour ecrire du code sophistique, le faire dans un fichier | 
|---|
| 522 | string dum = ""; | 
|---|
| 523 | bool lastchar = false; | 
|---|
| 524 | bool comment = false; | 
|---|
| 525 | for(uint_4 i=first;i<nus;i++) { | 
|---|
| 526 | const char* str = usercode[i].c_str(); | 
|---|
| 527 | size_t lstr= strlen(str); | 
|---|
| 528 | for(uint_4 j=0;j<lstr;j++) { | 
|---|
| 529 | // debut de mot dans un commentaire -> ajouter un blanc | 
|---|
| 530 | if(j==0 && comment) dum += " "; | 
|---|
| 531 | // on arrive sur un " : debut ou fin commentaire? inactif (\")? | 
|---|
| 532 | if(str[j]=='"') { | 
|---|
| 533 | if(j==0) comment = (comment) ? false: true; | 
|---|
| 534 | else {if(str[j-1]!='\\') comment = (comment) ? false: true;} | 
|---|
| 535 | } | 
|---|
| 536 | dum += str[j]; | 
|---|
| 537 | if( i==nus-1 && j==lstr-1 ) lastchar = true; | 
|---|
| 538 | // On charge la string "dum" si : 1-/ on a un ";" | 
|---|
| 539 | //                                2-/ c'est le dernier charactere | 
|---|
| 540 | if((str[j]==';' && !comment) || lastchar) | 
|---|
| 541 | {mUserCode.push_back(dum); dum = "";} | 
|---|
| 542 | } | 
|---|
| 543 | } | 
|---|
| 544 | cout<<"User code filled from standard input"<<endl; | 
|---|
| 545 | return 0; | 
|---|
| 546 | } | 
|---|
| 547 | */ | 
|---|
| 548 |  | 
|---|