0024510: Remove unused local variables
[occt.git] / src / IFSelect / IFSelect_SessionPilot.cxx
CommitLineData
973c2be1 1// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 2//
973c2be1 3// This file is part of Open CASCADE Technology software library.
b311480e 4//
973c2be1 5// This library is free software; you can redistribute it and / or modify it
6// under the terms of the GNU Lesser General Public version 2.1 as published
7// by the Free Software Foundation, with special exception defined in the file
8// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9// distribution for complete text of the license and disclaimer of any warranty.
b311480e 10//
973c2be1 11// Alternatively, this file may be used under the terms of Open CASCADE
12// commercial license or contractual agreement.
b311480e 13
7fd59977 14#include <Standard_Stream.hxx>
15
16#include <IFSelect_SessionPilot.ixx>
17#include <IFSelect_Activator.hxx>
18#include <IFSelect_Selection.hxx>
19#include <Interface_EntityIterator.hxx>
20#include <Interface_InterfaceModel.hxx>
21#include <TColStd_HSequenceOfAsciiString.hxx>
22
23#include <Interface_Macros.hxx>
24#include <Message_Messenger.hxx>
25#include <Message.hxx>
26
27#include <stdio.h>
28
29#define MAXWORDS 200
30#define MAXCARS 1000
31
32static int initactor = 0;
33static char* trace;
34
35static TCollection_AsciiString nulword;
36
37// Nb Maxi de words : cf thewords et method SetCommandLine
38
39 IFSelect_SessionPilot::IFSelect_SessionPilot (const Standard_CString prompt)
40 : theprompt (prompt) , thewords (0,MAXWORDS-1) , thewordeb (0,MAXWORDS-1)
41{
42 if (theprompt.Length() == 0) theprompt.AssignCat ("Test-XSTEP>");
43 therecord = Standard_False; thenbwords = 0;
44 if (initactor) return; initactor = 1;
45 Add (1,"x");
46 Add (1,"exit");
47 Add (2,"?");
48 Add (2,"xhelp");
49 Add (3,"xcommand");
50 Add (4,"xsource");
51 Add (5,"xstep");
52 Add (6,"xnew");
53 trace = getenv("DEBUGMODE");
54}
55
56
57 Handle(IFSelect_WorkSession) IFSelect_SessionPilot::Session () const
58 { return thesession; }
59
60 Handle(IFSelect_WorkLibrary) IFSelect_SessionPilot::Library () const
61 { return thesession->WorkLibrary(); }
62
63 Standard_Boolean IFSelect_SessionPilot::RecordMode () const
64 { return therecord; }
65
66 void IFSelect_SessionPilot::SetSession
67 (const Handle(IFSelect_WorkSession)& WS)
68 { thesession = WS; }
69
70 void IFSelect_SessionPilot::SetLibrary
71 (const Handle(IFSelect_WorkLibrary)& WL)
72 { if (!thesession.IsNull()) thesession->SetLibrary(WL); }
73
74 void IFSelect_SessionPilot::SetRecordMode (const Standard_Boolean mode)
75 { therecord = mode; }
76
77
78 void IFSelect_SessionPilot::SetCommandLine
79 (const TCollection_AsciiString& command)
80{
81 Standard_Integer lc = command.Length();
82 if (lc > 200) cout<<" Commande TRES LONGUE : "<<lc<<" caracteres :"<<endl
83 <<command.ToCString()<<endl;
84 thecommand = command;
85 if (thecommand.Value(lc) <= ' ') { thecommand.Remove(lc); lc --; }
86 thenbwords = 0;
87 Standard_Integer i, nc = 0;
88 char unarg[MAXCARS];
89 for (i = 1; i <= lc; i ++) {
90 char val = command.Value(i);
91 if (val <= ' ') {
92 if (nc == 0) continue;
93 if (thenbwords >= MAXWORDS) { unarg[nc] = val; nc ++; continue; }
94 unarg[nc] = '\0';
95 thewords(thenbwords).Clear(); thewords(thenbwords).AssignCat(unarg);
96 if (trace) cout<<"thewords("<<thenbwords<<") ="<<unarg<<endl;
97 thenbwords ++; nc = 0;
98 continue;
99 }
100 if (nc == 0) thewordeb.SetValue (thenbwords,i);
101 if (nc > MAXCARS) { cout<<"Arg."<<thenbwords<<" > "<<MAXCARS<<" car.s, tronque"<<endl; continue; }
102 unarg[nc] = val; nc ++;
103 }
104 if (nc > 0) {
105 unarg[nc] = '\0'; thewords(thenbwords).Clear();
106 thewords(thenbwords).AssignCat(unarg);
107 if (trace) cout<<"thewords("<<thenbwords<<")="<<unarg<<endl<<" .. Fin avec thenbwords="<<thenbwords+1<<endl;
108 thenbwords ++;
109 }
110/*
111 aligner sur MAXWORDS
112 char l0[80],l1[80],l2[80],l3[80],l4[80],l5[80],l6[80],l7[80],l8[80],l9[80];
113 char m0[80],m1[80],m2[80],m3[80],m4[80],m5[80],m6[80],m7[80],m8[80],m9[80];
114 thenbwords = sscanf
115 (thecommand.ToCString(),"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
116 l0,l1,l2,l3,l4,l5,l6,l7,l8,l9,m0,m1,m2,m3,m4,m5,m6,m7,m8,m9);
117 if (thenbwords < 0) thenbwords = 0;
118 if (thenbwords > MAXWORDS) thenbwords = MAXWORDS;
119 Standard_Integer nb = thewords.Upper();
120 for (i = 0; i <= nb; i ++) thewords(i).Clear();
121 switch (thenbwords) {
122 case 20 : thewords(19).AssignCat(m9);
123 case 19 : thewords(18).AssignCat(m8);
124 case 18 : thewords(17).AssignCat(m7);
125 case 17 : thewords(16).AssignCat(m6);
126 case 16 : thewords(15).AssignCat(m5);
127 case 15 : thewords(14).AssignCat(m4);
128 case 14 : thewords(13).AssignCat(m3);
129 case 13 : thewords(12).AssignCat(m2);
130 case 12 : thewords(11).AssignCat(m1);
131 case 11 : thewords(10).AssignCat(m0);
132 case 10 : thewords(9).AssignCat(l9);
133 case 9 : thewords(8).AssignCat(l8);
134 case 8 : thewords(7).AssignCat(l7);
135 case 7 : thewords(6).AssignCat(l6);
136 case 6 : thewords(5).AssignCat(l5);
137 case 5 : thewords(4).AssignCat(l4);
138 case 4 : thewords(3).AssignCat(l3);
139 case 3 : thewords(2).AssignCat(l2);
140 case 2 : thewords(1).AssignCat(l1);
141 case 1 : thewords(0).AssignCat(l0);
142 default : break;
143 }
144*/
145 thenumrec = 0;
146 theobjrec.Nullify();
147}
148
149 const TCollection_AsciiString& IFSelect_SessionPilot::CommandLine () const
150 { return thecommand; }
151
152 Standard_CString IFSelect_SessionPilot::CommandPart
153 (const Standard_Integer numarg) const
154{
155 if (numarg <= 0) return thecommand.ToCString();
156 if (numarg >= thenbwords) return "";
157 return &(thecommand.ToCString())[thewordeb(numarg)-1];
158}
159
160 Standard_Integer IFSelect_SessionPilot::NbWords () const
161 { return thenbwords; }
162
163 const TCollection_AsciiString& IFSelect_SessionPilot::Word
164 (const Standard_Integer num) const
165 { if (num < thenbwords) return thewords(num); return nulword; }
166
167 Standard_CString IFSelect_SessionPilot::Arg
168 (const Standard_Integer num) const
169 { return Word(num).ToCString(); }
170
171 Standard_Boolean IFSelect_SessionPilot::RemoveWord
172 (const Standard_Integer num)
173{
174 if (num < 0 || num > thenbwords) return Standard_False;
175 Standard_Integer i; // svv Jan11 2000 : porting on DEC
176 for (i = num; i < thenbwords; i ++) {
177 thewords(i).Clear();
178 thewords(i).AssignCat(thewords(i+1).ToCString());
179 }
180 thewords(thenbwords).Clear();
181 thenbwords --;
182// Et refaire thecommand. Si num = 0, on supprime le debut (facile)
183 if (num == 0) {
184 thecommand.Remove(1,thewordeb(1));
185 } else {
186// Sinon, reconstituer, a partir des words
187 thecommand.Clear();
188 for (i = 0; i < thenbwords; i ++) {
189 if (i > 0) thecommand.AssignCat(" ");
190 thecommand.AssignCat(thewords(i));
191 }
192 }
193
194 return Standard_True;
195}
196
197 Standard_Integer IFSelect_SessionPilot::NbCommands () const
198 { return thecomlist.Length(); }
199
200 const TCollection_AsciiString& IFSelect_SessionPilot::Command
201 (const Standard_Integer num) const
202 { return thecomlist(num); }
203
204
205 IFSelect_ReturnStatus IFSelect_SessionPilot::RecordItem
206 (const Handle(Standard_Transient)& item)
207{
208 theobjrec = item;
209 return (item.IsNull() ? IFSelect_RetFail : IFSelect_RetDone);
210}
211
212 Handle(Standard_Transient) IFSelect_SessionPilot::RecordedItem () const
213 { return theobjrec; }
214
215 void IFSelect_SessionPilot::Clear ()
216 { thecomlist.Clear(); }
217
218
219// #######################################################################
220// ######## CONTROLE D EXECUTION
221
222
223 IFSelect_ReturnStatus IFSelect_SessionPilot::ReadScript
224 (const Standard_CString file)
225{
226 FILE* fic; int lefic = 0;
227 if (file != NULL && file[0] != '\0') {
228 fic = fopen (file,"r");
229 if (fic) lefic = 1;
230 else { cout<<" ... Script File "<<file<<" not found"<<endl; return IFSelect_RetFail; }
231 cout << " ... Reading Script File " << file << endl;
232 }
233 else fic = stdin;
234 IFSelect_ReturnStatus stat = IFSelect_RetVoid;
235
236 for (;;) {
237 char ligne[100];
64531d9c 238 if (!lefic) std::cout << theprompt.ToCString();
7fd59977 239 ligne[0] = '\0';
240 fgets(ligne,100,fic);
241 if (feof(fic)) break;
242 if (ligne[0] == '\0') continue;
243// On interprete cette commande
244 TCollection_AsciiString command(ligne);
245 if (lefic) cout<<file<<":"<<command; // le return est dans la ligne ... !
246 stat = Execute(command);
247 if (stat == IFSelect_RetStop) break;
248 if ((stat == IFSelect_RetError || stat == IFSelect_RetFail) && lefic)
249 { cout << " ... Error in Script File, abandon"<<endl; break; }
250 }
251 if (!lefic) return IFSelect_RetStop;
252 fclose(fic);
253 cout<<"End of Reading Script File " << file << endl;
254 if (stat == IFSelect_RetError || stat == IFSelect_RetFail) return stat;
255 return IFSelect_RetVoid; // fin fichier : depiler
256}
257
258
259// On boucle sur la lecture jusqu a une commande de fin ou un EOF
260
261 IFSelect_ReturnStatus IFSelect_SessionPilot::Perform ()
262{
263 IFSelect_ReturnStatus stat = IFSelect_RetVoid;
264 if (thenbwords == 0) return stat;
265 if (thewords(0).Value(1) == '#') return stat; // commentaire
266
267 theobjrec.Nullify();
268// Est-ce un nom ?
269
270// Commande pour un Acteur
271 Handle(IFSelect_Activator) actor; Standard_Integer num;
272 if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
273 stat = actor->Do(num,this);
274// Prise en compte des commandes a resultat
275// Ici, resultat non nomme; Resultat nomme par commande x (plus loin)
276 if (!theobjrec.IsNull()) {
277 thesession->RemoveItem(theobjrec); //// depannage ?
278 Standard_Boolean addws = thesession->AddItem(theobjrec);
279 if (!addws) { cout<<"Could not add item to session, sorry"<<endl; return IFSelect_RetFail; }
280 }
281
282 if (stat == IFSelect_RetVoid || stat == IFSelect_RetDone) {
283 if (therecord) thecomlist.Append(thecommand);
284 }
285 else if (stat == IFSelect_RetError) cout<<"Error in Command : "<<thecommand<<endl;
286 else if (stat == IFSelect_RetFail) cout << "Execution Failure for : " <<thecommand<<endl;
287 return stat;
288 }
289 cout << " Command : " << thewords(0) << " unknown" << endl;
290 return IFSelect_RetError; // pas reconnu donc incorrect
291}
292
293 IFSelect_ReturnStatus IFSelect_SessionPilot::ExecuteAlias
294 (const TCollection_AsciiString& alias)
295{
296 if (alias.Length() > 0) thewords(0) = alias;
297 return Perform();
298}
299
300 IFSelect_ReturnStatus IFSelect_SessionPilot::Execute
301 (const TCollection_AsciiString& command)
302{
303 SetCommandLine(command);
304 return Perform();
305}
306
307 IFSelect_ReturnStatus IFSelect_SessionPilot::ExecuteCounter
308 (const Handle(IFSelect_SignCounter)& counter, const Standard_Integer numword,
309 const IFSelect_PrintCount mode)
310{
311 if (counter.IsNull()) return IFSelect_RetError;
312 counter->Clear();
313 if (NbWords() <= numword) counter->AddModel (thesession->Model());
314 else {
315// on demande un givelist
316 Handle(TColStd_HSequenceOfTransient) list = thesession->GiveList (CommandPart(numword));
317 if (list.IsNull()) {
318 cout<<"Nothing selected from : "<<CommandPart(numword)<<endl;
319 return IFSelect_RetError;
320 }
321 counter->AddWithGraph (list,thesession->Graph());
322 }
323 counter->PrintList(Message::DefaultMessenger(),thesession->Model(),mode);
324 return IFSelect_RetVoid;
325}
326
327 Standard_Integer IFSelect_SessionPilot::Number
328 (const Standard_CString val) const
329{
330 Standard_Integer num = thesession->NumberFromLabel (val);
331 if (num < 0) cout<<" Label:"<<val<<" ->"<<-num<<" ent.s, refus"<<endl;
332 return num;
333}
334
335
336// #########################################################################
337// ######## ACTIONS SPECIFIQUES DU PILOTE
338
339#define MAXCOMPERLINE 5
340#define LENGTHFORCOM 15
341
342 IFSelect_ReturnStatus IFSelect_SessionPilot::Do
343 (const Standard_Integer number,
344 const Handle(IFSelect_SessionPilot)& session)
345{
346// Commandes Propres : x, exit, undo, redo, ?, help
347 IFSelect_ReturnStatus stat = IFSelect_RetVoid;
348 Standard_Integer argc = NbWords();
349 const Standard_CString arg1 = Word(1).ToCString();
350 Standard_Integer modhelp = -1;
351 switch (number) {
352 case -1 : // **** HELP-XSNEW
353 modhelp = 1;
354 cout<<" -- Commands candidate for xsnew --"<<endl;
355// HELP : soit complet (par defaut) soit limite a xsnew
356 case 0 : { // **** HELP
357 Handle(TColStd_HSequenceOfAsciiString) list;
358// Help complet : on donne la liste des commandes, sans plus (deja pas mal)
359 if (thenbwords <= 1) {
360 list = IFSelect_Activator::Commands(modhelp);
361 Standard_Integer nbcom = 0;
362 Standard_Integer nb = list->Length();
363 cout << " -- Liste des Commands Disponibles --"<<endl;
364 for (Standard_Integer i = 1; i <= nb; i ++) {
365 const TCollection_AsciiString& uncom = list->Value(i);
366 Standard_Integer loncom = uncom.Length();
367 nbcom ++;
368 if (nbcom > MAXCOMPERLINE) { cout<<endl; nbcom = 1; }
369 cout<<" "<<uncom;
370 if (nbcom == MAXCOMPERLINE) continue;
371 for (Standard_Integer j = loncom; j < LENGTHFORCOM; j ++) cout<<" ";
372 }
373 if (nbcom > 0) cout<<endl;
374 cout<<"\nhelp * liste toutes les commandes avec un help sur chacune\n"
375 <<"help <com> liste la ou les commande debutant par <com>"
376 <<" avec un help sur chacune"<<endl;
377
378// Un Help particulier
379 } else {
380 if (thewords(1).IsEqual("*")) list = IFSelect_Activator::Commands(modhelp);
381
382 else list = IFSelect_Activator::Commands(modhelp,thewords(1).ToCString());
383
384 Standard_Integer nb = list->Length();
385 for (Standard_Integer i = 1; i <= nb; i ++) {
386 Handle(IFSelect_Activator) actor; Standard_Integer num;
387 if (IFSelect_Activator::Select
388 (list->Value(i).ToCString(),num,actor)) {
389 if (IFSelect_Activator::Mode (list->Value(i).ToCString()) == 1)
390 cout<<"[xsnew name] ";
391 cout << list->Value(i) << " : " << actor->Help(num) << endl;
392 }
393 }
394 if (nb == 0 && thenbwords > 1) cout<<" Command "<<Word(1)<<" unknown. "
395 << " help (without command) lists all the commands" << endl;
396 }
397 return IFSelect_RetVoid;
398 }
399 case 1 : return IFSelect_RetStop; // **** Fin de session
400 case 2 : { // **** HELP
401 return Do(0,this);
402 }
403 case 3 : { // **** COMMAND
404 if (argc < 2) { cout << "Donner une option :\n"
405 <<"a : analyse une ligne r : toggle record mode\n"
406 <<"l : list recorded c : clear f nom : sauver dans fichier de nom"
407 << endl; return IFSelect_RetVoid; }
408 switch (arg1[0]) {
409 case 'a' : { // **** command analyse
410 cout<<"Command n0 " << number <<" : "<< session->CommandLine()<<endl;
411 cout<<"Nb Words : " << argc-2 << " :\n";
412 for (Standard_Integer i = 2; i < argc; i ++) {
413 cout << " Word." << i-1 << " : " << session->Word(i) <<endl;
414 }
415 break;
416 }
417 case 'c' : session->Clear(); break; // **** command clear
418 case 'f' : {
419 if (argc < 3) { cout<<"Donner nom de fichier"<<endl; return IFSelect_RetError; }
420 Standard_Integer nb = session->NbCommands();
421 if (nb == 0) { cout<<"Aucune commande enregistree"<<endl; break; }
422 cout << "Nb Commandes enregistrees : " << nb <<endl;
423 ofstream fout(Word(2).ToCString(),ios::out);
424 for (Standard_Integer i = 1; i <= nb; i ++)
425 fout<<session->Command(i)<<endl;
426 break;
427 }
428 case 'l' : { // **** command list
429 if (session->RecordMode()) cout<<" -- Record Mode Actif"<<endl;
430 else cout<<" -- Record Mode Inactif"<<endl;
431 Standard_Integer nb = session->NbCommands();
432 cout << "Nb Commandes enregistrees : " << nb << " :"<<endl;
433 for (Standard_Integer i = 1; i <= nb; i ++) {
434 cout<<" "<<i<<" "<<session->Command(i)<<endl;
435 }
436 break;
437 }
438 case 'r' : { // **** command record
439 Standard_Boolean mode = session->RecordMode();
440 if (mode) cout << " -- Record Mode a present Inactif" <<endl;
441 else cout << " -- Record Mode a present Actif" <<endl;
442 session->SetRecordMode(!mode);
443 break;
444 }
445 default : cout << "Option de controle de commande non comprise"<<endl;
446 }
447 return IFSelect_RetVoid;
448 }
449
450 case 4 : { // **** FILE
451 if (argc < 2) { cout<<"Donner nom de fichier"<<endl; return IFSelect_RetError; }
452 return session->ReadScript
453 (TCollection_AsciiString(session->Word(1)).ToCString());
454// On recopie la string parce que Word(1) change tout le temps !
455 }
456
457 case 5 : { // **** XSTEP
458 if (argc < 2) {
459 cout<<"xstep : prefixe neutre pour toute commande xstep-draw"<<endl
460 <<"xstep command args equivaut a command args"<<endl;
461 return Do(2,this);
462 } else {
463 RemoveWord(0);
464 return Perform();
465 }
466 }
467 case 6 : { // **** XSNEW(variable)
468 if (argc < 3) {
469 cout<<"xsnew nomvar command [args] creates an item"<<endl
470 <<" nomvar : name of item (must be a new name) in the session"<<endl;
471 return Do (-1,this);
472 } else {
473
474 theobjrec.Nullify();
475 TCollection_AsciiString name = Word(1);
476// Le nom ne doit pas etre deja pris !
477 if (thesession.IsNull()) { cout<<"Command with a Name and no Session defined !"<<endl; return IFSelect_RetFail; }
478////// if (thesession->NameIdent(thewords(0).ToCString()) > 0)
479////// { cout<<"Command with name:"<<thewords(0)<<", already taken"<<endl; return IFSelect_RetFail; }
480 RemoveWord(0); RemoveWord(0);
481
482// Commande pour un Acteur
483 Handle(IFSelect_Activator) actor; Standard_Integer num;
484 if (IFSelect_Activator::Select(thewords(0).ToCString(),num,actor)) {
485 theobjrec.Nullify();
486 stat = actor->Do(num,this);
487// Prise en compte des commandes a resultat
488 if (!theobjrec.IsNull()) {
489 thesession->RemoveItem(theobjrec); //// depannage ?
490 Standard_Boolean addws =
491 thesession->AddNamedItem(name.ToCString(),theobjrec);
492 theobjrec.Nullify();
493 if (!addws) { cout<<"Could not add named item:"<<name<<", sorry"<<endl; return IFSelect_RetFail; }
494 }
495 else cout<<"Remark : xsnew with name:"<<name<<" and no result"<<endl;
496
497 return stat;
498 }
499 cout << " Command : " << thewords(0) << " unknown" << endl;
500 return IFSelect_RetError; // pas reconnu donc incorrect
501 }
502 }
503 default : return IFSelect_RetError;
504 }
505}
506
507
508
509 Standard_CString IFSelect_SessionPilot::Help
510 (const Standard_Integer number) const
511{
512 switch (number) {
513 case 1 : return "exit ou x : Fin de session";
514 case 2 : return "Liste les commandes. ? <titre> : commandes debutant par <titre>";
515 case 3 : return "controle de commande. command tout court pour help complet";
516 case 4 : return "lit les commandes depuis un fichier";
517 case 5 : return "prefixe neutre pour xstep-draw";
518 case 6 : return "creation item : donner nom_item puis commande args";
519 default : return "";
520 }
521}