Panda3D
 All Classes Functions Variables Enumerations
xFileDataObjectString.cxx
00001 // Filename: xFileDataObjectString.cxx
00002 // Created by:  drose (08Oct04)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "xFileDataObjectString.h"
00016 #include "string_utils.h"
00017 #include "indent.h"
00018 
00019 TypeHandle XFileDataObjectString::_type_handle;
00020 
00021 ////////////////////////////////////////////////////////////////////
00022 //     Function: XFileDataObjectString::Constructor
00023 //       Access: Public
00024 //  Description: 
00025 ////////////////////////////////////////////////////////////////////
00026 XFileDataObjectString::
00027 XFileDataObjectString(const XFileDataDef *data_def, const string &value) :
00028   XFileDataObject(data_def),
00029   _value(value)
00030 {
00031 }
00032 
00033 ////////////////////////////////////////////////////////////////////
00034 //     Function: XFileDataObjectString::output_data
00035 //       Access: Public, Virtual
00036 //  Description: Writes a suitable representation of this node to an
00037 //               .x file in text mode.
00038 ////////////////////////////////////////////////////////////////////
00039 void XFileDataObjectString::
00040 output_data(ostream &out) const {
00041   enquote_string(out);
00042 }
00043 
00044 ////////////////////////////////////////////////////////////////////
00045 //     Function: XFileDataObjectString::write_data
00046 //       Access: Public, Virtual
00047 //  Description: Writes a suitable representation of this node to an
00048 //               .x file in text mode.
00049 ////////////////////////////////////////////////////////////////////
00050 void XFileDataObjectString::
00051 write_data(ostream &out, int indent_level, const char *separator) const {
00052   indent(out, indent_level);
00053   enquote_string(out);
00054   out << separator << "\n";
00055 }
00056 
00057 ////////////////////////////////////////////////////////////////////
00058 //     Function: XFileDataObjectString::set_string_value
00059 //       Access: Protected, Virtual
00060 //  Description: Sets the object's value as a string, if this is
00061 //               legal.
00062 ////////////////////////////////////////////////////////////////////
00063 void XFileDataObjectString::
00064 set_string_value(const string &string_value) {
00065   _value = string_value;
00066 }
00067 
00068 ////////////////////////////////////////////////////////////////////
00069 //     Function: XFileDataObjectString::get_string_value
00070 //       Access: Protected, Virtual
00071 //  Description: Returns the object's representation as a string, if
00072 //               it has one.
00073 ////////////////////////////////////////////////////////////////////
00074 string XFileDataObjectString::
00075 get_string_value() const {
00076   return _value;
00077 }
00078 
00079 ////////////////////////////////////////////////////////////////////
00080 //     Function: XFileDataObjectString::enquote_string
00081 //       Access: Private
00082 //  Description: Writes the string to the output stream without
00083 //               quotation marks, quoting special characters as
00084 //               needed.
00085 ////////////////////////////////////////////////////////////////////
00086 void XFileDataObjectString::
00087 enquote_string(ostream &out) const {
00088   // Actually, the XFile spec doesn't tell us how to escape special
00089   // characters within quotation marks.  We'll just take a stab in the
00090   // dark here.
00091 
00092   out << '"';
00093   string::const_iterator si;
00094   for (si = _value.begin(); si != _value.end(); ++si) {
00095     switch (*si) {
00096     case '\n':
00097       out << "\\n";
00098       break;
00099 
00100     case '\r':
00101       out << "\\r";
00102       break;
00103 
00104     case '"':
00105     case '\\':
00106       out << '\\' << (*si);
00107       break;
00108 
00109     default:
00110       out << (*si);
00111     }
00112   }
00113   out << '"';
00114 }
 All Classes Functions Variables Enumerations