Panda3D
eggMiscFuncs.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 eggMiscFuncs.cxx
10  * @author drose
11  * @date 1999-01-16
12  */
13 
14 #include "pandabase.h"
15 #include "eggMiscFuncs.h"
16 #include "indent.h"
17 
18 #include <ctype.h>
19 
20 using std::ostream;
21 using std::string;
22 
23 
24 /**
25  * Writes the string to the indicated output stream. If the string contains
26  * any characters special to egg, writes quotation marks around it. If
27  * always_quote is 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 beginning
56  // 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  * A helper function to write out a 3x3 transform matrix.
74  */
75 void
76 write_transform(ostream &out, const LMatrix3d &mat, int indent_level) {
77  indent(out, indent_level) << "<Transform> {\n";
78 
79  indent(out, indent_level+2) << "<Matrix3> {\n";
80 
81  for (int r = 0; r < 3; r++) {
82  indent(out, indent_level+3);
83  for (int c = 0; c < 3; c++) {
84  out << " " << mat(r, c);
85  }
86  out << "\n";
87  }
88  indent(out, indent_level+2) << "}\n";
89  indent(out, indent_level) << "}\n";
90 }
91 
92 /**
93  * A helper function to write out a 4x4 transform matrix.
94  */
95 void
96 write_transform(ostream &out, const LMatrix4d &mat, int indent_level) {
97  indent(out, indent_level) << "<Transform> {\n";
98 
99  indent(out, indent_level+2) << "<Matrix4> {\n";
100 
101  for (int r = 0; r < 4; r++) {
102  indent(out, indent_level+3);
103  for (int c = 0; c < 4; c++) {
104  out << " " << mat(r, c);
105  }
106  out << "\n";
107  }
108  indent(out, indent_level+2) << "}\n";
109  indent(out, indent_level) << "}\n";
110 }
ostream & enquote_string(ostream &out, const string &str, int indent_level, bool always_quote)
Writes the string to the indicated output stream.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
void write_transform(ostream &out, const LMatrix3d &mat, int indent_level)
A helper function to write out a 3x3 transform matrix.
std::ostream & indent(std::ostream &out, int indent_level)
A handy function for doing text formatting.
Definition: indent.cxx:20
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.