summaryrefslogtreecommitdiff
path: root/src/VrmlData/VrmlData_WorldInfo.cxx
blob: c0652cbc042e9c5c839bb47fedbf409ed85ed31a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// File:      VrmlData_WorldInfo.cxx
// Created:   01.08.07 08:21
// Author:    Alexander GRIGORIEV
// Copyright: Open Cascade 2007


#include <VrmlData_WorldInfo.hxx>
#include <VrmlData_Scene.hxx>
#include <VrmlData_InBuffer.hxx>

#ifdef _MSC_VER
#define _CRT_SECURE_NO_DEPRECATE
#endif

#include <stdio.h>

IMPLEMENT_STANDARD_HANDLE  (VrmlData_WorldInfo, VrmlData_Node)
IMPLEMENT_STANDARD_RTTIEXT (VrmlData_WorldInfo, VrmlData_Node)

//=======================================================================
//function : VrmlData_WorldInfo::VrmlData_WorldInfo
//purpose  : Constructor
//=======================================================================

VrmlData_WorldInfo::VrmlData_WorldInfo (const VrmlData_Scene&  theScene,
                                        const char             * theName,
                                        const char             * theTitle)
  : VrmlData_Node (theScene, theName),
    myInfo        (theScene.Allocator())
{
  SetTitle (theTitle);
}

//=======================================================================
//function : SetTitle
//purpose  : Set or modify the title.
//=======================================================================

void VrmlData_WorldInfo::SetTitle (const char * theString)
{
  if (theString == 0L)
    myTitle = 0L;
  else {
    const size_t len = strlen (theString) + 1;
    if (len == 1)
      myTitle = 0L;
    else {
      myTitle = static_cast <const char *>(Scene().Allocator()->Allocate(len));
      memcpy (const_cast<char *> (myTitle), theString, len);
    }
  }
}

//=======================================================================
//function : AddInfo
//purpose  : Add a string to the list of info strings.
//=======================================================================

void VrmlData_WorldInfo::AddInfo (const char * theString)
{
  if (theString != 0L)
    if (* theString != '\0') {
      const size_t len = strlen (theString) + 1;
      char * aStr = static_cast <char *>(Scene().Allocator()->Allocate(len));
      memcpy (aStr, theString, len);
      myInfo.Append (aStr);
    }
}

//=======================================================================
//function : Clone
//purpose  : Create a copy of this node
//=======================================================================

Handle(VrmlData_Node) VrmlData_WorldInfo::Clone
                                (const Handle(VrmlData_Node)& theOther) const
{
  Handle(VrmlData_WorldInfo) aResult =
    Handle(VrmlData_WorldInfo)::DownCast (VrmlData_Node::Clone(theOther));
  if (aResult.IsNull())
    aResult =
      new VrmlData_WorldInfo (theOther.IsNull() ? Scene() : theOther->Scene(),
                             Name());

  if (&aResult->Scene() == &Scene()) {
    aResult->myTitle = myTitle;
    aResult->myInfo  = myInfo;
  } else {
    aResult->SetTitle (myTitle);
    NCollection_List <const char *>::Iterator anIter (myInfo);
    for (; anIter.More(); anIter.Next())
      aResult->AddInfo (anIter.Value());
  }
  return aResult;
}

//=======================================================================
//function : Read
//purpose  : Read the Node from input stream.
//=======================================================================

VrmlData_ErrorStatus VrmlData_WorldInfo::Read (VrmlData_InBuffer& theBuffer)
{
  VrmlData_ErrorStatus aStatus;
  while (OK(aStatus, VrmlData_Scene::ReadLine(theBuffer))) {

    if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "title")) {
      TCollection_AsciiString aTitleString;
      if (OK (aStatus, ReadString (theBuffer, aTitleString)))
        SetTitle (aTitleString.ToCString());

    } else if (VRMLDATA_LCOMPARE (theBuffer.LinePtr, "info")) {
      NCollection_List<TCollection_AsciiString> lstInfo;
      if (OK (aStatus, ReadMultiString (theBuffer, lstInfo))) {
        NCollection_List<TCollection_AsciiString>::Iterator anIter (lstInfo);
        for (; anIter.More(); anIter.Next())
          AddInfo (anIter.Value().ToCString());
      }
    } else
      break;
  }

  // Read the terminating (closing) brace
  if (OK(aStatus))
    aStatus = readBrace (theBuffer);
  return aStatus;
}

//=======================================================================
//function : Write
//purpose  : Write the Node to the Scene output.
//=======================================================================

VrmlData_ErrorStatus VrmlData_WorldInfo::Write (const char * thePrefix) const
{
  VrmlData_ErrorStatus aStatus (VrmlData_StatusOK);
  const VrmlData_Scene& aScene = Scene();
  static char header[] = "WorldInfo {";
  if (aScene.IsDummyWrite() == Standard_False &&
      OK (aStatus, aScene.WriteLine (thePrefix, header, GlobalIndent())))
  {
    char buf[4096];
    if (myTitle) {
      sprintf (buf, "title \"%s\"", myTitle);
      aStatus = aScene.WriteLine (buf);
    }

    if (myInfo.IsEmpty() == Standard_False && OK(aStatus)) {
      if (OK (aStatus, aScene.WriteLine ("info [", 0L, GlobalIndent()))) {
        NCollection_List<const char *>::Iterator anIter (myInfo);
        while (anIter.More()) {
          sprintf (buf, "\"%s\"", anIter.Value());
          anIter.Next();
          if (anIter.More())
            aStatus = aScene.WriteLine (buf, ",");
          else
            aStatus = aScene.WriteLine (buf);
        }
      }
      aStatus = aScene.WriteLine ("]", 0L, -GlobalIndent());
    }

    aStatus = WriteClosing();
  }
  return aStatus;
}

//=======================================================================
//function : IsDefault
//purpose  : 
//=======================================================================

Standard_Boolean VrmlData_WorldInfo::IsDefault() const
{
  return (myTitle == 0L && myInfo.IsEmpty());
}