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