00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "eggNameUniquifier.h"
00016 #include "eggNode.h"
00017 #include "eggGroupNode.h"
00018 #include "config_egg.h"
00019 #include "dcast.h"
00020
00021 #include "pnotify.h"
00022
00023 TypeHandle EggNameUniquifier::_type_handle;
00024
00025
00026
00027
00028
00029
00030
00031 EggNameUniquifier::
00032 EggNameUniquifier() {
00033 _index = 0;
00034 }
00035
00036
00037
00038
00039
00040
00041 EggNameUniquifier::
00042 ~EggNameUniquifier() {
00043 }
00044
00045
00046
00047
00048
00049
00050
00051 void EggNameUniquifier::
00052 clear() {
00053 _categories.clear();
00054 _index = 0;
00055 }
00056
00057
00058
00059
00060
00061
00062 void EggNameUniquifier::
00063 uniquify(EggNode *node) {
00064 string category = get_category(node);
00065 if (egg_cat.is_debug()) {
00066 egg_cat.debug()
00067 << "Uniquifying " << node->get_name() << ", category = " << category
00068 << "\n";
00069 }
00070
00071 if (!category.empty()) {
00072 string name = filter_name(node);
00073
00074 UsedNames &names = _categories[category];
00075 bool inserted = false;
00076 if (!name.empty()) {
00077 inserted = names.insert(UsedNames::value_type(name, node)).second;
00078 }
00079
00080 while (!inserted) {
00081 _index++;
00082 name = generate_name(node, category, _index);
00083 inserted = names.insert(UsedNames::value_type(name, node)).second;
00084 }
00085
00086 if (egg_cat.is_debug()) {
00087 egg_cat.debug()
00088 << "Uniquifying " << node->get_name() << " to "
00089 << name << "\n";
00090 }
00091
00092 node->set_name(name);
00093 }
00094
00095 if (node->is_of_type(EggGroupNode::get_class_type())) {
00096 EggGroupNode *group;
00097 DCAST_INTO_V(group, node);
00098
00099 EggGroupNode::iterator ci;
00100 for (ci = group->begin(); ci != group->end(); ++ci) {
00101 EggNode *child = (*ci);
00102 nassertv(child != (EggNode *)NULL);
00103 uniquify(child);
00104 }
00105 }
00106 }
00107
00108
00109
00110
00111
00112
00113
00114 EggNode *EggNameUniquifier::
00115 get_node(const string &category, const string &name) const {
00116 Categories::const_iterator ci;
00117 ci = _categories.find(category);
00118 if (ci == _categories.end()) {
00119 return (EggNode *)NULL;
00120 }
00121
00122 const UsedNames &names = (*ci).second;
00123 UsedNames::const_iterator ni;
00124 ni = names.find(name);
00125 if (ni == names.end()) {
00126 return (EggNode *)NULL;
00127 }
00128
00129 return (*ni).second;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138 bool EggNameUniquifier::
00139 has_name(const string &category, const string &name) const {
00140 Categories::const_iterator ci;
00141 ci = _categories.find(category);
00142 if (ci == _categories.end()) {
00143 return false;
00144 }
00145
00146 const UsedNames &names = (*ci).second;
00147 UsedNames::const_iterator ni;
00148 ni = names.find(name);
00149 if (ni == names.end()) {
00150 return false;
00151 }
00152
00153 return true;
00154 }
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164 bool EggNameUniquifier::
00165 add_name(const string &category, const string &name, EggNode *node) {
00166 UsedNames &names = _categories[category];
00167 bool inserted = names.insert(UsedNames::value_type(name, node)).second;
00168 return inserted;
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 string EggNameUniquifier::
00180 filter_name(EggNode *node) {
00181 return node->get_name();
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 string EggNameUniquifier::
00197 generate_name(EggNode *node, const string &category, int index) {
00198 string name = filter_name(node);
00199
00200 ostringstream str;
00201 if (name.empty()) {
00202 str << category << index;
00203 } else {
00204 str << name << "." << category << index;
00205 }
00206 return str.str();
00207 }