1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License 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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
15 #include <Interface_Check.hxx>
16 #include <Message.hxx>
17 #include <Message_Messenger.hxx>
18 #include <Message_Msg.hxx>
19 #include <Standard_OutOfRange.hxx>
20 #include <Standard_Transient.hxx>
21 #include <Standard_Type.hxx>
22 #include <TCollection_HAsciiString.hxx>
24 IMPLEMENT_STANDARD_RTTIEXT(Interface_Check,Standard_Transient)
26 // Un Check est cree au depart sans liste de message : celle ci est creee
27 // seulement si au moins une erreur doit y etre enregitree (Fail-Warning)
28 //=======================================================================
29 //function : Interface_Check
31 //=======================================================================
32 Interface_Check::Interface_Check()
37 //=======================================================================
38 //function : Interface_Check
40 //=======================================================================
42 Interface_Check::Interface_Check(const Handle(Standard_Transient)& anentity)
47 // .... Ajout de message d Erreur vraie (Fail)
50 //=======================================================================
53 //=======================================================================
55 void Interface_Check::SendFail(const Message_Msg& amsg)
61 //=======================================================================
64 //=======================================================================
66 void Interface_Check::AddFail (const Handle(TCollection_HAsciiString)& mess)
68 if (thefails.IsNull()) thefails = new TColStd_HSequenceOfHAsciiString();
69 if (thefailo.IsNull()) thefailo = new TColStd_HSequenceOfHAsciiString();
70 thefails->Append(mess); thefailo->Append(mess);
74 //=======================================================================
77 //=======================================================================
79 void Interface_Check::AddFail(const Handle(TCollection_HAsciiString)& mess,
80 const Handle(TCollection_HAsciiString)& orig)
82 if (thefails.IsNull()) thefails = new TColStd_HSequenceOfHAsciiString();
83 if (thefailo.IsNull()) thefailo = new TColStd_HSequenceOfHAsciiString();
84 thefails->Append(mess); thefailo->Append(orig);
88 //=======================================================================
91 //=======================================================================
93 void Interface_Check::AddFail(const Standard_CString amess,
94 const Standard_CString orig)
96 if (amess[0] == '\0') return;
97 if (!orig || orig[0] == '\0') AddFail (new TCollection_HAsciiString(amess));
98 else AddFail (new TCollection_HAsciiString(amess),
99 new TCollection_HAsciiString(orig) );
103 //=======================================================================
106 //=======================================================================
108 void Interface_Check::AddFail(const Message_Msg& amsg)
110 if (amsg.IsEdited()) AddFail (new TCollection_HAsciiString(TCollection_AsciiString(amsg.Value())),
111 new TCollection_HAsciiString(TCollection_AsciiString(amsg.Original())));
112 else AddFail (new TCollection_HAsciiString(TCollection_AsciiString(amsg.Value())));
116 //=======================================================================
117 //function : HasFailed
119 //=======================================================================
121 Standard_Boolean Interface_Check::HasFailed () const
123 return (!thefails.IsNull());
127 //=======================================================================
130 //=======================================================================
132 Standard_Integer Interface_Check::NbFails () const
134 return (thefails.IsNull() ? 0 : thefails->Length());
138 //=======================================================================
141 //=======================================================================
143 const Handle(TCollection_HAsciiString)& Interface_Check::Fail
144 (const Standard_Integer num, const Standard_Boolean final) const
146 if (thefails.IsNull()) throw Standard_OutOfRange();
147 return (final ? thefails->Value(num) : thefailo->Value(num));
151 //=======================================================================
154 //=======================================================================
156 Standard_CString Interface_Check::CFail(const Standard_Integer num,
157 const Standard_Boolean final) const
159 return Fail(num,final)->ToCString();
163 //=======================================================================
166 //=======================================================================
168 Handle(TColStd_HSequenceOfHAsciiString) Interface_Check::Fails
169 (const Standard_Boolean final) const
171 if (thefails.IsNull()) return new TColStd_HSequenceOfHAsciiString();
172 return (final ? thefails : thefailo);
175 // .... Ajout de message de Warning
178 //=======================================================================
179 //function : SendWarning
181 //=======================================================================
183 void Interface_Check::SendWarning(const Message_Msg& amsg)
189 //=======================================================================
190 //function : AddWarning
192 //=======================================================================
194 void Interface_Check::AddWarning (const Handle(TCollection_HAsciiString)& mess)
196 if (thewarns.IsNull()) thewarns = new TColStd_HSequenceOfHAsciiString();
197 if (thewarno.IsNull()) thewarno = new TColStd_HSequenceOfHAsciiString();
198 thewarns->Append(mess); thewarno->Append(mess);
202 //=======================================================================
203 //function : AddWarning
205 //=======================================================================
207 void Interface_Check::AddWarning(const Handle(TCollection_HAsciiString)& mess,
208 const Handle(TCollection_HAsciiString)& orig)
210 if (thewarns.IsNull()) thewarns = new TColStd_HSequenceOfHAsciiString();
211 if (thewarno.IsNull()) thewarno = new TColStd_HSequenceOfHAsciiString();
212 thewarns->Append(mess); thewarno->Append(orig);
216 //=======================================================================
217 //function : AddWarning
219 //=======================================================================
221 void Interface_Check::AddWarning(const Standard_CString amess,
222 const Standard_CString orig)
224 if (amess[0] == '\0') return;
225 if (!orig || orig[0] == '\0') AddWarning (new TCollection_HAsciiString(amess));
226 else AddWarning (new TCollection_HAsciiString(amess),
227 new TCollection_HAsciiString(orig) );
231 //=======================================================================
232 //function : AddWarning
234 //=======================================================================
236 void Interface_Check::AddWarning(const Message_Msg& amsg)
238 if (amsg.IsEdited()) AddWarning (new TCollection_HAsciiString(TCollection_AsciiString(amsg.Value())),
239 new TCollection_HAsciiString(TCollection_AsciiString(amsg.Original())));
240 else AddWarning (new TCollection_HAsciiString(TCollection_AsciiString(amsg.Value())));
244 //=======================================================================
245 //function : HasWarnings
247 //=======================================================================
249 Standard_Boolean Interface_Check::HasWarnings () const
251 return (!thewarns.IsNull());
255 //=======================================================================
256 //function : NbWarnings
258 //=======================================================================
260 Standard_Integer Interface_Check::NbWarnings () const
262 return (thewarns.IsNull() ? 0 : thewarns->Length());
266 //=======================================================================
269 //=======================================================================
271 const Handle(TCollection_HAsciiString)& Interface_Check::Warning
272 (const Standard_Integer num, const Standard_Boolean final) const
274 if (thewarns.IsNull()) throw Standard_OutOfRange();
275 return (final ? thewarns->Value(num) : thewarno->Value(num));
279 //=======================================================================
280 //function : CWarning
282 //=======================================================================
284 Standard_CString Interface_Check::CWarning(const Standard_Integer num,
285 const Standard_Boolean final) const
287 return Warning(num,final)->ToCString();
291 //=======================================================================
292 //function : Warnings
294 //=======================================================================
296 Handle(TColStd_HSequenceOfHAsciiString) Interface_Check::Warnings
297 (const Standard_Boolean final) const
299 if (thewarns.IsNull()) return new TColStd_HSequenceOfHAsciiString();
300 return (final ? thewarns : thewarno);
304 // .... Ajout de message d Info simple (not yet completed)
307 //=======================================================================
310 //=======================================================================
312 void Interface_Check::SendMsg (const Message_Msg& amsg)
314 Handle(TCollection_HAsciiString) mess =
315 new TCollection_HAsciiString (amsg.Value());
316 Handle(TCollection_HAsciiString) orig = mess;
317 if (amsg.IsEdited()) orig = new TCollection_HAsciiString (amsg.Original());
319 if (theinfos.IsNull()) theinfos = new TColStd_HSequenceOfHAsciiString();
320 if (theinfoo.IsNull()) theinfoo = new TColStd_HSequenceOfHAsciiString();
321 theinfos->Append(mess); theinfoo->Append(mess);
325 //=======================================================================
326 //function : NbInfoMsgs
328 //=======================================================================
330 Standard_Integer Interface_Check::NbInfoMsgs () const
332 return (theinfos.IsNull() ? 0 : theinfos->Length());
336 //=======================================================================
339 //=======================================================================
341 const Handle(TCollection_HAsciiString)& Interface_Check::InfoMsg
342 (const Standard_Integer num, const Standard_Boolean final) const
344 if (theinfos.IsNull()) throw Standard_OutOfRange();
345 return (final ? theinfos->Value(num) : theinfoo->Value(num));
349 //=======================================================================
350 //function : CInfoMsg
352 //=======================================================================
354 Standard_CString Interface_Check::CInfoMsg
355 (const Standard_Integer num, const Standard_Boolean final) const
357 return InfoMsg(num,final)->ToCString();
361 //=======================================================================
362 //function : InfoMsgs
364 //=======================================================================
366 Handle(TColStd_HSequenceOfHAsciiString) Interface_Check::InfoMsgs
367 (const Standard_Boolean final) const
369 if (theinfos.IsNull()) return new TColStd_HSequenceOfHAsciiString();
370 return (final ? theinfos : theinfoo);
374 // .... Gestion generale
377 //=======================================================================
380 //=======================================================================
382 Interface_CheckStatus Interface_Check::Status () const
384 if (!thefails.IsNull()) return Interface_CheckFail;
385 if (!thewarns.IsNull()) return Interface_CheckWarning;
386 return Interface_CheckOK;
390 //=======================================================================
391 //function : Complies
393 //=======================================================================
395 Standard_Boolean Interface_Check::Complies(const Interface_CheckStatus status) const
397 Standard_Boolean now = thewarns.IsNull();
398 Standard_Boolean nof = thefails.IsNull();
400 case Interface_CheckOK : return ( nof && now);
401 case Interface_CheckWarning : return ( nof && !now);
402 case Interface_CheckFail : return !nof;
403 case Interface_CheckAny : return Standard_True;
404 case Interface_CheckMessage : return (!nof || !now);
405 case Interface_CheckNoFail : return nof;
408 return Standard_False;
412 //=======================================================================
413 //function : Complies
415 //=======================================================================
417 Standard_Boolean Interface_Check::Complies(const Handle(TCollection_HAsciiString)& mess,
418 const Standard_Integer incl,
419 const Interface_CheckStatus status) const
421 if (mess.IsNull()) return Standard_False;
422 Standard_Integer lng = mess->Length();
423 if (status == Interface_CheckWarning || status == Interface_CheckAny) {
424 Standard_Integer i, nb = NbWarnings();
425 for (i = nb; i > 0; i --) {
426 Handle(TCollection_HAsciiString) ames = Warning(i);
427 if (incl == 0) { if (mess->IsSameString (ames)) return Standard_True; }
428 if (incl < 0) { if (ames->Location(mess,1,ames->Length()) > 0) return Standard_True; }
429 if (incl < 0) { if (mess->Location(mess,1,lng) > 0) return Standard_True; }
432 if (status == Interface_CheckFail || status == Interface_CheckAny) {
433 Standard_Integer i, nb = NbWarnings();
434 for (i = nb; i > 0; i --) {
435 Handle(TCollection_HAsciiString) ames = Warning(i);
436 if (incl == 0) { if (mess->IsSameString (ames)) return Standard_True; }
437 if (incl < 0) { if (ames->Location(mess,1,ames->Length()) > 0) return Standard_True; }
438 if (incl < 0) { if (mess->Location(mess,1,lng) > 0) return Standard_True; }
441 return Standard_False;
445 //=======================================================================
446 //function : HasEntity
448 //=======================================================================
450 Standard_Boolean Interface_Check::HasEntity () const
452 return (!theent.IsNull());
456 //=======================================================================
459 //=======================================================================
461 const Handle(Standard_Transient)& Interface_Check::Entity () const
467 //=======================================================================
470 //=======================================================================
472 void Interface_Check::Clear ()
474 thefails.Nullify(); thefailo.Nullify();
475 thewarns.Nullify(); thewarno.Nullify();
476 theinfos.Nullify(); theinfoo.Nullify();
481 //=======================================================================
482 //function : ClearFails
484 //=======================================================================
486 void Interface_Check::ClearFails ()
493 //=======================================================================
494 //function : ClearWarnings
496 //=======================================================================
498 void Interface_Check::ClearWarnings ()
505 //=======================================================================
506 //function : ClearInfoMsgs
508 //=======================================================================
510 void Interface_Check::ClearInfoMsgs ()
518 //=======================================================================
521 //=======================================================================
523 Standard_Boolean Interface_Check::Remove(const Handle(TCollection_HAsciiString)& mess,
524 const Standard_Integer incl,
525 const Interface_CheckStatus status)
527 if (mess.IsNull()) return Standard_False;
528 Standard_Boolean res = Standard_False;
529 Standard_Integer lng = mess->Length();
530 if (status == Interface_CheckWarning || status == Interface_CheckAny) {
531 Standard_Integer i, nb = NbWarnings();
532 for (i = nb; i > 0; i --) {
533 Standard_Boolean rem = Standard_False;
534 Handle(TCollection_HAsciiString) ames = Warning(i);
535 if (incl == 0) rem = mess->IsSameString (ames);
536 if (incl < 0) rem = (ames->Location(mess,1,ames->Length()) > 0);
537 if (incl < 0) rem = (mess->Location(mess,1,lng) > 0);
538 if (rem) { thewarns->Remove(i); thewarno->Remove(i); res = rem; }
541 if (status == Interface_CheckFail || status == Interface_CheckAny) {
542 Standard_Integer i, nb = NbWarnings();
543 for (i = nb; i > 0; i --) {
544 Standard_Boolean rem = Standard_False;
545 Handle(TCollection_HAsciiString) ames = Warning(i);
546 if (incl == 0) rem = mess->IsSameString (ames);
547 if (incl < 0) rem = (ames->Location(mess,1,ames->Length()) > 0);
548 if (incl < 0) rem = (mess->Location(mess,1,lng) > 0);
549 if (rem) { thefails->Remove(i); thefailo->Remove(i); res = rem; }
556 //=======================================================================
559 //=======================================================================
561 Standard_Boolean Interface_Check::Mend(const Standard_CString pref,
562 const Standard_Integer num)
564 Standard_Integer i, n1 = num, n2 = num;
565 if (pref && pref[2] == '\0') {
566 if (pref[0] == 'F' && pref[1] == 'M') return Mend ("Mended",num);
567 if (pref[0] == 'C' && pref[1] == 'A') { Clear(); return Standard_True; }
568 if (pref[0] == 'C' && pref[1] == 'W') {
569 if (num == 0) { ClearWarnings(); return Standard_True; }
570 if (num < 0 || num > NbWarnings()) return Standard_False;
571 thewarns->Remove(num); thewarno->Remove(num);
572 return Standard_True;
574 if (pref[0] == 'C' && pref[1] == 'F') {
575 if (num == 0) { ClearFails(); return Standard_True; }
576 if (num < 0 || num > NbFails()) return Standard_False;
577 thefails->Remove(num); thefailo->Remove(num);
578 return Standard_True;
583 n1 = 1; n2 = NbFails();
584 for (i = n2; i >= n1; i --) Mend (pref,i);
585 return Standard_True;
587 else if (num < 0 || num > NbFails()) return Standard_False;
589 Handle(TCollection_HAsciiString) strf = thefails->Value(num);
590 Handle(TCollection_HAsciiString) stro = thefailo->Value(num);
591 if (pref && pref[0] != '\0') {
592 strf->Insert (1," : "); strf->Insert (1,pref);
593 if (stro != strf) { stro->Insert (1," : "); stro->Insert (1,pref); }
595 thefails->Remove(num); thefailo->Remove(num);
596 if (stro == strf) AddFail (strf);
597 else AddFail (strf,stro);
598 return Standard_True;
602 //=======================================================================
603 //function : SetEntity
605 //=======================================================================
607 void Interface_Check::SetEntity(const Handle(Standard_Transient)& anentity)
613 //=======================================================================
614 //function : GetEntity
616 //=======================================================================
618 void Interface_Check::GetEntity(const Handle(Standard_Transient)& anentity)
620 if (theent.IsNull()) SetEntity(anentity);
623 // .. GetMessages, reprend les messages en les cumulant aux siens propres
624 // .. GetAsWarning, reprend les messages en les cumulant et en les
625 // considerant tous comme "Warning" . En outre, selon <failsonly>
626 // failsonly True : ne pas reprendre les Warnings originaux
627 // failsonly False : les prendre aussi
630 //=======================================================================
631 //function : GetMessages
633 //=======================================================================
635 void Interface_Check::GetMessages (const Handle(Interface_Check)& other)
637 Standard_Integer nb,i;
638 if ( (nb = other->NbFails()) != 0) {
639 if (thefails.IsNull()) thefails = new TColStd_HSequenceOfHAsciiString();
640 if (thefailo.IsNull()) thefailo = new TColStd_HSequenceOfHAsciiString();
641 for (i = 1; i <= nb; i ++) thefails->Append(other->Fail(i,Standard_True));
642 for (i = 1; i <= nb; i ++) thefailo->Append(other->Fail(i,Standard_False));
644 if ( (nb = other->NbWarnings()) != 0) {
645 if (thewarns.IsNull()) thewarns = new TColStd_HSequenceOfHAsciiString();
646 if (thewarno.IsNull()) thewarno = new TColStd_HSequenceOfHAsciiString();
647 for (i = 1; i <= nb; i ++) thewarns->Append(other->Warning(i,Standard_True));
648 for (i = 1; i <= nb; i ++) thewarno->Append(other->Warning(i,Standard_False));
650 if ( (nb = other->NbInfoMsgs()) != 0) {
651 if (theinfos.IsNull()) theinfos = new TColStd_HSequenceOfHAsciiString();
652 if (theinfoo.IsNull()) theinfoo = new TColStd_HSequenceOfHAsciiString();
653 for (i = 1; i <= nb; i ++) theinfos->Append(other->InfoMsg(i,Standard_True));
654 for (i = 1; i <= nb; i ++) theinfoo->Append(other->InfoMsg(i,Standard_False));
659 //=======================================================================
660 //function : GetAsWarning
662 //=======================================================================
664 void Interface_Check::GetAsWarning(const Handle(Interface_Check)& other,
665 const Standard_Boolean failsonly)
667 Standard_Integer nb,i;
668 if ( (nb = other->NbFails()) != 0) {
669 if (thewarns.IsNull()) thewarns = new TColStd_HSequenceOfHAsciiString();
670 if (thewarno.IsNull()) thewarno = new TColStd_HSequenceOfHAsciiString();
671 for (i = 1; i <= nb; i ++) thewarns->Append(other->Fail(i,Standard_True));
672 for (i = 1; i <= nb; i ++) thewarno->Append(other->Fail(i,Standard_False));
674 if ( (nb = other->NbWarnings()) != 0 || !failsonly) {
675 if (thewarns.IsNull()) thewarns = new TColStd_HSequenceOfHAsciiString();
676 if (thewarno.IsNull()) thewarno = new TColStd_HSequenceOfHAsciiString();
677 for (i = 1; i <= nb; i ++) thewarns->Append(other->Warning(i,Standard_True));
678 for (i = 1; i <= nb; i ++) thewarno->Append(other->Warning(i,Standard_False));
683 //=======================================================================
686 //=======================================================================
688 void Interface_Check::Print(Standard_OStream& S, const Standard_Integer level,
689 const Standard_Integer final) const
691 Standard_Integer j, nb = NbFails();
695 // if (nb > 0) S<<" --> Fails : "<<nb<<std::endl;
696 for (j = 1; j <= nb; j ++) {
698 S <<CFail(j,Standard_True)<<"\n";
700 S <<CFail(j,Standard_False)<<"\n";
702 // if (Fail(j,Standard_True) != Fail(j,Standard_False))
703 // S<<"[original:"<<CFail(j,Standard_False)<<"]\n";
709 // if (nb > 0) S<<" --> Warnings : "<<nb<<std::endl;
710 for (j = 1; j <= nb; j ++) {
712 S <<CWarning(j,Standard_True)<<"\n";
714 S <<CWarning(j,Standard_False)<<"\n";
716 // if (Warning(j,Standard_True) != Warning(j,Standard_False))
717 // S<<"[original:"<<CWarning(j,Standard_False)<<"]\n";
721 // InfoMsg : regle causant (user message)
724 // if (nb > 0) S<<" --> Infos : "<<nb<<std::endl;
725 for (j = 1; j <= nb; j ++) {
727 S <<CInfoMsg(j,Standard_True)<<"\n";
729 S <<CInfoMsg(j,Standard_False)<<"\n";
731 // if (InfoMsg(j,Standard_True) != InfoMsg(j,Standard_False))
732 // S<<"[original:"<<CInfoMsg(j,Standard_False)<<"]\n";
739 //=======================================================================
742 //=======================================================================
744 void Interface_Check::Trace(const Standard_Integer level,
745 const Standard_Integer final) const
747 Message_Messenger::StreamBuffer sout = Message::SendInfo(); // shouldn't it be changed to SendTrace()?
748 Print (sout, level, final);