0024830: Remove redundant keyword 'mutable' in CDL declarations
[occt.git] / src / IFSelect / IFSelect_ShareOut.cxx
1 // Copyright (c) 1999-2014 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
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.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #include <IFSelect_ShareOut.ixx>
15 #include <Interface_InterfaceError.hxx>
16 #include <Interface_Macros.hxx>
17 #include <stdio.h>
18
19
20 IFSelect_ShareOut::IFSelect_ShareOut ()
21 {
22   thedefrt  = new TCollection_HAsciiString ("Default");
23   thenbdefs = thelastrun = 0;
24 }
25
26
27     void  IFSelect_ShareOut::Clear (const Standard_Boolean onlydisp)
28 {
29   thedisps.Clear();
30   ClearResult(!onlydisp);
31   if (onlydisp) return;
32   themodelmodifiers.Clear();
33   thefilemodifiers.Clear();
34 }
35
36     Standard_Boolean  IFSelect_ShareOut::RemoveItem
37   (const Handle(Standard_Transient)& item)
38 {
39   DeclareAndCast(IFSelect_GeneralModifier,modifier,item);
40   if (!modifier.IsNull()) {
41     Standard_Boolean formodel = modifier->IsKind(STANDARD_TYPE(IFSelect_Modifier));
42     Standard_Integer atnum = ModifierRank(modifier);
43     return RemoveModifier (formodel,atnum);
44   }
45   DeclareAndCast(IFSelect_Dispatch,disp,item);
46   if (!disp.IsNull()) {
47     Standard_Integer atnum = DispatchRank(disp);
48     return RemoveDispatch(atnum);
49   }
50   return Standard_False;
51 }
52
53
54     void  IFSelect_ShareOut::ClearResult (const Standard_Boolean alsoname)
55 {
56   thelastrun = 0;
57   if (alsoname) thenbdefs = 0;
58 }
59
60     Standard_Integer  IFSelect_ShareOut::LastRun () const
61       {  return thelastrun;  }
62
63     void  IFSelect_ShareOut::SetLastRun (const Standard_Integer lastrun)
64       {  thelastrun = lastrun;  }
65
66 //  #######################################################################
67 //  ####                DISPATCHES (ENVOI DES FICHIERS)                ####
68
69     Standard_Integer  IFSelect_ShareOut::NbDispatches () const 
70       {  return thedisps.Length();  }
71
72     Standard_Integer  IFSelect_ShareOut::DispatchRank
73   (const Handle(IFSelect_Dispatch)& disp) const 
74 {
75   if (disp.IsNull()) return 0;
76   for (Standard_Integer i = thedisps.Length(); i >= 1; i --)
77     if (disp == thedisps.Value(i)) return i;
78   return 0;
79 }
80
81     const Handle(IFSelect_Dispatch)&  IFSelect_ShareOut::Dispatch
82   (const Standard_Integer num) const 
83 {
84   return thedisps.Value(num);
85 }
86
87     void  IFSelect_ShareOut::AddDispatch
88   (const Handle(IFSelect_Dispatch)& disp)
89 {
90   if (disp.IsNull()) return;
91   thedisps.Append(disp);
92 }
93
94
95     Standard_Boolean  IFSelect_ShareOut::RemoveDispatch
96   (const Standard_Integer rank)
97 {
98   if (rank <= thelastrun || rank > thedisps.Length()) return Standard_False;
99   thedisps.Remove(rank);
100   return Standard_True;
101 }
102
103 //  ##########################################################################
104 //  ####                            MODIFIERS                             ####
105
106     void  IFSelect_ShareOut::AddModifier
107   (const Handle(IFSelect_GeneralModifier)& modifier,
108    const Standard_Integer atnum)
109 {
110   Standard_Boolean formodel = modifier->IsKind(STANDARD_TYPE(IFSelect_Modifier));
111   if (ModifierRank(modifier) == 0)  AddModif (modifier,formodel,atnum);
112   Handle(IFSelect_Dispatch) nuldisp;
113   modifier->SetDispatch(nuldisp);
114 }
115
116     void  IFSelect_ShareOut::AddModifier
117   (const Handle(IFSelect_GeneralModifier)& modifier,
118    const Standard_Integer dispnum, const Standard_Integer atnum)
119 {
120   Standard_Boolean formodel = modifier->IsKind(STANDARD_TYPE(IFSelect_Modifier));
121   if (ModifierRank(modifier) == 0)  AddModif (modifier,formodel,atnum);
122   Handle(IFSelect_Dispatch) disp = Dispatch(dispnum);
123   modifier->SetDispatch(disp);
124 }
125
126
127     void  IFSelect_ShareOut::AddModif
128   (const Handle(IFSelect_GeneralModifier)& modifier,
129    const Standard_Boolean formodel, const Standard_Integer atnum)
130 {
131   if (formodel) {
132     if (atnum > 0 && atnum <= themodelmodifiers.Length())
133       themodelmodifiers.InsertBefore(atnum,modifier);
134     else themodelmodifiers.Append(modifier);
135   } else {
136     if (atnum > 0 && atnum <= thefilemodifiers.Length())
137       thefilemodifiers.InsertBefore(atnum,modifier);
138     else thefilemodifiers.Append(modifier);
139   }
140 }
141
142     Standard_Integer  IFSelect_ShareOut::NbModifiers
143   (const Standard_Boolean formodel) const 
144 {
145   if (formodel) return themodelmodifiers.Length();
146   else          return thefilemodifiers.Length();
147 }
148
149     Handle(IFSelect_GeneralModifier)  IFSelect_ShareOut::GeneralModifier
150   (const Standard_Boolean formodel, const Standard_Integer atnum) const
151 {
152   if (formodel) return themodelmodifiers.Value(atnum);
153   else          return thefilemodifiers.Value(atnum);
154 }
155
156     Handle(IFSelect_Modifier)  IFSelect_ShareOut::ModelModifier
157   (const Standard_Integer num) const 
158 {  return Handle(IFSelect_Modifier)::DownCast(themodelmodifiers.Value(num));  }
159
160     Standard_Integer  IFSelect_ShareOut::ModifierRank
161   (const Handle(IFSelect_GeneralModifier)& modifier) const 
162 {
163   Standard_Integer i;
164   Standard_Boolean formodel = modifier->IsKind(STANDARD_TYPE(IFSelect_Modifier));
165   if (formodel) {
166     for (i = themodelmodifiers.Length(); i >= 1; i --)
167       if (modifier == themodelmodifiers.Value(i)) return i;
168   } else {
169     for (i = thefilemodifiers.Length(); i >= 1; i --)
170       if (modifier == thefilemodifiers.Value(i)) return i;
171   }
172   return 0;
173 }
174
175
176     Standard_Boolean  IFSelect_ShareOut::RemoveModifier
177   (const Standard_Boolean formodel, const Standard_Integer atnum)
178 {
179   if (atnum <= 0) return Standard_False;
180   if (formodel) {
181     if (atnum > themodelmodifiers.Length()) return Standard_False;
182     themodelmodifiers.Remove(atnum);
183   } else {
184     if (atnum > thefilemodifiers.Length()) return Standard_False;
185     thefilemodifiers.Remove(atnum);
186   }
187   return Standard_True;
188 }
189
190
191 //    ChangeModifierRank revient a une permutation circulaire :
192 //    before est mis en after, ceux qui sont entre tournent
193     Standard_Boolean    IFSelect_ShareOut::ChangeModifierRank
194   (const Standard_Boolean formodel,
195    const Standard_Integer before,   const Standard_Integer after)
196 {
197   Standard_Integer nb;
198   if (before <= 0 || after <= 0) return Standard_False;
199   if (before == after) return Standard_True;
200   if (formodel) {
201     nb = themodelmodifiers.Length();
202     if (before > nb || after > nb) return Standard_False;
203     Handle(IFSelect_GeneralModifier) bef = themodelmodifiers.Value(before);
204     themodelmodifiers.Remove(before);
205     if (after == nb) themodelmodifiers.Append(bef);
206     else             themodelmodifiers.InsertBefore(after,bef);
207   } else {
208     nb = thefilemodifiers.Length();
209     if (before > nb || after > nb) return Standard_False;
210     Handle(IFSelect_GeneralModifier) bef = thefilemodifiers.Value(before);
211     thefilemodifiers.Remove(before);
212     if (after == nb) thefilemodifiers.Append(bef);
213     else             thefilemodifiers.InsertBefore(after,bef);
214   }
215   return Standard_True;
216 }
217
218 //  #######################################################################
219 //  ####                    NOMINATION DES FICHIERS                    ####
220 //  Rq : thenbdefs s applique tant que l on ne change pas les termes principaux
221
222     Standard_Boolean  IFSelect_ShareOut::SetRootName
223   (const Standard_Integer num, const Handle(TCollection_HAsciiString)& name)
224 {
225   if (num < 1 || num > thedisps.Length()) return Standard_False;
226   if (RootNumber(name) != 0) return Standard_False;
227   Dispatch(num)->SetRootName (name);
228   return Standard_True;
229 }
230
231     Standard_Boolean  IFSelect_ShareOut::HasRootName
232   (const Standard_Integer num) const
233 {
234   if (num < 1 || num > thedisps.Length()) return Standard_False;
235   return Dispatch(num)->HasRootName();
236 }
237
238     Handle(TCollection_HAsciiString)  IFSelect_ShareOut::RootName
239   (const Standard_Integer num) const
240 {
241   Handle(TCollection_HAsciiString) nulname;
242   if (num < 1 || num > thedisps.Length()) return nulname;
243   return Dispatch(num)->RootName();
244 }
245
246     Standard_Integer  IFSelect_ShareOut::RootNumber
247   (const Handle(TCollection_HAsciiString)& name) const
248 {
249   if (name.IsNull()) return 0;
250   if (!thedefrt.IsNull()) {
251     if (thedefrt->IsSameString(name)) return -1;
252   }
253   for (Standard_Integer i = 1; i <= thedisps.Length(); i ++) {
254     Handle(TCollection_HAsciiString) root = thedisps.Value(i)->RootName();
255     if (root.IsNull()) continue;
256     if (root->IsSameString(name)) return i;
257   }
258   return 0;
259 }
260
261
262     void  IFSelect_ShareOut::SetPrefix
263   (const Handle(TCollection_HAsciiString)& pref)
264       {  thepref = pref;  thenbdefs = 0;  }
265
266     Standard_Boolean  IFSelect_ShareOut::SetDefaultRootName
267   (const Handle(TCollection_HAsciiString)& defrt)
268 {
269   if (RootNumber(defrt) != 0) return Standard_False;
270   if (thedefrt.IsNull() || !thedefrt->IsSameString(defrt)) thenbdefs = 0;
271   thedefrt = defrt;
272   return Standard_True;
273 }
274
275     void  IFSelect_ShareOut::SetExtension
276   (const Handle(TCollection_HAsciiString)& ext)
277       {  theext = ext;  thenbdefs = 0;  }
278
279     Handle(TCollection_HAsciiString)  IFSelect_ShareOut::Prefix () const 
280 {
281   if (thepref.IsNull()) return new TCollection_HAsciiString("");
282   return thepref;
283 }
284
285     Handle(TCollection_HAsciiString)  IFSelect_ShareOut::DefaultRootName () const
286 {
287   if (thedefrt.IsNull()) return new TCollection_HAsciiString("");
288   return thedefrt;
289 }
290
291     Handle(TCollection_HAsciiString)  IFSelect_ShareOut::Extension () const 
292 {
293   if (theext.IsNull()) return new TCollection_HAsciiString("");
294   return theext;
295 }
296
297
298     TCollection_AsciiString  IFSelect_ShareOut::FileName
299   (const Standard_Integer dnum, const Standard_Integer pnum,
300    const Standard_Integer nbpack)
301 {
302   Handle(TCollection_HAsciiString) rot = RootName(dnum);
303   Standard_Integer num  = pnum;
304   Standard_Integer npac = nbpack;
305   Standard_Boolean sufnum = (npac > 1 || num > 1);
306   if (rot.IsNull()) {
307     rot = thedefrt;
308     thenbdefs ++;  num = thenbdefs;
309     npac = 0;
310     sufnum = Standard_True; // numeroter sur noms par defaut, des le 1er sans 0
311   }
312
313   TCollection_AsciiString res;
314   if (!thepref.IsNull()) res.AssignCat (thepref->ToCString());
315   if (!rot.IsNull())     res.AssignCat (rot->ToCString());
316
317 //  Suffixe numerique
318   if (sufnum) {    // sinon, pas de suffixe numerique
319 //  Nom du PacketSuffix : _ suivi du numero <num>
320 //  Si nbpack non nul, alors on a un majorant et on peut preceder de zeros
321 //  Ex.: nbpack = 50 (donc 2 chiffres), num = 3, cela donnera _03
322 //  idnum pas utilise : cette methode peut etre redefinie et utiliser idnum ...
323 //  Si nbpack = 0 ou 1, num = 1 pas de suffixe, sinon suffixe "_num" tel quel
324 //  MODIF du 3-NOV-1995 -> pour eviter toute confusion, num = 1 donne aussi _1
325     Standard_Integer nbch = 0;
326     char format[10],suffixe[30];  format[1] = ' ';
327     if (npac >= num) {
328       Standard_Integer nbpa = 1;
329       while (nbpa <= npac)  {  nbpa *= 10; nbch ++;  }
330     }
331     if (nbch > 1) {
332       sprintf(format,"_ %d.%dd",nbch,nbch);
333       format[1] = '%';
334     } else if (npac >= num || num >= 1) {
335       sprintf(format,"_ d");
336       format[1] = '%';
337     }
338     if (format[1] == '%') {
339       sprintf (suffixe,format,num);
340       res.AssignCat (suffixe);
341     }
342   }
343
344   if (!theext.IsNull())  res.AssignCat (theext->ToCString());
345   return res;
346 }