0023947: Eliminate trivial compiler warnings in MSVC++ with warning level 4
[occt.git] / src / XmlMDataStd / XmlMDataStd_IntegerArrayDriver.cxx
1 // Created on: 2001-08-24
2 // Created by: Alexnder GRIGORIEV
3 // Copyright (c) 2001-2012 OPEN CASCADE SAS
4 //
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
9 //
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 //
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
19
20 //AGV 150202: Changed prototype XmlObjMgt::SetStringValue()
21
22 #include <XmlMDataStd_IntegerArrayDriver.ixx>
23 #include <TDataStd_IntegerArray.hxx>
24 #include <NCollection_LocalArray.hxx>
25 #include <XmlObjMgt.hxx>
26 #include <XmlMDataStd.hxx>
27
28 IMPLEMENT_DOMSTRING (FirstIndexString, "first")
29 IMPLEMENT_DOMSTRING (LastIndexString,  "last")
30 IMPLEMENT_DOMSTRING (IsDeltaOn,        "delta")
31 //=======================================================================
32 //function : XmlMDataStd_IntegerArrayDriver
33 //purpose  : Constructor
34 //=======================================================================
35
36 XmlMDataStd_IntegerArrayDriver::XmlMDataStd_IntegerArrayDriver
37                         (const Handle(CDM_MessageDriver)& theMsgDriver)
38       : XmlMDF_ADriver (theMsgDriver, NULL)
39 {}
40
41 //=======================================================================
42 //function : NewEmpty
43 //purpose  : 
44 //=======================================================================
45 Handle(TDF_Attribute) XmlMDataStd_IntegerArrayDriver::NewEmpty() const
46 {
47   return (new TDataStd_IntegerArray());
48 }
49
50 //=======================================================================
51 //function : Paste
52 //purpose  : persistent -> transient (retrieve)
53 //=======================================================================
54 Standard_Boolean XmlMDataStd_IntegerArrayDriver::Paste
55                                 (const XmlObjMgt_Persistent&  theSource,
56                                  const Handle(TDF_Attribute)& theTarget,
57                                  XmlObjMgt_RRelocationTable&  ) const
58 {
59   Standard_Integer aFirstInd, aLastInd, aValue, ind;
60   const XmlObjMgt_Element& anElement = theSource;
61
62   // Read the FirstIndex; if the attribute is absent initialize to 1
63   XmlObjMgt_DOMString aFirstIndex= anElement.getAttribute(::FirstIndexString());
64   if (aFirstIndex == NULL)
65     aFirstInd = 1;
66   else if (!aFirstIndex.GetInteger(aFirstInd)) {
67     TCollection_ExtendedString aMessageString =
68       TCollection_ExtendedString("Cannot retrieve the first index"
69                                  " for IntegerArray attribute as \"")
70         + aFirstIndex + "\"";
71     WriteMessage (aMessageString);
72     return Standard_False;
73   }
74
75   // Read the LastIndex; the attribute should be present
76   if (!anElement.getAttribute(::LastIndexString()).GetInteger(aLastInd)) {
77     TCollection_ExtendedString aMessageString =
78       TCollection_ExtendedString("Cannot retrieve the last index"
79                                  " for IntegerArray attribute as \"")
80         + aFirstIndex + "\"";
81     WriteMessage (aMessageString);
82     return Standard_False;
83   }
84
85   Handle(TDataStd_IntegerArray) anIntArray =
86     Handle(TDataStd_IntegerArray)::DownCast(theTarget);
87   anIntArray->Init(aFirstInd, aLastInd);
88
89   if(aFirstInd == aLastInd) {
90     Standard_Integer anInteger;
91     if(!XmlObjMgt::GetStringValue(anElement).GetInteger( anInteger)) {
92       TCollection_ExtendedString aMessageString =
93         TCollection_ExtendedString("Cannot retrieve integer member"
94                                    " for IntegerArray attribute as \"");
95       WriteMessage (aMessageString);
96       return Standard_False;
97     }
98     anIntArray->SetValue(aFirstInd, anInteger);
99     
100   }
101   else {
102     // Warning: check implementation of XmlObjMgt_DOMString !! For LDOM this is OK
103     Standard_CString aValueStr =
104       Standard_CString(XmlObjMgt::GetStringValue(anElement).GetString());
105     
106     for (ind = aFirstInd; ind <= aLastInd; ind++)
107     {
108       if (!XmlObjMgt::GetInteger(aValueStr, aValue)) {
109         TCollection_ExtendedString aMessageString =
110           TCollection_ExtendedString("Cannot retrieve integer member"
111                                      " for IntegerArray attribute as \"")
112             + aValueStr + "\"";
113         WriteMessage (aMessageString);
114         return Standard_False;
115       }
116       anIntArray->SetValue(ind, aValue);
117     }
118   }
119 #ifdef DEB
120   //cout << "CurDocVersion = " << XmlMDataStd::DocumentVersion() <<endl;
121 #endif
122   Standard_Boolean aDelta(Standard_False);
123   
124   if(XmlMDataStd::DocumentVersion() > 2) {
125     Standard_Integer aDeltaValue;
126     if (!anElement.getAttribute(::IsDeltaOn()).GetInteger(aDeltaValue)) 
127       {
128         TCollection_ExtendedString aMessageString =
129           TCollection_ExtendedString("Cannot retrieve the isDelta value"
130                                  " for IntegerArray attribute as \"")
131         + aDeltaValue + "\"";
132         WriteMessage (aMessageString);
133         return Standard_False;
134       } 
135     else
136       aDelta = (Standard_Boolean)aDeltaValue;
137   }
138 #ifdef DEB
139   else if(XmlMDataStd::DocumentVersion() == -1)
140     cout << "Current DocVersion field is not initialized. "  <<endl;
141 #endif
142   anIntArray->SetDelta(aDelta);
143   
144   return Standard_True;
145 }
146
147 //=======================================================================
148 //function : Paste
149 //purpose  : transient -> persistent (store)
150 //=======================================================================
151 void XmlMDataStd_IntegerArrayDriver::Paste
152                                 (const Handle(TDF_Attribute)& theSource,
153                                  XmlObjMgt_Persistent&        theTarget,
154                                  XmlObjMgt_SRelocationTable&  ) const
155 {
156   Handle(TDataStd_IntegerArray) anIntArray =
157     Handle(TDataStd_IntegerArray)::DownCast(theSource);
158   const Handle(TColStd_HArray1OfInteger)& hIntArray = anIntArray->Array();
159   const TColStd_Array1OfInteger& intArray = hIntArray->Array1();
160   Standard_Integer aL = intArray.Lower(), anU = intArray.Upper();
161
162   if (aL != 1) 
163     theTarget.Element().setAttribute(::FirstIndexString(), aL);
164   theTarget.Element().setAttribute(::LastIndexString(), anU);
165   theTarget.Element().setAttribute(::IsDeltaOn(), anIntArray->GetDelta());
166
167   // Allocation of 12 chars for each integer including the space.
168   // An example: -2 147 483 648
169   Standard_Integer iChar = 0;
170   NCollection_LocalArray<Standard_Character> str;
171   if (intArray.Length())
172     str.Allocate(12 * intArray.Length() + 1);
173
174   Standard_Integer i = aL;
175   for (;;) 
176   {
177     iChar += Sprintf(&(str[iChar]), "%d ", intArray.Value(i));
178     if (i >= anU)
179       break;
180     ++i;
181   }
182
183   if (intArray.Length())
184   {
185     // No occurrence of '&', '<' and other irregular XML characters
186     str[iChar - 1] = '\0';
187     XmlObjMgt::SetStringValue (theTarget, (Standard_Character*)str, Standard_True);
188   }
189 }