Panda3D

eggMiscFuncs.cxx

00001 // Filename: eggMiscFuncs.cxx
00002 // Created by:  drose (16Jan99)
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 "pandabase.h"
00016 #include "eggMiscFuncs.h"
00017 #include "indent.h"
00018 
00019 #include <ctype.h>
00020 
00021 
00022 ////////////////////////////////////////////////////////////////////
00023 //     Function: enquote_string
00024 //  Description: Writes the string to the indicated output stream.  If
00025 //               the string contains any characters special to egg,
00026 //               writes quotation marks around it.  If always_quote is
00027 //               true, writes quotation marks regardless.
00028 ////////////////////////////////////////////////////////////////////
00029 ostream &
00030 enquote_string(ostream &out, const string &str, int indent_level,
00031                bool always_quote) {
00032   indent(out, indent_level);
00033 
00034   // First, see if we need to enquote it.
00035   bool legal = !always_quote;
00036   string::const_iterator p;
00037   for (p = str.begin(); p != str.end() && legal; ++p) {
00038     legal = (isalnum(*p) || *p=='-' || *p=='_' || *p=='#' || *p=='.');
00039   }
00040 
00041   if (legal) {
00042     out << str;
00043 
00044   } else {
00045     out << '"';
00046 
00047     for (p = str.begin(); p != str.end(); ++p) {
00048       switch (*p) {
00049       case '"':
00050         // Can't output nested quote marks at all.
00051         out << "'";
00052         break;
00053 
00054       case '\n':
00055         // A newline necessitates ending the quotes, newlining, and
00056         // beginning again.
00057         out << "\"\n";
00058         indent(out, indent_level) << '"';
00059         break;
00060 
00061       default:
00062         out << *p;
00063       }
00064     }
00065     out << '"';
00066   }
00067 
00068   return out;
00069 }
00070 
00071 
00072 ////////////////////////////////////////////////////////////////////
00073 //     Function: write_transform
00074 //  Description: A helper function to write out a 3x3 transform
00075 //               matrix.
00076 ////////////////////////////////////////////////////////////////////
00077 void
00078 write_transform(ostream &out, const LMatrix3d &mat, int indent_level) {
00079   indent(out, indent_level) << "<Transform> {\n";
00080 
00081   indent(out, indent_level+2) << "<Matrix3> {\n";
00082 
00083   for (int r = 0; r < 3; r++) {
00084     indent(out, indent_level+3);
00085     for (int c = 0; c < 3; c++) {
00086       out << " " << mat(r, c);
00087     }
00088     out << "\n";
00089   }
00090   indent(out, indent_level+2) << "}\n";
00091   indent(out, indent_level) << "}\n";
00092 }
00093 
00094 ////////////////////////////////////////////////////////////////////
00095 //     Function: write_transform
00096 //  Description: A helper function to write out a 4x4 transform
00097 //               matrix.
00098 ////////////////////////////////////////////////////////////////////
00099 void
00100 write_transform(ostream &out, const LMatrix4d &mat, int indent_level) {
00101   indent(out, indent_level) << "<Transform> {\n";
00102 
00103   indent(out, indent_level+2) << "<Matrix4> {\n";
00104 
00105   for (int r = 0; r < 4; r++) {
00106     indent(out, indent_level+3);
00107     for (int c = 0; c < 4; c++) {
00108       out << " " << mat(r, c);
00109     }
00110     out << "\n";
00111   }
00112   indent(out, indent_level+2) << "}\n";
00113   indent(out, indent_level) << "}\n";
00114 }
 All Classes Functions Variables Enumerations