Panda3D
xFileDataObjectString.cxx
1 // Filename: xFileDataObjectString.cxx
2 // Created by: drose (08Oct04)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "xFileDataObjectString.h"
16 #include "string_utils.h"
17 #include "indent.h"
18 
19 TypeHandle XFileDataObjectString::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: XFileDataObjectString::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 XFileDataObjectString::
27 XFileDataObjectString(const XFileDataDef *data_def, const string &value) :
28  XFileDataObject(data_def),
29  _value(value)
30 {
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: XFileDataObjectString::output_data
35 // Access: Public, Virtual
36 // Description: Writes a suitable representation of this node to an
37 // .x file in text mode.
38 ////////////////////////////////////////////////////////////////////
40 output_data(ostream &out) const {
41  enquote_string(out);
42 }
43 
44 ////////////////////////////////////////////////////////////////////
45 // Function: XFileDataObjectString::write_data
46 // Access: Public, Virtual
47 // Description: Writes a suitable representation of this node to an
48 // .x file in text mode.
49 ////////////////////////////////////////////////////////////////////
51 write_data(ostream &out, int indent_level, const char *separator) const {
52  indent(out, indent_level);
53  enquote_string(out);
54  out << separator << "\n";
55 }
56 
57 ////////////////////////////////////////////////////////////////////
58 // Function: XFileDataObjectString::set_string_value
59 // Access: Protected, Virtual
60 // Description: Sets the object's value as a string, if this is
61 // legal.
62 ////////////////////////////////////////////////////////////////////
63 void XFileDataObjectString::
64 set_string_value(const string &string_value) {
65  _value = string_value;
66 }
67 
68 ////////////////////////////////////////////////////////////////////
69 // Function: XFileDataObjectString::get_string_value
70 // Access: Protected, Virtual
71 // Description: Returns the object's representation as a string, if
72 // it has one.
73 ////////////////////////////////////////////////////////////////////
74 string XFileDataObjectString::
75 get_string_value() const {
76  return _value;
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: XFileDataObjectString::enquote_string
81 // Access: Private
82 // Description: Writes the string to the output stream without
83 // quotation marks, quoting special characters as
84 // needed.
85 ////////////////////////////////////////////////////////////////////
86 void XFileDataObjectString::
87 enquote_string(ostream &out) const {
88  // Actually, the XFile spec doesn't tell us how to escape special
89  // characters within quotation marks. We'll just take a stab in the
90  // dark here.
91 
92  out << '"';
93  string::const_iterator si;
94  for (si = _value.begin(); si != _value.end(); ++si) {
95  switch (*si) {
96  case '\n':
97  out << "\\n";
98  break;
99 
100  case '\r':
101  out << "\\r";
102  break;
103 
104  case '"':
105  case '\\':
106  out << '\\' << (*si);
107  break;
108 
109  default:
110  out << (*si);
111  }
112  }
113  out << '"';
114 }
A definition of a single data element appearing within a template record.
Definition: xFileDataDef.h:35
virtual void write_data(ostream &out, int indent_level, const char *separator) const
Writes a suitable representation of this node to an .x file in text mode.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85
virtual void output_data(ostream &out) const
Writes a suitable representation of this node to an .x file in text mode.
The abstract base class for a number of different types of data elements that may be stored in the X ...