source: Sophya/trunk/SophyaPI/PIext/piacmd.cc@ 4041

Last change on this file since 4041 was 3572, checked in by cmv, 17 years ago

char* -> const char* pour regler les problemes de deprecated string const... + comparaison unsigned signed + suppression EVOL_PLANCK rz+cmv 07/02/2009

File size: 12.1 KB
RevLine 
[2615]1#include "sopnamsp.h"
[293]2#include "piacmd.h"
[165]3#include <stdio.h>
4#include <stdlib.h>
[349]5#include <ctype.h>
[165]6#include <math.h>
7
8
[293]9#include "pdlmgr.h"
[165]10#include "ctimer.h"
[2214]11#include "strutil.h"
[2215]12#include "strutilxx.h"
[293]13// #include "dlftypes.h"
[2307]14#include "srandgen.h"
[165]15
[293]16#include "pistdimgapp.h"
[165]17#include "nobjmgr.h"
[2781]18
19// Les differentes classes CommandExecutor
20#include "basexecut.h"
21#include "graphexecut.h"
[361]22#include "piafitting.h"
[463]23#include "pawexecut.h"
[1224]24#include "cxxexecutor.h"
[1251]25#include "cxxexecwin.h"
[1828]26#include "contmodex.h"
[1920]27#include "flowmodex.h"
[165]28
[293]29#include PISTDWDG_H
30#include PILIST_H
[165]31
[293]32// ------------------------------------------------------------
[349]33// Gestion d'une fenetre d'aide interactive
34// Classe PIAHelpWind
[293]35// ------------------------------------------------------------
[165]36
[2474]37#define HGRPMSGOFFSET 100000
38
[293]39class PIAHelpWind : public PIWindow {
40public :
41 PIAHelpWind(PIStdImgApp* par, PIACmd* piacmd);
42 virtual ~PIAHelpWind();
[330]43 virtual void Show();
[293]44 virtual void Process(PIMessage msg, PIMsgHandler* sender, void* data=NULL);
[330]45 inline void AddHelpGroup(const char * hgrp, int gid)
[2474]46 { hgrpom->AppendItem(hgrp, HGRPMSGOFFSET+gid); }
[2465]47 inline void AddHelpGroup(string const & hgrp, int gid)
[2474]48 { hgrpom->AppendItem(hgrp.c_str(), HGRPMSGOFFSET+gid); }
[330]49 inline void ClearHelpList()
50 { mNitem=0; hitemlist->DeleteAllItems(); }
[293]51 inline void AddHelpItem(const char * hitem)
[330]52 { mNitem++; hitemlist->AppendItem(hitem, 100+mNitem); }
[2465]53 inline void SetHelpTextLabel(string const & htxt, string const & hlab)
54 { mTxt->SetText(htxt); mLab->SetLabel(hlab); }
55
[293]56protected :
57 PIStdImgApp* dap;
58 PIACmd* piac;
59 int mNitem;
60 PIList* hitemlist;
[330]61 PIOptMenu* hgrpom;
[293]62 PIButton * mBut;
63 PILabel * mLab;
64 PIText* mTxt;
65};
[165]66
[293]67/* --Methode-- */
68PIAHelpWind::PIAHelpWind(PIStdImgApp *par, PIACmd* piacmd)
69 : PIWindow((PIMsgHandler *)par, "Help-PIApp", PIWK_normal, 400, 300, 100, 350)
70{
71dap = par;
72piac = piacmd;
73mNitem = 0;
[330]74SetMsg(77);
[165]75
[293]76int bsx, bsy;
77int tsx, tsy;
78int spx, spy;
79PIApplicationPrefCompSize(bsx, bsy);
80spx = bsx/6; spy = bsy/6;
81tsx = 10*bsx+2*spx; tsy = 7*bsy+3*spy;
82SetSize(tsx,tsy);
[330]83hgrpom = new PIOptMenu(this, "hgrpoptmen", bsx*2.0, bsy, spx/2, spy);
84hgrpom->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
85hitemlist = new PIList(this, "hitemlist", bsx*2.0, tsy-3*spy-bsy, spx/2, 2*spy+bsy);
[293]86hitemlist->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
87// hitemlist->SetBorderWidth(2);
[324]88mTxt = new PIText(this, "helptext", true, true, bsx*8.0, 6*bsy, bsx*2.0+1.5*spx, spy);
89// mTxt->SetMutiLineMode(true);
[293]90mTxt->SetTextEditable(false);
91mTxt->SetText("");
92mTxt->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
93mLab = new PILabel(this, "helpitem", bsx*4, bsy, bsx*2.5+2*spx, tsy-spy-bsy);
94mLab->SetBorderWidth(1);
95mLab->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
96mLab->SetLabel("");
[330]97mBut = new PIButton(this, "Close", 70, bsx, bsy, tsx-bsx*1.5-spx, tsy-spy-bsy);
[293]98mBut->SetBinding(PIBK_elastic,PIBK_elastic, PIBK_elastic,PIBK_elastic);
99}
[165]100
101/* --Methode-- */
[293]102PIAHelpWind::~PIAHelpWind()
103{
[330]104delete hgrpom;
[293]105delete hitemlist;
106delete mTxt;
107delete mLab;
108delete mBut;
109}
110
111/* --Methode-- */
112void PIAHelpWind::Process(PIMessage msg, PIMsgHandler* sender, void* /*data*/)
113{
114PIMessage um = UserMsg(msg);
[330]115if (((um == 77) && (ModMsg(msg) == PIMsg_Close)) || (um == 70) ) {
[293]116 Hide();
117 return;
118 }
[2474]119else if ( (um >= HGRPMSGOFFSET) && (sender == hgrpom)) { // Selection de groupe de Help
[330]120 mTxt->SetText("");
121 mLab->SetLabel("");
[2474]122 piac->UpdateHelpList(this, um-HGRPMSGOFFSET);
[330]123}
124else if ( (um > 100) && (sender == hitemlist) && (ModMsg(msg) == PIMsg_Select) ) {
[293]125 string s = hitemlist->GetSelectionStr();
126 mTxt->SetText(piac->GetUsage(s));
127 mLab->SetLabel(s);
128 }
129}
130
[330]131/* --Methode-- */
132void PIAHelpWind::Show()
133{
[2474]134hgrpom->SetValue(HGRPMSGOFFSET); // Groupe All
[330]135mTxt->SetText("");
136mLab->SetLabel("");
137piac->UpdateHelpList(this, 0);
138PIWindow::Show();
139}
[293]140
[349]141
142// ------------------------------------------------------------
143// Classe PIACmd
144// ------------------------------------------------------------
145
[2463]146// static PIACmd* curpiacmd = NULL;
[293]147/* --Methode-- */
[2490]148PIACmd::PIACmd(PIStdImgApp* app)
[2463]149 : Commander()
[165]150{
[2490]151mObjMgr = new NamedObjMgr;
[165]152mImgApp = app;
[293]153helpwin = new PIAHelpWind(app, this);
[2465]154helpwin_initdone = false;
[293]155
[2305]156string grp = "PIACmd";
[2465]157string gdesc = "piapp interpreter (class PIACmd) additional builtin commands";
158AddHelpGroup(grp, gdesc);
159
[2672]160string kw = "exitpiapp";
[2463]161string usage = "To end the piapp session ";
[2419]162RegisterCommand(kw, usage, NULL, grp);
[2463]163kw = "helpwindow";
164usage = "To display the Help window ";
[2419]165RegisterCommand(kw, usage, NULL, grp);
[2677]166kw = "stop";
167usage = "To stop (break) interpreter (PIACmd/Commander) execution";
[2672]168RegisterCommand(kw, usage, NULL, grp);
[2203]169
[2490]170basexec = new PIABaseExecutor(this, mObjMgr, app);
[2781]171graphexec = new PIAGraphicExecutor(this, mObjMgr, app);
[361]172fitexec = new PIAFitter(this, app);
[463]173pawexec = new PAWExecutor(this, app);
[1251]174CxxExecutor * cxxe = new CxxExecutor(this, app);
175cxxexec = cxxe;
[1828]176
177ContModExecutor *cntxx = new ContModExecutor(this, app);//_OP_
178cntexec = cntxx; //_OP_
[1920]179FlowModExecutor *flwxx = new FlowModExecutor(this, app);//_OP_
180flwexec = flwxx; //_OP_
[1828]181
[1251]182cxxoptwin = new CxxOptionWind(app, cxxe);
183cxxexwin = new CxxExecWind(app, cxxe);
184
[2465]185InitializeHelpWindowMenu();
[2486]186// <ZThread> Thread control attributes
187fg_thrstop = false;
188fg_threxe = false;
[165]189}
190
191/* --Methode-- */
192PIACmd::~PIACmd()
193{
[2490]194delete mObjMgr;
[293]195delete helpwin;
[1251]196delete cxxexwin;
197delete cxxoptwin;
[361]198delete basexec;
[2781]199delete graphexec;
[361]200delete fitexec;
[463]201delete pawexec;
[1224]202delete cxxexec;
[165]203}
204
205
[293]206/* --Methode-- */
[2486]207void PIACmd::AddInputLine(string const & line)
208{
[2677]209 if (line == "stop") {
[2672]210 StopExecution();
211 return;
212 }
[2486]213 mutx_inps.lock();
[2536]214 inputlines.push(line); // Ajout d'un element en fin de queue
[2486]215 mutx_inps.unlock();
216 mutx_inps.broadcast();
217}
218
219/* --Methode-- */
220void PIACmd::StopThread()
221{
222 fg_thrstop = true;
223 mutx_inps.broadcast();
224}
225
226/* --Methode-- */
227void PIACmd::run()
228{
229 while (!fg_thrstop) {
230 mutx_inps.lock();
231 while (inputlines.empty()) {
232 fg_threxe = false;
233 mutx_inps.wait();
234 }
[2536]235 string line = inputlines.front(); // On recupere l'element de tete
236 inputlines.pop(); // On supprime l'element de tete
[2486]237 fg_threxe = true;
238 mutx_inps.unlock();
[2606]239 try {
240 Interpret(line);
241 }
242 catch (PThrowable& pex) { // Catching SOPHYA exceptions
243 cerr << "PIACmd::run() SOPHYA exception in call to Interpret(" << line << ")\n"
244 << (string)typeid(pex).name() << " Msg= " << pex.Msg() << endl;
245 }
246 catch (std::exception& sex) {
247 cerr << "PIACmd::run() std::exception in call to Interpret(" << line << ")\n"
248 << (string)typeid(sex).name() << " Msg= " << sex.what() << endl;
249 }
250 catch (...) {
251 cerr << "PIACmd::run() unknown exception (...) in call to Interpret("
252 << line << ")" << endl;
253 }
[2486]254 }
255}
256
257/* --Methode-- */
[2465]258bool PIACmd::CheckHelpGrp(string& grp, int& gid, string& desc)
[330]259{
[2465]260bool fgnewgrp = Commander::CheckHelpGrp(grp, gid, desc);
261if (helpwin_initdone && fgnewgrp) helpwin->AddHelpGroup(grp.c_str(), gid);
262return fgnewgrp;
[330]263}
264
265/* --Methode-- */
266void PIACmd::UpdateHelpList(PIAHelpWind* hw, int gid)
267{
268helpwin->ClearHelpList();
[2465]269if (gid == 0) {
270 string htxt = "All registered commands";
271 string hlab = "All (HelpGroup)";
272 helpwin->SetHelpTextLabel(htxt, hlab);
273}
274else {
275 CmdHGroup::iterator ith;
276 for(ith = cmdhgrp.begin(); ith != cmdhgrp.end(); ith++) {
277 if ((*ith).second.gid == gid) {
278 string hlab = (*ith).first;
279 hlab += " (HelpGroup)";
280 helpwin->SetHelpTextLabel((*ith).second.desc, hlab);
281 break;
282 }
283 }
284}
[330]285CmdExmap::iterator it;
286for(it = helpexmap.begin(); it != helpexmap.end(); it++) {
287 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
288 helpwin->AddHelpItem((*it).first.c_str());
289 }
290for(it = cmdexmap.begin(); it != cmdexmap.end(); it++) {
291 if ( (gid != 0) && ((*it).second.group != gid) ) continue;
292 helpwin->AddHelpItem((*it).first.c_str());
293 }
294}
295
[2465]296/* --Methode-- */
297void PIACmd::InitializeHelpWindowMenu()
298{
299 helpwin->AddHelpGroup("All", 0);
300 CmdHGroup::iterator it;
301 for(it = cmdhgrp.begin(); it != cmdhgrp.end(); it++)
302 helpwin->AddHelpGroup((*it).first, (*it).second.gid);
303 helpwin_initdone = true;
304}
[165]305
[357]306/* Fonction */
[368]307static string GetStringFrStdin(PIACmd* piac)
[357]308{
[368]309PIStdImgApp* piapp = piac->GetImgApp();
310if (piapp) {
311 PIBaseWdg* wdg = piapp->CurrentBaseWdg();
312 if (wdg) wdg->Refresh();
313}
[357]314char buff[128];
315fgets(buff, 128, stdin);
316buff[127] = '\0';
317return((string)buff);
318}
319
[2999]320/* --Methode-- */
321bool PIACmd::GetVarApp(string const & vn, string & vv)
322{
323 vv = "";
324 if (mObjMgr == NULL) return false;
325 vector<string> opts;
326 SplitStringToVString(vn, opts, '.');
327 string nom = opts[0];
328 opts.erase(opts.begin());
329 NObjMgrAdapter* obja = mObjMgr->GetObjAdapter(nom);
330 if (obja == NULL) return false;
331 vv = obja->GetInfoString(opts);
332 return true;
333}
[165]334
[1262]335/* --Methode-- */
336int PIACmd::ExecuteCommandLine(string & kw, vector<string> & tokens, string & toks)
337{
338int rc = 0;
339NamedObjMgr omg;
[349]340
[165]341// >>>>>>>>>>> Commande d'interpreteur
[2812]342if ( (kw == "helpwindow") && (mImgApp)) {
343 ZSync zs(mImgApp->getMutex());
[2465]344 ShowHelpWindow();
[2812]345 zs.NOp();
[2465]346 return 0;
347}
[2307]348// ----> Sortie d'application
[2812]349else if ((kw == "exitpiapp") && (mImgApp)) {
[2307]350 mImgApp->Stop();
[2465]351 return 0;
[2307]352}
[2463]353else return Commander::ExecuteCommandLine(kw, tokens, toks);
[2465]354return 0;
[165]355}
356
[333]357
[165]358/* --Methode-- */
[293]359void PIACmd::ShowHelpWindow()
[165]360{
[2812]361 helpwin->Show();
[165]362}
[357]363
[463]364/* --Methode-- */
[1251]365void PIACmd::ShowCxxOptionWindow()
366{
[2812]367 cxxoptwin->Show();
[1251]368}
369
370/* --Methode-- */
371void PIACmd::ShowCxxExecWindow()
372{
[2812]373 cxxexwin->Show();
[1251]374}
375
[2463]376/* --Methode-- */
377void PIACmd::SetCurrentPrompt(const char* pr)
[2377]378{
[2463]379 Commander::SetCurrentPrompt(pr);
[2489]380 if (mImgApp) {
[2755]381 ZSync zs(mImgApp->getMutex()); zs.NOp();
[2489]382 mImgApp->GetConsole()->SetPrompt(pr);
383 }
[2377]384}
385
[1251]386/* --Methode-- */
[2463]387void PIACmd::ShowMessage(const char * msg, int att)
[463]388{
[2489]389 /*
390 Il faut faire quelque chose pour gerer correctement ca en multithread
[2463]391 char va = (att == 0) ? 0 : PIVA_Magenta;
[2489]392 if (mImgApp) {
[2755]393 ZSync(mImgApp->getMutex()); zs.NOp();
[2489]394 mImgApp->GetConsole()->AddStr(msg, va);
395 }
396 */
397Commander::ShowMessage(msg, att);
[2263]398}
[3366]399
400
401/* --Methode-- */
402SOpExObj::SOpExObj(PIStdImgApp* app)
403{
404 mImgApp = app;
405 fg_thrstop = false;
406 fg_threxe = false;
407}
408
409/* --Methode-- */
410SOpExObj::~SOpExObj()
411{
412}
413
414/* --Methode-- */
415void SOpExObj::AddOpe(string const & line)
416{
417 mutx_inps.lock();
418 inputlines.push(line); // Ajout d'un element en fin de queue
419 mutx_inps.unlock();
420 mutx_inps.broadcast();
421}
422
423/* --Methode-- */
424void SOpExObj::run()
425{
426 NamedObjMgr omg;
427 while (!fg_thrstop) {
428 mutx_inps.lock();
429 while (inputlines.empty()) {
430 fg_threxe = false;
431 mutx_inps.wait();
432 }
433 string cmd = inputlines.front().substr(0,3); // On recupere l'element de tete
434 string nom = inputlines.front().substr(4); // On recupere l'element de tete
435 inputlines.pop(); // On supprime l'element de tete
436 fg_threxe = true;
437 mutx_inps.unlock();
438 try {
[3374]439 if (cmd == "UPD") { // UpdateObjMgrWindow()
440 int did = atoi(nom.c_str());
441 omg.UpdateObjMgrWindow(did);
442 }
443 else if (cmd == "SCO") { // set cobj
[3366]444 string ss = "set cobj ";
445 ss += nom;
446 cout << " Defining cobj : " << ss << endl;
447 mImgApp->CmdInterpreter()->AddInputLine(ss);
448 }
449 else if (cmd == "DIS") { // display obj
450 omg.DisplayObj(nom);
451 }
452 else if (cmd == "PRT") { // print obj
453 omg.PrintObj(nom);
454 }
455 else if (cmd == "DEL") { // delete obj
456 omg.DelObj(nom);
457 }
458 else if (cmd == "SAV") { // saveppf obj
459 if (mImgApp->mPpfout) omg.SaveObj(nom, *(mImgApp->mPpfout), false);
460 }
461 else cerr << "SOpExObj::run() ERROR CMD= " << cmd << endl;
462 }
463 catch (PThrowable& pex) { // Catching SOPHYA exceptions
464 cerr << "SOpExObj::run() SOPHYA exception CMD=" << cmd << " NOM=" << nom << "\n"
465 << (string)typeid(pex).name() << " Msg= " << pex.Msg() << endl;
466 }
467 catch (std::exception& sex) {
468 cerr << "SOpExObj::run() std::exception CMD=" << cmd << " NOM=" << nom << "\n"
469 << (string)typeid(sex).name() << " Msg= " << sex.what() << endl;
470 }
471 catch (...) {
472 cerr << "SOpExObj::run() unknown exception (...) CMD="
473 << cmd << " NOM=" << nom << endl;
474 }
475 }
476}
Note: See TracBrowser for help on using the repository browser.