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