Panda3D
eggMiscFuncs.cxx
1 // Filename: eggMiscFuncs.cxx
2 // Created by: drose (16Jan99)
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 "pandabase.h"
16 #include "eggMiscFuncs.h"
17 #include "indent.h"
18 
19 #include <ctype.h>
20 
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: enquote_string
24 // Description: Writes the string to the indicated output stream. If
25 // the string contains any characters special to egg,
26 // writes quotation marks around it. If always_quote is
27 // true, writes quotation marks regardless.
28 ////////////////////////////////////////////////////////////////////
29 ostream &
30 enquote_string(ostream &out, const string &str, int indent_level,
31  bool always_quote) {
32  indent(out, indent_level);
33 
34  // First, see if we need to enquote it.
35  bool legal = !always_quote;
36  string::const_iterator p;
37  for (p = str.begin(); p != str.end() && legal; ++p) {
38  legal = (isalnum(*p) || *p=='-' || *p=='_' || *p=='#' || *p=='.');
39  }
40 
41  if (legal) {
42  out << str;
43 
44  } else {
45  out << '"';
46 
47  for (p = str.begin(); p != str.end(); ++p) {
48  switch (*p) {
49  case '"':
50  // Can't output nested quote marks at all.
51  out << "'";
52  break;
53 
54  case '\n':
55  // A newline necessitates ending the quotes, newlining, and
56  // beginning again.
57  out << "\"\n";
58  indent(out, indent_level) << '"';
59  break;
60 
61  default:
62  out << *p;
63  }
64  }
65  out << '"';
66  }
67 
68  return out;
69 }
70 
71 
72 ////////////////////////////////////////////////////////////////////
73 // Function: write_transform
74 // Description: A helper function to write out a 3x3 transform
75 // matrix.
76 ////////////////////////////////////////////////////////////////////
77 void
78 write_transform(ostream &out, const LMatrix3d &mat, int indent_level) {
79  indent(out, indent_level) << "<Transform> {\n";
80 
81  indent(out, indent_level+2) << "<Matrix3> {\n";
82 
83  for (int r = 0; r < 3; r++) {
84  indent(out, indent_level+3);
85  for (int c = 0; c < 3; c++) {
86  out << " " << mat(r, c);
87  }
88  out << "\n";
89  }
90  indent(out, indent_level+2) << "}\n";
91  indent(out, indent_level) << "}\n";
92 }
93 
94 ////////////////////////////////////////////////////////////////////
95 // Function: write_transform
96 // Description: A helper function to write out a 4x4 transform
97 // matrix.
98 ////////////////////////////////////////////////////////////////////
99 void
100 write_transform(ostream &out, const LMatrix4d &mat, int indent_level) {
101  indent(out, indent_level) << "<Transform> {\n";
102 
103  indent(out, indent_level+2) << "<Matrix4> {\n";
104 
105  for (int r = 0; r < 4; r++) {
106  indent(out, indent_level+3);
107  for (int c = 0; c < 4; c++) {
108  out << " " << mat(r, c);
109  }
110  out << "\n";
111  }
112  indent(out, indent_level+2) << "}\n";
113  indent(out, indent_level) << "}\n";
114 }
This is a 4-by-4 transform matrix.
Definition: lmatrix.h:4716
This is a 3-by-3 transform matrix.
Definition: lmatrix.h:4375