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