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