source: Sophya/trunk/SophyaPI/PIext/cxxexecutor.cc@ 1230

Last change on this file since 1230 was 1230, checked in by ercodmgr, 25 years ago

Get and Set pour cxxexecutor options cmv 12/10/00

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