Panda3D
eggGroupUniquifier.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 eggGroupUniquifier.cxx
10  * @author drose
11  * @date 2001-02-22
12  */
13 
14 #include "eggGroupUniquifier.h"
15 #include "eggGroup.h"
16 
17 #include "pnotify.h"
18 
19 #include <ctype.h>
20 
21 using std::string;
22 
23 TypeHandle EggGroupUniquifier::_type_handle;
24 
25 
26 /**
27  * If filter_names is true, then the group names will be coerced into a fairly
28  * safe, standard convention that uses no characters other than a-z, A-Z, 0-9,
29  * and underscore. If filter_names is false, the group names will be left
30  * unchanged.
31  */
33 EggGroupUniquifier(bool filter_names)
34  : _filter_names(filter_names)
35 {
36 }
37 
38 /**
39  * Returns the category name into which the given node should be collected, or
40  * the empty string if the node's name should be left alone.
41  */
44  if (node->is_of_type(EggGroup::get_class_type()) && node->has_name()) {
45  return "group";
46  }
47 
48  return string();
49 }
50 
51 /**
52  * Returns the name of the given node, or at least the name it should be.
53  * This provides a hook to adjust the name before attempting to uniquify it,
54  * if desired, for instance to remove invalid characters.
55  */
58  string name = node->get_name();
59  if (!_filter_names) {
60  return name;
61  }
62  nassertr(!name.empty(), string());
63 
64  string result;
65 
66  // First, replace characters not A-Z, a-z, 0-9, or '_' with underscore, and
67  // remove consecutive underscores.
68  string::const_iterator pi;
69  bool last_underscore = false;
70  for (pi = name.begin(); pi != name.end(); ++pi) {
71  if (isalnum(*pi)) {
72  result += *pi;
73  last_underscore = false;
74 
75  } else if (!last_underscore) {
76  result += '_';
77  last_underscore = true;
78  }
79  }
80 
81  // Next, ensure the name does not begin with a digit.
82  nassertr(!result.empty(), string());
83  if (isdigit(result[0])) {
84  result = "_" + result;
85  }
86 
87  return result;
88 }
89 
90 /**
91  * Generates a new name for the given node when its existing name clashes with
92  * some other node. This function will be called repeatedly, if necessary,
93  * until it returns a name that actually is unique.
94  *
95  * The category is the string returned by get_category(), and index is a
96  * uniquely-generated number that may be useful for synthesizing the name.
97  */
99 generate_name(EggNode *node, const string &category, int index) {
100  std::ostringstream str;
101  str << node->get_name() << "_group" << index;
102  return str.str();
103 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual std::string get_category(EggNode *node)
Returns the category name into which the given node should be collected, or the empty string if the n...
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
virtual std::string generate_name(EggNode *node, const std::string &category, int index)
Generates a new name for the given node when its existing name clashes with some other node.
EggGroupUniquifier(bool filter_names=true)
If filter_names is true, then the group names will be coerced into a fairly safe, standard convention...
A base class for things that may be directly added into the egg hierarchy.
Definition: eggNode.h:35
bool is_of_type(TypeHandle handle) const
Returns true if the current object is or derives from the indicated type.
Definition: typedObject.I:28
bool has_name() const
Returns true if the Namable has a nonempty name set, false if the name is empty.
Definition: namable.I:44
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
virtual std::string filter_name(EggNode *node)
Returns the name of the given node, or at least the name it should be.