1 /**************************************************************************\
2  * Copyright (c) Kongsberg Oil & Gas Technologies AS
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 \**************************************************************************/
32 
33 /*!
34   \class SoMFString SoMFString.h Inventor/fields/SoMFString.h
35   \brief The SoMFString class is a container for SbString values.
36 
37   \ingroup fields
38 
39   This field is used where nodes, engines or other field containers
40   needs to store arrays of strings.
41 
42   \sa SoSFString
43 
44 */
45 
46 // *************************************************************************
47 
48 #include <Inventor/fields/SoMFString.h>
49 
50 #include <cassert>
51 
52 #include <Inventor/SoInput.h>
53 #include <Inventor/errors/SoDebugError.h>
54 
55 #include "fields/SoSubFieldP.h"
56 #include "fields/shared.h"
57 
58 // *************************************************************************
59 
60 SO_MFIELD_SOURCE(SoMFString, SbString, const SbString &);
61 
62 // *************************************************************************
63 
64 // Override from parent class.
65 void
initClass(void)66 SoMFString::initClass(void)
67 {
68   SO_MFIELD_INTERNAL_INIT_CLASS(SoMFString);
69 }
70 
71 // No need to document readValue() and writeValue() here, as the
72 // necessary information is provided by the documentation of the
73 // parent classes.
74 #ifndef DOXYGEN_SKIP_THIS
75 
76 SbBool
read1Value(SoInput * in,int idx)77 SoMFString::read1Value(SoInput * in, int idx)
78 {
79   assert(idx < this->maxNum);
80   return in->read(this->values[idx]);
81 }
82 
83 void
write1Value(SoOutput * out,int idx) const84 SoMFString::write1Value(SoOutput * out, int idx) const
85 {
86   sosfstring_write_value(this, out, (*this)[idx]);
87 }
88 
89 #endif // DOXYGEN_SKIP_THIS
90 
91 /*!
92   Set field to contain \a num \a strings from index \a start,
93   replacing any strings already present at the given indices.
94 */
95 void
setValues(const int start,const int numarg,const char * strings[])96 SoMFString::setValues(const int start, const int numarg, const char * strings[])
97 {
98   if (start+numarg > this->maxNum) this->allocValues(start+numarg);
99   else if (start+numarg > this->num) this->num = start+numarg;
100 
101   for (int i=0; i < numarg; i++) this->values[i+start] = SbString(strings[i]);
102   this->valueChanged();
103 }
104 
105 /*!
106   Set field to contain a single string, from \a str.
107 */
108 void
setValue(const char * str)109 SoMFString::setValue(const char * str)
110 {
111   this->setValue(SbString(str));
112 }
113 
114 /*!
115   Remove all text from \a fromchar on \a fromline and to \a tochar
116   on \a toline, including all lines between \a fromline and \a toline.
117   Merge \a fromline and \a toline after deletion.
118 */
119 void
deleteText(const int fromline,const int fromchar,const int toline,const int tochar)120 SoMFString::deleteText(const int fromline, const int fromchar,
121                        const int toline, const int tochar)
122 {
123 #if COIN_DEBUG && 1 // debug
124   if (fromline < 0 || toline >= this->getNum() || fromline > toline ||
125       (fromline == toline && fromchar >= tochar) ||
126       fromchar < 0 || fromchar >= (*this)[fromline].getLength() ||
127       tochar < 0 || tochar >= (*this)[toline].getLength()) {
128     SoDebugError::post("SoMFString::deleteText",
129                        "invalid argument(s), [%d, %d - %d, %d]",
130                        fromline, fromchar, toline, tochar);
131     return;
132   }
133 #endif // debug
134 
135   if (fromline == toline) {
136     this->values[fromline].deleteSubString(fromchar, tochar);
137   }
138   else {
139     this->values[fromline].deleteSubString(fromchar, -1);
140     this->values[toline].deleteSubString(0, tochar);
141     // Merge.
142     this->values[fromline] += (*this)[toline];
143     // Delete intermediate strings + toline string.
144     this->deleteValues(fromline + 1, toline - fromline);
145   }
146 }
147 
148 // *************************************************************************
149 
150 #ifdef COIN_TEST_SUITE
151 
BOOST_AUTO_TEST_CASE(initialized)152 BOOST_AUTO_TEST_CASE(initialized)
153 {
154   SoMFString field;
155   BOOST_CHECK_MESSAGE(field.getTypeId() != SoType::badType(),
156                       "missing class initialization");
157   BOOST_CHECK_EQUAL(field.getNum(), 0);
158 }
159 
160 #endif // COIN_TEST_SUITE
161